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