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