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