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