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