SourceManager.h revision e23ac65af568ffe611b0990818ac3a57c856a4d8
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/SourceLocation.h"
18#include "llvm/Support/Allocator.h"
19#include "llvm/Support/DataTypes.h"
20#include "llvm/ADT/PointerIntPair.h"
21#include "llvm/ADT/PointerUnion.h"
22#include "llvm/ADT/IntrusiveRefCntPtr.h"
23#include "llvm/ADT/DenseMap.h"
24#include <vector>
25#include <cassert>
26
27namespace llvm {
28class MemoryBuffer;
29class StringRef;
30}
31
32namespace clang {
33
34class Diagnostic;
35class SourceManager;
36class FileManager;
37class FileEntry;
38class LineTableInfo;
39
40/// SrcMgr - Public enums and private classes that are part of the
41/// SourceManager implementation.
42///
43namespace SrcMgr {
44  /// CharacteristicKind - This is used to represent whether a file or directory
45  /// holds normal user code, system code, or system code which is implicitly
46  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
47  /// (this is maintained by DirectoryLookup and friends) as can specific
48  /// FileIDInfos when a #pragma system_header is seen or various other cases.
49  ///
50  enum CharacteristicKind {
51    C_User, C_System, C_ExternCSystem
52  };
53
54  /// ContentCache - One instance of this struct is kept for every file
55  /// loaded or used.  This object owns the MemoryBuffer object.
56  class ContentCache {
57    enum CCFlags {
58      /// \brief Whether the buffer is invalid.
59      InvalidFlag = 0x01,
60      /// \brief Whether the buffer should not be freed on destruction.
61      DoNotFreeFlag = 0x02
62    };
63
64    /// Buffer - The actual buffer containing the characters from the input
65    /// file.  This is owned by the ContentCache object.
66    /// The bits indicate indicates whether the buffer is invalid.
67    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
68
69  public:
70    /// Reference to the file entry representing this ContentCache.
71    /// This reference does not own the FileEntry object.
72    /// It is possible for this to be NULL if
73    /// the ContentCache encapsulates an imaginary text buffer.
74    const FileEntry *OrigEntry;
75
76    /// \brief References the file which the contents were actually loaded from.
77    /// Can be different from 'Entry' if we overridden the contents of one file
78    /// with the contents of another file.
79    const FileEntry *ContentsEntry;
80
81    /// SourceLineCache - A bump pointer allocated array of offsets for each
82    /// source line.  This is lazily computed.  This is owned by the
83    /// SourceManager BumpPointerAllocator object.
84    unsigned *SourceLineCache;
85
86    /// NumLines - The number of lines in this ContentCache.  This is only valid
87    /// if SourceLineCache is non-null.
88    unsigned NumLines;
89
90    /// getBuffer - Returns the memory buffer for the associated content.
91    ///
92    /// \param Diag Object through which diagnostics will be emitted it the
93    /// buffer cannot be retrieved.
94    ///
95    /// \param Loc If specified, is the location that invalid file diagnostics
96    ///     will be emitted at.
97    ///
98    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
99    const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
100                                        const SourceManager &SM,
101                                        SourceLocation Loc = SourceLocation(),
102                                        bool *Invalid = 0) const;
103
104    /// getSize - Returns the size of the content encapsulated by this
105    ///  ContentCache. This can be the size of the source file or the size of an
106    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
107    ///  file this size is retrieved from the file's FileEntry.
108    unsigned getSize() const;
109
110    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
111    ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
112    ///  instantiated.
113    unsigned getSizeBytesMapped() const;
114
115    void setBuffer(const llvm::MemoryBuffer *B) {
116      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
117      Buffer.setPointer(B);
118      Buffer.setInt(false);
119    }
120
121    /// \brief Get the underlying buffer, returning NULL if the buffer is not
122    /// yet available.
123    const llvm::MemoryBuffer *getRawBuffer() const {
124      return Buffer.getPointer();
125    }
126
127    /// \brief Replace the existing buffer (which will be deleted)
128    /// with the given buffer.
129    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
130
131    /// \brief Determine whether the buffer itself is invalid.
132    bool isBufferInvalid() const {
133      return Buffer.getInt() & InvalidFlag;
134    }
135
136    /// \brief Determine whether the buffer should be freed.
137    bool shouldFreeBuffer() const {
138      return (Buffer.getInt() & DoNotFreeFlag) == 0;
139    }
140
141    ContentCache(const FileEntry *Ent = 0)
142      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
143        SourceLineCache(0), NumLines(0) {}
144
145    ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
146      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
147        SourceLineCache(0), NumLines(0) {}
148
149    ~ContentCache();
150
151    /// The copy ctor does not allow copies where source object has either
152    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
153    ///  is not transferred, so this is a logical error.
154    ContentCache(const ContentCache &RHS)
155      : Buffer(0, false), SourceLineCache(0)
156    {
157      OrigEntry = RHS.OrigEntry;
158      ContentsEntry = RHS.ContentsEntry;
159
160      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
161              && "Passed ContentCache object cannot own a buffer.");
162
163      NumLines = RHS.NumLines;
164    }
165
166  private:
167    // Disable assignments.
168    ContentCache &operator=(const ContentCache& RHS);
169  };
170
171  /// FileInfo - Information about a FileID, basically just the logical file
172  /// that it represents and include stack information.
173  ///
174  /// Each FileInfo has include stack information, indicating where it came
175  /// from.  This information encodes the #include chain that a token was
176  /// instantiated from.  The main include file has an invalid IncludeLoc.
177  ///
178  /// FileInfos contain a "ContentCache *", with the contents of the file.
179  ///
180  class FileInfo {
181    /// IncludeLoc - The location of the #include that brought in this file.
182    /// This is an invalid SLOC for the main file (top of the #include chain).
183    unsigned IncludeLoc;  // Really a SourceLocation
184
185    /// Data - This contains the ContentCache* and the bits indicating the
186    /// characteristic of the file and whether it has #line info, all bitmangled
187    /// together.
188    uintptr_t Data;
189  public:
190    /// get - Return a FileInfo object.
191    static FileInfo get(SourceLocation IL, const ContentCache *Con,
192                        CharacteristicKind FileCharacter) {
193      FileInfo X;
194      X.IncludeLoc = IL.getRawEncoding();
195      X.Data = (uintptr_t)Con;
196      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
197      assert((unsigned)FileCharacter < 4 && "invalid file character");
198      X.Data |= (unsigned)FileCharacter;
199      return X;
200    }
201
202    SourceLocation getIncludeLoc() const {
203      return SourceLocation::getFromRawEncoding(IncludeLoc);
204    }
205    const ContentCache* getContentCache() const {
206      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
207    }
208
209    /// getCharacteristic - Return whether this is a system header or not.
210    CharacteristicKind getFileCharacteristic() const {
211      return (CharacteristicKind)(Data & 3);
212    }
213
214    /// hasLineDirectives - Return true if this FileID has #line directives in
215    /// it.
216    bool hasLineDirectives() const { return (Data & 4) != 0; }
217
218    /// setHasLineDirectives - Set the flag that indicates that this FileID has
219    /// line table entries associated with it.
220    void setHasLineDirectives() {
221      Data |= 4;
222    }
223  };
224
225  /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
226  /// location - where the token was ultimately instantiated, and the
227  /// SpellingLoc - where the actual character data for the token came from.
228  class InstantiationInfo {
229     // Really these are all SourceLocations.
230
231    /// SpellingLoc - Where the spelling for the token can be found.
232    unsigned SpellingLoc;
233
234    /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
235    /// indicate the start and end of the instantiation.  In object-like macros,
236    /// these will be the same.  In a function-like macro instantiation, the
237    /// start will be the identifier and the end will be the ')'.
238    unsigned InstantiationLocStart, InstantiationLocEnd;
239  public:
240    SourceLocation getSpellingLoc() const {
241      return SourceLocation::getFromRawEncoding(SpellingLoc);
242    }
243    SourceLocation getInstantiationLocStart() const {
244      return SourceLocation::getFromRawEncoding(InstantiationLocStart);
245    }
246    SourceLocation getInstantiationLocEnd() const {
247      return SourceLocation::getFromRawEncoding(InstantiationLocEnd);
248    }
249
250    std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
251      return std::make_pair(getInstantiationLocStart(),
252                            getInstantiationLocEnd());
253    }
254
255    /// get - Return a InstantiationInfo for an expansion.  IL specifies
256    /// the instantiation location (where the macro is expanded), and SL
257    /// specifies the spelling location (where the characters from the token
258    /// come from).  IL and PL can both refer to normal File SLocs or
259    /// instantiation locations.
260    static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd,
261                                 SourceLocation SL) {
262      InstantiationInfo X;
263      X.SpellingLoc = SL.getRawEncoding();
264      X.InstantiationLocStart = ILStart.getRawEncoding();
265      X.InstantiationLocEnd = ILEnd.getRawEncoding();
266      return X;
267    }
268  };
269
270  /// SLocEntry - This is a discriminated union of FileInfo and
271  /// InstantiationInfo.  SourceManager keeps an array of these objects, and
272  /// they are uniquely identified by the FileID datatype.
273  class SLocEntry {
274    unsigned Offset;   // low bit is set for instantiation info.
275    union {
276      FileInfo File;
277      InstantiationInfo Instantiation;
278    };
279  public:
280    unsigned getOffset() const { return Offset >> 1; }
281
282    bool isInstantiation() const { return Offset & 1; }
283    bool isFile() const { return !isInstantiation(); }
284
285    const FileInfo &getFile() const {
286      assert(isFile() && "Not a file SLocEntry!");
287      return File;
288    }
289
290    const InstantiationInfo &getInstantiation() const {
291      assert(isInstantiation() && "Not an instantiation SLocEntry!");
292      return Instantiation;
293    }
294
295    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
296      SLocEntry E;
297      E.Offset = Offset << 1;
298      E.File = FI;
299      return E;
300    }
301
302    static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
303      SLocEntry E;
304      E.Offset = (Offset << 1) | 1;
305      E.Instantiation = II;
306      return E;
307    }
308  };
309}  // end SrcMgr namespace.
310
311/// \brief External source of source location entries.
312class ExternalSLocEntrySource {
313public:
314  virtual ~ExternalSLocEntrySource();
315
316  /// \brief Read the source location entry with index ID.
317  ///
318  /// \returns true if an error occurred that prevented the source-location
319  /// entry from being loaded.
320  virtual bool ReadSLocEntry(unsigned ID) = 0;
321};
322
323
324/// IsBeforeInTranslationUnitCache - This class holds the cache used by
325/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
326/// worth breaking out of SourceManager.
327class IsBeforeInTranslationUnitCache {
328  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
329  /// with a subsequent query, the result can be reused.
330  FileID LQueryFID, RQueryFID;
331
332  /// CommonFID - This is the file found in common between the two #include
333  /// traces.  It is the nearest common ancestor of the #include tree.
334  FileID CommonFID;
335
336  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
337  /// Usually, this represents the location of the #include for QueryFID, but if
338  /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
339  /// random token in the parent.
340  unsigned LCommonOffset, RCommonOffset;
341public:
342
343  /// isCacheValid - Return true if the currently cached values match up with
344  /// the specified LHS/RHS query.  If not, we can't use the cache.
345  bool isCacheValid(FileID LHS, FileID RHS) const {
346    return LQueryFID == LHS && RQueryFID == RHS;
347  }
348
349  /// getCachedResult - If the cache is valid, compute the result given the
350  /// specified offsets in the LHS/RHS FID's.
351  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
352    // If one of the query files is the common file, use the offset.  Otherwise,
353    // use the #include loc in the common file.
354    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
355    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
356    return LOffset < ROffset;
357  }
358
359  // Set up a new query.
360  void setQueryFIDs(FileID LHS, FileID RHS) {
361    LQueryFID = LHS;
362    RQueryFID = RHS;
363  }
364
365  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
366                    unsigned rCommonOffset) {
367    CommonFID = commonFID;
368    LCommonOffset = lCommonOffset;
369    RCommonOffset = rCommonOffset;
370  }
371
372};
373
374/// SourceManager - This file handles loading and caching of source files into
375/// memory.  This object owns the MemoryBuffer objects for all of the loaded
376/// files and assigns unique FileID's for each unique #include chain.
377///
378/// The SourceManager can be queried for information about SourceLocation
379/// objects, turning them into either spelling or instantiation locations.
380/// Spelling locations represent where the bytes corresponding to a token came
381/// from and instantiation locations represent where the location is in the
382/// user's view.  In the case of a macro expansion, for example, the spelling
383/// location indicates  where the expanded token came from and the instantiation
384/// location specifies where it was expanded.
385class SourceManager : public llvm::RefCountedBase<SourceManager> {
386  /// \brief Diagnostic object.
387  Diagnostic &Diag;
388
389  FileManager &FileMgr;
390
391  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
392
393  /// FileInfos - Memoized information about all of the files tracked by this
394  /// SourceManager.  This set allows us to merge ContentCache entries based
395  /// on their FileEntry*.  All ContentCache objects will thus have unique,
396  /// non-null, FileEntry pointers.
397  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
398
399  /// \brief True if the ContentCache for files that are overriden by other
400  /// files, should report the original file name. Defaults to true.
401  bool OverridenFilesKeepOriginalName;
402
403  /// \brief Files that have been overriden with the contents from another file.
404  llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
405
406  /// MemBufferInfos - Information about various memory buffers that we have
407  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
408  /// as they do not refer to a file.
409  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
410
411  /// SLocEntryTable - This is an array of SLocEntry's that we have created.
412  /// FileID is an index into this vector.  This array is sorted by the offset.
413  std::vector<SrcMgr::SLocEntry> SLocEntryTable;
414  /// NextOffset - This is the next available offset that a new SLocEntry can
415  /// start at.  It is SLocEntryTable.back().getOffset()+size of back() entry.
416  unsigned NextOffset;
417
418  /// \brief If source location entries are being lazily loaded from
419  /// an external source, this vector indicates whether the Ith source
420  /// location entry has already been loaded from the external storage.
421  std::vector<bool> SLocEntryLoaded;
422
423  /// \brief An external source for source location entries.
424  ExternalSLocEntrySource *ExternalSLocEntries;
425
426  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
427  /// LastFileIDLookup records the last FileID looked up or created, because it
428  /// is very common to look up many tokens from the same file.
429  mutable FileID LastFileIDLookup;
430
431  /// LineTable - This holds information for #line directives.  It is referenced
432  /// by indices from SLocEntryTable.
433  LineTableInfo *LineTable;
434
435  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
436  /// method which is used to speedup getLineNumber calls to nearby locations.
437  mutable FileID LastLineNoFileIDQuery;
438  mutable SrcMgr::ContentCache *LastLineNoContentCache;
439  mutable unsigned LastLineNoFilePos;
440  mutable unsigned LastLineNoResult;
441
442  /// MainFileID - The file ID for the main source file of the translation unit.
443  FileID MainFileID;
444
445  // Statistics for -print-stats.
446  mutable unsigned NumLinearScans, NumBinaryProbes;
447
448  // Cache results for the isBeforeInTranslationUnit method.
449  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
450
451  // Cache for the "fake" buffer used for error-recovery purposes.
452  mutable llvm::MemoryBuffer *FakeBufferForRecovery;
453
454  // SourceManager doesn't support copy construction.
455  explicit SourceManager(const SourceManager&);
456  void operator=(const SourceManager&);
457public:
458  SourceManager(Diagnostic &Diag, FileManager &FileMgr);
459  ~SourceManager();
460
461  void clearIDTables();
462
463  Diagnostic &getDiagnostics() const { return Diag; }
464
465  FileManager &getFileManager() const { return FileMgr; }
466
467  /// \brief Set true if the SourceManager should report the original file name
468  /// for contents of files that were overriden by other files.Defaults to true.
469  void setOverridenFilesKeepOriginalName(bool value) {
470    OverridenFilesKeepOriginalName = value;
471  }
472
473  //===--------------------------------------------------------------------===//
474  // MainFileID creation and querying methods.
475  //===--------------------------------------------------------------------===//
476
477  /// getMainFileID - Returns the FileID of the main source file.
478  FileID getMainFileID() const { return MainFileID; }
479
480  /// createMainFileID - Create the FileID for the main source file.
481  FileID createMainFileID(const FileEntry *SourceFile) {
482    assert(MainFileID.isInvalid() && "MainFileID already set!");
483    MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
484    return MainFileID;
485  }
486
487  /// \brief Set the file ID for the precompiled preamble, which is also the
488  /// main file.
489  void SetPreambleFileID(FileID Preamble) {
490    assert(MainFileID.isInvalid() && "MainFileID already set!");
491    MainFileID = Preamble;
492  }
493
494  //===--------------------------------------------------------------------===//
495  // Methods to create new FileID's and instantiations.
496  //===--------------------------------------------------------------------===//
497
498  /// createFileID - Create a new FileID that represents the specified file
499  /// being #included from the specified IncludePosition.  This returns 0 on
500  /// error and translates NULL into standard input.
501  /// PreallocateID should be non-zero to specify which pre-allocated,
502  /// lazily computed source location is being filled in by this operation.
503  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
504                      SrcMgr::CharacteristicKind FileCharacter,
505                      unsigned PreallocatedID = 0,
506                      unsigned Offset = 0) {
507    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
508    assert(IR && "getOrCreateContentCache() cannot return NULL");
509    return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset);
510  }
511
512  /// createFileIDForMemBuffer - Create a new FileID that represents the
513  /// specified memory buffer.  This does no caching of the buffer and takes
514  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
515  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
516                                  unsigned PreallocatedID = 0,
517                                  unsigned Offset = 0) {
518    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
519                        SrcMgr::C_User, PreallocatedID, Offset);
520  }
521
522  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
523  ///  that will represent the FileID for the main source.  One example
524  ///  of when this would be used is when the main source is read from STDIN.
525  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
526    assert(MainFileID.isInvalid() && "MainFileID already set!");
527    MainFileID = createFileIDForMemBuffer(Buffer);
528    return MainFileID;
529  }
530
531  /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
532  /// that a token at Loc should actually be referenced from InstantiationLoc.
533  /// TokLength is the length of the token being instantiated.
534  SourceLocation createInstantiationLoc(SourceLocation Loc,
535                                        SourceLocation InstantiationLocStart,
536                                        SourceLocation InstantiationLocEnd,
537                                        unsigned TokLength,
538                                        unsigned PreallocatedID = 0,
539                                        unsigned Offset = 0);
540
541  /// \brief Retrieve the memory buffer associated with the given file.
542  ///
543  /// \param Invalid If non-NULL, will be set \c true if an error
544  /// occurs while retrieving the memory buffer.
545  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
546                                                   bool *Invalid = 0);
547
548  /// \brief Override the contents of the given source file by providing an
549  /// already-allocated buffer.
550  ///
551  /// \param SourceFile the source file whose contents will be overriden.
552  ///
553  /// \param Buffer the memory buffer whose contents will be used as the
554  /// data in the given source file.
555  ///
556  /// \param DoNotFree If true, then the buffer will not be freed when the
557  /// source manager is destroyed.
558  void overrideFileContents(const FileEntry *SourceFile,
559                            const llvm::MemoryBuffer *Buffer,
560                            bool DoNotFree = false);
561
562  /// \brief Override the the given source file with another one.
563  ///
564  /// \param SourceFile the source file which will be overriden.
565  ///
566  /// \param NewFile the file whose contents will be used as the
567  /// data instead of the contents of the given source file.
568  void overrideFileContents(const FileEntry *SourceFile,
569                            const FileEntry *NewFile);
570
571  //===--------------------------------------------------------------------===//
572  // FileID manipulation methods.
573  //===--------------------------------------------------------------------===//
574
575  /// getBuffer - Return the buffer for the specified FileID. If there is an
576  /// error opening this buffer the first time, this manufactures a temporary
577  /// buffer and returns a non-empty error string.
578  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
579                                      bool *Invalid = 0) const {
580    bool MyInvalid = false;
581    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
582    if (MyInvalid || !Entry.isFile()) {
583      if (Invalid)
584        *Invalid = true;
585
586      return getFakeBufferForRecovery();
587    }
588
589    return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
590                                                        Invalid);
591  }
592
593  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
594    bool MyInvalid = false;
595    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
596    if (MyInvalid || !Entry.isFile()) {
597      if (Invalid)
598        *Invalid = true;
599
600      return getFakeBufferForRecovery();
601    }
602
603    return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
604                                                        SourceLocation(),
605                                                        Invalid);
606  }
607
608  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
609  const FileEntry *getFileEntryForID(FileID FID) const {
610    bool MyInvalid = false;
611    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
612    if (MyInvalid || !Entry.isFile())
613      return 0;
614
615    return Entry.getFile().getContentCache()->OrigEntry;
616  }
617
618  /// Returns the FileEntry record for the provided SLocEntry.
619  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
620  {
621    return sloc.getFile().getContentCache()->OrigEntry;
622  }
623
624  /// getBufferData - Return a StringRef to the source buffer data for the
625  /// specified FileID.
626  ///
627  /// \param FID The file ID whose contents will be returned.
628  /// \param Invalid If non-NULL, will be set true if an error occurred.
629  llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
630
631
632  //===--------------------------------------------------------------------===//
633  // SourceLocation manipulation methods.
634  //===--------------------------------------------------------------------===//
635
636  /// getFileID - Return the FileID for a SourceLocation.  This is a very
637  /// hot method that is used for all SourceManager queries that start with a
638  /// SourceLocation object.  It is responsible for finding the entry in
639  /// SLocEntryTable which contains the specified location.
640  ///
641  FileID getFileID(SourceLocation SpellingLoc) const {
642    unsigned SLocOffset = SpellingLoc.getOffset();
643
644    // If our one-entry cache covers this offset, just return it.
645    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
646      return LastFileIDLookup;
647
648    return getFileIDSlow(SLocOffset);
649  }
650
651  /// getLocForStartOfFile - Return the source location corresponding to the
652  /// first byte of the specified file.
653  SourceLocation getLocForStartOfFile(FileID FID) const {
654    assert(FID.ID < SLocEntryTable.size() && "FileID out of range");
655    bool Invalid = false;
656    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
657    if (Invalid || !Entry.isFile())
658      return SourceLocation();
659
660    unsigned FileOffset = Entry.getOffset();
661    return SourceLocation::getFileLoc(FileOffset);
662  }
663
664  /// getInstantiationLoc - Given a SourceLocation object, return the
665  /// instantiation location referenced by the ID.
666  SourceLocation getInstantiationLoc(SourceLocation Loc) const {
667    // Handle the non-mapped case inline, defer to out of line code to handle
668    // instantiations.
669    if (Loc.isFileID()) return Loc;
670    return getInstantiationLocSlowCase(Loc);
671  }
672
673  /// getImmediateInstantiationRange - Loc is required to be an instantiation
674  /// location.  Return the start/end of the instantiation information.
675  std::pair<SourceLocation,SourceLocation>
676  getImmediateInstantiationRange(SourceLocation Loc) const;
677
678  /// getInstantiationRange - Given a SourceLocation object, return the
679  /// range of tokens covered by the instantiation in the ultimate file.
680  std::pair<SourceLocation,SourceLocation>
681  getInstantiationRange(SourceLocation Loc) const;
682
683
684  /// getSpellingLoc - Given a SourceLocation object, return the spelling
685  /// location referenced by the ID.  This is the place where the characters
686  /// that make up the lexed token can be found.
687  SourceLocation getSpellingLoc(SourceLocation Loc) const {
688    // Handle the non-mapped case inline, defer to out of line code to handle
689    // instantiations.
690    if (Loc.isFileID()) return Loc;
691    return getSpellingLocSlowCase(Loc);
692  }
693
694  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
695  /// spelling location referenced by the ID.  This is the first level down
696  /// towards the place where the characters that make up the lexed token can be
697  /// found.  This should not generally be used by clients.
698  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
699
700  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
701  /// Offset pair.  The first element is the FileID, the second is the
702  /// offset from the start of the buffer of the location.
703  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
704    FileID FID = getFileID(Loc);
705    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
706  }
707
708  /// getDecomposedInstantiationLoc - Decompose the specified location into a
709  /// raw FileID + Offset pair.  If the location is an instantiation record,
710  /// walk through it until we find the final location instantiated.
711  std::pair<FileID, unsigned>
712  getDecomposedInstantiationLoc(SourceLocation Loc) const {
713    FileID FID = getFileID(Loc);
714    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
715
716    unsigned Offset = Loc.getOffset()-E->getOffset();
717    if (Loc.isFileID())
718      return std::make_pair(FID, Offset);
719
720    return getDecomposedInstantiationLocSlowCase(E, Offset);
721  }
722
723  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
724  /// FileID + Offset pair.  If the location is an instantiation record, walk
725  /// through it until we find its spelling record.
726  std::pair<FileID, unsigned>
727  getDecomposedSpellingLoc(SourceLocation Loc) const {
728    FileID FID = getFileID(Loc);
729    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
730
731    unsigned Offset = Loc.getOffset()-E->getOffset();
732    if (Loc.isFileID())
733      return std::make_pair(FID, Offset);
734    return getDecomposedSpellingLocSlowCase(E, Offset);
735  }
736
737  /// getFileOffset - This method returns the offset from the start
738  /// of the file that the specified SourceLocation represents. This is not very
739  /// meaningful for a macro ID.
740  unsigned getFileOffset(SourceLocation SpellingLoc) const {
741    return getDecomposedLoc(SpellingLoc).second;
742  }
743
744
745  //===--------------------------------------------------------------------===//
746  // Queries about the code at a SourceLocation.
747  //===--------------------------------------------------------------------===//
748
749  /// getCharacterData - Return a pointer to the start of the specified location
750  /// in the appropriate spelling MemoryBuffer.
751  ///
752  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
753  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
754
755  /// getColumnNumber - Return the column # for the specified file position.
756  /// This is significantly cheaper to compute than the line number.  This
757  /// returns zero if the column number isn't known.  This may only be called on
758  /// a file sloc, so you must choose a spelling or instantiation location
759  /// before calling this method.
760  unsigned getColumnNumber(FileID FID, unsigned FilePos,
761                           bool *Invalid = 0) const;
762  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
763  unsigned getInstantiationColumnNumber(SourceLocation Loc,
764                                        bool *Invalid = 0) const;
765  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
766
767
768  /// getLineNumber - Given a SourceLocation, return the spelling line number
769  /// for the position indicated.  This requires building and caching a table of
770  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
771  /// about to emit a diagnostic.
772  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
773  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
774  unsigned getInstantiationLineNumber(SourceLocation Loc,
775                                      bool *Invalid = 0) const;
776  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
777
778  /// Return the filename or buffer identifier of the buffer the location is in.
779  /// Note that this name does not respect #line directives.  Use getPresumedLoc
780  /// for normal clients.
781  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
782
783  /// getFileCharacteristic - return the file characteristic of the specified
784  /// source location, indicating whether this is a normal file, a system
785  /// header, or an "implicit extern C" system header.
786  ///
787  /// This state can be modified with flags on GNU linemarker directives like:
788  ///   # 4 "foo.h" 3
789  /// which changes all source locations in the current file after that to be
790  /// considered to be from a system header.
791  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
792
793  /// getPresumedLoc - This method returns the "presumed" location of a
794  /// SourceLocation specifies.  A "presumed location" can be modified by #line
795  /// or GNU line marker directives.  This provides a view on the data that a
796  /// user should see in diagnostics, for example.
797  ///
798  /// Note that a presumed location is always given as the instantiation point
799  /// of an instantiation location, not at the spelling location.
800  ///
801  /// \returns The presumed location of the specified SourceLocation. If the
802  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
803  /// or the file containing \p Loc has changed on disk), returns an invalid
804  /// presumed location.
805  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
806
807  /// isFromSameFile - Returns true if both SourceLocations correspond to
808  ///  the same file.
809  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
810    return getFileID(Loc1) == getFileID(Loc2);
811  }
812
813  /// isFromMainFile - Returns true if the file of provided SourceLocation is
814  ///   the main file.
815  bool isFromMainFile(SourceLocation Loc) const {
816    return getFileID(Loc) == getMainFileID();
817  }
818
819  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
820  bool isInSystemHeader(SourceLocation Loc) const {
821    return getFileCharacteristic(Loc) != SrcMgr::C_User;
822  }
823
824  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
825  /// system header.
826  bool isInExternCSystemHeader(SourceLocation Loc) const {
827    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
828  }
829
830  //===--------------------------------------------------------------------===//
831  // Line Table Manipulation Routines
832  //===--------------------------------------------------------------------===//
833
834  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
835  ///
836  unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
837
838  /// AddLineNote - Add a line note to the line table for the FileID and offset
839  /// specified by Loc.  If FilenameID is -1, it is considered to be
840  /// unspecified.
841  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
842  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
843                   bool IsFileEntry, bool IsFileExit,
844                   bool IsSystemHeader, bool IsExternCHeader);
845
846  /// \brief Determine if the source manager has a line table.
847  bool hasLineTable() const { return LineTable != 0; }
848
849  /// \brief Retrieve the stored line table.
850  LineTableInfo &getLineTable();
851
852  //===--------------------------------------------------------------------===//
853  // Other miscellaneous methods.
854  //===--------------------------------------------------------------------===//
855
856  /// \brief Get the source location for the given file:line:col triplet.
857  ///
858  /// If the source file is included multiple times, the source location will
859  /// be based upon the first inclusion.
860  SourceLocation getLocation(const FileEntry *SourceFile,
861                             unsigned Line, unsigned Col);
862
863  /// \brief Determines the order of 2 source locations in the translation unit.
864  ///
865  /// \returns true if LHS source location comes before RHS, false otherwise.
866  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
867
868  // Iterators over FileInfos.
869  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
870      ::const_iterator fileinfo_iterator;
871  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
872  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
873  bool hasFileInfo(const FileEntry *File) const {
874    return FileInfos.find(File) != FileInfos.end();
875  }
876
877  /// PrintStats - Print statistics to stderr.
878  ///
879  void PrintStats() const;
880
881  unsigned sloc_entry_size() const { return SLocEntryTable.size(); }
882
883  // FIXME: Exposing this is a little gross; what we want is a good way
884  //  to iterate the entries that were not defined in an AST file (or
885  //  any other external source).
886  unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); }
887
888  const SrcMgr::SLocEntry &getSLocEntry(unsigned ID, bool *Invalid = 0) const {
889    assert(ID < SLocEntryTable.size() && "Invalid id");
890    // If we haven't loaded this source-location entry from the external source
891    // yet, do so now.
892    if (ExternalSLocEntries &&
893        ID < SLocEntryLoaded.size() &&
894        !SLocEntryLoaded[ID] &&
895        ExternalSLocEntries->ReadSLocEntry(ID) &&
896        Invalid)
897      *Invalid = true;
898
899    return SLocEntryTable[ID];
900  }
901
902  const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
903    return getSLocEntry(FID.ID, Invalid);
904  }
905
906  unsigned getNextOffset() const { return NextOffset; }
907
908  /// \brief Preallocate some number of source location entries, which
909  /// will be loaded as needed from the given external source.
910  void PreallocateSLocEntries(ExternalSLocEntrySource *Source,
911                              unsigned NumSLocEntries,
912                              unsigned NextOffset);
913
914  /// \brief Clear out any preallocated source location entries that
915  /// haven't already been loaded.
916  void ClearPreallocatedSLocEntries();
917
918private:
919  const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
920
921  /// isOffsetInFileID - Return true if the specified FileID contains the
922  /// specified SourceLocation offset.  This is a very hot method.
923  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
924    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
925    // If the entry is after the offset, it can't contain it.
926    if (SLocOffset < Entry.getOffset()) return false;
927
928    // If this is the last entry than it does.  Otherwise, the entry after it
929    // has to not include it.
930    if (FID.ID+1 == SLocEntryTable.size()) return true;
931
932    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
933  }
934
935  /// createFileID - Create a new fileID for the specified ContentCache and
936  ///  include position.  This works regardless of whether the ContentCache
937  ///  corresponds to a file or some other input source.
938  FileID createFileID(const SrcMgr::ContentCache* File,
939                      SourceLocation IncludePos,
940                      SrcMgr::CharacteristicKind DirCharacter,
941                      unsigned PreallocatedID = 0,
942                      unsigned Offset = 0);
943
944  const SrcMgr::ContentCache *
945    getOrCreateContentCache(const FileEntry *SourceFile);
946
947  /// createMemBufferContentCache - Create a new ContentCache for the specified
948  ///  memory buffer.
949  const SrcMgr::ContentCache*
950  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
951
952  FileID getFileIDSlow(unsigned SLocOffset) const;
953
954  SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
955  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
956
957  std::pair<FileID, unsigned>
958  getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
959                                        unsigned Offset) const;
960  std::pair<FileID, unsigned>
961  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
962                                   unsigned Offset) const;
963};
964
965
966}  // end namespace clang
967
968#endif
969