SourceManager.h revision ac836e442cbd17f33533bd0b4879258945bc1723
1d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
2d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com//
3d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com//                     The LLVM Compiler Infrastructure
4d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com//
5d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com// This file is distributed under the University of Illinois Open Source
6d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com// License. See LICENSE.TXT for details.
7d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com//
8d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com//===----------------------------------------------------------------------===//
9d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com//
10d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com//  This file defines the SourceManager interface.
11d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com//
12d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com//===----------------------------------------------------------------------===//
13d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com
14d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com#ifndef LLVM_CLANG_SOURCEMANAGER_H
15d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com#define LLVM_CLANG_SOURCEMANAGER_H
16d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com
17d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com#include "clang/Basic/LLVM.h"
18d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com#include "clang/Basic/SourceLocation.h"
19d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com#include "llvm/Support/Allocator.h"
20d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com#include "llvm/Support/DataTypes.h"
21d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com#include "llvm/ADT/PointerIntPair.h"
22a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com#include "llvm/ADT/PointerUnion.h"
23a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com#include "llvm/ADT/IntrusiveRefCntPtr.h"
24a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com#include "llvm/ADT/DenseMap.h"
25a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com#include "llvm/Support/MemoryBuffer.h"
26a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com#include <vector>
27a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com#include <cassert>
28a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
29a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.comnamespace clang {
30a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
31a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.comclass Diagnostic;
32a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.comclass SourceManager;
33a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.comclass FileManager;
34a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.comclass FileEntry;
3572ae6bd24eb72be13d5745129c16058e4d54e2f4scroggo@google.comclass LineTableInfo;
36a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.comclass LangOptions;
37a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
38a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com/// SrcMgr - Public enums and private classes that are part of the
39a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com/// SourceManager implementation.
40a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com///
41a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.comnamespace SrcMgr {
42a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  /// CharacteristicKind - This is used to represent whether a file or directory
43a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  /// holds normal user code, system code, or system code which is implicitly
44a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
45a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  /// (this is maintained by DirectoryLookup and friends) as can specific
46a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  /// FileInfos when a #pragma system_header is seen or various other cases.
47a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  ///
48d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com  enum CharacteristicKind {
49a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    C_User, C_System, C_ExternCSystem
50a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  };
51a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
52a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  /// ContentCache - One instance of this struct is kept for every file
53a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  /// loaded or used.  This object owns the MemoryBuffer object.
54a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  class ContentCache {
55a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    enum CCFlags {
56a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com      /// \brief Whether the buffer is invalid.
57a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com      InvalidFlag = 0x01,
58a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com      /// \brief Whether the buffer should not be freed on destruction.
59a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com      DoNotFreeFlag = 0x02
60a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    };
6172ae6bd24eb72be13d5745129c16058e4d54e2f4scroggo@google.com
62a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// Buffer - The actual buffer containing the characters from the input
63a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// file.  This is owned by the ContentCache object.
64a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// The bits indicate indicates whether the buffer is invalid.
65a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
66a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
67a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com  public:
68a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// Reference to the file entry representing this ContentCache.
69a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// This reference does not own the FileEntry object.
70a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// It is possible for this to be NULL if
71941ee9303b62163ae08bbdcd7ad514e1a6389bdarobertphillips@google.com    /// the ContentCache encapsulates an imaginary text buffer.
72a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    const FileEntry *OrigEntry;
73a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
74a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// \brief References the file which the contents were actually loaded from.
75a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// Can be different from 'Entry' if we overridden the contents of one file
76a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// with the contents of another file.
77a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    const FileEntry *ContentsEntry;
78a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
79a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// SourceLineCache - A bump pointer allocated array of offsets for each
80a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// source line.  This is lazily computed.  This is owned by the
815370cd969d8f3957e4306068e6195ac1bca3d6cddjsollen@google.com    /// SourceManager BumpPointerAllocator object.
825370cd969d8f3957e4306068e6195ac1bca3d6cddjsollen@google.com    unsigned *SourceLineCache;
835370cd969d8f3957e4306068e6195ac1bca3d6cddjsollen@google.com
845370cd969d8f3957e4306068e6195ac1bca3d6cddjsollen@google.com    /// NumLines - The number of lines in this ContentCache.  This is only valid
85a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// if SourceLineCache is non-null.
86a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    unsigned NumLines;
87a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com
88a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    /// getBuffer - Returns the memory buffer for the associated content.
89a2ca41e3afdd8fad5e0e924dec029f33918e0a67djsollen@google.com    ///
90d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com    /// \param Diag Object through which diagnostics will be emitted if the
91d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com    /// buffer cannot be retrieved.
92d26147adbbdca85f07dff432025afee0c8614387caryclark@google.com    ///
93    /// \param Loc If specified, is the location that invalid file diagnostics
94    ///     will be emitted at.
95    ///
96    /// \param Invalid If non-NULL, will be set \c true if an error occurred.
97    const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
98                                        const SourceManager &SM,
99                                        SourceLocation Loc = SourceLocation(),
100                                        bool *Invalid = 0) const;
101
102    /// getSize - Returns the size of the content encapsulated by this
103    ///  ContentCache. This can be the size of the source file or the size of an
104    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
105    ///  file this size is retrieved from the file's FileEntry.
106    unsigned getSize() const;
107
108    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
109    /// this ContentCache. This can be 0 if the MemBuffer was not actually
110    /// expanded.
111    unsigned getSizeBytesMapped() const;
112
113    /// Returns the kind of memory used to back the memory buffer for
114    /// this content cache.  This is used for performance analysis.
115    llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
116
117    void setBuffer(const llvm::MemoryBuffer *B) {
118      assert(!Buffer.getPointer() && "MemoryBuffer already set.");
119      Buffer.setPointer(B);
120      Buffer.setInt(false);
121    }
122
123    /// \brief Get the underlying buffer, returning NULL if the buffer is not
124    /// yet available.
125    const llvm::MemoryBuffer *getRawBuffer() const {
126      return Buffer.getPointer();
127    }
128
129    /// \brief Replace the existing buffer (which will be deleted)
130    /// with the given buffer.
131    void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
132
133    /// \brief Determine whether the buffer itself is invalid.
134    bool isBufferInvalid() const {
135      return Buffer.getInt() & InvalidFlag;
136    }
137
138    /// \brief Determine whether the buffer should be freed.
139    bool shouldFreeBuffer() const {
140      return (Buffer.getInt() & DoNotFreeFlag) == 0;
141    }
142
143    ContentCache(const FileEntry *Ent = 0)
144      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
145        SourceLineCache(0), NumLines(0) {}
146
147    ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
148      : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
149        SourceLineCache(0), NumLines(0) {}
150
151    ~ContentCache();
152
153    /// The copy ctor does not allow copies where source object has either
154    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
155    ///  is not transferred, so this is a logical error.
156    ContentCache(const ContentCache &RHS)
157      : Buffer(0, false), SourceLineCache(0)
158    {
159      OrigEntry = RHS.OrigEntry;
160      ContentsEntry = RHS.ContentsEntry;
161
162      assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
163              && "Passed ContentCache object cannot own a buffer.");
164
165      NumLines = RHS.NumLines;
166    }
167
168  private:
169    // Disable assignments.
170    ContentCache &operator=(const ContentCache& RHS);
171  };
172
173  /// FileInfo - Information about a FileID, basically just the logical file
174  /// that it represents and include stack information.
175  ///
176  /// Each FileInfo has include stack information, indicating where it came
177  /// from. This information encodes the #include chain that a token was
178  /// expanded from. The main include file has an invalid IncludeLoc.
179  ///
180  /// FileInfos contain a "ContentCache *", with the contents of the file.
181  ///
182  class FileInfo {
183    /// IncludeLoc - The location of the #include that brought in this file.
184    /// This is an invalid SLOC for the main file (top of the #include chain).
185    unsigned IncludeLoc;  // Really a SourceLocation
186
187    /// Data - This contains the ContentCache* and the bits indicating the
188    /// characteristic of the file and whether it has #line info, all bitmangled
189    /// together.
190    uintptr_t Data;
191  public:
192    /// get - Return a FileInfo object.
193    static FileInfo get(SourceLocation IL, const ContentCache *Con,
194                        CharacteristicKind FileCharacter) {
195      FileInfo X;
196      X.IncludeLoc = IL.getRawEncoding();
197      X.Data = (uintptr_t)Con;
198      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
199      assert((unsigned)FileCharacter < 4 && "invalid file character");
200      X.Data |= (unsigned)FileCharacter;
201      return X;
202    }
203
204    SourceLocation getIncludeLoc() const {
205      return SourceLocation::getFromRawEncoding(IncludeLoc);
206    }
207    const ContentCache* getContentCache() const {
208      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
209    }
210
211    /// getCharacteristic - Return whether this is a system header or not.
212    CharacteristicKind getFileCharacteristic() const {
213      return (CharacteristicKind)(Data & 3);
214    }
215
216    /// hasLineDirectives - Return true if this FileID has #line directives in
217    /// it.
218    bool hasLineDirectives() const { return (Data & 4) != 0; }
219
220    /// setHasLineDirectives - Set the flag that indicates that this FileID has
221    /// line table entries associated with it.
222    void setHasLineDirectives() {
223      Data |= 4;
224    }
225  };
226
227  /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where
228  /// the token was ultimately expanded, and the SpellingLoc - where the actual
229  /// character data for the token came from.
230  class ExpansionInfo {
231    // Really these are all SourceLocations.
232
233    /// SpellingLoc - Where the spelling for the token can be found.
234    unsigned SpellingLoc;
235
236    /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these
237    /// indicate the start and end of the expansion. In object-like macros,
238    /// these will be the same. In a function-like macro expansion, the start
239    /// will be the identifier and the end will be the ')'. Finally, in
240    /// macro-argument instantitions, the end will be 'SourceLocation()', an
241    /// invalid location.
242    unsigned ExpansionLocStart, ExpansionLocEnd;
243
244  public:
245    SourceLocation getSpellingLoc() const {
246      return SourceLocation::getFromRawEncoding(SpellingLoc);
247    }
248    SourceLocation getExpansionLocStart() const {
249      return SourceLocation::getFromRawEncoding(ExpansionLocStart);
250    }
251    SourceLocation getExpansionLocEnd() const {
252      SourceLocation EndLoc =
253        SourceLocation::getFromRawEncoding(ExpansionLocEnd);
254      return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
255    }
256
257    std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
258      return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
259    }
260
261    bool isMacroArgExpansion() const {
262      // Note that this needs to return false for default constructed objects.
263      return getExpansionLocStart().isValid() &&
264        SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
265    }
266
267    /// create - Return a ExpansionInfo for an expansion. Start and End specify
268    /// the expansion range (where the macro is expanded), and SpellingLoc
269    /// specifies the spelling location (where the characters from the token
270    /// come from). All three can refer to normal File SLocs or expansion
271    /// locations.
272    static ExpansionInfo create(SourceLocation SpellingLoc,
273                                SourceLocation Start, SourceLocation End) {
274      ExpansionInfo X;
275      X.SpellingLoc = SpellingLoc.getRawEncoding();
276      X.ExpansionLocStart = Start.getRawEncoding();
277      X.ExpansionLocEnd = End.getRawEncoding();
278      return X;
279    }
280
281    /// createForMacroArg - Return a special ExpansionInfo for the expansion of
282    /// a macro argument into a function-like macro's body. ExpansionLoc
283    /// specifies the expansion location (where the macro is expanded). This
284    /// doesn't need to be a range because a macro is always expanded at
285    /// a macro parameter reference, and macro parameters are always exactly
286    /// one token. SpellingLoc specifies the spelling location (where the
287    /// characters from the token come from). ExpansionLoc and SpellingLoc can
288    /// both refer to normal File SLocs or expansion locations.
289    ///
290    /// Given the code:
291    /// \code
292    ///   #define F(x) f(x)
293    ///   F(42);
294    /// \endcode
295    ///
296    /// When expanding '\c F(42)', the '\c x' would call this with an
297    /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its
298    /// location in the definition of '\c F'.
299    static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
300                                           SourceLocation ExpansionLoc) {
301      // We store an intentionally invalid source location for the end of the
302      // expansion range to mark that this is a macro argument ion rather than
303      // a normal one.
304      return create(SpellingLoc, ExpansionLoc, SourceLocation());
305    }
306  };
307
308  /// SLocEntry - This is a discriminated union of FileInfo and
309  /// ExpansionInfo.  SourceManager keeps an array of these objects, and
310  /// they are uniquely identified by the FileID datatype.
311  class SLocEntry {
312    unsigned Offset;   // low bit is set for expansion info.
313    union {
314      FileInfo File;
315      ExpansionInfo Expansion;
316    };
317  public:
318    unsigned getOffset() const { return Offset >> 1; }
319
320    bool isExpansion() const { return Offset & 1; }
321    bool isFile() const { return !isExpansion(); }
322
323    const FileInfo &getFile() const {
324      assert(isFile() && "Not a file SLocEntry!");
325      return File;
326    }
327
328    const ExpansionInfo &getExpansion() const {
329      assert(isExpansion() && "Not a macro expansion SLocEntry!");
330      return Expansion;
331    }
332
333    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
334      SLocEntry E;
335      E.Offset = Offset << 1;
336      E.File = FI;
337      return E;
338    }
339
340    static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
341      SLocEntry E;
342      E.Offset = (Offset << 1) | 1;
343      E.Expansion = Expansion;
344      return E;
345    }
346  };
347}  // end SrcMgr namespace.
348
349/// \brief External source of source location entries.
350class ExternalSLocEntrySource {
351public:
352  virtual ~ExternalSLocEntrySource();
353
354  /// \brief Read the source location entry with index ID, which will always be
355  /// less than -1.
356  ///
357  /// \returns true if an error occurred that prevented the source-location
358  /// entry from being loaded.
359  virtual bool ReadSLocEntry(int ID) = 0;
360};
361
362
363/// IsBeforeInTranslationUnitCache - This class holds the cache used by
364/// isBeforeInTranslationUnit.  The cache structure is complex enough to be
365/// worth breaking out of SourceManager.
366class IsBeforeInTranslationUnitCache {
367  /// L/R QueryFID - These are the FID's of the cached query.  If these match up
368  /// with a subsequent query, the result can be reused.
369  FileID LQueryFID, RQueryFID;
370
371  /// \brief True if LQueryFID was created before RQueryFID. This is used
372  /// to compare macro expansion locations.
373  bool IsLQFIDBeforeRQFID;
374
375  /// CommonFID - This is the file found in common between the two #include
376  /// traces.  It is the nearest common ancestor of the #include tree.
377  FileID CommonFID;
378
379  /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
380  /// Usually, this represents the location of the #include for QueryFID, but if
381  /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
382  /// random token in the parent.
383  unsigned LCommonOffset, RCommonOffset;
384public:
385
386  /// isCacheValid - Return true if the currently cached values match up with
387  /// the specified LHS/RHS query.  If not, we can't use the cache.
388  bool isCacheValid(FileID LHS, FileID RHS) const {
389    return LQueryFID == LHS && RQueryFID == RHS;
390  }
391
392  /// getCachedResult - If the cache is valid, compute the result given the
393  /// specified offsets in the LHS/RHS FID's.
394  bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
395    // If one of the query files is the common file, use the offset.  Otherwise,
396    // use the #include loc in the common file.
397    if (LQueryFID != CommonFID) LOffset = LCommonOffset;
398    if (RQueryFID != CommonFID) ROffset = RCommonOffset;
399
400    // It is common for multiple macro expansions to be "included" from the same
401    // location (expansion location), in which case use the order of the FileIDs
402    // to determine which came first.
403    if (LOffset == ROffset && LQueryFID != CommonFID && RQueryFID != CommonFID)
404      return IsLQFIDBeforeRQFID;
405
406    return LOffset < ROffset;
407  }
408
409  // Set up a new query.
410  void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
411    assert(LHS != RHS);
412    LQueryFID = LHS;
413    RQueryFID = RHS;
414    IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
415  }
416
417  void clear() {
418    LQueryFID = RQueryFID = FileID();
419    IsLQFIDBeforeRQFID = false;
420  }
421
422  void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
423                    unsigned rCommonOffset) {
424    CommonFID = commonFID;
425    LCommonOffset = lCommonOffset;
426    RCommonOffset = rCommonOffset;
427  }
428
429};
430
431/// \brief This class handles loading and caching of source files into memory.
432///
433/// This object owns the MemoryBuffer objects for all of the loaded
434/// files and assigns unique FileID's for each unique #include chain.
435///
436/// The SourceManager can be queried for information about SourceLocation
437/// objects, turning them into either spelling or expansion locations. Spelling
438/// locations represent where the bytes corresponding to a token came from and
439/// expansion locations represent where the location is in the user's view. In
440/// the case of a macro expansion, for example, the spelling location indicates
441/// where the expanded token came from and the expansion location specifies
442/// where it was expanded.
443class SourceManager : public llvm::RefCountedBase<SourceManager> {
444  /// \brief Diagnostic object.
445  Diagnostic &Diag;
446
447  FileManager &FileMgr;
448
449  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
450
451  /// FileInfos - Memoized information about all of the files tracked by this
452  /// SourceManager.  This set allows us to merge ContentCache entries based
453  /// on their FileEntry*.  All ContentCache objects will thus have unique,
454  /// non-null, FileEntry pointers.
455  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
456
457  /// \brief True if the ContentCache for files that are overriden by other
458  /// files, should report the original file name. Defaults to true.
459  bool OverridenFilesKeepOriginalName;
460
461  /// \brief Files that have been overriden with the contents from another file.
462  llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
463
464  /// MemBufferInfos - Information about various memory buffers that we have
465  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
466  /// as they do not refer to a file.
467  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
468
469  /// \brief The table of SLocEntries that are local to this module.
470  ///
471  /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
472  /// expansion.
473  std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
474
475  /// \brief The table of SLocEntries that are loaded from other modules.
476  ///
477  /// Negative FileIDs are indexes into this table. To get from ID to an index,
478  /// use (-ID - 2).
479  std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
480
481  /// \brief The starting offset of the next local SLocEntry.
482  ///
483  /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
484  unsigned NextLocalOffset;
485
486  /// \brief The starting offset of the latest batch of loaded SLocEntries.
487  ///
488  /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
489  /// not have been loaded, so that value would be unknown.
490  unsigned CurrentLoadedOffset;
491
492  /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
493  /// starts at 2^31.
494  static const unsigned MaxLoadedOffset = 1U << 31U;
495
496  /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
497  /// have already been loaded from the external source.
498  ///
499  /// Same indexing as LoadedSLocEntryTable.
500  std::vector<bool> SLocEntryLoaded;
501
502  /// \brief An external source for source location entries.
503  ExternalSLocEntrySource *ExternalSLocEntries;
504
505  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
506  /// LastFileIDLookup records the last FileID looked up or created, because it
507  /// is very common to look up many tokens from the same file.
508  mutable FileID LastFileIDLookup;
509
510  /// LineTable - This holds information for #line directives.  It is referenced
511  /// by indices from SLocEntryTable.
512  LineTableInfo *LineTable;
513
514  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
515  /// method which is used to speedup getLineNumber calls to nearby locations.
516  mutable FileID LastLineNoFileIDQuery;
517  mutable SrcMgr::ContentCache *LastLineNoContentCache;
518  mutable unsigned LastLineNoFilePos;
519  mutable unsigned LastLineNoResult;
520
521  /// MainFileID - The file ID for the main source file of the translation unit.
522  FileID MainFileID;
523
524  // Statistics for -print-stats.
525  mutable unsigned NumLinearScans, NumBinaryProbes;
526
527  // Cache results for the isBeforeInTranslationUnit method.
528  mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
529
530  // Cache for the "fake" buffer used for error-recovery purposes.
531  mutable llvm::MemoryBuffer *FakeBufferForRecovery;
532
533  // SourceManager doesn't support copy construction.
534  explicit SourceManager(const SourceManager&);
535  void operator=(const SourceManager&);
536public:
537  SourceManager(Diagnostic &Diag, FileManager &FileMgr);
538  ~SourceManager();
539
540  void clearIDTables();
541
542  Diagnostic &getDiagnostics() const { return Diag; }
543
544  FileManager &getFileManager() const { return FileMgr; }
545
546  /// \brief Set true if the SourceManager should report the original file name
547  /// for contents of files that were overriden by other files.Defaults to true.
548  void setOverridenFilesKeepOriginalName(bool value) {
549    OverridenFilesKeepOriginalName = value;
550  }
551
552  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
553  ///  that will represent the FileID for the main source.  One example
554  ///  of when this would be used is when the main source is read from STDIN.
555  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
556    assert(MainFileID.isInvalid() && "MainFileID already set!");
557    MainFileID = createFileIDForMemBuffer(Buffer);
558    return MainFileID;
559  }
560
561  //===--------------------------------------------------------------------===//
562  // MainFileID creation and querying methods.
563  //===--------------------------------------------------------------------===//
564
565  /// getMainFileID - Returns the FileID of the main source file.
566  FileID getMainFileID() const { return MainFileID; }
567
568  /// createMainFileID - Create the FileID for the main source file.
569  FileID createMainFileID(const FileEntry *SourceFile) {
570    assert(MainFileID.isInvalid() && "MainFileID already set!");
571    MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
572    return MainFileID;
573  }
574
575  /// \brief Set the file ID for the precompiled preamble, which is also the
576  /// main file.
577  void SetPreambleFileID(FileID Preamble) {
578    assert(MainFileID.isInvalid() && "MainFileID already set!");
579    MainFileID = Preamble;
580  }
581
582  //===--------------------------------------------------------------------===//
583  // Methods to create new FileID's and macro expansions.
584  //===--------------------------------------------------------------------===//
585
586  /// createFileID - Create a new FileID that represents the specified file
587  /// being #included from the specified IncludePosition.  This translates NULL
588  /// into standard input.
589  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
590                      SrcMgr::CharacteristicKind FileCharacter,
591                      int LoadedID = 0, unsigned LoadedOffset = 0) {
592    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
593    assert(IR && "getOrCreateContentCache() cannot return NULL");
594    return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
595  }
596
597  /// createFileIDForMemBuffer - Create a new FileID that represents the
598  /// specified memory buffer.  This does no caching of the buffer and takes
599  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
600  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
601                                  int LoadedID = 0, unsigned LoadedOffset = 0) {
602    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
603                        SrcMgr::C_User, LoadedID, LoadedOffset);
604  }
605
606  /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the
607  /// fact that a token from SpellingLoc should actually be referenced from
608  /// ExpansionLoc, and that it represents the expansion of a macro argument
609  /// into the function-like macro body.
610  SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
611                                            SourceLocation ExpansionLoc,
612                                            unsigned TokLength);
613
614  /// createExpansionLoc - Return a new SourceLocation that encodes the fact
615  /// that a token from SpellingLoc should actually be referenced from
616  /// ExpansionLoc.
617  SourceLocation createExpansionLoc(SourceLocation Loc,
618                                    SourceLocation ExpansionLocStart,
619                                    SourceLocation ExpansionLocEnd,
620                                    unsigned TokLength,
621                                    int LoadedID = 0,
622                                    unsigned LoadedOffset = 0);
623
624  /// \brief Retrieve the memory buffer associated with the given file.
625  ///
626  /// \param Invalid If non-NULL, will be set \c true if an error
627  /// occurs while retrieving the memory buffer.
628  const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
629                                                   bool *Invalid = 0);
630
631  /// \brief Override the contents of the given source file by providing an
632  /// already-allocated buffer.
633  ///
634  /// \param SourceFile the source file whose contents will be overriden.
635  ///
636  /// \param Buffer the memory buffer whose contents will be used as the
637  /// data in the given source file.
638  ///
639  /// \param DoNotFree If true, then the buffer will not be freed when the
640  /// source manager is destroyed.
641  void overrideFileContents(const FileEntry *SourceFile,
642                            const llvm::MemoryBuffer *Buffer,
643                            bool DoNotFree = false);
644
645  /// \brief Override the the given source file with another one.
646  ///
647  /// \param SourceFile the source file which will be overriden.
648  ///
649  /// \param NewFile the file whose contents will be used as the
650  /// data instead of the contents of the given source file.
651  void overrideFileContents(const FileEntry *SourceFile,
652                            const FileEntry *NewFile);
653
654  //===--------------------------------------------------------------------===//
655  // FileID manipulation methods.
656  //===--------------------------------------------------------------------===//
657
658  /// getBuffer - Return the buffer for the specified FileID. If there is an
659  /// error opening this buffer the first time, this manufactures a temporary
660  /// buffer and returns a non-empty error string.
661  const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
662                                      bool *Invalid = 0) const {
663    bool MyInvalid = false;
664    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
665    if (MyInvalid || !Entry.isFile()) {
666      if (Invalid)
667        *Invalid = true;
668
669      return getFakeBufferForRecovery();
670    }
671
672    return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
673                                                        Invalid);
674  }
675
676  const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
677    bool MyInvalid = false;
678    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
679    if (MyInvalid || !Entry.isFile()) {
680      if (Invalid)
681        *Invalid = true;
682
683      return getFakeBufferForRecovery();
684    }
685
686    return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
687                                                        SourceLocation(),
688                                                        Invalid);
689  }
690
691  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
692  const FileEntry *getFileEntryForID(FileID FID) const {
693    bool MyInvalid = false;
694    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
695    if (MyInvalid || !Entry.isFile())
696      return 0;
697
698    return Entry.getFile().getContentCache()->OrigEntry;
699  }
700
701  /// Returns the FileEntry record for the provided SLocEntry.
702  const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
703  {
704    return sloc.getFile().getContentCache()->OrigEntry;
705  }
706
707  /// getBufferData - Return a StringRef to the source buffer data for the
708  /// specified FileID.
709  ///
710  /// \param FID The file ID whose contents will be returned.
711  /// \param Invalid If non-NULL, will be set true if an error occurred.
712  StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
713
714
715  //===--------------------------------------------------------------------===//
716  // SourceLocation manipulation methods.
717  //===--------------------------------------------------------------------===//
718
719  /// getFileID - Return the FileID for a SourceLocation.  This is a very
720  /// hot method that is used for all SourceManager queries that start with a
721  /// SourceLocation object.  It is responsible for finding the entry in
722  /// SLocEntryTable which contains the specified location.
723  ///
724  FileID getFileID(SourceLocation SpellingLoc) const {
725    unsigned SLocOffset = SpellingLoc.getOffset();
726
727    // If our one-entry cache covers this offset, just return it.
728    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
729      return LastFileIDLookup;
730
731    return getFileIDSlow(SLocOffset);
732  }
733
734  /// getLocForStartOfFile - Return the source location corresponding to the
735  /// first byte of the specified file.
736  SourceLocation getLocForStartOfFile(FileID FID) const {
737    bool Invalid = false;
738    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
739    if (Invalid || !Entry.isFile())
740      return SourceLocation();
741
742    unsigned FileOffset = Entry.getOffset();
743    return SourceLocation::getFileLoc(FileOffset);
744  }
745
746  /// getExpansionLoc - Given a SourceLocation object, return the expansion
747  /// location referenced by the ID.
748  SourceLocation getExpansionLoc(SourceLocation Loc) const {
749    // Handle the non-mapped case inline, defer to out of line code to handle
750    // expansions.
751    if (Loc.isFileID()) return Loc;
752    return getExpansionLocSlowCase(Loc);
753  }
754
755  /// getImmediateExpansionRange - Loc is required to be an expansion location.
756  /// Return the start/end of the expansion information.
757  std::pair<SourceLocation,SourceLocation>
758  getImmediateExpansionRange(SourceLocation Loc) const;
759
760  /// getExpansionRange - Given a SourceLocation object, return the range of
761  /// tokens covered by the expansion the ultimate file.
762  std::pair<SourceLocation,SourceLocation>
763  getExpansionRange(SourceLocation Loc) const;
764
765
766  /// getSpellingLoc - Given a SourceLocation object, return the spelling
767  /// location referenced by the ID.  This is the place where the characters
768  /// that make up the lexed token can be found.
769  SourceLocation getSpellingLoc(SourceLocation Loc) const {
770    // Handle the non-mapped case inline, defer to out of line code to handle
771    // expansions.
772    if (Loc.isFileID()) return Loc;
773    return getSpellingLocSlowCase(Loc);
774  }
775
776  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
777  /// spelling location referenced by the ID.  This is the first level down
778  /// towards the place where the characters that make up the lexed token can be
779  /// found.  This should not generally be used by clients.
780  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
781
782  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
783  /// Offset pair.  The first element is the FileID, the second is the
784  /// offset from the start of the buffer of the location.
785  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
786    FileID FID = getFileID(Loc);
787    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
788  }
789
790  /// getDecomposedExpansionLoc - Decompose the specified location into a raw
791  /// FileID + Offset pair. If the location is an expansion record, walk
792  /// through it until we find the final location expanded.
793  std::pair<FileID, unsigned>
794  getDecomposedExpansionLoc(SourceLocation Loc) const {
795    FileID FID = getFileID(Loc);
796    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
797
798    unsigned Offset = Loc.getOffset()-E->getOffset();
799    if (Loc.isFileID())
800      return std::make_pair(FID, Offset);
801
802    return getDecomposedExpansionLocSlowCase(E);
803  }
804
805  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
806  /// FileID + Offset pair.  If the location is an expansion record, walk
807  /// through it until we find its spelling record.
808  std::pair<FileID, unsigned>
809  getDecomposedSpellingLoc(SourceLocation Loc) const {
810    FileID FID = getFileID(Loc);
811    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
812
813    unsigned Offset = Loc.getOffset()-E->getOffset();
814    if (Loc.isFileID())
815      return std::make_pair(FID, Offset);
816    return getDecomposedSpellingLocSlowCase(E, Offset);
817  }
818
819  /// getFileOffset - This method returns the offset from the start
820  /// of the file that the specified SourceLocation represents. This is not very
821  /// meaningful for a macro ID.
822  unsigned getFileOffset(SourceLocation SpellingLoc) const {
823    return getDecomposedLoc(SpellingLoc).second;
824  }
825
826  /// isMacroArgExpansion - This method tests whether the given source location
827  /// represents a macro argument's expansion into the function-like macro
828  /// definition. Such source locations only appear inside of the expansion
829  /// locations representing where a particular function-like macro was
830  /// expanded.
831  bool isMacroArgExpansion(SourceLocation Loc) const;
832
833  //===--------------------------------------------------------------------===//
834  // Queries about the code at a SourceLocation.
835  //===--------------------------------------------------------------------===//
836
837  /// getCharacterData - Return a pointer to the start of the specified location
838  /// in the appropriate spelling MemoryBuffer.
839  ///
840  /// \param Invalid If non-NULL, will be set \c true if an error occurs.
841  const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
842
843  /// getColumnNumber - Return the column # for the specified file position.
844  /// This is significantly cheaper to compute than the line number.  This
845  /// returns zero if the column number isn't known.  This may only be called
846  /// on a file sloc, so you must choose a spelling or expansion location
847  /// before calling this method.
848  unsigned getColumnNumber(FileID FID, unsigned FilePos,
849                           bool *Invalid = 0) const;
850  unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
851  unsigned getExpansionColumnNumber(SourceLocation Loc,
852                                    bool *Invalid = 0) const;
853  unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
854
855
856  /// getLineNumber - Given a SourceLocation, return the spelling line number
857  /// for the position indicated.  This requires building and caching a table of
858  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
859  /// about to emit a diagnostic.
860  unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
861  unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
862  unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
863  unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
864
865  /// Return the filename or buffer identifier of the buffer the location is in.
866  /// Note that this name does not respect #line directives.  Use getPresumedLoc
867  /// for normal clients.
868  const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
869
870  /// getFileCharacteristic - return the file characteristic of the specified
871  /// source location, indicating whether this is a normal file, a system
872  /// header, or an "implicit extern C" system header.
873  ///
874  /// This state can be modified with flags on GNU linemarker directives like:
875  ///   # 4 "foo.h" 3
876  /// which changes all source locations in the current file after that to be
877  /// considered to be from a system header.
878  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
879
880  /// getPresumedLoc - This method returns the "presumed" location of a
881  /// SourceLocation specifies.  A "presumed location" can be modified by #line
882  /// or GNU line marker directives.  This provides a view on the data that a
883  /// user should see in diagnostics, for example.
884  ///
885  /// Note that a presumed location is always given as the expansion point of
886  /// an expansion location, not at the spelling location.
887  ///
888  /// \returns The presumed location of the specified SourceLocation. If the
889  /// presumed location cannot be calculate (e.g., because \p Loc is invalid
890  /// or the file containing \p Loc has changed on disk), returns an invalid
891  /// presumed location.
892  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
893
894  /// isFromSameFile - Returns true if both SourceLocations correspond to
895  ///  the same file.
896  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
897    return getFileID(Loc1) == getFileID(Loc2);
898  }
899
900  /// isFromMainFile - Returns true if the file of provided SourceLocation is
901  ///   the main file.
902  bool isFromMainFile(SourceLocation Loc) const {
903    return getFileID(Loc) == getMainFileID();
904  }
905
906  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
907  bool isInSystemHeader(SourceLocation Loc) const {
908    return getFileCharacteristic(Loc) != SrcMgr::C_User;
909  }
910
911  /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
912  /// system header.
913  bool isInExternCSystemHeader(SourceLocation Loc) const {
914    return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
915  }
916
917  /// \brief Given a specific chunk of a FileID (FileID with offset+length),
918  /// returns true if \arg Loc is inside that chunk and sets relative offset
919  /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset.
920  bool isInFileID(SourceLocation Loc,
921                  FileID FID, unsigned offset, unsigned length,
922                  unsigned *relativeOffset = 0) const {
923    assert(!FID.isInvalid());
924    if (Loc.isInvalid())
925      return false;
926
927    unsigned start = getSLocEntry(FID).getOffset() + offset;
928    unsigned end = start + length;
929
930#ifndef NDEBUG
931    // Make sure offset/length describe a chunk inside the given FileID.
932    unsigned NextOffset;
933    if (FID.ID == -2)
934      NextOffset = 1U << 31U;
935    else if (FID.ID+1 == (int)LocalSLocEntryTable.size())
936      NextOffset = getNextLocalOffset();
937    else
938      NextOffset = getSLocEntryByID(FID.ID+1).getOffset();
939    assert(start < NextOffset);
940    assert(end   < NextOffset);
941#endif
942
943    if (Loc.getOffset() >= start && Loc.getOffset() < end) {
944      if (relativeOffset)
945        *relativeOffset = Loc.getOffset() - start;
946      return true;
947    }
948
949    return false;
950  }
951
952  //===--------------------------------------------------------------------===//
953  // Line Table Manipulation Routines
954  //===--------------------------------------------------------------------===//
955
956  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
957  ///
958  unsigned getLineTableFilenameID(StringRef Str);
959
960  /// AddLineNote - Add a line note to the line table for the FileID and offset
961  /// specified by Loc.  If FilenameID is -1, it is considered to be
962  /// unspecified.
963  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
964  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
965                   bool IsFileEntry, bool IsFileExit,
966                   bool IsSystemHeader, bool IsExternCHeader);
967
968  /// \brief Determine if the source manager has a line table.
969  bool hasLineTable() const { return LineTable != 0; }
970
971  /// \brief Retrieve the stored line table.
972  LineTableInfo &getLineTable();
973
974  //===--------------------------------------------------------------------===//
975  // Queries for performance analysis.
976  //===--------------------------------------------------------------------===//
977
978  /// Return the total amount of physical memory allocated by the
979  /// ContentCache allocator.
980  size_t getContentCacheSize() const {
981    return ContentCacheAlloc.getTotalMemory();
982  }
983
984  struct MemoryBufferSizes {
985    const size_t malloc_bytes;
986    const size_t mmap_bytes;
987
988    MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
989      : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
990  };
991
992  /// Return the amount of memory used by memory buffers, breaking down
993  /// by heap-backed versus mmap'ed memory.
994  MemoryBufferSizes getMemoryBufferSizes() const;
995
996  // Return the amount of memory used for various side tables and
997  // data structures in the SourceManager.
998  size_t getDataStructureSizes() const;
999
1000  //===--------------------------------------------------------------------===//
1001  // Other miscellaneous methods.
1002  //===--------------------------------------------------------------------===//
1003
1004  /// \brief Get the source location for the given file:line:col triplet.
1005  ///
1006  /// If the source file is included multiple times, the source location will
1007  /// be based upon the first inclusion.
1008  ///
1009  /// If the location points inside a function macro argument, the returned
1010  /// location will be the macro location in which the argument was expanded.
1011  /// \sa getMacroArgExpandedLocation
1012  SourceLocation getLocation(const FileEntry *SourceFile,
1013                             unsigned Line, unsigned Col) {
1014    SourceLocation Loc = translateFileLineCol(SourceFile, Line, Col);
1015    return getMacroArgExpandedLocation(Loc);
1016  }
1017
1018  /// \brief Get the source location for the given file:line:col triplet.
1019  ///
1020  /// If the source file is included multiple times, the source location will
1021  /// be based upon the first inclusion.
1022  SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1023                                      unsigned Line, unsigned Col);
1024
1025  /// \brief If \arg Loc points inside a function macro argument, the returned
1026  /// location will be the macro location in which the argument was expanded.
1027  /// If a macro argument is used multiple times, the expanded location will
1028  /// be at the first expansion of the argument.
1029  /// e.g.
1030  ///   MY_MACRO(foo);
1031  ///             ^
1032  /// Passing a file location pointing at 'foo', will yield a macro location
1033  /// where 'foo' was expanded into.
1034  SourceLocation getMacroArgExpandedLocation(SourceLocation Loc);
1035
1036  /// \brief Determines the order of 2 source locations in the translation unit.
1037  ///
1038  /// \returns true if LHS source location comes before RHS, false otherwise.
1039  bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1040
1041  /// \brief Determines the order of 2 source locations in the "source location
1042  /// address space".
1043  bool isBeforeInSourceLocationOffset(SourceLocation LHS,
1044                                      SourceLocation RHS) const {
1045    return isBeforeInSourceLocationOffset(LHS, RHS.getOffset());
1046  }
1047
1048  /// \brief Determines the order of a source location and a source location
1049  /// offset in the "source location address space".
1050  ///
1051  /// Note that we always consider source locations loaded from
1052  bool isBeforeInSourceLocationOffset(SourceLocation LHS, unsigned RHS) const {
1053    unsigned LHSOffset = LHS.getOffset();
1054    bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1055    bool RHSLoaded = RHS >= CurrentLoadedOffset;
1056    if (LHSLoaded == RHSLoaded)
1057      return LHS.getOffset() < RHS;
1058
1059    return LHSLoaded;
1060  }
1061
1062  // Iterators over FileInfos.
1063  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1064      ::const_iterator fileinfo_iterator;
1065  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1066  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1067  bool hasFileInfo(const FileEntry *File) const {
1068    return FileInfos.find(File) != FileInfos.end();
1069  }
1070
1071  /// PrintStats - Print statistics to stderr.
1072  ///
1073  void PrintStats() const;
1074
1075  /// \brief Get the number of local SLocEntries we have.
1076  unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1077
1078  /// \brief Get a local SLocEntry. This is exposed for indexing.
1079  const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1080                                             bool *Invalid = 0) const {
1081    assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1082    return LocalSLocEntryTable[Index];
1083  }
1084
1085  /// \brief Get the number of loaded SLocEntries we have.
1086  unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1087
1088  /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1089  const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const {
1090    assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1091    if (!SLocEntryLoaded[Index])
1092      ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2));
1093    return LoadedSLocEntryTable[Index];
1094  }
1095
1096  const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1097    return getSLocEntryByID(FID.ID);
1098  }
1099
1100  unsigned getNextLocalOffset() const { return NextLocalOffset; }
1101
1102  void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1103    assert(LoadedSLocEntryTable.empty() &&
1104           "Invalidating existing loaded entries");
1105    ExternalSLocEntries = Source;
1106  }
1107
1108  /// \brief Allocate a number of loaded SLocEntries, which will be actually
1109  /// loaded on demand from the external source.
1110  ///
1111  /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1112  /// in the global source view. The lowest ID and the base offset of the
1113  /// entries will be returned.
1114  std::pair<int, unsigned>
1115  AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1116
1117private:
1118  const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1119
1120  /// \brief Get the entry with the given unwrapped FileID.
1121  const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1122    assert(ID != -1 && "Using FileID sentinel value");
1123    if (ID < 0)
1124      return getLoadedSLocEntryByID(ID);
1125    return getLocalSLocEntry(static_cast<unsigned>(ID));
1126  }
1127
1128  const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const {
1129    return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2));
1130  }
1131
1132  /// createExpansionLoc - Implements the common elements of storing an
1133  /// expansion info struct into the SLocEntry table and producing a source
1134  /// location that refers to it.
1135  SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1136                                        unsigned TokLength,
1137                                        int LoadedID = 0,
1138                                        unsigned LoadedOffset = 0);
1139
1140  /// isOffsetInFileID - Return true if the specified FileID contains the
1141  /// specified SourceLocation offset.  This is a very hot method.
1142  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1143    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1144    // If the entry is after the offset, it can't contain it.
1145    if (SLocOffset < Entry.getOffset()) return false;
1146
1147    // If this is the very last entry then it does.
1148    if (FID.ID == -2)
1149      return true;
1150
1151    // If it is the last local entry, then it does if the location is local.
1152    if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1153      return SLocOffset < NextLocalOffset;
1154    }
1155
1156    // Otherwise, the entry after it has to not include it. This works for both
1157    // local and loaded entries.
1158    return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1159  }
1160
1161  /// createFileID - Create a new fileID for the specified ContentCache and
1162  ///  include position.  This works regardless of whether the ContentCache
1163  ///  corresponds to a file or some other input source.
1164  FileID createFileID(const SrcMgr::ContentCache* File,
1165                      SourceLocation IncludePos,
1166                      SrcMgr::CharacteristicKind DirCharacter,
1167                      int LoadedID, unsigned LoadedOffset);
1168
1169  const SrcMgr::ContentCache *
1170    getOrCreateContentCache(const FileEntry *SourceFile);
1171
1172  /// createMemBufferContentCache - Create a new ContentCache for the specified
1173  ///  memory buffer.
1174  const SrcMgr::ContentCache*
1175  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
1176
1177  FileID getFileIDSlow(unsigned SLocOffset) const;
1178  FileID getFileIDLocal(unsigned SLocOffset) const;
1179  FileID getFileIDLoaded(unsigned SLocOffset) const;
1180
1181  SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1182  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1183
1184  std::pair<FileID, unsigned>
1185  getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1186  std::pair<FileID, unsigned>
1187  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1188                                   unsigned Offset) const;
1189};
1190
1191
1192}  // end namespace clang
1193
1194#endif
1195