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