SourceManager.h revision 414cb64f09ce48a36377458ce5e5a90c3ad41d00
1//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the SourceManager interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_SOURCEMANAGER_H
15#define LLVM_CLANG_SOURCEMANAGER_H
16
17#include "clang/Basic/SourceLocation.h"
18#include "llvm/Support/Allocator.h"
19#include "llvm/Support/DataTypes.h"
20#include "llvm/ADT/PointerIntPair.h"
21#include "llvm/ADT/PointerUnion.h"
22#include "llvm/ADT/DenseMap.h"
23#include <vector>
24#include <cassert>
25
26namespace llvm {
27class MemoryBuffer;
28class StringRef;
29}
30
31namespace clang {
32
33class Diagnostic;
34class SourceManager;
35class FileManager;
36class FileEntry;
37class LineTableInfo;
38
39/// SrcMgr - Public enums and private classes that are part of the
40/// SourceManager implementation.
41///
42namespace SrcMgr {
43  /// CharacteristicKind - This is used to represent whether a file or directory
44  /// holds normal user code, system code, or system code which is implicitly
45  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
46  /// (this is maintained by DirectoryLookup and friends) as can specific
47  /// FileIDInfos when a #pragma system_header is seen or various other cases.
48  ///
49  enum CharacteristicKind {
50    C_User, C_System, C_ExternCSystem
51  };
52
53  /// ContentCache - One instance of this struct is kept for every file
54  /// loaded or used.  This object owns the MemoryBuffer object.
55  class ContentCache {
56    enum CCFlags {
57      /// \brief Whether the buffer is invalid.
58      InvalidFlag = 0x01,
59      /// \brief Whether the buffer should not be freed on destruction.
60      DoNotFreeFlag = 0x02
61    };
62
63    /// Buffer - The actual buffer containing the characters from the input
64    /// file.  This is owned by the ContentCache object.
65    /// The bits indicate indicates whether the buffer is invalid.
66    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
67
68  public:
69    /// Reference to the file entry.  This reference does not own
70    /// the FileEntry object.  It is possible for this to be NULL if
71    /// the ContentCache encapsulates an imaginary text buffer.
72    const FileEntry *Entry;
73
74    /// SourceLineCache - A bump pointer allocated array of offsets for each
75    /// source line.  This is lazily computed.  This is owned by the
76    /// SourceManager BumpPointerAllocator object.
77    unsigned *SourceLineCache;
78
79    /// NumLines - The number of lines in this ContentCache.  This is only valid
80    /// if SourceLineCache is non-null.
81    unsigned NumLines;
82
83    /// getBuffer - Returns the memory buffer for the associated content.
84    ///
85    /// \param Diag Object through which diagnostics will be emitted it the
86    /// buffer cannot be retrieved.
87    ///
88    /// \param Loc If specified, is the location that invalid file diagnostics
89    ///     will be emitted at.
90    ///
91    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
92    const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
93                                        const SourceManager &SM,
94                                        SourceLocation Loc = SourceLocation(),
95                                        bool *Invalid = 0) const;
96
97    /// getSize - Returns the size of the content encapsulated by this
98    ///  ContentCache. This can be the size of the source file or the size of an
99    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
100    ///  file this size is retrieved from the file's FileEntry.
101    unsigned getSize() const;
102
103    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
104    ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
105    ///  instantiated.
106    unsigned getSizeBytesMapped() const;
107
108    void setBuffer(const llvm::MemoryBuffer *B) {
109      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
110      Buffer.setPointer(B);
111      Buffer.setInt(false);
112    }
113
114    /// \brief Get the underlying buffer, returning NULL if the buffer is not
115    /// yet available.
116    const llvm::MemoryBuffer *getRawBuffer() const {
117      return Buffer.getPointer();
118    }
119
120    /// \brief Replace the existing buffer (which will be deleted)
121    /// with the given buffer.
122    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
123
124    /// \brief Determine whether the buffer itself is invalid.
125    bool isBufferInvalid() const {
126      return Buffer.getInt() & InvalidFlag;
127    }
128
129    /// \brief Determine whether the buffer should be freed.
130    bool shouldFreeBuffer() const {
131      return (Buffer.getInt() & DoNotFreeFlag) == 0;
132    }
133
134    ContentCache(const FileEntry *Ent = 0)
135      : Buffer(0, false), Entry(Ent), SourceLineCache(0), NumLines(0) {}
136
137    ~ContentCache();
138
139    /// The copy ctor does not allow copies where source object has either
140    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
141    ///  is not transfered, so this is a logical error.
142    ContentCache(const ContentCache &RHS)
143      : Buffer(0, false), SourceLineCache(0)
144    {
145      Entry = RHS.Entry;
146
147      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
148              && "Passed ContentCache object cannot own a buffer.");
149
150      NumLines = RHS.NumLines;
151    }
152
153  private:
154    // Disable assignments.
155    ContentCache &operator=(const ContentCache& RHS);
156  };
157
158  /// FileInfo - Information about a FileID, basically just the logical file
159  /// that it represents and include stack information.
160  ///
161  /// Each FileInfo has include stack information, indicating where it came
162  /// from.  This information encodes the #include chain that a token was
163  /// instantiated from.  The main include file has an invalid IncludeLoc.
164  ///
165  /// FileInfos contain a "ContentCache *", with the contents of the file.
166  ///
167  class FileInfo {
168    /// IncludeLoc - The location of the #include that brought in this file.
169    /// This is an invalid SLOC for the main file (top of the #include chain).
170    unsigned IncludeLoc;  // Really a SourceLocation
171
172    /// Data - This contains the ContentCache* and the bits indicating the
173    /// characteristic of the file and whether it has #line info, all bitmangled
174    /// together.
175    uintptr_t Data;
176  public:
177    /// get - Return a FileInfo object.
178    static FileInfo get(SourceLocation IL, const ContentCache *Con,
179                        CharacteristicKind FileCharacter) {
180      FileInfo X;
181      X.IncludeLoc = IL.getRawEncoding();
182      X.Data = (uintptr_t)Con;
183      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
184      assert((unsigned)FileCharacter < 4 && "invalid file character");
185      X.Data |= (unsigned)FileCharacter;
186      return X;
187    }
188
189    SourceLocation getIncludeLoc() const {
190      return SourceLocation::getFromRawEncoding(IncludeLoc);
191    }
192    const ContentCache* getContentCache() const {
193      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
194    }
195
196    /// getCharacteristic - Return whether this is a system header or not.
197    CharacteristicKind getFileCharacteristic() const {
198      return (CharacteristicKind)(Data & 3);
199    }
200
201    /// hasLineDirectives - Return true if this FileID has #line directives in
202    /// it.
203    bool hasLineDirectives() const { return (Data & 4) != 0; }
204
205    /// setHasLineDirectives - Set the flag that indicates that this FileID has
206    /// line table entries associated with it.
207    void setHasLineDirectives() {
208      Data |= 4;
209    }
210  };
211
212  /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
213  /// location - where the token was ultimately instantiated, and the
214  /// SpellingLoc - where the actual character data for the token came from.
215  class InstantiationInfo {
216     // Really these are all SourceLocations.
217
218    /// SpellingLoc - Where the spelling for the token can be found.
219    unsigned SpellingLoc;
220
221    /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
222    /// indicate the start and end of the instantiation.  In object-like macros,
223    /// these will be the same.  In a function-like macro instantiation, the
224    /// start will be the identifier and the end will be the ')'.
225    unsigned InstantiationLocStart, InstantiationLocEnd;
226  public:
227    SourceLocation getSpellingLoc() const {
228      return SourceLocation::getFromRawEncoding(SpellingLoc);
229    }
230    SourceLocation getInstantiationLocStart() const {
231      return SourceLocation::getFromRawEncoding(InstantiationLocStart);
232    }
233    SourceLocation getInstantiationLocEnd() const {
234      return SourceLocation::getFromRawEncoding(InstantiationLocEnd);
235    }
236
237    std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
238      return std::make_pair(getInstantiationLocStart(),
239                            getInstantiationLocEnd());
240    }
241
242    /// get - Return a InstantiationInfo for an expansion.  IL specifies
243    /// the instantiation location (where the macro is expanded), and SL
244    /// specifies the spelling location (where the characters from the token
245    /// come from).  IL and PL can both refer to normal File SLocs or
246    /// instantiation locations.
247    static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd,
248                                 SourceLocation SL) {
249      InstantiationInfo X;
250      X.SpellingLoc = SL.getRawEncoding();
251      X.InstantiationLocStart = ILStart.getRawEncoding();
252      X.InstantiationLocEnd = ILEnd.getRawEncoding();
253      return X;
254    }
255  };
256
257  /// SLocEntry - This is a discriminated union of FileInfo and
258  /// InstantiationInfo.  SourceManager keeps an array of these objects, and
259  /// they are uniquely identified by the FileID datatype.
260  class SLocEntry {
261    unsigned Offset;   // low bit is set for instantiation info.
262    union {
263      FileInfo File;
264      InstantiationInfo Instantiation;
265    };
266  public:
267    unsigned getOffset() const { return Offset >> 1; }
268
269    bool isInstantiation() const { return Offset & 1; }
270    bool isFile() const { return !isInstantiation(); }
271
272    const FileInfo &getFile() const {
273      assert(isFile() && "Not a file SLocEntry!");
274      return File;
275    }
276
277    const InstantiationInfo &getInstantiation() const {
278      assert(isInstantiation() && "Not an instantiation SLocEntry!");
279      return Instantiation;
280    }
281
282    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
283      SLocEntry E;
284      E.Offset = Offset << 1;
285      E.File = FI;
286      return E;
287    }
288
289    static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
290      SLocEntry E;
291      E.Offset = (Offset << 1) | 1;
292      E.Instantiation = II;
293      return E;
294    }
295  };
296}  // end SrcMgr namespace.
297
298/// \brief External source of source location entries.
299class ExternalSLocEntrySource {
300public:
301  virtual ~ExternalSLocEntrySource();
302
303  /// \brief Read the source location entry with index ID.
304  virtual void ReadSLocEntry(unsigned ID) = 0;
305};
306
307
308/// IsBeforeInTranslationUnitCache - This class holds the cache used by
309/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
310/// worth breaking out of SourceManager.
311class IsBeforeInTranslationUnitCache {
312  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
313  /// with a subsequent query, the result can be reused.
314  FileID LQueryFID, RQueryFID;
315
316  /// CommonFID - This is the file found in common between the two #include
317  /// traces.  It is the nearest common ancestor of the #include tree.
318  FileID CommonFID;
319
320  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
321  /// Usually, this represents the location of the #include for QueryFID, but if
322  /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
323  /// random token in the parent.
324  unsigned LCommonOffset, RCommonOffset;
325public:
326
327  /// isCacheValid - Return true if the currently cached values match up with
328  /// the specified LHS/RHS query.  If not, we can't use the cache.
329  bool isCacheValid(FileID LHS, FileID RHS) const {
330    return LQueryFID == LHS && RQueryFID == RHS;
331  }
332
333  /// getCachedResult - If the cache is valid, compute the result given the
334  /// specified offsets in the LHS/RHS FID's.
335  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
336    // If one of the query files is the common file, use the offset.  Otherwise,
337    // use the #include loc in the common file.
338    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
339    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
340    return LOffset < ROffset;
341  }
342
343  // Set up a new query.
344  void setQueryFIDs(FileID LHS, FileID RHS) {
345    LQueryFID = LHS;
346    RQueryFID = RHS;
347  }
348
349  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
350                    unsigned rCommonOffset) {
351    CommonFID = commonFID;
352    LCommonOffset = lCommonOffset;
353    RCommonOffset = rCommonOffset;
354  }
355
356};
357
358/// SourceManager - This file handles loading and caching of source files into
359/// memory.  This object owns the MemoryBuffer objects for all of the loaded
360/// files and assigns unique FileID's for each unique #include chain.
361///
362/// The SourceManager can be queried for information about SourceLocation
363/// objects, turning them into either spelling or instantiation locations.
364/// Spelling locations represent where the bytes corresponding to a token came
365/// from and instantiation locations represent where the location is in the
366/// user's view.  In the case of a macro expansion, for example, the spelling
367/// location indicates where the expanded token came from and the instantiation
368/// location specifies where it was expanded.
369class SourceManager {
370  /// \brief Diagnostic object.
371  Diagnostic &Diag;
372
373  FileManager &FileMgr;
374
375  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
376
377  /// FileInfos - Memoized information about all of the files tracked by this
378  /// SourceManager.  This set allows us to merge ContentCache entries based
379  /// on their FileEntry*.  All ContentCache objects will thus have unique,
380  /// non-null, FileEntry pointers.
381  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
382
383  /// MemBufferInfos - Information about various memory buffers that we have
384  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
385  /// as they do not refer to a file.
386  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
387
388  /// SLocEntryTable - This is an array of SLocEntry's that we have created.
389  /// FileID is an index into this vector.  This array is sorted by the offset.
390  std::vector<SrcMgr::SLocEntry> SLocEntryTable;
391  /// NextOffset - This is the next available offset that a new SLocEntry can
392  /// start at.  It is SLocEntryTable.back().getOffset()+size of back() entry.
393  unsigned NextOffset;
394
395  /// \brief If source location entries are being lazily loaded from
396  /// an external source, this vector indicates whether the Ith source
397  /// location entry has already been loaded from the external storage.
398  std::vector<bool> SLocEntryLoaded;
399
400  /// \brief An external source for source location entries.
401  ExternalSLocEntrySource *ExternalSLocEntries;
402
403  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
404  /// LastFileIDLookup records the last FileID looked up or created, because it
405  /// is very common to look up many tokens from the same file.
406  mutable FileID LastFileIDLookup;
407
408  /// LineTable - This holds information for #line directives.  It is referenced
409  /// by indices from SLocEntryTable.
410  LineTableInfo *LineTable;
411
412  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
413  /// method which is used to speedup getLineNumber calls to nearby locations.
414  mutable FileID LastLineNoFileIDQuery;
415  mutable SrcMgr::ContentCache *LastLineNoContentCache;
416  mutable unsigned LastLineNoFilePos;
417  mutable unsigned LastLineNoResult;
418
419  /// MainFileID - The file ID for the main source file of the translation unit.
420  FileID MainFileID;
421
422  // Statistics for -print-stats.
423  mutable unsigned NumLinearScans, NumBinaryProbes;
424
425  // Cache results for the isBeforeInTranslationUnit method.
426  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
427
428  // SourceManager doesn't support copy construction.
429  explicit SourceManager(const SourceManager&);
430  void operator=(const SourceManager&);
431public:
432  SourceManager(Diagnostic &Diag, FileManager &FileMgr);
433  ~SourceManager();
434
435  void clearIDTables();
436
437  Diagnostic &getDiagnostics() const { return Diag; }
438
439  FileManager &getFileManager() const { return FileMgr; }
440
441  //===--------------------------------------------------------------------===//
442  // MainFileID creation and querying methods.
443  //===--------------------------------------------------------------------===//
444
445  /// getMainFileID - Returns the FileID of the main source file.
446  FileID getMainFileID() const { return MainFileID; }
447
448  /// createMainFileID - Create the FileID for the main source file.
449  FileID createMainFileID(const FileEntry *SourceFile) {
450    assert(MainFileID.isInvalid() && "MainFileID already set!");
451    MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
452    return MainFileID;
453  }
454
455  /// \brief Set the file ID for the precompiled preamble, which is also the
456  /// main file.
457  void SetPreambleFileID(FileID Preamble) {
458    assert(MainFileID.isInvalid() && "MainFileID already set!");
459    MainFileID = Preamble;
460  }
461
462  //===--------------------------------------------------------------------===//
463  // Methods to create new FileID's and instantiations.
464  //===--------------------------------------------------------------------===//
465
466  /// createFileID - Create a new FileID that represents the specified file
467  /// being #included from the specified IncludePosition.  This returns 0 on
468  /// error and translates NULL into standard input.
469  /// PreallocateID should be non-zero to specify which pre-allocated,
470  /// lazily computed source location is being filled in by this operation.
471  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
472                      SrcMgr::CharacteristicKind FileCharacter,
473                      unsigned PreallocatedID = 0,
474                      unsigned Offset = 0) {
475    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
476    assert(IR && "getOrCreateContentCache() cannot return NULL");
477    return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset);
478  }
479
480  /// createFileIDForMemBuffer - Create a new FileID that represents the
481  /// specified memory buffer.  This does no caching of the buffer and takes
482  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
483  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
484                                  unsigned PreallocatedID = 0,
485                                  unsigned Offset = 0) {
486    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
487                        SrcMgr::C_User, PreallocatedID, Offset);
488  }
489
490  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
491  ///  that will represent the FileID for the main source.  One example
492  ///  of when this would be used is when the main source is read from STDIN.
493  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
494    assert(MainFileID.isInvalid() && "MainFileID already set!");
495    MainFileID = createFileIDForMemBuffer(Buffer);
496    return MainFileID;
497  }
498
499  /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
500  /// that a token at Loc should actually be referenced from InstantiationLoc.
501  /// TokLength is the length of the token being instantiated.
502  SourceLocation createInstantiationLoc(SourceLocation Loc,
503                                        SourceLocation InstantiationLocStart,
504                                        SourceLocation InstantiationLocEnd,
505                                        unsigned TokLength,
506                                        unsigned PreallocatedID = 0,
507                                        unsigned Offset = 0);
508
509  /// \brief Retrieve the memory buffer associated with the given file.
510  ///
511  /// \param Invalid If non-NULL, will be set \c true if an error
512  /// occurs while retrieving the memory buffer.
513  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
514                                                   bool *Invalid = 0);
515
516  /// \brief Override the contents of the given source file by providing an
517  /// already-allocated buffer.
518  ///
519  /// \param SourceFile the source file whose contents will be overriden.
520  ///
521  /// \param Buffer the memory buffer whose contents will be used as the
522  /// data in the given source file.
523  ///
524  /// \param DoNotFree If true, then the buffer will not be freed when the
525  /// source manager is destroyed.
526  void overrideFileContents(const FileEntry *SourceFile,
527                            const llvm::MemoryBuffer *Buffer,
528                            bool DoNotFree = false);
529
530  //===--------------------------------------------------------------------===//
531  // FileID manipulation methods.
532  //===--------------------------------------------------------------------===//
533
534  /// getBuffer - Return the buffer for the specified FileID. If there is an
535  /// error opening this buffer the first time, this manufactures a temporary
536  /// buffer and returns a non-empty error string.
537  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
538                                      bool *Invalid = 0) const {
539    return getSLocEntry(FID).getFile().getContentCache()
540       ->getBuffer(Diag, *this, Loc, Invalid);
541  }
542
543  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
544    return getSLocEntry(FID).getFile().getContentCache()
545       ->getBuffer(Diag, *this, SourceLocation(), Invalid);
546  }
547
548  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
549  const FileEntry *getFileEntryForID(FileID FID) const {
550    return getSLocEntry(FID).getFile().getContentCache()->Entry;
551  }
552
553  /// getBufferData - Return a StringRef to the source buffer data for the
554  /// specified FileID.
555  ///
556  /// \param FID The file ID whose contents will be returned.
557  /// \param Invalid If non-NULL, will be set true if an error occurred.
558  llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
559
560
561  //===--------------------------------------------------------------------===//
562  // SourceLocation manipulation methods.
563  //===--------------------------------------------------------------------===//
564
565  /// getFileID - Return the FileID for a SourceLocation.  This is a very
566  /// hot method that is used for all SourceManager queries that start with a
567  /// SourceLocation object.  It is responsible for finding the entry in
568  /// SLocEntryTable which contains the specified location.
569  ///
570  FileID getFileID(SourceLocation SpellingLoc) const {
571    unsigned SLocOffset = SpellingLoc.getOffset();
572
573    // If our one-entry cache covers this offset, just return it.
574    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
575      return LastFileIDLookup;
576
577    return getFileIDSlow(SLocOffset);
578  }
579
580  /// getLocForStartOfFile - Return the source location corresponding to the
581  /// first byte of the specified file.
582  SourceLocation getLocForStartOfFile(FileID FID) const {
583    assert(FID.ID < SLocEntryTable.size() && "FileID out of range");
584    assert(getSLocEntry(FID).isFile() && "FileID is not a file");
585    unsigned FileOffset = getSLocEntry(FID).getOffset();
586    return SourceLocation::getFileLoc(FileOffset);
587  }
588
589  /// getInstantiationLoc - Given a SourceLocation object, return the
590  /// instantiation location referenced by the ID.
591  SourceLocation getInstantiationLoc(SourceLocation Loc) const {
592    // Handle the non-mapped case inline, defer to out of line code to handle
593    // instantiations.
594    if (Loc.isFileID()) return Loc;
595    return getInstantiationLocSlowCase(Loc);
596  }
597
598  /// getImmediateInstantiationRange - Loc is required to be an instantiation
599  /// location.  Return the start/end of the instantiation information.
600  std::pair<SourceLocation,SourceLocation>
601  getImmediateInstantiationRange(SourceLocation Loc) const;
602
603  /// getInstantiationRange - Given a SourceLocation object, return the
604  /// range of tokens covered by the instantiation in the ultimate file.
605  std::pair<SourceLocation,SourceLocation>
606  getInstantiationRange(SourceLocation Loc) const;
607
608
609  /// getSpellingLoc - Given a SourceLocation object, return the spelling
610  /// location referenced by the ID.  This is the place where the characters
611  /// that make up the lexed token can be found.
612  SourceLocation getSpellingLoc(SourceLocation Loc) const {
613    // Handle the non-mapped case inline, defer to out of line code to handle
614    // instantiations.
615    if (Loc.isFileID()) return Loc;
616    return getSpellingLocSlowCase(Loc);
617  }
618
619  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
620  /// spelling location referenced by the ID.  This is the first level down
621  /// towards the place where the characters that make up the lexed token can be
622  /// found.  This should not generally be used by clients.
623  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
624
625  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
626  /// Offset pair.  The first element is the FileID, the second is the
627  /// offset from the start of the buffer of the location.
628  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
629    FileID FID = getFileID(Loc);
630    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
631  }
632
633  /// getDecomposedInstantiationLoc - Decompose the specified location into a
634  /// raw FileID + Offset pair.  If the location is an instantiation record,
635  /// walk through it until we find the final location instantiated.
636  std::pair<FileID, unsigned>
637  getDecomposedInstantiationLoc(SourceLocation Loc) const {
638    FileID FID = getFileID(Loc);
639    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
640
641    unsigned Offset = Loc.getOffset()-E->getOffset();
642    if (Loc.isFileID())
643      return std::make_pair(FID, Offset);
644
645    return getDecomposedInstantiationLocSlowCase(E, Offset);
646  }
647
648  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
649  /// FileID + Offset pair.  If the location is an instantiation record, walk
650  /// through it until we find its spelling record.
651  std::pair<FileID, unsigned>
652  getDecomposedSpellingLoc(SourceLocation Loc) const {
653    FileID FID = getFileID(Loc);
654    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
655
656    unsigned Offset = Loc.getOffset()-E->getOffset();
657    if (Loc.isFileID())
658      return std::make_pair(FID, Offset);
659    return getDecomposedSpellingLocSlowCase(E, Offset);
660  }
661
662  /// getFileOffset - This method returns the offset from the start
663  /// of the file that the specified SourceLocation represents. This is not very
664  /// meaningful for a macro ID.
665  unsigned getFileOffset(SourceLocation SpellingLoc) const {
666    return getDecomposedLoc(SpellingLoc).second;
667  }
668
669
670  //===--------------------------------------------------------------------===//
671  // Queries about the code at a SourceLocation.
672  //===--------------------------------------------------------------------===//
673
674  /// getCharacterData - Return a pointer to the start of the specified location
675  /// in the appropriate spelling MemoryBuffer.
676  ///
677  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
678  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
679
680  /// getColumnNumber - Return the column # for the specified file position.
681  /// This is significantly cheaper to compute than the line number.  This
682  /// returns zero if the column number isn't known.  This may only be called on
683  /// a file sloc, so you must choose a spelling or instantiation location
684  /// before calling this method.
685  unsigned getColumnNumber(FileID FID, unsigned FilePos,
686                           bool *Invalid = 0) const;
687  unsigned getSpellingColumnNumber(SourceLocation Loc,
688                                   bool *Invalid = 0) const;
689  unsigned getInstantiationColumnNumber(SourceLocation Loc,
690                                        bool *Invalid = 0) const;
691
692
693  /// getLineNumber - Given a SourceLocation, return the spelling line number
694  /// for the position indicated.  This requires building and caching a table of
695  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
696  /// about to emit a diagnostic.
697  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
698
699  unsigned getInstantiationLineNumber(SourceLocation Loc,
700                                      bool *Invalid = 0) const;
701  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
702
703  /// Return the filename or buffer identifier of the buffer the location is in.
704  /// Note that this name does not respect #line directives.  Use getPresumedLoc
705  /// for normal clients.
706  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
707
708  /// getFileCharacteristic - return the file characteristic of the specified
709  /// source location, indicating whether this is a normal file, a system
710  /// header, or an "implicit extern C" system header.
711  ///
712  /// This state can be modified with flags on GNU linemarker directives like:
713  ///   # 4 "foo.h" 3
714  /// which changes all source locations in the current file after that to be
715  /// considered to be from a system header.
716  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
717
718  /// getPresumedLoc - This method returns the "presumed" location of a
719  /// SourceLocation specifies.  A "presumed location" can be modified by #line
720  /// or GNU line marker directives.  This provides a view on the data that a
721  /// user should see in diagnostics, for example.
722  ///
723  /// Note that a presumed location is always given as the instantiation point
724  /// of an instantiation location, not at the spelling location.
725  ///
726  /// \returns The presumed location of the specified SourceLocation. If the
727  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
728  /// or the file containing \p Loc has changed on disk), returns an invalid
729  /// presumed location.
730  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
731
732  /// isFromSameFile - Returns true if both SourceLocations correspond to
733  ///  the same file.
734  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
735    return getFileID(Loc1) == getFileID(Loc2);
736  }
737
738  /// isFromMainFile - Returns true if the file of provided SourceLocation is
739  ///   the main file.
740  bool isFromMainFile(SourceLocation Loc) const {
741    return getFileID(Loc) == getMainFileID();
742  }
743
744  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
745  bool isInSystemHeader(SourceLocation Loc) const {
746    return getFileCharacteristic(Loc) != SrcMgr::C_User;
747  }
748
749  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
750  /// system header.
751  bool isInExternCSystemHeader(SourceLocation Loc) const {
752    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
753  }
754
755  //===--------------------------------------------------------------------===//
756  // Line Table Manipulation Routines
757  //===--------------------------------------------------------------------===//
758
759  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
760  ///
761  unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
762
763  /// AddLineNote - Add a line note to the line table for the FileID and offset
764  /// specified by Loc.  If FilenameID is -1, it is considered to be
765  /// unspecified.
766  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
767  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
768                   bool IsFileEntry, bool IsFileExit,
769                   bool IsSystemHeader, bool IsExternCHeader);
770
771  /// \brief Determine if the source manager has a line table.
772  bool hasLineTable() const { return LineTable != 0; }
773
774  /// \brief Retrieve the stored line table.
775  LineTableInfo &getLineTable();
776
777  //===--------------------------------------------------------------------===//
778  // Other miscellaneous methods.
779  //===--------------------------------------------------------------------===//
780
781  /// \brief Get the source location for the given file:line:col triplet.
782  ///
783  /// If the source file is included multiple times, the source location will
784  /// be based upon the first inclusion.
785  SourceLocation getLocation(const FileEntry *SourceFile,
786                             unsigned Line, unsigned Col) const;
787
788  /// \brief Determines the order of 2 source locations in the translation unit.
789  ///
790  /// \returns true if LHS source location comes before RHS, false otherwise.
791  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
792
793  // Iterators over FileInfos.
794  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
795      ::const_iterator fileinfo_iterator;
796  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
797  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
798  bool hasFileInfo(const FileEntry *File) const {
799    return FileInfos.find(File) != FileInfos.end();
800  }
801
802  /// PrintStats - Print statistics to stderr.
803  ///
804  void PrintStats() const;
805
806  unsigned sloc_entry_size() const { return SLocEntryTable.size(); }
807
808  // FIXME: Exposing this is a little gross; what we want is a good way
809  //  to iterate the entries that were not defined in an AST file (or
810  //  any other external source).
811  unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); }
812
813  const SrcMgr::SLocEntry &getSLocEntry(unsigned ID) const {
814    assert(ID < SLocEntryTable.size() && "Invalid id");
815    if (ExternalSLocEntries &&
816        ID < SLocEntryLoaded.size() &&
817        !SLocEntryLoaded[ID])
818      ExternalSLocEntries->ReadSLocEntry(ID);
819    return SLocEntryTable[ID];
820  }
821
822  const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const {
823    return getSLocEntry(FID.ID);
824  }
825
826  unsigned getNextOffset() const { return NextOffset; }
827
828  /// \brief Preallocate some number of source location entries, which
829  /// will be loaded as needed from the given external source.
830  void PreallocateSLocEntries(ExternalSLocEntrySource *Source,
831                              unsigned NumSLocEntries,
832                              unsigned NextOffset);
833
834  /// \brief Clear out any preallocated source location entries that
835  /// haven't already been loaded.
836  void ClearPreallocatedSLocEntries();
837
838private:
839  /// isOffsetInFileID - Return true if the specified FileID contains the
840  /// specified SourceLocation offset.  This is a very hot method.
841  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
842    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
843    // If the entry is after the offset, it can't contain it.
844    if (SLocOffset < Entry.getOffset()) return false;
845
846    // If this is the last entry than it does.  Otherwise, the entry after it
847    // has to not include it.
848    if (FID.ID+1 == SLocEntryTable.size()) return true;
849
850    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
851  }
852
853  /// createFileID - Create a new fileID for the specified ContentCache and
854  ///  include position.  This works regardless of whether the ContentCache
855  ///  corresponds to a file or some other input source.
856  FileID createFileID(const SrcMgr::ContentCache* File,
857                      SourceLocation IncludePos,
858                      SrcMgr::CharacteristicKind DirCharacter,
859                      unsigned PreallocatedID = 0,
860                      unsigned Offset = 0);
861
862  const SrcMgr::ContentCache *
863    getOrCreateContentCache(const FileEntry *SourceFile);
864
865  /// createMemBufferContentCache - Create a new ContentCache for the specified
866  ///  memory buffer.
867  const SrcMgr::ContentCache*
868  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
869
870  FileID getFileIDSlow(unsigned SLocOffset) const;
871
872  SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
873  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
874
875  std::pair<FileID, unsigned>
876  getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
877                                        unsigned Offset) const;
878  std::pair<FileID, unsigned>
879  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
880                                   unsigned Offset) const;
881};
882
883
884}  // end namespace clang
885
886#endif
887