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/DenseMap.h"
25 #include "llvm/Support/MemoryBuffer.h"
38 /// SrcMgr - Public enums and private classes that are part of the
39 /// SourceManager implementation.
42 /// CharacteristicKind - This is used to represent whether a file or directory
43 /// holds normal user code, system code, or system code which is implicitly
44 /// 'extern "C"' in C++ mode. Entire directories can be tagged with this
45 /// (this is maintained by DirectoryLookup and friends) as can specific
46 /// FileInfos when a #pragma system_header is seen or various other cases.
48 enum CharacteristicKind {
49 C_User, C_System, C_ExternCSystem
52 /// ContentCache - One instance of this struct is kept for every file
53 /// loaded or used. This object owns the MemoryBuffer object.
56 /// \brief Whether the buffer is invalid.
58 /// \brief Whether the buffer should not be freed on destruction.
62 /// Buffer - The actual buffer containing the characters from the input
63 /// file. This is owned by the ContentCache object.
64 /// The bits indicate indicates whether the buffer is invalid.
65 mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
68 /// Reference to the file entry representing this ContentCache.
69 /// This reference does not own the FileEntry object.
70 /// It is possible for this to be NULL if
71 /// the ContentCache encapsulates an imaginary text buffer.
72 const FileEntry *OrigEntry;
74 /// \brief References the file which the contents were actually loaded from.
75 /// Can be different from 'Entry' if we overridden the contents of one file
76 /// with the contents of another file.
77 const FileEntry *ContentsEntry;
79 /// SourceLineCache - A bump pointer allocated array of offsets for each
80 /// source line. This is lazily computed. This is owned by the
81 /// SourceManager BumpPointerAllocator object.
82 unsigned *SourceLineCache;
84 /// NumLines - The number of lines in this ContentCache. This is only valid
85 /// if SourceLineCache is non-null.
88 /// getBuffer - Returns the memory buffer for the associated content.
90 /// \param Diag Object through which diagnostics will be emitted if the
91 /// buffer cannot be retrieved.
93 /// \param Loc If specified, is the location that invalid file diagnostics
94 /// will be emitted at.
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;
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;
108 /// getSizeBytesMapped - Returns the number of bytes actually mapped for
109 /// this ContentCache. This can be 0 if the MemBuffer was not actually
111 unsigned getSizeBytesMapped() const;
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;
117 void setBuffer(const llvm::MemoryBuffer *B) {
118 assert(!Buffer.getPointer() && "MemoryBuffer already set.");
119 Buffer.setPointer(B);
120 Buffer.setInt(false);
123 /// \brief Get the underlying buffer, returning NULL if the buffer is not
125 const llvm::MemoryBuffer *getRawBuffer() const {
126 return Buffer.getPointer();
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);
133 /// \brief Determine whether the buffer itself is invalid.
134 bool isBufferInvalid() const {
135 return Buffer.getInt() & InvalidFlag;
138 /// \brief Determine whether the buffer should be freed.
139 bool shouldFreeBuffer() const {
140 return (Buffer.getInt() & DoNotFreeFlag) == 0;
143 ContentCache(const FileEntry *Ent = 0)
144 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
145 SourceLineCache(0), NumLines(0) {}
147 ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
148 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
149 SourceLineCache(0), NumLines(0) {}
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)
159 OrigEntry = RHS.OrigEntry;
160 ContentsEntry = RHS.ContentsEntry;
162 assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
163 && "Passed ContentCache object cannot own a buffer.");
165 NumLines = RHS.NumLines;
169 // Disable assignments.
170 ContentCache &operator=(const ContentCache& RHS);
173 /// FileInfo - Information about a FileID, basically just the logical file
174 /// that it represents and include stack information.
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.
180 /// FileInfos contain a "ContentCache *", with the contents of the file.
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
187 /// Data - This contains the ContentCache* and the bits indicating the
188 /// characteristic of the file and whether it has #line info, all bitmangled
192 /// get - Return a FileInfo object.
193 static FileInfo get(SourceLocation IL, const ContentCache *Con,
194 CharacteristicKind FileCharacter) {
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;
204 SourceLocation getIncludeLoc() const {
205 return SourceLocation::getFromRawEncoding(IncludeLoc);
207 const ContentCache* getContentCache() const {
208 return reinterpret_cast<const ContentCache*>(Data & ~7UL);
211 /// getCharacteristic - Return whether this is a system header or not.
212 CharacteristicKind getFileCharacteristic() const {
213 return (CharacteristicKind)(Data & 3);
216 /// hasLineDirectives - Return true if this FileID has #line directives in
218 bool hasLineDirectives() const { return (Data & 4) != 0; }
220 /// setHasLineDirectives - Set the flag that indicates that this FileID has
221 /// line table entries associated with it.
222 void setHasLineDirectives() {
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.
233 /// SpellingLoc - Where the spelling for the token can be found.
234 unsigned SpellingLoc;
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;
245 SourceLocation getSpellingLoc() const {
246 return SourceLocation::getFromRawEncoding(SpellingLoc);
248 SourceLocation getExpansionLocStart() const {
249 return SourceLocation::getFromRawEncoding(ExpansionLocStart);
251 SourceLocation getExpansionLocEnd() const {
252 SourceLocation EndLoc =
253 SourceLocation::getFromRawEncoding(ExpansionLocEnd);
254 return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
257 std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
258 return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
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();
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
272 static ExpansionInfo create(SourceLocation SpellingLoc,
273 SourceLocation Start, SourceLocation End) {
275 X.SpellingLoc = SpellingLoc.getRawEncoding();
276 X.ExpansionLocStart = Start.getRawEncoding();
277 X.ExpansionLocEnd = End.getRawEncoding();
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.
292 /// #define F(x) f(x)
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
304 return create(SpellingLoc, ExpansionLoc, SourceLocation());
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.
312 unsigned Offset; // low bit is set for expansion info.
315 ExpansionInfo Expansion;
318 unsigned getOffset() const { return Offset >> 1; }
320 bool isExpansion() const { return Offset & 1; }
321 bool isFile() const { return !isExpansion(); }
323 const FileInfo &getFile() const {
324 assert(isFile() && "Not a file SLocEntry!");
328 const ExpansionInfo &getExpansion() const {
329 assert(isExpansion() && "Not a macro expansion SLocEntry!");
333 static SLocEntry get(unsigned Offset, const FileInfo &FI) {
335 E.Offset = Offset << 1;
340 static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
342 E.Offset = (Offset << 1) | 1;
343 E.Expansion = Expansion;
347 } // end SrcMgr namespace.
349 /// \brief External source of source location entries.
350 class ExternalSLocEntrySource {
352 virtual ~ExternalSLocEntrySource();
354 /// \brief Read the source location entry with index ID, which will always be
357 /// \returns true if an error occurred that prevented the source-location
358 /// entry from being loaded.
359 virtual bool ReadSLocEntry(int ID) = 0;
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.
366 class 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;
371 /// \brief True if LQueryFID was created before RQueryFID. This is used
372 /// to compare macro expansion locations.
373 bool IsLQFIDBeforeRQFID;
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.
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;
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;
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;
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;
406 return LOffset < ROffset;
409 // Set up a new query.
410 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
414 IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
418 LQueryFID = RQueryFID = FileID();
419 IsLQFIDBeforeRQFID = false;
422 void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
423 unsigned rCommonOffset) {
424 CommonFID = commonFID;
425 LCommonOffset = lCommonOffset;
426 RCommonOffset = rCommonOffset;
431 /// \brief This class handles loading and caching of source files into memory.
433 /// This object owns the MemoryBuffer objects for all of the loaded
434 /// files and assigns unique FileID's for each unique #include chain.
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.
443 class SourceManager : public llvm::RefCountedBase<SourceManager> {
444 /// \brief Diagnostic object.
447 FileManager &FileMgr;
449 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
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;
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;
461 /// \brief Files that have been overriden with the contents from another file.
462 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
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;
469 /// \brief The table of SLocEntries that are local to this module.
471 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
473 std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
475 /// \brief The table of SLocEntries that are loaded from other modules.
477 /// Negative FileIDs are indexes into this table. To get from ID to an index,
479 std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
481 /// \brief The starting offset of the next local SLocEntry.
483 /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
484 unsigned NextLocalOffset;
486 /// \brief The starting offset of the latest batch of loaded SLocEntries.
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;
492 /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
493 /// have already been loaded from the external source.
495 /// Same indexing as LoadedSLocEntryTable.
496 std::vector<bool> SLocEntryLoaded;
498 /// \brief An external source for source location entries.
499 ExternalSLocEntrySource *ExternalSLocEntries;
501 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
502 /// LastFileIDLookup records the last FileID looked up or created, because it
503 /// is very common to look up many tokens from the same file.
504 mutable FileID LastFileIDLookup;
506 /// LineTable - This holds information for #line directives. It is referenced
507 /// by indices from SLocEntryTable.
508 LineTableInfo *LineTable;
510 /// LastLineNo - These ivars serve as a cache used in the getLineNumber
511 /// method which is used to speedup getLineNumber calls to nearby locations.
512 mutable FileID LastLineNoFileIDQuery;
513 mutable SrcMgr::ContentCache *LastLineNoContentCache;
514 mutable unsigned LastLineNoFilePos;
515 mutable unsigned LastLineNoResult;
517 /// MainFileID - The file ID for the main source file of the translation unit.
520 // Statistics for -print-stats.
521 mutable unsigned NumLinearScans, NumBinaryProbes;
523 // Cache results for the isBeforeInTranslationUnit method.
524 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
526 // Cache for the "fake" buffer used for error-recovery purposes.
527 mutable llvm::MemoryBuffer *FakeBufferForRecovery;
529 // SourceManager doesn't support copy construction.
530 explicit SourceManager(const SourceManager&);
531 void operator=(const SourceManager&);
533 SourceManager(Diagnostic &Diag, FileManager &FileMgr);
536 void clearIDTables();
538 Diagnostic &getDiagnostics() const { return Diag; }
540 FileManager &getFileManager() const { return FileMgr; }
542 /// \brief Set true if the SourceManager should report the original file name
543 /// for contents of files that were overriden by other files.Defaults to true.
544 void setOverridenFilesKeepOriginalName(bool value) {
545 OverridenFilesKeepOriginalName = value;
548 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
549 /// that will represent the FileID for the main source. One example
550 /// of when this would be used is when the main source is read from STDIN.
551 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
552 assert(MainFileID.isInvalid() && "MainFileID already set!");
553 MainFileID = createFileIDForMemBuffer(Buffer);
557 //===--------------------------------------------------------------------===//
558 // MainFileID creation and querying methods.
559 //===--------------------------------------------------------------------===//
561 /// getMainFileID - Returns the FileID of the main source file.
562 FileID getMainFileID() const { return MainFileID; }
564 /// createMainFileID - Create the FileID for the main source file.
565 FileID createMainFileID(const FileEntry *SourceFile) {
566 assert(MainFileID.isInvalid() && "MainFileID already set!");
567 MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
571 /// \brief Set the file ID for the precompiled preamble, which is also the
573 void SetPreambleFileID(FileID Preamble) {
574 assert(MainFileID.isInvalid() && "MainFileID already set!");
575 MainFileID = Preamble;
578 //===--------------------------------------------------------------------===//
579 // Methods to create new FileID's and macro expansions.
580 //===--------------------------------------------------------------------===//
582 /// createFileID - Create a new FileID that represents the specified file
583 /// being #included from the specified IncludePosition. This translates NULL
584 /// into standard input.
585 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
586 SrcMgr::CharacteristicKind FileCharacter,
587 int LoadedID = 0, unsigned LoadedOffset = 0) {
588 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
589 assert(IR && "getOrCreateContentCache() cannot return NULL");
590 return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
593 /// createFileIDForMemBuffer - Create a new FileID that represents the
594 /// specified memory buffer. This does no caching of the buffer and takes
595 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
596 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
597 int LoadedID = 0, unsigned LoadedOffset = 0) {
598 return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
599 SrcMgr::C_User, LoadedID, LoadedOffset);
602 /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the
603 /// fact that a token from SpellingLoc should actually be referenced from
604 /// ExpansionLoc, and that it represents the expansion of a macro argument
605 /// into the function-like macro body.
606 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
607 SourceLocation ExpansionLoc,
610 /// createExpansionLoc - Return a new SourceLocation that encodes the fact
611 /// that a token from SpellingLoc should actually be referenced from
613 SourceLocation createExpansionLoc(SourceLocation Loc,
614 SourceLocation ExpansionLocStart,
615 SourceLocation ExpansionLocEnd,
618 unsigned LoadedOffset = 0);
620 /// \brief Retrieve the memory buffer associated with the given file.
622 /// \param Invalid If non-NULL, will be set \c true if an error
623 /// occurs while retrieving the memory buffer.
624 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
627 /// \brief Override the contents of the given source file by providing an
628 /// already-allocated buffer.
630 /// \param SourceFile the source file whose contents will be overriden.
632 /// \param Buffer the memory buffer whose contents will be used as the
633 /// data in the given source file.
635 /// \param DoNotFree If true, then the buffer will not be freed when the
636 /// source manager is destroyed.
637 void overrideFileContents(const FileEntry *SourceFile,
638 const llvm::MemoryBuffer *Buffer,
639 bool DoNotFree = false);
641 /// \brief Override the the given source file with another one.
643 /// \param SourceFile the source file which will be overriden.
645 /// \param NewFile the file whose contents will be used as the
646 /// data instead of the contents of the given source file.
647 void overrideFileContents(const FileEntry *SourceFile,
648 const FileEntry *NewFile);
650 //===--------------------------------------------------------------------===//
651 // FileID manipulation methods.
652 //===--------------------------------------------------------------------===//
654 /// getBuffer - Return the buffer for the specified FileID. If there is an
655 /// error opening this buffer the first time, this manufactures a temporary
656 /// buffer and returns a non-empty error string.
657 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
658 bool *Invalid = 0) const {
659 bool MyInvalid = false;
660 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
661 if (MyInvalid || !Entry.isFile()) {
665 return getFakeBufferForRecovery();
668 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
672 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
673 bool MyInvalid = false;
674 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
675 if (MyInvalid || !Entry.isFile()) {
679 return getFakeBufferForRecovery();
682 return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
687 /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
688 const FileEntry *getFileEntryForID(FileID FID) const {
689 bool MyInvalid = false;
690 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
691 if (MyInvalid || !Entry.isFile())
694 return Entry.getFile().getContentCache()->OrigEntry;
697 /// Returns the FileEntry record for the provided SLocEntry.
698 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
700 return sloc.getFile().getContentCache()->OrigEntry;
703 /// getBufferData - Return a StringRef to the source buffer data for the
704 /// specified FileID.
706 /// \param FID The file ID whose contents will be returned.
707 /// \param Invalid If non-NULL, will be set true if an error occurred.
708 StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
711 //===--------------------------------------------------------------------===//
712 // SourceLocation manipulation methods.
713 //===--------------------------------------------------------------------===//
715 /// getFileID - Return the FileID for a SourceLocation. This is a very
716 /// hot method that is used for all SourceManager queries that start with a
717 /// SourceLocation object. It is responsible for finding the entry in
718 /// SLocEntryTable which contains the specified location.
720 FileID getFileID(SourceLocation SpellingLoc) const {
721 unsigned SLocOffset = SpellingLoc.getOffset();
723 // If our one-entry cache covers this offset, just return it.
724 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
725 return LastFileIDLookup;
727 return getFileIDSlow(SLocOffset);
730 /// getLocForStartOfFile - Return the source location corresponding to the
731 /// first byte of the specified file.
732 SourceLocation getLocForStartOfFile(FileID FID) const {
733 bool Invalid = false;
734 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
735 if (Invalid || !Entry.isFile())
736 return SourceLocation();
738 unsigned FileOffset = Entry.getOffset();
739 return SourceLocation::getFileLoc(FileOffset);
742 /// getExpansionLoc - Given a SourceLocation object, return the expansion
743 /// location referenced by the ID.
744 SourceLocation getExpansionLoc(SourceLocation Loc) const {
745 // Handle the non-mapped case inline, defer to out of line code to handle
747 if (Loc.isFileID()) return Loc;
748 return getExpansionLocSlowCase(Loc);
751 /// getImmediateExpansionRange - Loc is required to be an expansion location.
752 /// Return the start/end of the expansion information.
753 std::pair<SourceLocation,SourceLocation>
754 getImmediateExpansionRange(SourceLocation Loc) const;
756 /// getExpansionRange - Given a SourceLocation object, return the range of
757 /// tokens covered by the expansion the ultimate file.
758 std::pair<SourceLocation,SourceLocation>
759 getExpansionRange(SourceLocation Loc) const;
762 /// getSpellingLoc - Given a SourceLocation object, return the spelling
763 /// location referenced by the ID. This is the place where the characters
764 /// that make up the lexed token can be found.
765 SourceLocation getSpellingLoc(SourceLocation Loc) const {
766 // Handle the non-mapped case inline, defer to out of line code to handle
768 if (Loc.isFileID()) return Loc;
769 return getSpellingLocSlowCase(Loc);
772 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
773 /// spelling location referenced by the ID. This is the first level down
774 /// towards the place where the characters that make up the lexed token can be
775 /// found. This should not generally be used by clients.
776 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
778 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
779 /// Offset pair. The first element is the FileID, the second is the
780 /// offset from the start of the buffer of the location.
781 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
782 FileID FID = getFileID(Loc);
783 return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
786 /// getDecomposedExpansionLoc - Decompose the specified location into a raw
787 /// FileID + Offset pair. If the location is an expansion record, walk
788 /// through it until we find the final location expanded.
789 std::pair<FileID, unsigned>
790 getDecomposedExpansionLoc(SourceLocation Loc) const {
791 FileID FID = getFileID(Loc);
792 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
794 unsigned Offset = Loc.getOffset()-E->getOffset();
796 return std::make_pair(FID, Offset);
798 return getDecomposedExpansionLocSlowCase(E);
801 /// getDecomposedSpellingLoc - Decompose the specified location into a raw
802 /// FileID + Offset pair. If the location is an expansion record, walk
803 /// through it until we find its spelling record.
804 std::pair<FileID, unsigned>
805 getDecomposedSpellingLoc(SourceLocation Loc) const {
806 FileID FID = getFileID(Loc);
807 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
809 unsigned Offset = Loc.getOffset()-E->getOffset();
811 return std::make_pair(FID, Offset);
812 return getDecomposedSpellingLocSlowCase(E, Offset);
815 /// getFileOffset - This method returns the offset from the start
816 /// of the file that the specified SourceLocation represents. This is not very
817 /// meaningful for a macro ID.
818 unsigned getFileOffset(SourceLocation SpellingLoc) const {
819 return getDecomposedLoc(SpellingLoc).second;
822 /// isMacroArgExpansion - This method tests whether the given source location
823 /// represents a macro argument's expansion into the function-like macro
824 /// definition. Such source locations only appear inside of the expansion
825 /// locations representing where a particular function-like macro was
827 bool isMacroArgExpansion(SourceLocation Loc) const;
829 //===--------------------------------------------------------------------===//
830 // Queries about the code at a SourceLocation.
831 //===--------------------------------------------------------------------===//
833 /// getCharacterData - Return a pointer to the start of the specified location
834 /// in the appropriate spelling MemoryBuffer.
836 /// \param Invalid If non-NULL, will be set \c true if an error occurs.
837 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
839 /// getColumnNumber - Return the column # for the specified file position.
840 /// This is significantly cheaper to compute than the line number. This
841 /// returns zero if the column number isn't known. This may only be called
842 /// on a file sloc, so you must choose a spelling or expansion location
843 /// before calling this method.
844 unsigned getColumnNumber(FileID FID, unsigned FilePos,
845 bool *Invalid = 0) const;
846 unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
847 unsigned getExpansionColumnNumber(SourceLocation Loc,
848 bool *Invalid = 0) const;
849 unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
852 /// getLineNumber - Given a SourceLocation, return the spelling line number
853 /// for the position indicated. This requires building and caching a table of
854 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
855 /// about to emit a diagnostic.
856 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
857 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
858 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
859 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
861 /// Return the filename or buffer identifier of the buffer the location is in.
862 /// Note that this name does not respect #line directives. Use getPresumedLoc
863 /// for normal clients.
864 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
866 /// getFileCharacteristic - return the file characteristic of the specified
867 /// source location, indicating whether this is a normal file, a system
868 /// header, or an "implicit extern C" system header.
870 /// This state can be modified with flags on GNU linemarker directives like:
872 /// which changes all source locations in the current file after that to be
873 /// considered to be from a system header.
874 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
876 /// getPresumedLoc - This method returns the "presumed" location of a
877 /// SourceLocation specifies. A "presumed location" can be modified by #line
878 /// or GNU line marker directives. This provides a view on the data that a
879 /// user should see in diagnostics, for example.
881 /// Note that a presumed location is always given as the expansion point of
882 /// an expansion location, not at the spelling location.
884 /// \returns The presumed location of the specified SourceLocation. If the
885 /// presumed location cannot be calculate (e.g., because \p Loc is invalid
886 /// or the file containing \p Loc has changed on disk), returns an invalid
887 /// presumed location.
888 PresumedLoc getPresumedLoc(SourceLocation Loc) const;
890 /// isFromSameFile - Returns true if both SourceLocations correspond to
892 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
893 return getFileID(Loc1) == getFileID(Loc2);
896 /// isFromMainFile - Returns true if the file of provided SourceLocation is
898 bool isFromMainFile(SourceLocation Loc) const {
899 return getFileID(Loc) == getMainFileID();
902 /// isInSystemHeader - Returns if a SourceLocation is in a system header.
903 bool isInSystemHeader(SourceLocation Loc) const {
904 return getFileCharacteristic(Loc) != SrcMgr::C_User;
907 /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
909 bool isInExternCSystemHeader(SourceLocation Loc) const {
910 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
913 /// \brief Given a specific chunk of a FileID (FileID with offset+length),
914 /// returns true if \arg Loc is inside that chunk and sets relative offset
915 /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset.
916 bool isInFileID(SourceLocation Loc,
917 FileID FID, unsigned offset, unsigned length,
918 unsigned *relativeOffset = 0) const {
919 assert(!FID.isInvalid());
923 unsigned start = getSLocEntry(FID).getOffset() + offset;
924 unsigned end = start + length;
927 // Make sure offset/length describe a chunk inside the given FileID.
930 NextOffset = 1U << 31U;
931 else if (FID.ID+1 == (int)LocalSLocEntryTable.size())
932 NextOffset = getNextLocalOffset();
934 NextOffset = getSLocEntryByID(FID.ID+1).getOffset();
935 assert(start < NextOffset);
936 assert(end < NextOffset);
939 if (Loc.getOffset() >= start && Loc.getOffset() < end) {
941 *relativeOffset = Loc.getOffset() - start;
948 //===--------------------------------------------------------------------===//
949 // Line Table Manipulation Routines
950 //===--------------------------------------------------------------------===//
952 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
954 unsigned getLineTableFilenameID(StringRef Str);
956 /// AddLineNote - Add a line note to the line table for the FileID and offset
957 /// specified by Loc. If FilenameID is -1, it is considered to be
959 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
960 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
961 bool IsFileEntry, bool IsFileExit,
962 bool IsSystemHeader, bool IsExternCHeader);
964 /// \brief Determine if the source manager has a line table.
965 bool hasLineTable() const { return LineTable != 0; }
967 /// \brief Retrieve the stored line table.
968 LineTableInfo &getLineTable();
970 //===--------------------------------------------------------------------===//
971 // Queries for performance analysis.
972 //===--------------------------------------------------------------------===//
974 /// Return the total amount of physical memory allocated by the
975 /// ContentCache allocator.
976 size_t getContentCacheSize() const {
977 return ContentCacheAlloc.getTotalMemory();
980 struct MemoryBufferSizes {
981 const size_t malloc_bytes;
982 const size_t mmap_bytes;
984 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
985 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
988 /// Return the amount of memory used by memory buffers, breaking down
989 /// by heap-backed versus mmap'ed memory.
990 MemoryBufferSizes getMemoryBufferSizes() const;
992 // Return the amount of memory used for various side tables and
993 // data structures in the SourceManager.
994 size_t getDataStructureSizes() const;
996 //===--------------------------------------------------------------------===//
997 // Other miscellaneous methods.
998 //===--------------------------------------------------------------------===//
1000 /// \brief Get the source location for the given file:line:col triplet.
1002 /// If the source file is included multiple times, the source location will
1003 /// be based upon the first inclusion.
1004 SourceLocation getLocation(const FileEntry *SourceFile,
1005 unsigned Line, unsigned Col);
1007 /// \brief Determines the order of 2 source locations in the translation unit.
1009 /// \returns true if LHS source location comes before RHS, false otherwise.
1010 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1012 /// \brief Determines the order of 2 source locations in the "source location
1014 bool isBeforeInSourceLocationOffset(SourceLocation LHS,
1015 SourceLocation RHS) const {
1016 return isBeforeInSourceLocationOffset(LHS, RHS.getOffset());
1019 /// \brief Determines the order of a source location and a source location
1020 /// offset in the "source location address space".
1022 /// Note that we always consider source locations loaded from
1023 bool isBeforeInSourceLocationOffset(SourceLocation LHS, unsigned RHS) const {
1024 unsigned LHSOffset = LHS.getOffset();
1025 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1026 bool RHSLoaded = RHS >= CurrentLoadedOffset;
1027 if (LHSLoaded == RHSLoaded)
1028 return LHS.getOffset() < RHS;
1033 // Iterators over FileInfos.
1034 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1035 ::const_iterator fileinfo_iterator;
1036 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1037 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1038 bool hasFileInfo(const FileEntry *File) const {
1039 return FileInfos.find(File) != FileInfos.end();
1042 /// PrintStats - Print statistics to stderr.
1044 void PrintStats() const;
1046 /// \brief Get the number of local SLocEntries we have.
1047 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1049 /// \brief Get a local SLocEntry. This is exposed for indexing.
1050 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1051 bool *Invalid = 0) const {
1052 assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1053 return LocalSLocEntryTable[Index];
1056 /// \brief Get the number of loaded SLocEntries we have.
1057 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1059 /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1060 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const {
1061 assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1062 if (!SLocEntryLoaded[Index])
1063 ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2));
1064 return LoadedSLocEntryTable[Index];
1067 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1068 return getSLocEntryByID(FID.ID);
1071 unsigned getNextLocalOffset() const { return NextLocalOffset; }
1073 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1074 assert(LoadedSLocEntryTable.empty() &&
1075 "Invalidating existing loaded entries");
1076 ExternalSLocEntries = Source;
1079 /// \brief Allocate a number of loaded SLocEntries, which will be actually
1080 /// loaded on demand from the external source.
1082 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1083 /// in the global source view. The lowest ID and the base offset of the
1084 /// entries will be returned.
1085 std::pair<int, unsigned>
1086 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1089 const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1091 /// \brief Get the entry with the given unwrapped FileID.
1092 const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1093 assert(ID != -1 && "Using FileID sentinel value");
1095 return getLoadedSLocEntryByID(ID);
1096 return getLocalSLocEntry(static_cast<unsigned>(ID));
1099 const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const {
1100 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2));
1103 /// createExpansionLoc - Implements the common elements of storing an
1104 /// expansion info struct into the SLocEntry table and producing a source
1105 /// location that refers to it.
1106 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1109 unsigned LoadedOffset = 0);
1111 /// isOffsetInFileID - Return true if the specified FileID contains the
1112 /// specified SourceLocation offset. This is a very hot method.
1113 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1114 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1115 // If the entry is after the offset, it can't contain it.
1116 if (SLocOffset < Entry.getOffset()) return false;
1118 // If this is the very last entry then it does.
1122 // If it is the last local entry, then it does if the location is local.
1123 if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1124 return SLocOffset < NextLocalOffset;
1127 // Otherwise, the entry after it has to not include it. This works for both
1128 // local and loaded entries.
1129 return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1132 /// createFileID - Create a new fileID for the specified ContentCache and
1133 /// include position. This works regardless of whether the ContentCache
1134 /// corresponds to a file or some other input source.
1135 FileID createFileID(const SrcMgr::ContentCache* File,
1136 SourceLocation IncludePos,
1137 SrcMgr::CharacteristicKind DirCharacter,
1138 int LoadedID, unsigned LoadedOffset);
1140 const SrcMgr::ContentCache *
1141 getOrCreateContentCache(const FileEntry *SourceFile);
1143 /// createMemBufferContentCache - Create a new ContentCache for the specified
1145 const SrcMgr::ContentCache*
1146 createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
1148 FileID getFileIDSlow(unsigned SLocOffset) const;
1149 FileID getFileIDLocal(unsigned SLocOffset) const;
1150 FileID getFileIDLoaded(unsigned SLocOffset) const;
1152 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1153 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1155 std::pair<FileID, unsigned>
1156 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1157 std::pair<FileID, unsigned>
1158 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1159 unsigned Offset) const;
1163 } // end namespace clang