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