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