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