]> granicus.if.org Git - clang/blob - include/clang/Basic/SourceManager.h
Add a FileCharacteristic parameter to SourceManager::createFileIDForMemBuffer
[clang] / include / clang / Basic / SourceManager.h
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 /// \file
11 /// \brief Defines the SourceManager interface.
12 ///
13 /// There are three different types of locations in a file: a spelling
14 /// location, an expansion location, and a presumed location.
15 ///
16 /// Given an example of:
17 /// \code
18 /// #define min(x, y) x < y ? x : y
19 /// \endcode
20 ///
21 /// and then later on a use of min:
22 /// \code
23 /// #line 17
24 /// return min(a, b);
25 /// \endcode
26 ///
27 /// The expansion location is the line in the source code where the macro
28 /// was expanded (the return statement), the spelling location is the
29 /// location in the source where the macro was originally defined,
30 /// and the presumed location is where the line directive states that
31 /// the line is 17, or any other line.
32 ///
33 //===----------------------------------------------------------------------===//
34
35 #ifndef LLVM_CLANG_SOURCEMANAGER_H
36 #define LLVM_CLANG_SOURCEMANAGER_H
37
38 #include "clang/Basic/LLVM.h"
39 #include "clang/Basic/FileManager.h"
40 #include "clang/Basic/SourceLocation.h"
41 #include "llvm/Support/Allocator.h"
42 #include "llvm/Support/DataTypes.h"
43 #include "llvm/ADT/PointerIntPair.h"
44 #include "llvm/ADT/PointerUnion.h"
45 #include "llvm/ADT/IntrusiveRefCntPtr.h"
46 #include "llvm/ADT/OwningPtr.h"
47 #include "llvm/ADT/DenseMap.h"
48 #include "llvm/ADT/DenseSet.h"
49 #include "llvm/Support/MemoryBuffer.h"
50 #include <map>
51 #include <vector>
52 #include <cassert>
53
54 namespace clang {
55
56 class DiagnosticsEngine;
57 class SourceManager;
58 class FileManager;
59 class FileEntry;
60 class LineTableInfo;
61 class LangOptions;
62 class ASTWriter;
63 class ASTReader;
64
65 /// \brief Public enums and private classes that are part of the
66 /// SourceManager implementation.
67 ///
68 namespace SrcMgr {
69   /// \brief Indicates whether a file or directory holds normal user code,
70   /// system code, or system code which is implicitly 'extern "C"' in C++ mode.
71   ///
72   /// Entire directories can be tagged with this (this is maintained by
73   /// DirectoryLookup and friends) as can specific FileInfos when a \#pragma
74   /// system_header is seen or in various other cases.
75   ///
76   enum CharacteristicKind {
77     C_User, C_System, C_ExternCSystem
78   };
79
80   /// \brief One instance of this struct is kept for every file loaded or used.
81   ////
82   /// This object owns the MemoryBuffer object.
83   class ContentCache {
84     enum CCFlags {
85       /// \brief Whether the buffer is invalid.
86       InvalidFlag = 0x01,
87       /// \brief Whether the buffer should not be freed on destruction.
88       DoNotFreeFlag = 0x02
89     };
90
91     /// \brief The actual buffer containing the characters from the input
92     /// file.
93     ///
94     /// This is owned by the ContentCache object.  The bits indicate
95     /// whether the buffer is invalid.
96     mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
97
98   public:
99     /// \brief Reference to the file entry representing this ContentCache.
100     ///
101     /// This reference does not own the FileEntry object.
102     ///
103     /// It is possible for this to be NULL if the ContentCache encapsulates
104     /// an imaginary text buffer.
105     const FileEntry *OrigEntry;
106
107     /// \brief References the file which the contents were actually loaded from.
108     ///
109     /// Can be different from 'Entry' if we overridden the contents of one file
110     /// with the contents of another file.
111     const FileEntry *ContentsEntry;
112
113     /// \brief A bump pointer allocated array of offsets for each source line.
114     ///
115     /// This is lazily computed.  This is owned by the SourceManager
116     /// BumpPointerAllocator object.
117     unsigned *SourceLineCache;
118
119     /// \brief The number of lines in this ContentCache.
120     ///
121     /// This is only valid if SourceLineCache is non-null.
122     unsigned NumLines : 31;
123
124     /// \brief Indicates whether the buffer itself was provided to override
125     /// the actual file contents.
126     ///
127     /// When true, the original entry may be a virtual file that does not
128     /// exist.
129     unsigned BufferOverridden : 1;
130
131     /// \brief True if this content cache was initially created for a source
132     /// file considered as a system one.
133     unsigned IsSystemFile : 1;
134     
135     ContentCache(const FileEntry *Ent = 0)
136       : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
137         SourceLineCache(0), NumLines(0), BufferOverridden(false),
138         IsSystemFile(false) {}
139     
140     ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
141       : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
142         SourceLineCache(0), NumLines(0), BufferOverridden(false),
143         IsSystemFile(false) {}
144     
145     ~ContentCache();
146     
147     /// The copy ctor does not allow copies where source object has either
148     /// a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
149     /// is not transferred, so this is a logical error.
150     ContentCache(const ContentCache &RHS)
151       : Buffer(0, false), SourceLineCache(0), BufferOverridden(false),
152         IsSystemFile(false)
153     {
154       OrigEntry = RHS.OrigEntry;
155       ContentsEntry = RHS.ContentsEntry;
156       
157       assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 &&
158               "Passed ContentCache object cannot own a buffer.");
159       
160       NumLines = RHS.NumLines;
161     }
162
163     /// \brief Returns the memory buffer for the associated content.
164     ///
165     /// \param Diag Object through which diagnostics will be emitted if the
166     ///   buffer cannot be retrieved.
167     ///
168     /// \param Loc If specified, is the location that invalid file diagnostics
169     ///   will be emitted at.
170     ///
171     /// \param Invalid If non-NULL, will be set \c true if an error occurred.
172     const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag,
173                                         const SourceManager &SM,
174                                         SourceLocation Loc = SourceLocation(),
175                                         bool *Invalid = 0) const;
176
177     /// \brief Returns the size of the content encapsulated by this
178     /// ContentCache.
179     ///
180     /// This can be the size of the source file or the size of an
181     /// arbitrary scratch buffer.  If the ContentCache encapsulates a source
182     /// file this size is retrieved from the file's FileEntry.
183     unsigned getSize() const;
184
185     /// \brief Returns the number of bytes actually mapped for this
186     /// ContentCache.
187     ///
188     /// This can be 0 if the MemBuffer was not actually expanded.
189     unsigned getSizeBytesMapped() const;
190
191     /// Returns the kind of memory used to back the memory buffer for
192     /// this content cache.  This is used for performance analysis.
193     llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
194
195     void setBuffer(const llvm::MemoryBuffer *B) {
196       assert(!Buffer.getPointer() && "MemoryBuffer already set.");
197       Buffer.setPointer(B);
198       Buffer.setInt(false);
199     }
200
201     /// \brief Get the underlying buffer, returning NULL if the buffer is not
202     /// yet available.
203     const llvm::MemoryBuffer *getRawBuffer() const {
204       return Buffer.getPointer();
205     }
206
207     /// \brief Replace the existing buffer (which will be deleted)
208     /// with the given buffer.
209     void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
210
211     /// \brief Determine whether the buffer itself is invalid.
212     bool isBufferInvalid() const {
213       return Buffer.getInt() & InvalidFlag;
214     }
215
216     /// \brief Determine whether the buffer should be freed.
217     bool shouldFreeBuffer() const {
218       return (Buffer.getInt() & DoNotFreeFlag) == 0;
219     }
220
221   private:
222     // Disable assignments.
223     ContentCache &operator=(const ContentCache& RHS) LLVM_DELETED_FUNCTION;
224   };
225
226   /// \brief Information about a FileID, basically just the logical file
227   /// that it represents and include stack information.
228   ///
229   /// Each FileInfo has include stack information, indicating where it came
230   /// from. This information encodes the \#include chain that a token was
231   /// expanded from. The main include file has an invalid IncludeLoc.
232   ///
233   /// FileInfos contain a "ContentCache *", with the contents of the file.
234   ///
235   class FileInfo {
236     /// \brief The location of the \#include that brought in this file.
237     ///
238     /// This is an invalid SLOC for the main file (top of the \#include chain).
239     unsigned IncludeLoc;  // Really a SourceLocation
240
241     /// \brief Number of FileIDs (files and macros) that were created during
242     /// preprocessing of this \#include, including this SLocEntry.
243     ///
244     /// Zero means the preprocessor didn't provide such info for this SLocEntry.
245     unsigned NumCreatedFIDs;
246
247     /// \brief Contains the ContentCache* and the bits indicating the
248     /// characteristic of the file and whether it has \#line info, all
249     /// bitmangled together.
250     uintptr_t Data;
251
252     friend class clang::SourceManager;
253     friend class clang::ASTWriter;
254     friend class clang::ASTReader;
255   public:
256     /// \brief Return a FileInfo object.
257     static FileInfo get(SourceLocation IL, const ContentCache *Con,
258                         CharacteristicKind FileCharacter) {
259       FileInfo X;
260       X.IncludeLoc = IL.getRawEncoding();
261       X.NumCreatedFIDs = 0;
262       X.Data = (uintptr_t)Con;
263       assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
264       assert((unsigned)FileCharacter < 4 && "invalid file character");
265       X.Data |= (unsigned)FileCharacter;
266       return X;
267     }
268
269     SourceLocation getIncludeLoc() const {
270       return SourceLocation::getFromRawEncoding(IncludeLoc);
271     }
272     const ContentCache* getContentCache() const {
273       return reinterpret_cast<const ContentCache*>(Data & ~7UL);
274     }
275
276     /// \brief Return whether this is a system header or not.
277     CharacteristicKind getFileCharacteristic() const {
278       return (CharacteristicKind)(Data & 3);
279     }
280
281     /// \brief Return true if this FileID has \#line directives in it.
282     bool hasLineDirectives() const { return (Data & 4) != 0; }
283
284     /// \brief Set the flag that indicates that this FileID has
285     /// line table entries associated with it.
286     void setHasLineDirectives() {
287       Data |= 4;
288     }
289   };
290
291   /// \brief Each ExpansionInfo encodes the expansion location - where
292   /// the token was ultimately expanded, and the SpellingLoc - where the actual
293   /// character data for the token came from.
294   class ExpansionInfo {
295     // Really these are all SourceLocations.
296
297     /// \brief Where the spelling for the token can be found.
298     unsigned SpellingLoc;
299
300     /// In a macro expansion, ExpansionLocStart and ExpansionLocEnd
301     /// indicate the start and end of the expansion. In object-like macros,
302     /// they will be the same. In a function-like macro expansion, the start
303     /// will be the identifier and the end will be the ')'. Finally, in
304     /// macro-argument instantiations, the end will be 'SourceLocation()', an
305     /// invalid location.
306     unsigned ExpansionLocStart, ExpansionLocEnd;
307
308   public:
309     SourceLocation getSpellingLoc() const {
310       return SourceLocation::getFromRawEncoding(SpellingLoc);
311     }
312     SourceLocation getExpansionLocStart() const {
313       return SourceLocation::getFromRawEncoding(ExpansionLocStart);
314     }
315     SourceLocation getExpansionLocEnd() const {
316       SourceLocation EndLoc =
317         SourceLocation::getFromRawEncoding(ExpansionLocEnd);
318       return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
319     }
320
321     std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
322       return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
323     }
324
325     bool isMacroArgExpansion() const {
326       // Note that this needs to return false for default constructed objects.
327       return getExpansionLocStart().isValid() &&
328         SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
329     }
330
331     bool isFunctionMacroExpansion() const {
332       return getExpansionLocStart().isValid() &&
333           getExpansionLocStart() != getExpansionLocEnd();
334     }
335
336     /// \brief Return a ExpansionInfo for an expansion.
337     ///
338     /// Start and End specify the expansion range (where the macro is
339     /// expanded), and SpellingLoc specifies the spelling location (where
340     /// the characters from the token come from). All three can refer to
341     /// normal File SLocs or expansion locations.
342     static ExpansionInfo create(SourceLocation SpellingLoc,
343                                 SourceLocation Start, SourceLocation End) {
344       ExpansionInfo X;
345       X.SpellingLoc = SpellingLoc.getRawEncoding();
346       X.ExpansionLocStart = Start.getRawEncoding();
347       X.ExpansionLocEnd = End.getRawEncoding();
348       return X;
349     }
350
351     /// \brief Return a special ExpansionInfo for the expansion of
352     /// a macro argument into a function-like macro's body.
353     ///
354     /// ExpansionLoc specifies the expansion location (where the macro is
355     /// expanded). This doesn't need to be a range because a macro is always
356     /// expanded at a macro parameter reference, and macro parameters are
357     /// always exactly one token. SpellingLoc specifies the spelling location
358     /// (where the characters from the token come from). ExpansionLoc and
359     /// SpellingLoc can both refer to normal File SLocs or expansion locations.
360     ///
361     /// Given the code:
362     /// \code
363     ///   #define F(x) f(x)
364     ///   F(42);
365     /// \endcode
366     ///
367     /// When expanding '\c F(42)', the '\c x' would call this with an
368     /// SpellingLoc pointing at '\c 42' and an ExpansionLoc pointing at its
369     /// location in the definition of '\c F'.
370     static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
371                                            SourceLocation ExpansionLoc) {
372       // We store an intentionally invalid source location for the end of the
373       // expansion range to mark that this is a macro argument ion rather than
374       // a normal one.
375       return create(SpellingLoc, ExpansionLoc, SourceLocation());
376     }
377   };
378
379   /// \brief This is a discriminated union of FileInfo and ExpansionInfo.
380   ///
381   /// SourceManager keeps an array of these objects, and they are uniquely
382   /// identified by the FileID datatype.
383   class SLocEntry {
384     unsigned Offset;   // low bit is set for expansion info.
385     union {
386       FileInfo File;
387       ExpansionInfo Expansion;
388     };
389   public:
390     unsigned getOffset() const { return Offset >> 1; }
391
392     bool isExpansion() const { return Offset & 1; }
393     bool isFile() const { return !isExpansion(); }
394
395     const FileInfo &getFile() const {
396       assert(isFile() && "Not a file SLocEntry!");
397       return File;
398     }
399
400     const ExpansionInfo &getExpansion() const {
401       assert(isExpansion() && "Not a macro expansion SLocEntry!");
402       return Expansion;
403     }
404
405     static SLocEntry get(unsigned Offset, const FileInfo &FI) {
406       SLocEntry E;
407       E.Offset = Offset << 1;
408       E.File = FI;
409       return E;
410     }
411
412     static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
413       SLocEntry E;
414       E.Offset = (Offset << 1) | 1;
415       E.Expansion = Expansion;
416       return E;
417     }
418   };
419 }  // end SrcMgr namespace.
420
421 /// \brief External source of source location entries.
422 class ExternalSLocEntrySource {
423 public:
424   virtual ~ExternalSLocEntrySource();
425
426   /// \brief Read the source location entry with index ID, which will always be
427   /// less than -1.
428   ///
429   /// \returns true if an error occurred that prevented the source-location
430   /// entry from being loaded.
431   virtual bool ReadSLocEntry(int ID) = 0;
432 };
433
434
435 /// \brief Holds the cache used by isBeforeInTranslationUnit.
436 ///
437 /// The cache structure is complex enough to be worth breaking out of
438 /// SourceManager.
439 class IsBeforeInTranslationUnitCache {
440   /// \brief The FileID's of the cached query.
441   ///
442   /// If these match up with a subsequent query, the result can be reused.
443   FileID LQueryFID, RQueryFID;
444
445   /// \brief True if LQueryFID was created before RQueryFID.
446   ///
447   /// This is used to compare macro expansion locations.
448   bool IsLQFIDBeforeRQFID;
449
450   /// \brief The file found in common between the two \#include traces, i.e.,
451   /// the nearest common ancestor of the \#include tree.
452   FileID CommonFID;
453
454   /// \brief The offset of the previous query in CommonFID.
455   ///
456   /// Usually, this represents the location of the \#include for QueryFID, but
457   /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a
458   /// random token in the parent.
459   unsigned LCommonOffset, RCommonOffset;
460 public:
461
462   /// \brief Return true if the currently cached values match up with
463   /// the specified LHS/RHS query.
464   ///
465   /// If not, we can't use the cache.
466   bool isCacheValid(FileID LHS, FileID RHS) const {
467     return LQueryFID == LHS && RQueryFID == RHS;
468   }
469
470   /// \brief If the cache is valid, compute the result given the
471   /// specified offsets in the LHS/RHS FileID's.
472   bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
473     // If one of the query files is the common file, use the offset.  Otherwise,
474     // use the #include loc in the common file.
475     if (LQueryFID != CommonFID) LOffset = LCommonOffset;
476     if (RQueryFID != CommonFID) ROffset = RCommonOffset;
477
478     // It is common for multiple macro expansions to be "included" from the same
479     // location (expansion location), in which case use the order of the FileIDs
480     // to determine which came first. This will also take care the case where
481     // one of the locations points at the inclusion/expansion point of the other
482     // in which case its FileID will come before the other.
483     if (LOffset == ROffset)
484       return IsLQFIDBeforeRQFID;
485
486     return LOffset < ROffset;
487   }
488
489   /// \brief Set up a new query.
490   void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
491     assert(LHS != RHS);
492     LQueryFID = LHS;
493     RQueryFID = RHS;
494     IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
495   }
496
497   void clear() {
498     LQueryFID = RQueryFID = FileID();
499     IsLQFIDBeforeRQFID = false;
500   }
501
502   void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
503                     unsigned rCommonOffset) {
504     CommonFID = commonFID;
505     LCommonOffset = lCommonOffset;
506     RCommonOffset = rCommonOffset;
507   }
508
509 };
510
511 /// \brief This class handles loading and caching of source files into memory.
512 ///
513 /// This object owns the MemoryBuffer objects for all of the loaded
514 /// files and assigns unique FileID's for each unique \#include chain.
515 ///
516 /// The SourceManager can be queried for information about SourceLocation
517 /// objects, turning them into either spelling or expansion locations. Spelling
518 /// locations represent where the bytes corresponding to a token came from and
519 /// expansion locations represent where the location is in the user's view. In
520 /// the case of a macro expansion, for example, the spelling location indicates
521 /// where the expanded token came from and the expansion location specifies
522 /// where it was expanded.
523 class SourceManager : public RefCountedBase<SourceManager> {
524   /// \brief DiagnosticsEngine object.
525   DiagnosticsEngine &Diag;
526
527   FileManager &FileMgr;
528
529   mutable llvm::BumpPtrAllocator ContentCacheAlloc;
530
531   /// \brief Memoized information about all of the files tracked by this
532   /// SourceManager.
533   ///
534   /// This map allows us to merge ContentCache entries based
535   /// on their FileEntry*.  All ContentCache objects will thus have unique,
536   /// non-null, FileEntry pointers.
537   llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
538
539   /// \brief True if the ContentCache for files that are overriden by other
540   /// files, should report the original file name. Defaults to true.
541   bool OverridenFilesKeepOriginalName;
542
543   /// \brief True if non-system source files should be treated as volatile
544   /// (likely to change while trying to use them). Defaults to false.
545   bool UserFilesAreVolatile;
546
547   struct OverriddenFilesInfoTy {
548     /// \brief Files that have been overriden with the contents from another
549     /// file.
550     llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
551     /// \brief Files that were overridden with a memory buffer.
552     llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer;
553   };
554
555   /// \brief Lazily create the object keeping overridden files info, since
556   /// it is uncommonly used.
557   OwningPtr<OverriddenFilesInfoTy> OverriddenFilesInfo;
558
559   OverriddenFilesInfoTy &getOverriddenFilesInfo() {
560     if (!OverriddenFilesInfo)
561       OverriddenFilesInfo.reset(new OverriddenFilesInfoTy);
562     return *OverriddenFilesInfo;
563   }
564
565   /// \brief Information about various memory buffers that we have read in.
566   ///
567   /// All FileEntry* within the stored ContentCache objects are NULL,
568   /// as they do not refer to a file.
569   std::vector<SrcMgr::ContentCache*> MemBufferInfos;
570
571   /// \brief The table of SLocEntries that are local to this module.
572   ///
573   /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
574   /// expansion.
575   std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
576
577   /// \brief The table of SLocEntries that are loaded from other modules.
578   ///
579   /// Negative FileIDs are indexes into this table. To get from ID to an index,
580   /// use (-ID - 2).
581   mutable std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
582
583   /// \brief The starting offset of the next local SLocEntry.
584   ///
585   /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
586   unsigned NextLocalOffset;
587
588   /// \brief The starting offset of the latest batch of loaded SLocEntries.
589   ///
590   /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
591   /// not have been loaded, so that value would be unknown.
592   unsigned CurrentLoadedOffset;
593
594   /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
595   /// starts at 2^31.
596   static const unsigned MaxLoadedOffset = 1U << 31U;
597
598   /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
599   /// have already been loaded from the external source.
600   ///
601   /// Same indexing as LoadedSLocEntryTable.
602   std::vector<bool> SLocEntryLoaded;
603
604   /// \brief An external source for source location entries.
605   ExternalSLocEntrySource *ExternalSLocEntries;
606
607   /// \brief A one-entry cache to speed up getFileID.
608   ///
609   /// LastFileIDLookup records the last FileID looked up or created, because it
610   /// is very common to look up many tokens from the same file.
611   mutable FileID LastFileIDLookup;
612
613   /// \brief Holds information for \#line directives.
614   ///
615   /// This is referenced by indices from SLocEntryTable.
616   LineTableInfo *LineTable;
617
618   /// \brief These ivars serve as a cache used in the getLineNumber
619   /// method which is used to speedup getLineNumber calls to nearby locations.
620   mutable FileID LastLineNoFileIDQuery;
621   mutable SrcMgr::ContentCache *LastLineNoContentCache;
622   mutable unsigned LastLineNoFilePos;
623   mutable unsigned LastLineNoResult;
624
625   /// \brief The file ID for the main source file of the translation unit.
626   FileID MainFileID;
627
628   /// \brief The file ID for the precompiled preamble there is one.
629   FileID PreambleFileID;
630
631   // Statistics for -print-stats.
632   mutable unsigned NumLinearScans, NumBinaryProbes;
633
634   // Cache results for the isBeforeInTranslationUnit method.
635   mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
636
637   // Cache for the "fake" buffer used for error-recovery purposes.
638   mutable llvm::MemoryBuffer *FakeBufferForRecovery;
639
640   mutable SrcMgr::ContentCache *FakeContentCacheForRecovery;
641
642   /// \brief Lazily computed map of macro argument chunks to their expanded
643   /// source location.
644   typedef std::map<unsigned, SourceLocation> MacroArgsMap;
645
646   mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap;
647
648   // SourceManager doesn't support copy construction.
649   explicit SourceManager(const SourceManager&) LLVM_DELETED_FUNCTION;
650   void operator=(const SourceManager&) LLVM_DELETED_FUNCTION;
651 public:
652   SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
653                 bool UserFilesAreVolatile = false);
654   ~SourceManager();
655
656   void clearIDTables();
657
658   DiagnosticsEngine &getDiagnostics() const { return Diag; }
659
660   FileManager &getFileManager() const { return FileMgr; }
661
662   /// \brief Set true if the SourceManager should report the original file name
663   /// for contents of files that were overriden by other files.Defaults to true.
664   void setOverridenFilesKeepOriginalName(bool value) {
665     OverridenFilesKeepOriginalName = value;
666   }
667
668   /// \brief True if non-system source files should be treated as volatile
669   /// (likely to change while trying to use them).
670   bool userFilesAreVolatile() const { return UserFilesAreVolatile; }
671
672   /// \brief Create the FileID for a memory buffer that will represent the
673   /// FileID for the main source.
674   ///
675   /// One example of when this would be used is when the main source is read
676   /// from STDIN.
677   FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
678                              SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
679     assert(MainFileID.isInvalid() && "MainFileID already set!");
680     MainFileID = createFileIDForMemBuffer(Buffer, Kind);
681     return MainFileID;
682   }
683
684   //===--------------------------------------------------------------------===//
685   // MainFileID creation and querying methods.
686   //===--------------------------------------------------------------------===//
687
688   /// \brief Returns the FileID of the main source file.
689   FileID getMainFileID() const { return MainFileID; }
690
691   /// \brief Create the FileID for the main source file.
692   FileID createMainFileID(const FileEntry *SourceFile, 
693                           SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
694     assert(MainFileID.isInvalid() && "MainFileID already set!");
695     MainFileID = createFileID(SourceFile, SourceLocation(), Kind);
696     return MainFileID;
697   }
698
699   /// \brief Set the file ID for the main source file.
700   void setMainFileID(FileID FID) {
701     assert(MainFileID.isInvalid() && "MainFileID already set!");
702     MainFileID = FID;
703   }
704
705   /// \brief Set the file ID for the precompiled preamble.
706   void setPreambleFileID(FileID Preamble) {
707     assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
708     PreambleFileID = Preamble;
709   }
710
711   /// \brief Get the file ID for the precompiled preamble if there is one.
712   FileID getPreambleFileID() const { return PreambleFileID; }
713
714   //===--------------------------------------------------------------------===//
715   // Methods to create new FileID's and macro expansions.
716   //===--------------------------------------------------------------------===//
717
718   /// \brief Create a new FileID that represents the specified file
719   /// being \#included from the specified IncludePosition.
720   ///
721   /// This translates NULL into standard input.
722   FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
723                       SrcMgr::CharacteristicKind FileCharacter,
724                       int LoadedID = 0, unsigned LoadedOffset = 0) {
725     const SrcMgr::ContentCache *
726       IR = getOrCreateContentCache(SourceFile,
727                               /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
728     assert(IR && "getOrCreateContentCache() cannot return NULL");
729     return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
730   }
731
732   /// \brief Create a new FileID that represents the specified memory buffer.
733   ///
734   /// This does no caching of the buffer and takes ownership of the
735   /// MemoryBuffer, so only pass a MemoryBuffer to this once.
736   FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
737                       SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
738                                   int LoadedID = 0, unsigned LoadedOffset = 0,
739                                  SourceLocation IncludeLoc = SourceLocation()) {
740     return createFileID(createMemBufferContentCache(Buffer), IncludeLoc,
741                         FileCharacter, LoadedID, LoadedOffset);
742   }
743
744   /// \brief Return a new SourceLocation that encodes the
745   /// fact that a token from SpellingLoc should actually be referenced from
746   /// ExpansionLoc, and that it represents the expansion of a macro argument
747   /// into the function-like macro body.
748   SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
749                                             SourceLocation ExpansionLoc,
750                                             unsigned TokLength);
751
752   /// \brief Return a new SourceLocation that encodes the fact
753   /// that a token from SpellingLoc should actually be referenced from
754   /// ExpansionLoc.
755   SourceLocation createExpansionLoc(SourceLocation Loc,
756                                     SourceLocation ExpansionLocStart,
757                                     SourceLocation ExpansionLocEnd,
758                                     unsigned TokLength,
759                                     int LoadedID = 0,
760                                     unsigned LoadedOffset = 0);
761
762   /// \brief Retrieve the memory buffer associated with the given file.
763   ///
764   /// \param Invalid If non-NULL, will be set \c true if an error
765   /// occurs while retrieving the memory buffer.
766   const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
767                                                    bool *Invalid = 0);
768
769   /// \brief Override the contents of the given source file by providing an
770   /// already-allocated buffer.
771   ///
772   /// \param SourceFile the source file whose contents will be overriden.
773   ///
774   /// \param Buffer the memory buffer whose contents will be used as the
775   /// data in the given source file.
776   ///
777   /// \param DoNotFree If true, then the buffer will not be freed when the
778   /// source manager is destroyed.
779   void overrideFileContents(const FileEntry *SourceFile,
780                             const llvm::MemoryBuffer *Buffer,
781                             bool DoNotFree = false);
782
783   /// \brief Override the given source file with another one.
784   ///
785   /// \param SourceFile the source file which will be overriden.
786   ///
787   /// \param NewFile the file whose contents will be used as the
788   /// data instead of the contents of the given source file.
789   void overrideFileContents(const FileEntry *SourceFile,
790                             const FileEntry *NewFile);
791
792   /// \brief Returns true if the file contents have been overridden.
793   bool isFileOverridden(const FileEntry *File) {
794     if (OverriddenFilesInfo) {
795       if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
796         return true;
797       if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
798           OverriddenFilesInfo->OverriddenFiles.end())
799         return true;
800     }
801     return false;
802   }
803
804   /// \brief Disable overridding the contents of a file, previously enabled
805   /// with #overrideFileContents.
806   ///
807   /// This should be called before parsing has begun.
808   void disableFileContentsOverride(const FileEntry *File);
809
810   //===--------------------------------------------------------------------===//
811   // FileID manipulation methods.
812   //===--------------------------------------------------------------------===//
813
814   /// \brief Return the buffer for the specified FileID.
815   ///
816   /// If there is an error opening this buffer the first time, this
817   /// manufactures a temporary buffer and returns a non-empty error string.
818   const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
819                                       bool *Invalid = 0) const {
820     bool MyInvalid = false;
821     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
822     if (MyInvalid || !Entry.isFile()) {
823       if (Invalid)
824         *Invalid = true;
825
826       return getFakeBufferForRecovery();
827     }
828
829     return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
830                                                         Invalid);
831   }
832
833   const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
834     bool MyInvalid = false;
835     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
836     if (MyInvalid || !Entry.isFile()) {
837       if (Invalid)
838         *Invalid = true;
839
840       return getFakeBufferForRecovery();
841     }
842
843     return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
844                                                         SourceLocation(),
845                                                         Invalid);
846   }
847
848   /// \brief Returns the FileEntry record for the provided FileID.
849   const FileEntry *getFileEntryForID(FileID FID) const {
850     bool MyInvalid = false;
851     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
852     if (MyInvalid || !Entry.isFile())
853       return 0;
854
855     const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
856     if (!Content)
857       return 0;
858     return Content->OrigEntry;
859   }
860
861   /// \brief Returns the FileEntry record for the provided SLocEntry.
862   const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
863   {
864     const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
865     if (!Content)
866       return 0;
867     return Content->OrigEntry;
868   }
869
870   /// \brief Return a StringRef to the source buffer data for the
871   /// specified FileID.
872   ///
873   /// \param FID The file ID whose contents will be returned.
874   /// \param Invalid If non-NULL, will be set true if an error occurred.
875   StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
876
877   /// \brief Get the number of FileIDs (files and macros) that were created
878   /// during preprocessing of \p FID, including it.
879   unsigned getNumCreatedFIDsForFileID(FileID FID) const {
880     bool Invalid = false;
881     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
882     if (Invalid || !Entry.isFile())
883       return 0;
884
885     return Entry.getFile().NumCreatedFIDs;
886   }
887
888   /// \brief Set the number of FileIDs (files and macros) that were created
889   /// during preprocessing of \p FID, including it.
890   void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
891     bool Invalid = false;
892     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
893     if (Invalid || !Entry.isFile())
894       return;
895
896     assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
897     const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
898   }
899
900   //===--------------------------------------------------------------------===//
901   // SourceLocation manipulation methods.
902   //===--------------------------------------------------------------------===//
903
904   /// \brief Return the FileID for a SourceLocation.
905   ///
906   /// This is a very hot method that is used for all SourceManager queries
907   /// that start with a SourceLocation object.  It is responsible for finding
908   /// the entry in SLocEntryTable which contains the specified location.
909   ///
910   FileID getFileID(SourceLocation SpellingLoc) const {
911     unsigned SLocOffset = SpellingLoc.getOffset();
912
913     // If our one-entry cache covers this offset, just return it.
914     if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
915       return LastFileIDLookup;
916
917     return getFileIDSlow(SLocOffset);
918   }
919
920   /// \brief Return the filename of the file containing a SourceLocation.
921   StringRef getFilename(SourceLocation SpellingLoc) const {
922     if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
923       return F->getName();
924     return StringRef();
925   }
926
927   /// \brief Return the source location corresponding to the first byte of
928   /// the specified file.
929   SourceLocation getLocForStartOfFile(FileID FID) const {
930     bool Invalid = false;
931     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
932     if (Invalid || !Entry.isFile())
933       return SourceLocation();
934
935     unsigned FileOffset = Entry.getOffset();
936     return SourceLocation::getFileLoc(FileOffset);
937   }
938   
939   /// \brief Return the source location corresponding to the last byte of the
940   /// specified file.
941   SourceLocation getLocForEndOfFile(FileID FID) const {
942     bool Invalid = false;
943     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
944     if (Invalid || !Entry.isFile())
945       return SourceLocation();
946     
947     unsigned FileOffset = Entry.getOffset();
948     return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1);
949   }
950
951   /// \brief Returns the include location if \p FID is a \#include'd file
952   /// otherwise it returns an invalid location.
953   SourceLocation getIncludeLoc(FileID FID) const {
954     bool Invalid = false;
955     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
956     if (Invalid || !Entry.isFile())
957       return SourceLocation();
958
959     return Entry.getFile().getIncludeLoc();
960   }
961
962   /// \brief Given a SourceLocation object \p Loc, return the expansion
963   /// location referenced by the ID.
964   SourceLocation getExpansionLoc(SourceLocation Loc) const {
965     // Handle the non-mapped case inline, defer to out of line code to handle
966     // expansions.
967     if (Loc.isFileID()) return Loc;
968     return getExpansionLocSlowCase(Loc);
969   }
970
971   /// \brief Given \p Loc, if it is a macro location return the expansion
972   /// location or the spelling location, depending on if it comes from a
973   /// macro argument or not.
974   SourceLocation getFileLoc(SourceLocation Loc) const {
975     if (Loc.isFileID()) return Loc;
976     return getFileLocSlowCase(Loc);
977   }
978
979   /// \brief Return the start/end of the expansion information for an
980   /// expansion location.
981   ///
982   /// \pre \p Loc is required to be an expansion location.
983   std::pair<SourceLocation,SourceLocation>
984   getImmediateExpansionRange(SourceLocation Loc) const;
985
986   /// \brief Given a SourceLocation object, return the range of
987   /// tokens covered by the expansion the ultimate file.
988   std::pair<SourceLocation,SourceLocation>
989   getExpansionRange(SourceLocation Loc) const;
990
991
992   /// \brief Given a SourceLocation object, return the spelling
993   /// location referenced by the ID.
994   ///
995   /// This is the place where the characters that make up the lexed token
996   /// can be found.
997   SourceLocation getSpellingLoc(SourceLocation Loc) const {
998     // Handle the non-mapped case inline, defer to out of line code to handle
999     // expansions.
1000     if (Loc.isFileID()) return Loc;
1001     return getSpellingLocSlowCase(Loc);
1002   }
1003
1004   /// \brief Given a SourceLocation object, return the spelling location
1005   /// referenced by the ID.
1006   ///
1007   /// This is the first level down towards the place where the characters
1008   /// that make up the lexed token can be found.  This should not generally
1009   /// be used by clients.
1010   SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
1011
1012   /// \brief Decompose the specified location into a raw FileID + Offset pair.
1013   ///
1014   /// The first element is the FileID, the second is the offset from the
1015   /// start of the buffer of the location.
1016   std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
1017     FileID FID = getFileID(Loc);
1018     bool Invalid = false;
1019     const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
1020     if (Invalid)
1021       return std::make_pair(FileID(), 0);
1022     return std::make_pair(FID, Loc.getOffset()-E.getOffset());
1023   }
1024
1025   /// \brief Decompose the specified location into a raw FileID + Offset pair.
1026   ///
1027   /// If the location is an expansion record, walk through it until we find
1028   /// the final location expanded.
1029   std::pair<FileID, unsigned>
1030   getDecomposedExpansionLoc(SourceLocation Loc) const {
1031     FileID FID = getFileID(Loc);
1032     bool Invalid = false;
1033     const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1034     if (Invalid)
1035       return std::make_pair(FileID(), 0);
1036
1037     unsigned Offset = Loc.getOffset()-E->getOffset();
1038     if (Loc.isFileID())
1039       return std::make_pair(FID, Offset);
1040
1041     return getDecomposedExpansionLocSlowCase(E);
1042   }
1043
1044   /// \brief Decompose the specified location into a raw FileID + Offset pair.
1045   ///
1046   /// If the location is an expansion record, walk through it until we find
1047   /// its spelling record.
1048   std::pair<FileID, unsigned>
1049   getDecomposedSpellingLoc(SourceLocation Loc) const {
1050     FileID FID = getFileID(Loc);
1051     bool Invalid = false;
1052     const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1053     if (Invalid)
1054       return std::make_pair(FileID(), 0);
1055
1056     unsigned Offset = Loc.getOffset()-E->getOffset();
1057     if (Loc.isFileID())
1058       return std::make_pair(FID, Offset);
1059     return getDecomposedSpellingLocSlowCase(E, Offset);
1060   }
1061
1062   /// \brief Returns the offset from the start of the file that the
1063   /// specified SourceLocation represents.
1064   ///
1065   /// This is not very meaningful for a macro ID.
1066   unsigned getFileOffset(SourceLocation SpellingLoc) const {
1067     return getDecomposedLoc(SpellingLoc).second;
1068   }
1069
1070   /// \brief Tests whether the given source location represents a macro
1071   /// argument's expansion into the function-like macro definition.
1072   ///
1073   /// Such source locations only appear inside of the expansion
1074   /// locations representing where a particular function-like macro was
1075   /// expanded.
1076   bool isMacroArgExpansion(SourceLocation Loc) const;
1077
1078   /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
1079   /// chunk of the source location address space.
1080   ///
1081   /// If it's true and \p RelativeOffset is non-null, it will be set to the
1082   /// relative offset of \p Loc inside the chunk.
1083   bool isInSLocAddrSpace(SourceLocation Loc,
1084                          SourceLocation Start, unsigned Length,
1085                          unsigned *RelativeOffset = 0) const {
1086     assert(((Start.getOffset() < NextLocalOffset &&
1087                Start.getOffset()+Length <= NextLocalOffset) ||
1088             (Start.getOffset() >= CurrentLoadedOffset &&
1089                 Start.getOffset()+Length < MaxLoadedOffset)) &&
1090            "Chunk is not valid SLoc address space");
1091     unsigned LocOffs = Loc.getOffset();
1092     unsigned BeginOffs = Start.getOffset();
1093     unsigned EndOffs = BeginOffs + Length;
1094     if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1095       if (RelativeOffset)
1096         *RelativeOffset = LocOffs - BeginOffs;
1097       return true;
1098     }
1099
1100     return false;
1101   }
1102
1103   /// \brief Return true if both \p LHS and \p RHS are in the local source
1104   /// location address space or the loaded one.
1105   ///
1106   /// If it's true and \p RelativeOffset is non-null, it will be set to the
1107   /// offset of \p RHS relative to \p LHS.
1108   bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1109                              int *RelativeOffset) const {
1110     unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1111     bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1112     bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1113
1114     if (LHSLoaded == RHSLoaded) {
1115       if (RelativeOffset)
1116         *RelativeOffset = RHSOffs - LHSOffs;
1117       return true;
1118     }
1119
1120     return false;
1121   }
1122
1123   //===--------------------------------------------------------------------===//
1124   // Queries about the code at a SourceLocation.
1125   //===--------------------------------------------------------------------===//
1126
1127   /// \brief Return a pointer to the start of the specified location
1128   /// in the appropriate spelling MemoryBuffer.
1129   ///
1130   /// \param Invalid If non-NULL, will be set \c true if an error occurs.
1131   const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
1132
1133   /// \brief Return the column # for the specified file position.
1134   ///
1135   /// This is significantly cheaper to compute than the line number.  This
1136   /// returns zero if the column number isn't known.  This may only be called
1137   /// on a file sloc, so you must choose a spelling or expansion location
1138   /// before calling this method.
1139   unsigned getColumnNumber(FileID FID, unsigned FilePos,
1140                            bool *Invalid = 0) const;
1141   unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1142   unsigned getExpansionColumnNumber(SourceLocation Loc,
1143                                     bool *Invalid = 0) const;
1144   unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1145
1146
1147   /// \brief Given a SourceLocation, return the spelling line number
1148   /// for the position indicated.
1149   ///
1150   /// This requires building and caching a table of line offsets for the
1151   /// MemoryBuffer, so this is not cheap: use only when about to emit a
1152   /// diagnostic.
1153   unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
1154   unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1155   unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1156   unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1157
1158   /// \brief Return the filename or buffer identifier of the buffer the
1159   /// location is in.
1160   ///
1161   /// Note that this name does not respect \#line directives.  Use
1162   /// getPresumedLoc for normal clients.
1163   const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
1164
1165   /// \brief Return the file characteristic of the specified source
1166   /// location, indicating whether this is a normal file, a system
1167   /// header, or an "implicit extern C" system header.
1168   ///
1169   /// This state can be modified with flags on GNU linemarker directives like:
1170   /// \code
1171   ///   # 4 "foo.h" 3
1172   /// \endcode
1173   /// which changes all source locations in the current file after that to be
1174   /// considered to be from a system header.
1175   SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
1176
1177   /// \brief Returns the "presumed" location of a SourceLocation specifies.
1178   ///
1179   /// A "presumed location" can be modified by \#line or GNU line marker
1180   /// directives.  This provides a view on the data that a user should see
1181   /// in diagnostics, for example.
1182   ///
1183   /// Note that a presumed location is always given as the expansion point of
1184   /// an expansion location, not at the spelling location.
1185   ///
1186   /// \returns The presumed location of the specified SourceLocation. If the
1187   /// presumed location cannot be calculate (e.g., because \p Loc is invalid
1188   /// or the file containing \p Loc has changed on disk), returns an invalid
1189   /// presumed location.
1190   PresumedLoc getPresumedLoc(SourceLocation Loc) const;
1191
1192   /// \brief Returns true if both SourceLocations correspond to the same file.
1193   bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1194     return getFileID(Loc1) == getFileID(Loc2);
1195   }
1196
1197   /// \brief Returns true if the file of provided SourceLocation is the main
1198   /// file.
1199   bool isFromMainFile(SourceLocation Loc) const {
1200     return getFileID(Loc) == getMainFileID();
1201   }
1202
1203   /// \brief Returns if a SourceLocation is in a system header.
1204   bool isInSystemHeader(SourceLocation Loc) const {
1205     return getFileCharacteristic(Loc) != SrcMgr::C_User;
1206   }
1207
1208   /// \brief Returns if a SourceLocation is in an "extern C" system header.
1209   bool isInExternCSystemHeader(SourceLocation Loc) const {
1210     return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1211   }
1212
1213   /// \brief Returns whether \p Loc is expanded from a macro in a system header.
1214   bool isInSystemMacro(SourceLocation loc) {
1215     return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
1216   }
1217
1218   /// \brief The size of the SLocEnty that \p FID represents.
1219   unsigned getFileIDSize(FileID FID) const;
1220
1221   /// \brief Given a specific FileID, returns true if \p Loc is inside that
1222   /// FileID chunk and sets relative offset (offset of \p Loc from beginning
1223   /// of FileID) to \p relativeOffset.
1224   bool isInFileID(SourceLocation Loc, FileID FID,
1225                   unsigned *RelativeOffset = 0) const {
1226     unsigned Offs = Loc.getOffset();
1227     if (isOffsetInFileID(FID, Offs)) {
1228       if (RelativeOffset)
1229         *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1230       return true;
1231     }
1232
1233     return false;
1234   }
1235
1236   //===--------------------------------------------------------------------===//
1237   // Line Table Manipulation Routines
1238   //===--------------------------------------------------------------------===//
1239
1240   /// \brief Return the uniqued ID for the specified filename.
1241   ///
1242   unsigned getLineTableFilenameID(StringRef Str);
1243
1244   /// \brief Add a line note to the line table for the FileID and offset
1245   /// specified by Loc.
1246   ///
1247   /// If FilenameID is -1, it is considered to be unspecified.
1248   void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
1249   void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
1250                    bool IsFileEntry, bool IsFileExit,
1251                    bool IsSystemHeader, bool IsExternCHeader);
1252
1253   /// \brief Determine if the source manager has a line table.
1254   bool hasLineTable() const { return LineTable != 0; }
1255
1256   /// \brief Retrieve the stored line table.
1257   LineTableInfo &getLineTable();
1258
1259   //===--------------------------------------------------------------------===//
1260   // Queries for performance analysis.
1261   //===--------------------------------------------------------------------===//
1262
1263   /// \brief Return the total amount of physical memory allocated by the
1264   /// ContentCache allocator.
1265   size_t getContentCacheSize() const {
1266     return ContentCacheAlloc.getTotalMemory();
1267   }
1268
1269   struct MemoryBufferSizes {
1270     const size_t malloc_bytes;
1271     const size_t mmap_bytes;
1272
1273     MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1274       : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1275   };
1276
1277   /// \brief Return the amount of memory used by memory buffers, breaking down
1278   /// by heap-backed versus mmap'ed memory.
1279   MemoryBufferSizes getMemoryBufferSizes() const;
1280
1281   /// \brief Return the amount of memory used for various side tables and
1282   /// data structures in the SourceManager.
1283   size_t getDataStructureSizes() const;
1284
1285   //===--------------------------------------------------------------------===//
1286   // Other miscellaneous methods.
1287   //===--------------------------------------------------------------------===//
1288
1289   /// \brief Get the source location for the given file:line:col triplet.
1290   ///
1291   /// If the source file is included multiple times, the source location will
1292   /// be based upon the first inclusion.
1293   SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1294                                       unsigned Line, unsigned Col) const;
1295
1296   /// \brief Get the FileID for the given file.
1297   ///
1298   /// If the source file is included multiple times, the FileID will be the
1299   /// first inclusion.
1300   FileID translateFile(const FileEntry *SourceFile) const;
1301
1302   /// \brief Get the source location in \p FID for the given line:col.
1303   /// Returns null location if \p FID is not a file SLocEntry.
1304   SourceLocation translateLineCol(FileID FID,
1305                                   unsigned Line, unsigned Col) const;
1306
1307   /// \brief If \p Loc points inside a function macro argument, the returned
1308   /// location will be the macro location in which the argument was expanded.
1309   /// If a macro argument is used multiple times, the expanded location will
1310   /// be at the first expansion of the argument.
1311   /// e.g.
1312   ///   MY_MACRO(foo);
1313   ///             ^
1314   /// Passing a file location pointing at 'foo', will yield a macro location
1315   /// where 'foo' was expanded into.
1316   SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
1317
1318   /// \brief Determines the order of 2 source locations in the translation unit.
1319   ///
1320   /// \returns true if LHS source location comes before RHS, false otherwise.
1321   bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1322
1323   /// \brief Determines the order of 2 source locations in the "source location
1324   /// address space".
1325   bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1326     return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1327   }
1328
1329   /// \brief Determines the order of a source location and a source location
1330   /// offset in the "source location address space".
1331   ///
1332   /// Note that we always consider source locations loaded from
1333   bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1334     unsigned LHSOffset = LHS.getOffset();
1335     bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1336     bool RHSLoaded = RHS >= CurrentLoadedOffset;
1337     if (LHSLoaded == RHSLoaded)
1338       return LHSOffset < RHS;
1339
1340     return LHSLoaded;
1341   }
1342
1343   // Iterators over FileInfos.
1344   typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1345       ::const_iterator fileinfo_iterator;
1346   fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1347   fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1348   bool hasFileInfo(const FileEntry *File) const {
1349     return FileInfos.find(File) != FileInfos.end();
1350   }
1351
1352   /// \brief Print statistics to stderr.
1353   ///
1354   void PrintStats() const;
1355
1356   /// \brief Get the number of local SLocEntries we have.
1357   unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1358
1359   /// \brief Get a local SLocEntry. This is exposed for indexing.
1360   const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1361                                              bool *Invalid = 0) const {
1362     assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1363     return LocalSLocEntryTable[Index];
1364   }
1365
1366   /// \brief Get the number of loaded SLocEntries we have.
1367   unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1368
1369   /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1370   const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
1371                                               bool *Invalid = 0) const {
1372     assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1373     if (SLocEntryLoaded[Index])
1374       return LoadedSLocEntryTable[Index];
1375     return loadSLocEntry(Index, Invalid);
1376   }
1377
1378   const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1379     if (FID.ID == 0 || FID.ID == -1) {
1380       if (Invalid) *Invalid = true;
1381       return LocalSLocEntryTable[0];
1382     }
1383     return getSLocEntryByID(FID.ID);
1384   }
1385
1386   unsigned getNextLocalOffset() const { return NextLocalOffset; }
1387
1388   void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1389     assert(LoadedSLocEntryTable.empty() &&
1390            "Invalidating existing loaded entries");
1391     ExternalSLocEntries = Source;
1392   }
1393
1394   /// \brief Allocate a number of loaded SLocEntries, which will be actually
1395   /// loaded on demand from the external source.
1396   ///
1397   /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1398   /// in the global source view. The lowest ID and the base offset of the
1399   /// entries will be returned.
1400   std::pair<int, unsigned>
1401   AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1402
1403   /// \brief Returns true if \p Loc came from a PCH/Module.
1404   bool isLoadedSourceLocation(SourceLocation Loc) const {
1405     return Loc.getOffset() >= CurrentLoadedOffset;
1406   }
1407
1408   /// \brief Returns true if \p Loc did not come from a PCH/Module.
1409   bool isLocalSourceLocation(SourceLocation Loc) const {
1410     return Loc.getOffset() < NextLocalOffset;
1411   }
1412
1413   /// \brief Returns true if \p FID came from a PCH/Module.
1414   bool isLoadedFileID(FileID FID) const {
1415     assert(FID.ID != -1 && "Using FileID sentinel value");
1416     return FID.ID < 0;
1417   }
1418
1419   /// \brief Returns true if \p FID did not come from a PCH/Module.
1420   bool isLocalFileID(FileID FID) const {
1421     return !isLoadedFileID(FID);
1422   }
1423
1424   /// Get a presumed location suitable for displaying in a diagnostic message,
1425   /// taking into account macro arguments and expansions.
1426   PresumedLoc getPresumedLocForDisplay(SourceLocation Loc) const {
1427     // This is a condensed form of the algorithm used by emitCaretDiagnostic to
1428     // walk to the top of the macro call stack.
1429     while (Loc.isMacroID()) {
1430       Loc = skipToMacroArgExpansion(Loc);
1431       Loc = getImmediateMacroCallerLoc(Loc);
1432     }
1433
1434     return getPresumedLoc(Loc);
1435   }
1436
1437   /// Look through spelling locations for a macro argument expansion, and if
1438   /// found skip to it so that we can trace the argument rather than the macros
1439   /// in which that argument is used. If no macro argument expansion is found,
1440   /// don't skip anything and return the starting location.
1441   SourceLocation skipToMacroArgExpansion(SourceLocation StartLoc) const {
1442     for (SourceLocation L = StartLoc; L.isMacroID();
1443          L = getImmediateSpellingLoc(L)) {
1444       if (isMacroArgExpansion(L))
1445         return L;
1446     }
1447     // Otherwise just return initial location, there's nothing to skip.
1448     return StartLoc;
1449   }
1450
1451   /// Gets the location of the immediate macro caller, one level up the stack
1452   /// toward the initial macro typed into the source.
1453   SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const {
1454     if (!Loc.isMacroID()) return Loc;
1455
1456     // When we have the location of (part of) an expanded parameter, its
1457     // spelling location points to the argument as typed into the macro call,
1458     // and therefore is used to locate the macro caller.
1459     if (isMacroArgExpansion(Loc))
1460       return getImmediateSpellingLoc(Loc);
1461
1462     // Otherwise, the caller of the macro is located where this macro is
1463     // expanded (while the spelling is part of the macro definition).
1464     return getImmediateExpansionRange(Loc).first;
1465   }
1466
1467   /// Gets the location of the immediate macro callee, one level down the stack
1468   /// toward the leaf macro.
1469   SourceLocation getImmediateMacroCalleeLoc(SourceLocation Loc) const {
1470     if (!Loc.isMacroID()) return Loc;
1471
1472     // When we have the location of (part of) an expanded parameter, its
1473     // expansion location points to the unexpanded parameter reference within
1474     // the macro definition (or callee).
1475     if (isMacroArgExpansion(Loc))
1476       return getImmediateExpansionRange(Loc).first;
1477
1478     // Otherwise, the callee of the macro is located where this location was
1479     // spelled inside the macro definition.
1480     return getImmediateSpellingLoc(Loc);
1481   }
1482
1483 private:
1484   const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1485   const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
1486
1487   const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1488
1489   /// \brief Get the entry with the given unwrapped FileID.
1490   const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1491     assert(ID != -1 && "Using FileID sentinel value");
1492     if (ID < 0)
1493       return getLoadedSLocEntryByID(ID);
1494     return getLocalSLocEntry(static_cast<unsigned>(ID));
1495   }
1496
1497   const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID,
1498                                                   bool *Invalid = 0) const {
1499     return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1500   }
1501
1502   /// Implements the common elements of storing an expansion info struct into
1503   /// the SLocEntry table and producing a source location that refers to it.
1504   SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1505                                         unsigned TokLength,
1506                                         int LoadedID = 0,
1507                                         unsigned LoadedOffset = 0);
1508
1509   /// \brief Return true if the specified FileID contains the
1510   /// specified SourceLocation offset.  This is a very hot method.
1511   inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1512     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1513     // If the entry is after the offset, it can't contain it.
1514     if (SLocOffset < Entry.getOffset()) return false;
1515
1516     // If this is the very last entry then it does.
1517     if (FID.ID == -2)
1518       return true;
1519
1520     // If it is the last local entry, then it does if the location is local.
1521     if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size()))
1522       return SLocOffset < NextLocalOffset;
1523
1524     // Otherwise, the entry after it has to not include it. This works for both
1525     // local and loaded entries.
1526     return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset();
1527   }
1528
1529   /// \brief Create a new fileID for the specified ContentCache and
1530   /// include position.
1531   ///
1532   /// This works regardless of whether the ContentCache corresponds to a
1533   /// file or some other input source.
1534   FileID createFileID(const SrcMgr::ContentCache* File,
1535                       SourceLocation IncludePos,
1536                       SrcMgr::CharacteristicKind DirCharacter,
1537                       int LoadedID, unsigned LoadedOffset);
1538
1539   const SrcMgr::ContentCache *
1540     getOrCreateContentCache(const FileEntry *SourceFile,
1541                             bool isSystemFile = false);
1542
1543   /// \brief Create a new ContentCache for the specified  memory buffer.
1544   const SrcMgr::ContentCache*
1545   createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
1546
1547   FileID getFileIDSlow(unsigned SLocOffset) const;
1548   FileID getFileIDLocal(unsigned SLocOffset) const;
1549   FileID getFileIDLoaded(unsigned SLocOffset) const;
1550
1551   SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1552   SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1553   SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1554
1555   std::pair<FileID, unsigned>
1556   getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1557   std::pair<FileID, unsigned>
1558   getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1559                                    unsigned Offset) const;
1560   void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const;
1561   void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
1562                                          FileID FID,
1563                                          SourceLocation SpellLoc,
1564                                          SourceLocation ExpansionLoc,
1565                                          unsigned ExpansionLength) const;
1566   friend class ASTReader;
1567   friend class ASTWriter;
1568 };
1569
1570 /// \brief Comparison function object.
1571 template<typename T>
1572 class BeforeThanCompare;
1573
1574 /// \brief Compare two source locations.
1575 template<>
1576 class BeforeThanCompare<SourceLocation> {
1577   SourceManager &SM;
1578
1579 public:
1580   explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { }
1581
1582   bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1583     return SM.isBeforeInTranslationUnit(LHS, RHS);
1584   }
1585 };
1586
1587 /// \brief Compare two non-overlapping source ranges.
1588 template<>
1589 class BeforeThanCompare<SourceRange> {
1590   SourceManager &SM;
1591
1592 public:
1593   explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { }
1594
1595   bool operator()(SourceRange LHS, SourceRange RHS) {
1596     return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin());
1597   }
1598 };
1599
1600 }  // end namespace clang
1601
1602 #endif