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