1 //===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SourceManager interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SOURCEMANAGER_H
15 #define LLVM_CLANG_SOURCEMANAGER_H
17 #include "clang/Basic/LLVM.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 #include "llvm/ADT/PointerUnion.h"
23 #include "llvm/ADT/IntrusiveRefCntPtr.h"
24 #include "llvm/ADT/OwningPtr.h"
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/DenseSet.h"
27 #include "llvm/Support/MemoryBuffer.h"
34 class DiagnosticsEngine;
43 /// There are three different types of locations in a file: a spelling
44 /// location, an expansion location, and a presumed location.
46 /// Given an example of:
47 /// #define min(x, y) x < y ? x : y
49 /// and then later on a use of min:
53 /// The expansion location is the line in the source code where the macro
54 /// was expanded (the return statement), the spelling location is the
55 /// location in the source where the macro was originally defined,
56 /// and the presumed location is where the line directive states that
57 /// the line is 17, or any other line.
59 /// SrcMgr - Public enums and private classes that are part of the
60 /// SourceManager implementation.
63 /// CharacteristicKind - This is used to represent whether a file or directory
64 /// holds normal user code, system code, or system code which is implicitly
65 /// 'extern "C"' in C++ mode. Entire directories can be tagged with this
66 /// (this is maintained by DirectoryLookup and friends) as can specific
67 /// FileInfos when a #pragma system_header is seen or various other cases.
69 enum CharacteristicKind {
70 C_User, C_System, C_ExternCSystem
73 /// ContentCache - One instance of this struct is kept for every file
74 /// loaded or used. This object owns the MemoryBuffer object.
77 /// \brief Whether the buffer is invalid.
79 /// \brief Whether the buffer should not be freed on destruction.
83 /// Buffer - The actual buffer containing the characters from the input
84 /// file. This is owned by the ContentCache object.
85 /// The bits indicate indicates whether the buffer is invalid.
86 mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
89 /// Reference to the file entry representing this ContentCache.
90 /// This reference does not own the FileEntry object.
91 /// It is possible for this to be NULL if
92 /// the ContentCache encapsulates an imaginary text buffer.
93 const FileEntry *OrigEntry;
95 /// \brief References the file which the contents were actually loaded from.
96 /// Can be different from 'Entry' if we overridden the contents of one file
97 /// with the contents of another file.
98 const FileEntry *ContentsEntry;
100 /// SourceLineCache - A bump pointer allocated array of offsets for each
101 /// source line. This is lazily computed. This is owned by the
102 /// SourceManager BumpPointerAllocator object.
103 unsigned *SourceLineCache;
105 /// NumLines - The number of lines in this ContentCache. This is only valid
106 /// if SourceLineCache is non-null.
107 unsigned NumLines : 31;
109 /// \brief Indicates whether the buffer itself was provided to override
110 /// the actual file contents.
112 /// When true, the original entry may be a virtual file that does not
114 unsigned BufferOverridden : 1;
116 ContentCache(const FileEntry *Ent = 0)
117 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
118 SourceLineCache(0), NumLines(0), BufferOverridden(false) {}
120 ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
121 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
122 SourceLineCache(0), NumLines(0), BufferOverridden(false) {}
126 /// The copy ctor does not allow copies where source object has either
127 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
128 /// is not transferred, so this is a logical error.
129 ContentCache(const ContentCache &RHS)
130 : Buffer(0, false), SourceLineCache(0), BufferOverridden(false)
132 OrigEntry = RHS.OrigEntry;
133 ContentsEntry = RHS.ContentsEntry;
135 assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 &&
136 "Passed ContentCache object cannot own a buffer.");
138 NumLines = RHS.NumLines;
141 /// getBuffer - Returns the memory buffer for the associated content.
143 /// \param Diag Object through which diagnostics will be emitted if the
144 /// buffer cannot be retrieved.
146 /// \param Loc If specified, is the location that invalid file diagnostics
147 /// will be emitted at.
149 /// \param Invalid If non-NULL, will be set \c true if an error occurred.
150 const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag,
151 const SourceManager &SM,
152 SourceLocation Loc = SourceLocation(),
153 bool *Invalid = 0) const;
155 /// getSize - Returns the size of the content encapsulated by this
156 /// ContentCache. This can be the size of the source file or the size of an
157 /// arbitrary scratch buffer. If the ContentCache encapsulates a source
158 /// file this size is retrieved from the file's FileEntry.
159 unsigned getSize() const;
161 /// getSizeBytesMapped - Returns the number of bytes actually mapped for
162 /// this ContentCache. This can be 0 if the MemBuffer was not actually
164 unsigned getSizeBytesMapped() const;
166 /// Returns the kind of memory used to back the memory buffer for
167 /// this content cache. This is used for performance analysis.
168 llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
170 void setBuffer(const llvm::MemoryBuffer *B) {
171 assert(!Buffer.getPointer() && "MemoryBuffer already set.");
172 Buffer.setPointer(B);
173 Buffer.setInt(false);
176 /// \brief Get the underlying buffer, returning NULL if the buffer is not
178 const llvm::MemoryBuffer *getRawBuffer() const {
179 return Buffer.getPointer();
182 /// \brief Replace the existing buffer (which will be deleted)
183 /// with the given buffer.
184 void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
186 /// \brief Determine whether the buffer itself is invalid.
187 bool isBufferInvalid() const {
188 return Buffer.getInt() & InvalidFlag;
191 /// \brief Determine whether the buffer should be freed.
192 bool shouldFreeBuffer() const {
193 return (Buffer.getInt() & DoNotFreeFlag) == 0;
197 // Disable assignments.
198 ContentCache &operator=(const ContentCache& RHS);
201 /// FileInfo - Information about a FileID, basically just the logical file
202 /// that it represents and include stack information.
204 /// Each FileInfo has include stack information, indicating where it came
205 /// from. This information encodes the #include chain that a token was
206 /// expanded from. The main include file has an invalid IncludeLoc.
208 /// FileInfos contain a "ContentCache *", with the contents of the file.
211 /// IncludeLoc - The location of the #include that brought in this file.
212 /// This is an invalid SLOC for the main file (top of the #include chain).
213 unsigned IncludeLoc; // Really a SourceLocation
215 /// \brief Number of FileIDs (files and macros) that were created during
216 /// preprocessing of this #include, including this SLocEntry.
217 /// Zero means the preprocessor didn't provide such info for this SLocEntry.
218 unsigned NumCreatedFIDs;
220 /// Data - This contains the ContentCache* and the bits indicating the
221 /// characteristic of the file and whether it has #line info, all bitmangled
225 friend class clang::SourceManager;
226 friend class clang::ASTWriter;
227 friend class clang::ASTReader;
229 /// get - Return a FileInfo object.
230 static FileInfo get(SourceLocation IL, const ContentCache *Con,
231 CharacteristicKind FileCharacter) {
233 X.IncludeLoc = IL.getRawEncoding();
234 X.NumCreatedFIDs = 0;
235 X.Data = (uintptr_t)Con;
236 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
237 assert((unsigned)FileCharacter < 4 && "invalid file character");
238 X.Data |= (unsigned)FileCharacter;
242 SourceLocation getIncludeLoc() const {
243 return SourceLocation::getFromRawEncoding(IncludeLoc);
245 const ContentCache* getContentCache() const {
246 return reinterpret_cast<const ContentCache*>(Data & ~7UL);
249 /// getCharacteristic - Return whether this is a system header or not.
250 CharacteristicKind getFileCharacteristic() const {
251 return (CharacteristicKind)(Data & 3);
254 /// hasLineDirectives - Return true if this FileID has #line directives in
256 bool hasLineDirectives() const { return (Data & 4) != 0; }
258 /// setHasLineDirectives - Set the flag that indicates that this FileID has
259 /// line table entries associated with it.
260 void setHasLineDirectives() {
265 /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where
266 /// the token was ultimately expanded, and the SpellingLoc - where the actual
267 /// character data for the token came from.
268 class ExpansionInfo {
269 // Really these are all SourceLocations.
271 /// SpellingLoc - Where the spelling for the token can be found.
272 unsigned SpellingLoc;
274 /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these
275 /// indicate the start and end of the expansion. In object-like macros,
276 /// these will be the same. In a function-like macro expansion, the start
277 /// will be the identifier and the end will be the ')'. Finally, in
278 /// macro-argument instantitions, the end will be 'SourceLocation()', an
279 /// invalid location.
280 unsigned ExpansionLocStart, ExpansionLocEnd;
283 SourceLocation getSpellingLoc() const {
284 return SourceLocation::getFromRawEncoding(SpellingLoc);
286 SourceLocation getExpansionLocStart() const {
287 return SourceLocation::getFromRawEncoding(ExpansionLocStart);
289 SourceLocation getExpansionLocEnd() const {
290 SourceLocation EndLoc =
291 SourceLocation::getFromRawEncoding(ExpansionLocEnd);
292 return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
295 std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
296 return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
299 bool isMacroArgExpansion() const {
300 // Note that this needs to return false for default constructed objects.
301 return getExpansionLocStart().isValid() &&
302 SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
305 bool isFunctionMacroExpansion() const {
306 return getExpansionLocStart().isValid() &&
307 getExpansionLocStart() != getExpansionLocEnd();
310 /// create - Return a ExpansionInfo for an expansion. Start and End specify
311 /// the expansion range (where the macro is expanded), and SpellingLoc
312 /// specifies the spelling location (where the characters from the token
313 /// come from). All three can refer to normal File SLocs or expansion
315 static ExpansionInfo create(SourceLocation SpellingLoc,
316 SourceLocation Start, SourceLocation End) {
318 X.SpellingLoc = SpellingLoc.getRawEncoding();
319 X.ExpansionLocStart = Start.getRawEncoding();
320 X.ExpansionLocEnd = End.getRawEncoding();
324 /// createForMacroArg - Return a special ExpansionInfo for the expansion of
325 /// a macro argument into a function-like macro's body. ExpansionLoc
326 /// specifies the expansion location (where the macro is expanded). This
327 /// doesn't need to be a range because a macro is always expanded at
328 /// a macro parameter reference, and macro parameters are always exactly
329 /// one token. SpellingLoc specifies the spelling location (where the
330 /// characters from the token come from). ExpansionLoc and SpellingLoc can
331 /// both refer to normal File SLocs or expansion locations.
335 /// #define F(x) f(x)
339 /// When expanding '\c F(42)', the '\c x' would call this with an
340 /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its
341 /// location in the definition of '\c F'.
342 static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
343 SourceLocation ExpansionLoc) {
344 // We store an intentionally invalid source location for the end of the
345 // expansion range to mark that this is a macro argument ion rather than
347 return create(SpellingLoc, ExpansionLoc, SourceLocation());
351 /// SLocEntry - This is a discriminated union of FileInfo and
352 /// ExpansionInfo. SourceManager keeps an array of these objects, and
353 /// they are uniquely identified by the FileID datatype.
355 unsigned Offset; // low bit is set for expansion info.
358 ExpansionInfo Expansion;
361 unsigned getOffset() const { return Offset >> 1; }
363 bool isExpansion() const { return Offset & 1; }
364 bool isFile() const { return !isExpansion(); }
366 const FileInfo &getFile() const {
367 assert(isFile() && "Not a file SLocEntry!");
371 const ExpansionInfo &getExpansion() const {
372 assert(isExpansion() && "Not a macro expansion SLocEntry!");
376 static SLocEntry get(unsigned Offset, const FileInfo &FI) {
378 E.Offset = Offset << 1;
383 static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
385 E.Offset = (Offset << 1) | 1;
386 E.Expansion = Expansion;
390 } // end SrcMgr namespace.
392 /// \brief External source of source location entries.
393 class ExternalSLocEntrySource {
395 virtual ~ExternalSLocEntrySource();
397 /// \brief Read the source location entry with index ID, which will always be
400 /// \returns true if an error occurred that prevented the source-location
401 /// entry from being loaded.
402 virtual bool ReadSLocEntry(int ID) = 0;
406 /// IsBeforeInTranslationUnitCache - This class holds the cache used by
407 /// isBeforeInTranslationUnit. The cache structure is complex enough to be
408 /// worth breaking out of SourceManager.
409 class IsBeforeInTranslationUnitCache {
410 /// L/R QueryFID - These are the FID's of the cached query. If these match up
411 /// with a subsequent query, the result can be reused.
412 FileID LQueryFID, RQueryFID;
414 /// \brief True if LQueryFID was created before RQueryFID. This is used
415 /// to compare macro expansion locations.
416 bool IsLQFIDBeforeRQFID;
418 /// CommonFID - This is the file found in common between the two #include
419 /// traces. It is the nearest common ancestor of the #include tree.
422 /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
423 /// Usually, this represents the location of the #include for QueryFID, but if
424 /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
425 /// random token in the parent.
426 unsigned LCommonOffset, RCommonOffset;
429 /// isCacheValid - Return true if the currently cached values match up with
430 /// the specified LHS/RHS query. If not, we can't use the cache.
431 bool isCacheValid(FileID LHS, FileID RHS) const {
432 return LQueryFID == LHS && RQueryFID == RHS;
435 /// getCachedResult - If the cache is valid, compute the result given the
436 /// specified offsets in the LHS/RHS FID's.
437 bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
438 // If one of the query files is the common file, use the offset. Otherwise,
439 // use the #include loc in the common file.
440 if (LQueryFID != CommonFID) LOffset = LCommonOffset;
441 if (RQueryFID != CommonFID) ROffset = RCommonOffset;
443 // It is common for multiple macro expansions to be "included" from the same
444 // location (expansion location), in which case use the order of the FileIDs
445 // to determine which came first. This will also take care the case where
446 // one of the locations points at the inclusion/expansion point of the other
447 // in which case its FileID will come before the other.
448 if (LOffset == ROffset)
449 return IsLQFIDBeforeRQFID;
451 return LOffset < ROffset;
454 // Set up a new query.
455 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
459 IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
463 LQueryFID = RQueryFID = FileID();
464 IsLQFIDBeforeRQFID = false;
467 void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
468 unsigned rCommonOffset) {
469 CommonFID = commonFID;
470 LCommonOffset = lCommonOffset;
471 RCommonOffset = rCommonOffset;
476 /// \brief This class handles loading and caching of source files into memory.
478 /// This object owns the MemoryBuffer objects for all of the loaded
479 /// files and assigns unique FileID's for each unique #include chain.
481 /// The SourceManager can be queried for information about SourceLocation
482 /// objects, turning them into either spelling or expansion locations. Spelling
483 /// locations represent where the bytes corresponding to a token came from and
484 /// expansion locations represent where the location is in the user's view. In
485 /// the case of a macro expansion, for example, the spelling location indicates
486 /// where the expanded token came from and the expansion location specifies
487 /// where it was expanded.
488 class SourceManager : public RefCountedBase<SourceManager> {
489 /// \brief DiagnosticsEngine object.
490 DiagnosticsEngine &Diag;
492 FileManager &FileMgr;
494 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
496 /// FileInfos - Memoized information about all of the files tracked by this
497 /// SourceManager. This set allows us to merge ContentCache entries based
498 /// on their FileEntry*. All ContentCache objects will thus have unique,
499 /// non-null, FileEntry pointers.
500 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
502 /// \brief True if the ContentCache for files that are overriden by other
503 /// files, should report the original file name. Defaults to true.
504 bool OverridenFilesKeepOriginalName;
506 struct OverriddenFilesInfoTy {
507 /// \brief Files that have been overriden with the contents from another
509 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
510 /// \brief Files that were overridden with a memory buffer.
511 llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer;
514 /// \brief Lazily create the object keeping overridden files info, since
515 /// it is uncommonly used.
516 OwningPtr<OverriddenFilesInfoTy> OverriddenFilesInfo;
518 OverriddenFilesInfoTy &getOverriddenFilesInfo() {
519 if (!OverriddenFilesInfo)
520 OverriddenFilesInfo.reset(new OverriddenFilesInfoTy);
521 return *OverriddenFilesInfo;
524 /// MemBufferInfos - Information about various memory buffers that we have
525 /// read in. All FileEntry* within the stored ContentCache objects are NULL,
526 /// as they do not refer to a file.
527 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
529 /// \brief The table of SLocEntries that are local to this module.
531 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
533 std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
535 /// \brief The table of SLocEntries that are loaded from other modules.
537 /// Negative FileIDs are indexes into this table. To get from ID to an index,
539 mutable std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
541 /// \brief The starting offset of the next local SLocEntry.
543 /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
544 unsigned NextLocalOffset;
546 /// \brief The starting offset of the latest batch of loaded SLocEntries.
548 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
549 /// not have been loaded, so that value would be unknown.
550 unsigned CurrentLoadedOffset;
552 /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
554 static const unsigned MaxLoadedOffset = 1U << 31U;
556 /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
557 /// have already been loaded from the external source.
559 /// Same indexing as LoadedSLocEntryTable.
560 std::vector<bool> SLocEntryLoaded;
562 /// \brief An external source for source location entries.
563 ExternalSLocEntrySource *ExternalSLocEntries;
565 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
566 /// LastFileIDLookup records the last FileID looked up or created, because it
567 /// is very common to look up many tokens from the same file.
568 mutable FileID LastFileIDLookup;
570 /// LineTable - This holds information for #line directives. It is referenced
571 /// by indices from SLocEntryTable.
572 LineTableInfo *LineTable;
574 /// LastLineNo - These ivars serve as a cache used in the getLineNumber
575 /// method which is used to speedup getLineNumber calls to nearby locations.
576 mutable FileID LastLineNoFileIDQuery;
577 mutable SrcMgr::ContentCache *LastLineNoContentCache;
578 mutable unsigned LastLineNoFilePos;
579 mutable unsigned LastLineNoResult;
581 /// MainFileID - The file ID for the main source file of the translation unit.
584 /// \brief The file ID for the precompiled preamble there is one.
585 FileID PreambleFileID;
587 /// \brief The file ID for the preprocessor's predefines.
588 FileID PredefinesFileID;
590 // Statistics for -print-stats.
591 mutable unsigned NumLinearScans, NumBinaryProbes;
593 // Cache results for the isBeforeInTranslationUnit method.
594 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
596 // Cache for the "fake" buffer used for error-recovery purposes.
597 mutable llvm::MemoryBuffer *FakeBufferForRecovery;
599 mutable SrcMgr::ContentCache *FakeContentCacheForRecovery;
601 /// \brief Lazily computed map of macro argument chunks to their expanded
603 typedef std::map<unsigned, SourceLocation> MacroArgsMap;
605 mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap;
607 // SourceManager doesn't support copy construction.
608 explicit SourceManager(const SourceManager&);
609 void operator=(const SourceManager&);
611 SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr);
614 void clearIDTables();
616 DiagnosticsEngine &getDiagnostics() const { return Diag; }
618 FileManager &getFileManager() const { return FileMgr; }
620 /// \brief Set true if the SourceManager should report the original file name
621 /// for contents of files that were overriden by other files.Defaults to true.
622 void setOverridenFilesKeepOriginalName(bool value) {
623 OverridenFilesKeepOriginalName = value;
626 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
627 /// that will represent the FileID for the main source. One example
628 /// of when this would be used is when the main source is read from STDIN.
629 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
630 assert(MainFileID.isInvalid() && "MainFileID already set!");
631 MainFileID = createFileIDForMemBuffer(Buffer);
635 /// \brief Create the FileID for a memory buffer that contains the
636 /// preprocessor's predefines.
637 FileID createPredefinesFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
638 assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!");
639 PredefinesFileID = createFileIDForMemBuffer(Buffer);
640 return PredefinesFileID;
643 //===--------------------------------------------------------------------===//
644 // MainFileID creation and querying methods.
645 //===--------------------------------------------------------------------===//
647 /// getMainFileID - Returns the FileID of the main source file.
648 FileID getMainFileID() const { return MainFileID; }
650 /// \brief Returns the FileID of the preprocessor predefines buffer.
651 FileID getPredefinesFileID() const { return PredefinesFileID; }
653 /// createMainFileID - Create the FileID for the main source file.
654 FileID createMainFileID(const FileEntry *SourceFile,
655 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
656 assert(MainFileID.isInvalid() && "MainFileID already set!");
657 MainFileID = createFileID(SourceFile, SourceLocation(), Kind);
661 /// \brief Set the file ID for the main source file.
662 void setMainFileID(FileID FID) {
663 assert(MainFileID.isInvalid() && "MainFileID already set!");
667 /// \brief Set the file ID for the precompiled preamble.
668 void setPreambleFileID(FileID Preamble) {
669 assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
670 PreambleFileID = Preamble;
673 /// \brief Get the file ID for the precompiled preamble if there is one.
674 FileID getPreambleFileID() const { return PreambleFileID; }
676 //===--------------------------------------------------------------------===//
677 // Methods to create new FileID's and macro expansions.
678 //===--------------------------------------------------------------------===//
680 /// createFileID - Create a new FileID that represents the specified file
681 /// being #included from the specified IncludePosition. This translates NULL
682 /// into standard input.
683 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
684 SrcMgr::CharacteristicKind FileCharacter,
685 int LoadedID = 0, unsigned LoadedOffset = 0) {
686 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
687 assert(IR && "getOrCreateContentCache() cannot return NULL");
688 return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
691 /// createFileIDForMemBuffer - Create a new FileID that represents the
692 /// specified memory buffer. This does no caching of the buffer and takes
693 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
694 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
695 int LoadedID = 0, unsigned LoadedOffset = 0,
696 SourceLocation IncludeLoc = SourceLocation()) {
697 return createFileID(createMemBufferContentCache(Buffer), IncludeLoc,
698 SrcMgr::C_User, LoadedID, LoadedOffset);
701 /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the
702 /// fact that a token from SpellingLoc should actually be referenced from
703 /// ExpansionLoc, and that it represents the expansion of a macro argument
704 /// into the function-like macro body.
705 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
706 SourceLocation ExpansionLoc,
709 /// createExpansionLoc - Return a new SourceLocation that encodes the fact
710 /// that a token from SpellingLoc should actually be referenced from
712 SourceLocation createExpansionLoc(SourceLocation Loc,
713 SourceLocation ExpansionLocStart,
714 SourceLocation ExpansionLocEnd,
717 unsigned LoadedOffset = 0);
719 /// \brief Retrieve the memory buffer associated with the given file.
721 /// \param Invalid If non-NULL, will be set \c true if an error
722 /// occurs while retrieving the memory buffer.
723 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
726 /// \brief Override the contents of the given source file by providing an
727 /// already-allocated buffer.
729 /// \param SourceFile the source file whose contents will be overriden.
731 /// \param Buffer the memory buffer whose contents will be used as the
732 /// data in the given source file.
734 /// \param DoNotFree If true, then the buffer will not be freed when the
735 /// source manager is destroyed.
736 void overrideFileContents(const FileEntry *SourceFile,
737 const llvm::MemoryBuffer *Buffer,
738 bool DoNotFree = false);
740 /// \brief Override the the given source file with another one.
742 /// \param SourceFile the source file which will be overriden.
744 /// \param NewFile the file whose contents will be used as the
745 /// data instead of the contents of the given source file.
746 void overrideFileContents(const FileEntry *SourceFile,
747 const FileEntry *NewFile);
749 /// \brief Returns true if the file contents have been overridden.
750 bool isFileOverridden(const FileEntry *File) {
751 if (OverriddenFilesInfo) {
752 if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
754 if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
755 OverriddenFilesInfo->OverriddenFiles.end())
761 /// \brief Disable overridding the contents of a file, previously enabled
762 /// with \see overrideFileContents.
763 /// This should be called before parsing has begun.
764 void disableFileContentsOverride(const FileEntry *File);
766 //===--------------------------------------------------------------------===//
767 // FileID manipulation methods.
768 //===--------------------------------------------------------------------===//
770 /// getBuffer - Return the buffer for the specified FileID. If there is an
771 /// error opening this buffer the first time, this manufactures a temporary
772 /// buffer and returns a non-empty error string.
773 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
774 bool *Invalid = 0) const {
775 bool MyInvalid = false;
776 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
777 if (MyInvalid || !Entry.isFile()) {
781 return getFakeBufferForRecovery();
784 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
788 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
789 bool MyInvalid = false;
790 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
791 if (MyInvalid || !Entry.isFile()) {
795 return getFakeBufferForRecovery();
798 return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
803 /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
804 const FileEntry *getFileEntryForID(FileID FID) const {
805 bool MyInvalid = false;
806 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
807 if (MyInvalid || !Entry.isFile())
810 const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
813 return Content->OrigEntry;
816 /// Returns the FileEntry record for the provided SLocEntry.
817 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
819 const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
822 return Content->OrigEntry;
825 /// getBufferData - Return a StringRef to the source buffer data for the
826 /// specified FileID.
828 /// \param FID The file ID whose contents will be returned.
829 /// \param Invalid If non-NULL, will be set true if an error occurred.
830 StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
832 /// \brief Get the number of FileIDs (files and macros) that were created
833 /// during preprocessing of \p FID, including it.
834 unsigned getNumCreatedFIDsForFileID(FileID FID) const {
835 bool Invalid = false;
836 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
837 if (Invalid || !Entry.isFile())
840 return Entry.getFile().NumCreatedFIDs;
843 /// \brief Set the number of FileIDs (files and macros) that were created
844 /// during preprocessing of \p FID, including it.
845 void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
846 bool Invalid = false;
847 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
848 if (Invalid || !Entry.isFile())
851 assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
852 const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
855 //===--------------------------------------------------------------------===//
856 // SourceLocation manipulation methods.
857 //===--------------------------------------------------------------------===//
859 /// getFileID - Return the FileID for a SourceLocation. This is a very
860 /// hot method that is used for all SourceManager queries that start with a
861 /// SourceLocation object. It is responsible for finding the entry in
862 /// SLocEntryTable which contains the specified location.
864 FileID getFileID(SourceLocation SpellingLoc) const {
865 unsigned SLocOffset = SpellingLoc.getOffset();
867 // If our one-entry cache covers this offset, just return it.
868 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
869 return LastFileIDLookup;
871 return getFileIDSlow(SLocOffset);
874 /// getLocForStartOfFile - Return the source location corresponding to the
875 /// first byte of the specified file.
876 SourceLocation getLocForStartOfFile(FileID FID) const {
877 bool Invalid = false;
878 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
879 if (Invalid || !Entry.isFile())
880 return SourceLocation();
882 unsigned FileOffset = Entry.getOffset();
883 return SourceLocation::getFileLoc(FileOffset);
886 /// \brief Return the source location corresponding to the last byte of the
888 SourceLocation getLocForEndOfFile(FileID FID) const {
889 bool Invalid = false;
890 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
891 if (Invalid || !Entry.isFile())
892 return SourceLocation();
894 unsigned FileOffset = Entry.getOffset();
895 return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1);
898 /// \brief Returns the include location if \p FID is a #include'd file
899 /// otherwise it returns an invalid location.
900 SourceLocation getIncludeLoc(FileID FID) const {
901 bool Invalid = false;
902 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
903 if (Invalid || !Entry.isFile())
904 return SourceLocation();
906 return Entry.getFile().getIncludeLoc();
909 /// getExpansionLoc - Given a SourceLocation object, return the expansion
910 /// location referenced by the ID.
911 SourceLocation getExpansionLoc(SourceLocation Loc) const {
912 // Handle the non-mapped case inline, defer to out of line code to handle
914 if (Loc.isFileID()) return Loc;
915 return getExpansionLocSlowCase(Loc);
918 /// \brief Given \p Loc, if it is a macro location return the expansion
919 /// location or the spelling location, depending on if it comes from a
920 /// macro argument or not.
921 SourceLocation getFileLoc(SourceLocation Loc) const {
922 if (Loc.isFileID()) return Loc;
923 return getFileLocSlowCase(Loc);
926 /// getImmediateExpansionRange - Loc is required to be an expansion location.
927 /// Return the start/end of the expansion information.
928 std::pair<SourceLocation,SourceLocation>
929 getImmediateExpansionRange(SourceLocation Loc) const;
931 /// getExpansionRange - Given a SourceLocation object, return the range of
932 /// tokens covered by the expansion the ultimate file.
933 std::pair<SourceLocation,SourceLocation>
934 getExpansionRange(SourceLocation Loc) const;
937 /// getSpellingLoc - Given a SourceLocation object, return the spelling
938 /// location referenced by the ID. This is the place where the characters
939 /// that make up the lexed token can be found.
940 SourceLocation getSpellingLoc(SourceLocation Loc) const {
941 // Handle the non-mapped case inline, defer to out of line code to handle
943 if (Loc.isFileID()) return Loc;
944 return getSpellingLocSlowCase(Loc);
947 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
948 /// spelling location referenced by the ID. This is the first level down
949 /// towards the place where the characters that make up the lexed token can be
950 /// found. This should not generally be used by clients.
951 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
953 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
954 /// Offset pair. The first element is the FileID, the second is the
955 /// offset from the start of the buffer of the location.
956 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
957 FileID FID = getFileID(Loc);
958 bool Invalid = false;
959 const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
961 return std::make_pair(FileID(), 0);
962 return std::make_pair(FID, Loc.getOffset()-E.getOffset());
965 /// getDecomposedExpansionLoc - Decompose the specified location into a raw
966 /// FileID + Offset pair. If the location is an expansion record, walk
967 /// through it until we find the final location expanded.
968 std::pair<FileID, unsigned>
969 getDecomposedExpansionLoc(SourceLocation Loc) const {
970 FileID FID = getFileID(Loc);
971 bool Invalid = false;
972 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
974 return std::make_pair(FileID(), 0);
976 unsigned Offset = Loc.getOffset()-E->getOffset();
978 return std::make_pair(FID, Offset);
980 return getDecomposedExpansionLocSlowCase(E);
983 /// getDecomposedSpellingLoc - Decompose the specified location into a raw
984 /// FileID + Offset pair. If the location is an expansion record, walk
985 /// through it until we find its spelling record.
986 std::pair<FileID, unsigned>
987 getDecomposedSpellingLoc(SourceLocation Loc) const {
988 FileID FID = getFileID(Loc);
989 bool Invalid = false;
990 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
992 return std::make_pair(FileID(), 0);
994 unsigned Offset = Loc.getOffset()-E->getOffset();
996 return std::make_pair(FID, Offset);
997 return getDecomposedSpellingLocSlowCase(E, Offset);
1000 /// getFileOffset - This method returns the offset from the start
1001 /// of the file that the specified SourceLocation represents. This is not very
1002 /// meaningful for a macro ID.
1003 unsigned getFileOffset(SourceLocation SpellingLoc) const {
1004 return getDecomposedLoc(SpellingLoc).second;
1007 /// isMacroArgExpansion - This method tests whether the given source location
1008 /// represents a macro argument's expansion into the function-like macro
1009 /// definition. Such source locations only appear inside of the expansion
1010 /// locations representing where a particular function-like macro was
1012 bool isMacroArgExpansion(SourceLocation Loc) const;
1014 /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
1015 /// chunk of the source location address space.
1016 /// If it's true and \p RelativeOffset is non-null, it will be set to the
1017 /// relative offset of \p Loc inside the chunk.
1018 bool isInSLocAddrSpace(SourceLocation Loc,
1019 SourceLocation Start, unsigned Length,
1020 unsigned *RelativeOffset = 0) const {
1021 assert(((Start.getOffset() < NextLocalOffset &&
1022 Start.getOffset()+Length <= NextLocalOffset) ||
1023 (Start.getOffset() >= CurrentLoadedOffset &&
1024 Start.getOffset()+Length < MaxLoadedOffset)) &&
1025 "Chunk is not valid SLoc address space");
1026 unsigned LocOffs = Loc.getOffset();
1027 unsigned BeginOffs = Start.getOffset();
1028 unsigned EndOffs = BeginOffs + Length;
1029 if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1031 *RelativeOffset = LocOffs - BeginOffs;
1038 /// \brief Return true if both \p LHS and \p RHS are in the local source
1039 /// location address space or the loaded one. If it's true and \p
1040 /// RelativeOffset is non-null, it will be set to the offset of \p RHS
1041 /// relative to \p LHS.
1042 bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1043 int *RelativeOffset) const {
1044 unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1045 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1046 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1048 if (LHSLoaded == RHSLoaded) {
1050 *RelativeOffset = RHSOffs - LHSOffs;
1057 //===--------------------------------------------------------------------===//
1058 // Queries about the code at a SourceLocation.
1059 //===--------------------------------------------------------------------===//
1061 /// getCharacterData - Return a pointer to the start of the specified location
1062 /// in the appropriate spelling MemoryBuffer.
1064 /// \param Invalid If non-NULL, will be set \c true if an error occurs.
1065 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
1067 /// getColumnNumber - Return the column # for the specified file position.
1068 /// This is significantly cheaper to compute than the line number. This
1069 /// returns zero if the column number isn't known. This may only be called
1070 /// on a file sloc, so you must choose a spelling or expansion location
1071 /// before calling this method.
1072 unsigned getColumnNumber(FileID FID, unsigned FilePos,
1073 bool *Invalid = 0) const;
1074 unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1075 unsigned getExpansionColumnNumber(SourceLocation Loc,
1076 bool *Invalid = 0) const;
1077 unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1080 /// getLineNumber - Given a SourceLocation, return the spelling line number
1081 /// for the position indicated. This requires building and caching a table of
1082 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
1083 /// about to emit a diagnostic.
1084 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
1085 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1086 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1087 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1089 /// Return the filename or buffer identifier of the buffer the location is in.
1090 /// Note that this name does not respect #line directives. Use getPresumedLoc
1091 /// for normal clients.
1092 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
1094 /// getFileCharacteristic - return the file characteristic of the specified
1095 /// source location, indicating whether this is a normal file, a system
1096 /// header, or an "implicit extern C" system header.
1098 /// This state can be modified with flags on GNU linemarker directives like:
1100 /// which changes all source locations in the current file after that to be
1101 /// considered to be from a system header.
1102 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
1104 /// getPresumedLoc - This method returns the "presumed" location of a
1105 /// SourceLocation specifies. A "presumed location" can be modified by #line
1106 /// or GNU line marker directives. This provides a view on the data that a
1107 /// user should see in diagnostics, for example.
1109 /// Note that a presumed location is always given as the expansion point of
1110 /// an expansion location, not at the spelling location.
1112 /// \returns The presumed location of the specified SourceLocation. If the
1113 /// presumed location cannot be calculate (e.g., because \p Loc is invalid
1114 /// or the file containing \p Loc has changed on disk), returns an invalid
1115 /// presumed location.
1116 PresumedLoc getPresumedLoc(SourceLocation Loc) const;
1118 /// isFromSameFile - Returns true if both SourceLocations correspond to
1120 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1121 return getFileID(Loc1) == getFileID(Loc2);
1124 /// isFromMainFile - Returns true if the file of provided SourceLocation is
1126 bool isFromMainFile(SourceLocation Loc) const {
1127 return getFileID(Loc) == getMainFileID();
1130 /// isFromPredefines - Returns true if the provided SourceLocation is
1131 /// within the processor's predefines buffer.
1132 bool isFromPredefines(SourceLocation Loc) const {
1133 return getFileID(Loc) == getPredefinesFileID();
1136 /// isInSystemHeader - Returns if a SourceLocation is in a system header.
1137 bool isInSystemHeader(SourceLocation Loc) const {
1138 return getFileCharacteristic(Loc) != SrcMgr::C_User;
1141 /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
1143 bool isInExternCSystemHeader(SourceLocation Loc) const {
1144 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1147 /// \brief Returns whether \p Loc is expanded from a macro in a system header.
1148 bool isInSystemMacro(SourceLocation loc) {
1149 return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
1152 /// \brief The size of the SLocEnty that \p FID represents.
1153 unsigned getFileIDSize(FileID FID) const;
1155 /// \brief Given a specific FileID, returns true if \p Loc is inside that
1156 /// FileID chunk and sets relative offset (offset of \p Loc from beginning
1157 /// of FileID) to \p relativeOffset.
1158 bool isInFileID(SourceLocation Loc, FileID FID,
1159 unsigned *RelativeOffset = 0) const {
1160 unsigned Offs = Loc.getOffset();
1161 if (isOffsetInFileID(FID, Offs)) {
1163 *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1170 //===--------------------------------------------------------------------===//
1171 // Line Table Manipulation Routines
1172 //===--------------------------------------------------------------------===//
1174 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
1176 unsigned getLineTableFilenameID(StringRef Str);
1178 /// AddLineNote - Add a line note to the line table for the FileID and offset
1179 /// specified by Loc. If FilenameID is -1, it is considered to be
1181 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
1182 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
1183 bool IsFileEntry, bool IsFileExit,
1184 bool IsSystemHeader, bool IsExternCHeader);
1186 /// \brief Determine if the source manager has a line table.
1187 bool hasLineTable() const { return LineTable != 0; }
1189 /// \brief Retrieve the stored line table.
1190 LineTableInfo &getLineTable();
1192 //===--------------------------------------------------------------------===//
1193 // Queries for performance analysis.
1194 //===--------------------------------------------------------------------===//
1196 /// Return the total amount of physical memory allocated by the
1197 /// ContentCache allocator.
1198 size_t getContentCacheSize() const {
1199 return ContentCacheAlloc.getTotalMemory();
1202 struct MemoryBufferSizes {
1203 const size_t malloc_bytes;
1204 const size_t mmap_bytes;
1206 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1207 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1210 /// Return the amount of memory used by memory buffers, breaking down
1211 /// by heap-backed versus mmap'ed memory.
1212 MemoryBufferSizes getMemoryBufferSizes() const;
1214 // Return the amount of memory used for various side tables and
1215 // data structures in the SourceManager.
1216 size_t getDataStructureSizes() const;
1218 //===--------------------------------------------------------------------===//
1219 // Other miscellaneous methods.
1220 //===--------------------------------------------------------------------===//
1222 /// \brief Get the source location for the given file:line:col triplet.
1224 /// If the source file is included multiple times, the source location will
1225 /// be based upon the first inclusion.
1226 SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1227 unsigned Line, unsigned Col) const;
1229 /// \brief Get the FileID for the given file.
1231 /// If the source file is included multiple times, the FileID will be the
1232 /// first inclusion.
1233 FileID translateFile(const FileEntry *SourceFile) const;
1235 /// \brief Get the source location in \p FID for the given line:col.
1236 /// Returns null location if \p FID is not a file SLocEntry.
1237 SourceLocation translateLineCol(FileID FID,
1238 unsigned Line, unsigned Col) const;
1240 /// \brief If \p Loc points inside a function macro argument, the returned
1241 /// location will be the macro location in which the argument was expanded.
1242 /// If a macro argument is used multiple times, the expanded location will
1243 /// be at the first expansion of the argument.
1247 /// Passing a file location pointing at 'foo', will yield a macro location
1248 /// where 'foo' was expanded into.
1249 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
1251 /// \brief Determines the order of 2 source locations in the translation unit.
1253 /// \returns true if LHS source location comes before RHS, false otherwise.
1254 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1256 /// \brief Comparison function class.
1257 class LocBeforeThanCompare : public std::binary_function<SourceLocation,
1258 SourceLocation, bool> {
1262 explicit LocBeforeThanCompare(SourceManager &SM) : SM(SM) { }
1264 bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1265 return SM.isBeforeInTranslationUnit(LHS, RHS);
1269 /// \brief Determines the order of 2 source locations in the "source location
1271 bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1272 return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1275 /// \brief Determines the order of a source location and a source location
1276 /// offset in the "source location address space".
1278 /// Note that we always consider source locations loaded from
1279 bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1280 unsigned LHSOffset = LHS.getOffset();
1281 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1282 bool RHSLoaded = RHS >= CurrentLoadedOffset;
1283 if (LHSLoaded == RHSLoaded)
1284 return LHSOffset < RHS;
1289 // Iterators over FileInfos.
1290 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1291 ::const_iterator fileinfo_iterator;
1292 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1293 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1294 bool hasFileInfo(const FileEntry *File) const {
1295 return FileInfos.find(File) != FileInfos.end();
1298 /// PrintStats - Print statistics to stderr.
1300 void PrintStats() const;
1302 /// \brief Get the number of local SLocEntries we have.
1303 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1305 /// \brief Get a local SLocEntry. This is exposed for indexing.
1306 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1307 bool *Invalid = 0) const {
1308 assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1309 return LocalSLocEntryTable[Index];
1312 /// \brief Get the number of loaded SLocEntries we have.
1313 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1315 /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1316 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
1317 bool *Invalid = 0) const {
1318 assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1319 if (SLocEntryLoaded[Index])
1320 return LoadedSLocEntryTable[Index];
1321 return loadSLocEntry(Index, Invalid);
1324 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1325 if (FID.ID == 0 || FID.ID == -1) {
1326 if (Invalid) *Invalid = true;
1327 return LocalSLocEntryTable[0];
1329 return getSLocEntryByID(FID.ID);
1332 unsigned getNextLocalOffset() const { return NextLocalOffset; }
1334 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1335 assert(LoadedSLocEntryTable.empty() &&
1336 "Invalidating existing loaded entries");
1337 ExternalSLocEntries = Source;
1340 /// \brief Allocate a number of loaded SLocEntries, which will be actually
1341 /// loaded on demand from the external source.
1343 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1344 /// in the global source view. The lowest ID and the base offset of the
1345 /// entries will be returned.
1346 std::pair<int, unsigned>
1347 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1349 /// \brief Returns true if \p Loc came from a PCH/Module.
1350 bool isLoadedSourceLocation(SourceLocation Loc) const {
1351 return Loc.getOffset() >= CurrentLoadedOffset;
1354 /// \brief Returns true if \p Loc did not come from a PCH/Module.
1355 bool isLocalSourceLocation(SourceLocation Loc) const {
1356 return Loc.getOffset() < NextLocalOffset;
1359 /// \brief Returns true if \p FID came from a PCH/Module.
1360 bool isLoadedFileID(FileID FID) const {
1361 assert(FID.ID != -1 && "Using FileID sentinel value");
1365 /// \brief Returns true if \p FID did not come from a PCH/Module.
1366 bool isLocalFileID(FileID FID) const {
1367 return !isLoadedFileID(FID);
1371 const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1372 const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
1374 const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1376 /// \brief Get the entry with the given unwrapped FileID.
1377 const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1378 assert(ID != -1 && "Using FileID sentinel value");
1380 return getLoadedSLocEntryByID(ID);
1381 return getLocalSLocEntry(static_cast<unsigned>(ID));
1384 const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID,
1385 bool *Invalid = 0) const {
1386 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1389 /// createExpansionLoc - Implements the common elements of storing an
1390 /// expansion info struct into the SLocEntry table and producing a source
1391 /// location that refers to it.
1392 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1395 unsigned LoadedOffset = 0);
1397 /// isOffsetInFileID - Return true if the specified FileID contains the
1398 /// specified SourceLocation offset. This is a very hot method.
1399 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1400 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1401 // If the entry is after the offset, it can't contain it.
1402 if (SLocOffset < Entry.getOffset()) return false;
1404 // If this is the very last entry then it does.
1408 // If it is the last local entry, then it does if the location is local.
1409 if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1410 return SLocOffset < NextLocalOffset;
1413 // Otherwise, the entry after it has to not include it. This works for both
1414 // local and loaded entries.
1415 return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1418 /// createFileID - Create a new fileID for the specified ContentCache and
1419 /// include position. This works regardless of whether the ContentCache
1420 /// corresponds to a file or some other input source.
1421 FileID createFileID(const SrcMgr::ContentCache* File,
1422 SourceLocation IncludePos,
1423 SrcMgr::CharacteristicKind DirCharacter,
1424 int LoadedID, unsigned LoadedOffset);
1426 const SrcMgr::ContentCache *
1427 getOrCreateContentCache(const FileEntry *SourceFile);
1429 /// createMemBufferContentCache - Create a new ContentCache for the specified
1431 const SrcMgr::ContentCache*
1432 createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
1434 FileID getFileIDSlow(unsigned SLocOffset) const;
1435 FileID getFileIDLocal(unsigned SLocOffset) const;
1436 FileID getFileIDLoaded(unsigned SLocOffset) const;
1438 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1439 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1440 SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1442 std::pair<FileID, unsigned>
1443 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1444 std::pair<FileID, unsigned>
1445 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1446 unsigned Offset) const;
1447 void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const;
1449 friend class ASTReader;
1450 friend class ASTWriter;
1454 } // end namespace clang