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/SourceLocation.h"
18 #include "llvm/Support/Allocator.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/ADT/PointerIntPair.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/ADT/IntrusiveRefCntPtr.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/Support/MemoryBuffer.h"
41 /// SrcMgr - Public enums and private classes that are part of the
42 /// SourceManager implementation.
45 /// CharacteristicKind - This is used to represent whether a file or directory
46 /// holds normal user code, system code, or system code which is implicitly
47 /// 'extern "C"' in C++ mode. Entire directories can be tagged with this
48 /// (this is maintained by DirectoryLookup and friends) as can specific
49 /// FileIDInfos when a #pragma system_header is seen or various other cases.
51 enum CharacteristicKind {
52 C_User, C_System, C_ExternCSystem
55 /// ContentCache - One instance of this struct is kept for every file
56 /// loaded or used. This object owns the MemoryBuffer object.
59 /// \brief Whether the buffer is invalid.
61 /// \brief Whether the buffer should not be freed on destruction.
65 /// Buffer - The actual buffer containing the characters from the input
66 /// file. This is owned by the ContentCache object.
67 /// The bits indicate indicates whether the buffer is invalid.
68 mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
71 /// Reference to the file entry representing this ContentCache.
72 /// This reference does not own the FileEntry object.
73 /// It is possible for this to be NULL if
74 /// the ContentCache encapsulates an imaginary text buffer.
75 const FileEntry *OrigEntry;
77 /// \brief References the file which the contents were actually loaded from.
78 /// Can be different from 'Entry' if we overridden the contents of one file
79 /// with the contents of another file.
80 const FileEntry *ContentsEntry;
82 /// SourceLineCache - A bump pointer allocated array of offsets for each
83 /// source line. This is lazily computed. This is owned by the
84 /// SourceManager BumpPointerAllocator object.
85 unsigned *SourceLineCache;
87 /// NumLines - The number of lines in this ContentCache. This is only valid
88 /// if SourceLineCache is non-null.
91 /// getBuffer - Returns the memory buffer for the associated content.
93 /// \param Diag Object through which diagnostics will be emitted if the
94 /// buffer cannot be retrieved.
96 /// \param Loc If specified, is the location that invalid file diagnostics
97 /// will be emitted at.
99 /// \param Invalid If non-NULL, will be set \c true if an error occurred.
100 const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
101 const SourceManager &SM,
102 SourceLocation Loc = SourceLocation(),
103 bool *Invalid = 0) const;
105 /// getSize - Returns the size of the content encapsulated by this
106 /// ContentCache. This can be the size of the source file or the size of an
107 /// arbitrary scratch buffer. If the ContentCache encapsulates a source
108 /// file this size is retrieved from the file's FileEntry.
109 unsigned getSize() const;
111 /// getSizeBytesMapped - Returns the number of bytes actually mapped for
112 /// this ContentCache. This can be 0 if the MemBuffer was not actually
114 unsigned getSizeBytesMapped() const;
116 /// Returns the kind of memory used to back the memory buffer for
117 /// this content cache. This is used for performance analysis.
118 llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
120 void setBuffer(const llvm::MemoryBuffer *B) {
121 assert(!Buffer.getPointer() && "MemoryBuffer already set.");
122 Buffer.setPointer(B);
123 Buffer.setInt(false);
126 /// \brief Get the underlying buffer, returning NULL if the buffer is not
128 const llvm::MemoryBuffer *getRawBuffer() const {
129 return Buffer.getPointer();
132 /// \brief Replace the existing buffer (which will be deleted)
133 /// with the given buffer.
134 void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
136 /// \brief Determine whether the buffer itself is invalid.
137 bool isBufferInvalid() const {
138 return Buffer.getInt() & InvalidFlag;
141 /// \brief Determine whether the buffer should be freed.
142 bool shouldFreeBuffer() const {
143 return (Buffer.getInt() & DoNotFreeFlag) == 0;
146 ContentCache(const FileEntry *Ent = 0)
147 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
148 SourceLineCache(0), NumLines(0) {}
150 ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
151 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
152 SourceLineCache(0), NumLines(0) {}
156 /// The copy ctor does not allow copies where source object has either
157 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
158 /// is not transferred, so this is a logical error.
159 ContentCache(const ContentCache &RHS)
160 : Buffer(0, false), SourceLineCache(0)
162 OrigEntry = RHS.OrigEntry;
163 ContentsEntry = RHS.ContentsEntry;
165 assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
166 && "Passed ContentCache object cannot own a buffer.");
168 NumLines = RHS.NumLines;
172 // Disable assignments.
173 ContentCache &operator=(const ContentCache& RHS);
176 /// FileInfo - Information about a FileID, basically just the logical file
177 /// that it represents and include stack information.
179 /// Each FileInfo has include stack information, indicating where it came
180 /// from. This information encodes the #include chain that a token was
181 /// instantiated from. The main include file has an invalid IncludeLoc.
183 /// FileInfos contain a "ContentCache *", with the contents of the file.
186 /// IncludeLoc - The location of the #include that brought in this file.
187 /// This is an invalid SLOC for the main file (top of the #include chain).
188 unsigned IncludeLoc; // Really a SourceLocation
190 /// Data - This contains the ContentCache* and the bits indicating the
191 /// characteristic of the file and whether it has #line info, all bitmangled
195 /// get - Return a FileInfo object.
196 static FileInfo get(SourceLocation IL, const ContentCache *Con,
197 CharacteristicKind FileCharacter) {
199 X.IncludeLoc = IL.getRawEncoding();
200 X.Data = (uintptr_t)Con;
201 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
202 assert((unsigned)FileCharacter < 4 && "invalid file character");
203 X.Data |= (unsigned)FileCharacter;
207 SourceLocation getIncludeLoc() const {
208 return SourceLocation::getFromRawEncoding(IncludeLoc);
210 const ContentCache* getContentCache() const {
211 return reinterpret_cast<const ContentCache*>(Data & ~7UL);
214 /// getCharacteristic - Return whether this is a system header or not.
215 CharacteristicKind getFileCharacteristic() const {
216 return (CharacteristicKind)(Data & 3);
219 /// hasLineDirectives - Return true if this FileID has #line directives in
221 bool hasLineDirectives() const { return (Data & 4) != 0; }
223 /// setHasLineDirectives - Set the flag that indicates that this FileID has
224 /// line table entries associated with it.
225 void setHasLineDirectives() {
230 /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
231 /// location - where the token was ultimately instantiated, and the
232 /// SpellingLoc - where the actual character data for the token came from.
233 class InstantiationInfo {
234 // Really these are all SourceLocations.
236 /// SpellingLoc - Where the spelling for the token can be found.
237 unsigned SpellingLoc;
239 /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
240 /// indicate the start and end of the instantiation. In object-like macros,
241 /// these will be the same. In a function-like macro instantiation, the
242 /// start will be the identifier and the end will be the ')'. Finally, in
243 /// macro-argument instantitions, the end will be 'SourceLocation()', an
244 /// invalid location.
245 unsigned InstantiationLocStart, InstantiationLocEnd;
248 SourceLocation getSpellingLoc() const {
249 return SourceLocation::getFromRawEncoding(SpellingLoc);
251 SourceLocation getInstantiationLocStart() const {
252 return SourceLocation::getFromRawEncoding(InstantiationLocStart);
254 SourceLocation getInstantiationLocEnd() const {
255 SourceLocation EndLoc =
256 SourceLocation::getFromRawEncoding(InstantiationLocEnd);
257 return EndLoc.isInvalid() ? getInstantiationLocStart() : EndLoc;
260 std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
261 return std::make_pair(getInstantiationLocStart(),
262 getInstantiationLocEnd());
265 bool isMacroArgInstantiation() const {
266 // Note that this needs to return false for default constructed objects.
267 return getInstantiationLocStart().isValid() &&
268 SourceLocation::getFromRawEncoding(InstantiationLocEnd).isInvalid();
271 /// create - Return a InstantiationInfo for an expansion. ILStart and
272 /// ILEnd specify the instantiation range (where the macro is expanded),
273 /// and SL specifies the spelling location (where the characters from the
274 /// token come from). All three can refer to normal File SLocs or
275 /// instantiation locations.
276 static InstantiationInfo create(SourceLocation SL,
277 SourceLocation ILStart,
278 SourceLocation ILEnd) {
280 X.SpellingLoc = SL.getRawEncoding();
281 X.InstantiationLocStart = ILStart.getRawEncoding();
282 X.InstantiationLocEnd = ILEnd.getRawEncoding();
286 /// createForMacroArg - Return a special InstantiationInfo for the
287 /// expansion of a macro argument into a function-like macro's body. IL
288 /// specifies the instantiation location (where the macro is expanded).
289 /// This doesn't need to be a range because a macro is always instantiated
290 /// at a macro parameter reference, and macro parameters are always exactly
291 /// one token. SL specifies the spelling location (where the characters
292 /// from the token come from). IL and SL can both refer to normal File
293 /// SLocs or instantiation locations.
297 /// #define F(x) f(x)
301 /// When expanding '\c F(42)', the '\c x' would call this with an SL
302 /// pointing at '\c 42' anad an IL pointing at its location in the
303 /// definition of '\c F'.
304 static InstantiationInfo createForMacroArg(SourceLocation SL,
306 // We store an intentionally invalid source location for the end of the
307 // instantiation range to mark that this is a macro argument instantation
308 // rather than a normal one.
309 return create(SL, IL, SourceLocation());
313 /// SLocEntry - This is a discriminated union of FileInfo and
314 /// InstantiationInfo. SourceManager keeps an array of these objects, and
315 /// they are uniquely identified by the FileID datatype.
317 unsigned Offset; // low bit is set for instantiation info.
320 InstantiationInfo Instantiation;
323 unsigned getOffset() const { return Offset >> 1; }
325 bool isInstantiation() const { return Offset & 1; }
326 bool isFile() const { return !isInstantiation(); }
328 const FileInfo &getFile() const {
329 assert(isFile() && "Not a file SLocEntry!");
333 const InstantiationInfo &getInstantiation() const {
334 assert(isInstantiation() && "Not an instantiation SLocEntry!");
335 return Instantiation;
338 static SLocEntry get(unsigned Offset, const FileInfo &FI) {
340 E.Offset = Offset << 1;
345 static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
347 E.Offset = (Offset << 1) | 1;
348 E.Instantiation = II;
352 } // end SrcMgr namespace.
354 /// \brief External source of source location entries.
355 class ExternalSLocEntrySource {
357 virtual ~ExternalSLocEntrySource();
359 /// \brief Read the source location entry with index ID.
361 /// \returns true if an error occurred that prevented the source-location
362 /// entry from being loaded.
363 virtual bool ReadSLocEntry(unsigned ID) = 0;
367 /// IsBeforeInTranslationUnitCache - This class holds the cache used by
368 /// isBeforeInTranslationUnit. The cache structure is complex enough to be
369 /// worth breaking out of SourceManager.
370 class IsBeforeInTranslationUnitCache {
371 /// L/R QueryFID - These are the FID's of the cached query. If these match up
372 /// with a subsequent query, the result can be reused.
373 FileID LQueryFID, RQueryFID;
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;
399 return LOffset < ROffset;
402 // Set up a new query.
403 void setQueryFIDs(FileID LHS, FileID RHS) {
408 void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
409 unsigned rCommonOffset) {
410 CommonFID = commonFID;
411 LCommonOffset = lCommonOffset;
412 RCommonOffset = rCommonOffset;
417 /// SourceManager - This file handles loading and caching of source files into
418 /// memory. This object owns the MemoryBuffer objects for all of the loaded
419 /// files and assigns unique FileID's for each unique #include chain.
421 /// The SourceManager can be queried for information about SourceLocation
422 /// objects, turning them into either spelling or instantiation locations.
423 /// Spelling locations represent where the bytes corresponding to a token came
424 /// from and instantiation locations represent where the location is in the
425 /// user's view. In the case of a macro expansion, for example, the spelling
426 /// location indicates where the expanded token came from and the instantiation
427 /// location specifies where it was expanded.
428 class SourceManager : public llvm::RefCountedBase<SourceManager> {
429 /// \brief Diagnostic object.
432 FileManager &FileMgr;
434 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
436 /// FileInfos - Memoized information about all of the files tracked by this
437 /// SourceManager. This set allows us to merge ContentCache entries based
438 /// on their FileEntry*. All ContentCache objects will thus have unique,
439 /// non-null, FileEntry pointers.
440 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
442 /// \brief True if the ContentCache for files that are overriden by other
443 /// files, should report the original file name. Defaults to true.
444 bool OverridenFilesKeepOriginalName;
446 /// \brief Files that have been overriden with the contents from another file.
447 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
449 /// MemBufferInfos - Information about various memory buffers that we have
450 /// read in. All FileEntry* within the stored ContentCache objects are NULL,
451 /// as they do not refer to a file.
452 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
454 /// SLocEntryTable - This is an array of SLocEntry's that we have created.
455 /// FileID is an index into this vector. This array is sorted by the offset.
456 std::vector<SrcMgr::SLocEntry> SLocEntryTable;
457 /// NextOffset - This is the next available offset that a new SLocEntry can
458 /// start at. It is SLocEntryTable.back().getOffset()+size of back() entry.
461 /// \brief If source location entries are being lazily loaded from
462 /// an external source, this vector indicates whether the Ith source
463 /// location entry has already been loaded from the external storage.
464 std::vector<bool> SLocEntryLoaded;
466 /// \brief An external source for source location entries.
467 ExternalSLocEntrySource *ExternalSLocEntries;
469 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
470 /// LastFileIDLookup records the last FileID looked up or created, because it
471 /// is very common to look up many tokens from the same file.
472 mutable FileID LastFileIDLookup;
474 /// LineTable - This holds information for #line directives. It is referenced
475 /// by indices from SLocEntryTable.
476 LineTableInfo *LineTable;
478 /// LastLineNo - These ivars serve as a cache used in the getLineNumber
479 /// method which is used to speedup getLineNumber calls to nearby locations.
480 mutable FileID LastLineNoFileIDQuery;
481 mutable SrcMgr::ContentCache *LastLineNoContentCache;
482 mutable unsigned LastLineNoFilePos;
483 mutable unsigned LastLineNoResult;
485 /// MainFileID - The file ID for the main source file of the translation unit.
488 // Statistics for -print-stats.
489 mutable unsigned NumLinearScans, NumBinaryProbes;
491 // Cache results for the isBeforeInTranslationUnit method.
492 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
494 // Cache for the "fake" buffer used for error-recovery purposes.
495 mutable llvm::MemoryBuffer *FakeBufferForRecovery;
497 // SourceManager doesn't support copy construction.
498 explicit SourceManager(const SourceManager&);
499 void operator=(const SourceManager&);
501 SourceManager(Diagnostic &Diag, FileManager &FileMgr);
504 void clearIDTables();
506 Diagnostic &getDiagnostics() const { return Diag; }
508 FileManager &getFileManager() const { return FileMgr; }
510 /// \brief Set true if the SourceManager should report the original file name
511 /// for contents of files that were overriden by other files.Defaults to true.
512 void setOverridenFilesKeepOriginalName(bool value) {
513 OverridenFilesKeepOriginalName = value;
516 //===--------------------------------------------------------------------===//
517 // MainFileID creation and querying methods.
518 //===--------------------------------------------------------------------===//
520 /// getMainFileID - Returns the FileID of the main source file.
521 FileID getMainFileID() const { return MainFileID; }
523 /// createMainFileID - Create the FileID for the main source file.
524 FileID createMainFileID(const FileEntry *SourceFile) {
525 assert(MainFileID.isInvalid() && "MainFileID already set!");
526 MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
530 /// \brief Set the file ID for the precompiled preamble, which is also the
532 void SetPreambleFileID(FileID Preamble) {
533 assert(MainFileID.isInvalid() && "MainFileID already set!");
534 MainFileID = Preamble;
537 //===--------------------------------------------------------------------===//
538 // Methods to create new FileID's and instantiations.
539 //===--------------------------------------------------------------------===//
541 /// createFileID - Create a new FileID that represents the specified file
542 /// being #included from the specified IncludePosition. This translates NULL
543 /// into standard input.
544 /// PreallocateID should be non-zero to specify which pre-allocated,
545 /// lazily computed source location is being filled in by this operation.
546 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
547 SrcMgr::CharacteristicKind FileCharacter,
548 unsigned PreallocatedID = 0,
549 unsigned Offset = 0) {
550 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
551 assert(IR && "getOrCreateContentCache() cannot return NULL");
552 return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset);
555 /// createFileIDForMemBuffer - Create a new FileID that represents the
556 /// specified memory buffer. This does no caching of the buffer and takes
557 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
558 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
559 unsigned PreallocatedID = 0,
560 unsigned Offset = 0) {
561 return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
562 SrcMgr::C_User, PreallocatedID, Offset);
565 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
566 /// that will represent the FileID for the main source. One example
567 /// of when this would be used is when the main source is read from STDIN.
568 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
569 assert(MainFileID.isInvalid() && "MainFileID already set!");
570 MainFileID = createFileIDForMemBuffer(Buffer);
574 /// createMacroArgInstantiationLoc - Return a new SourceLocation that encodes
575 /// the fact that a token from SpellingLoc should actually be referenced from
576 /// InstantiationLoc, and that it represents the instantiation of a macro
577 /// argument into the function-like macro body.
578 SourceLocation createMacroArgInstantiationLoc(SourceLocation Loc,
579 SourceLocation InstantiationLoc,
582 /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
583 /// that a token from SpellingLoc should actually be referenced from
584 /// InstantiationLoc.
585 SourceLocation createInstantiationLoc(SourceLocation Loc,
586 SourceLocation InstantiationLocStart,
587 SourceLocation InstantiationLocEnd,
589 unsigned PreallocatedID = 0,
590 unsigned Offset = 0);
592 /// \brief Retrieve the memory buffer associated with the given file.
594 /// \param Invalid If non-NULL, will be set \c true if an error
595 /// occurs while retrieving the memory buffer.
596 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
599 /// \brief Override the contents of the given source file by providing an
600 /// already-allocated buffer.
602 /// \param SourceFile the source file whose contents will be overriden.
604 /// \param Buffer the memory buffer whose contents will be used as the
605 /// data in the given source file.
607 /// \param DoNotFree If true, then the buffer will not be freed when the
608 /// source manager is destroyed.
609 void overrideFileContents(const FileEntry *SourceFile,
610 const llvm::MemoryBuffer *Buffer,
611 bool DoNotFree = false);
613 /// \brief Override the the given source file with another one.
615 /// \param SourceFile the source file which will be overriden.
617 /// \param NewFile the file whose contents will be used as the
618 /// data instead of the contents of the given source file.
619 void overrideFileContents(const FileEntry *SourceFile,
620 const FileEntry *NewFile);
622 //===--------------------------------------------------------------------===//
623 // FileID manipulation methods.
624 //===--------------------------------------------------------------------===//
626 /// getBuffer - Return the buffer for the specified FileID. If there is an
627 /// error opening this buffer the first time, this manufactures a temporary
628 /// buffer and returns a non-empty error string.
629 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
630 bool *Invalid = 0) const {
631 bool MyInvalid = false;
632 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
633 if (MyInvalid || !Entry.isFile()) {
637 return getFakeBufferForRecovery();
640 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
644 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
645 bool MyInvalid = false;
646 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
647 if (MyInvalid || !Entry.isFile()) {
651 return getFakeBufferForRecovery();
654 return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
659 /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
660 const FileEntry *getFileEntryForID(FileID FID) const {
661 bool MyInvalid = false;
662 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
663 if (MyInvalid || !Entry.isFile())
666 return Entry.getFile().getContentCache()->OrigEntry;
669 /// Returns the FileEntry record for the provided SLocEntry.
670 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
672 return sloc.getFile().getContentCache()->OrigEntry;
675 /// getBufferData - Return a StringRef to the source buffer data for the
676 /// specified FileID.
678 /// \param FID The file ID whose contents will be returned.
679 /// \param Invalid If non-NULL, will be set true if an error occurred.
680 llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
683 //===--------------------------------------------------------------------===//
684 // SourceLocation manipulation methods.
685 //===--------------------------------------------------------------------===//
687 /// getFileID - Return the FileID for a SourceLocation. This is a very
688 /// hot method that is used for all SourceManager queries that start with a
689 /// SourceLocation object. It is responsible for finding the entry in
690 /// SLocEntryTable which contains the specified location.
692 FileID getFileID(SourceLocation SpellingLoc) const {
693 unsigned SLocOffset = SpellingLoc.getOffset();
695 // If our one-entry cache covers this offset, just return it.
696 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
697 return LastFileIDLookup;
699 return getFileIDSlow(SLocOffset);
702 /// getLocForStartOfFile - Return the source location corresponding to the
703 /// first byte of the specified file.
704 SourceLocation getLocForStartOfFile(FileID FID) const {
705 assert(FID.ID < SLocEntryTable.size() && "FileID out of range");
706 bool Invalid = false;
707 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
708 if (Invalid || !Entry.isFile())
709 return SourceLocation();
711 unsigned FileOffset = Entry.getOffset();
712 return SourceLocation::getFileLoc(FileOffset);
715 /// getInstantiationLoc - Given a SourceLocation object, return the
716 /// instantiation location referenced by the ID.
717 SourceLocation getInstantiationLoc(SourceLocation Loc) const {
718 // Handle the non-mapped case inline, defer to out of line code to handle
720 if (Loc.isFileID()) return Loc;
721 return getInstantiationLocSlowCase(Loc);
724 /// getImmediateInstantiationRange - Loc is required to be an instantiation
725 /// location. Return the start/end of the instantiation information.
726 std::pair<SourceLocation,SourceLocation>
727 getImmediateInstantiationRange(SourceLocation Loc) const;
729 /// getInstantiationRange - Given a SourceLocation object, return the
730 /// range of tokens covered by the instantiation in the ultimate file.
731 std::pair<SourceLocation,SourceLocation>
732 getInstantiationRange(SourceLocation Loc) const;
735 /// getSpellingLoc - Given a SourceLocation object, return the spelling
736 /// location referenced by the ID. This is the place where the characters
737 /// that make up the lexed token can be found.
738 SourceLocation getSpellingLoc(SourceLocation Loc) const {
739 // Handle the non-mapped case inline, defer to out of line code to handle
741 if (Loc.isFileID()) return Loc;
742 return getSpellingLocSlowCase(Loc);
745 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
746 /// spelling location referenced by the ID. This is the first level down
747 /// towards the place where the characters that make up the lexed token can be
748 /// found. This should not generally be used by clients.
749 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
751 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
752 /// Offset pair. The first element is the FileID, the second is the
753 /// offset from the start of the buffer of the location.
754 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
755 FileID FID = getFileID(Loc);
756 return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
759 /// getDecomposedInstantiationLoc - Decompose the specified location into a
760 /// raw FileID + Offset pair. If the location is an instantiation record,
761 /// walk through it until we find the final location instantiated.
762 std::pair<FileID, unsigned>
763 getDecomposedInstantiationLoc(SourceLocation Loc) const {
764 FileID FID = getFileID(Loc);
765 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
767 unsigned Offset = Loc.getOffset()-E->getOffset();
769 return std::make_pair(FID, Offset);
771 return getDecomposedInstantiationLocSlowCase(E);
774 /// getDecomposedSpellingLoc - Decompose the specified location into a raw
775 /// FileID + Offset pair. If the location is an instantiation record, walk
776 /// through it until we find its spelling record.
777 std::pair<FileID, unsigned>
778 getDecomposedSpellingLoc(SourceLocation Loc) const {
779 FileID FID = getFileID(Loc);
780 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
782 unsigned Offset = Loc.getOffset()-E->getOffset();
784 return std::make_pair(FID, Offset);
785 return getDecomposedSpellingLocSlowCase(E, Offset);
788 /// getFileOffset - This method returns the offset from the start
789 /// of the file that the specified SourceLocation represents. This is not very
790 /// meaningful for a macro ID.
791 unsigned getFileOffset(SourceLocation SpellingLoc) const {
792 return getDecomposedLoc(SpellingLoc).second;
795 /// isMacroArgInstantiation - This method tests whether the given source
796 /// location represents a macro argument's instantiation into the
797 /// function-like macro definition. Such source locations only appear inside
798 /// of the instantiation locations representing where a particular
799 /// function-like macro was expanded.
800 bool isMacroArgInstantiation(SourceLocation Loc) const;
802 //===--------------------------------------------------------------------===//
803 // Queries about the code at a SourceLocation.
804 //===--------------------------------------------------------------------===//
806 /// getCharacterData - Return a pointer to the start of the specified location
807 /// in the appropriate spelling MemoryBuffer.
809 /// \param Invalid If non-NULL, will be set \c true if an error occurs.
810 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
812 /// getColumnNumber - Return the column # for the specified file position.
813 /// This is significantly cheaper to compute than the line number. This
814 /// returns zero if the column number isn't known. This may only be called on
815 /// a file sloc, so you must choose a spelling or instantiation location
816 /// before calling this method.
817 unsigned getColumnNumber(FileID FID, unsigned FilePos,
818 bool *Invalid = 0) const;
819 unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
820 unsigned getInstantiationColumnNumber(SourceLocation Loc,
821 bool *Invalid = 0) const;
822 unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
825 /// getLineNumber - Given a SourceLocation, return the spelling line number
826 /// for the position indicated. This requires building and caching a table of
827 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
828 /// about to emit a diagnostic.
829 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
830 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
831 unsigned getInstantiationLineNumber(SourceLocation Loc,
832 bool *Invalid = 0) const;
833 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
835 /// Return the filename or buffer identifier of the buffer the location is in.
836 /// Note that this name does not respect #line directives. Use getPresumedLoc
837 /// for normal clients.
838 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
840 /// getFileCharacteristic - return the file characteristic of the specified
841 /// source location, indicating whether this is a normal file, a system
842 /// header, or an "implicit extern C" system header.
844 /// This state can be modified with flags on GNU linemarker directives like:
846 /// which changes all source locations in the current file after that to be
847 /// considered to be from a system header.
848 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
850 /// getPresumedLoc - This method returns the "presumed" location of a
851 /// SourceLocation specifies. A "presumed location" can be modified by #line
852 /// or GNU line marker directives. This provides a view on the data that a
853 /// user should see in diagnostics, for example.
855 /// Note that a presumed location is always given as the instantiation point
856 /// of an instantiation location, not at the spelling location.
858 /// \returns The presumed location of the specified SourceLocation. If the
859 /// presumed location cannot be calculate (e.g., because \p Loc is invalid
860 /// or the file containing \p Loc has changed on disk), returns an invalid
861 /// presumed location.
862 PresumedLoc getPresumedLoc(SourceLocation Loc) const;
864 /// isFromSameFile - Returns true if both SourceLocations correspond to
866 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
867 return getFileID(Loc1) == getFileID(Loc2);
870 /// isFromMainFile - Returns true if the file of provided SourceLocation is
872 bool isFromMainFile(SourceLocation Loc) const {
873 return getFileID(Loc) == getMainFileID();
876 /// isInSystemHeader - Returns if a SourceLocation is in a system header.
877 bool isInSystemHeader(SourceLocation Loc) const {
878 return getFileCharacteristic(Loc) != SrcMgr::C_User;
881 /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
883 bool isInExternCSystemHeader(SourceLocation Loc) const {
884 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
887 /// \brief Given a specific chunk of a FileID (FileID with offset+length),
888 /// returns true if \arg Loc is inside that chunk and sets relative offset
889 /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset.
890 bool isInFileID(SourceLocation Loc,
891 FileID FID, unsigned offset, unsigned length,
892 unsigned *relativeOffset = 0) const {
893 assert(!FID.isInvalid());
897 unsigned start = getSLocEntry(FID).getOffset() + offset;
898 unsigned end = start + length;
901 // Make sure offset/length describe a chunk inside the given FileID.
903 if (FID.ID+1 == SLocEntryTable.size())
904 NextOffset = getNextOffset();
906 NextOffset = getSLocEntry(FID.ID+1).getOffset();
907 assert(start < NextOffset);
908 assert(end < NextOffset);
911 if (Loc.getOffset() >= start && Loc.getOffset() < end) {
913 *relativeOffset = Loc.getOffset() - start;
920 //===--------------------------------------------------------------------===//
921 // Line Table Manipulation Routines
922 //===--------------------------------------------------------------------===//
924 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
926 unsigned getLineTableFilenameID(llvm::StringRef Str);
928 /// AddLineNote - Add a line note to the line table for the FileID and offset
929 /// specified by Loc. If FilenameID is -1, it is considered to be
931 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
932 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
933 bool IsFileEntry, bool IsFileExit,
934 bool IsSystemHeader, bool IsExternCHeader);
936 /// \brief Determine if the source manager has a line table.
937 bool hasLineTable() const { return LineTable != 0; }
939 /// \brief Retrieve the stored line table.
940 LineTableInfo &getLineTable();
942 //===--------------------------------------------------------------------===//
943 // Queries for performance analysis.
944 //===--------------------------------------------------------------------===//
946 /// Return the total amount of physical memory allocated by the
947 /// ContentCache allocator.
948 size_t getContentCacheSize() const {
949 return ContentCacheAlloc.getTotalMemory();
952 struct MemoryBufferSizes {
953 const size_t malloc_bytes;
954 const size_t mmap_bytes;
956 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
957 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
960 /// Return the amount of memory used by memory buffers, breaking down
961 /// by heap-backed versus mmap'ed memory.
962 MemoryBufferSizes getMemoryBufferSizes() const;
964 //===--------------------------------------------------------------------===//
965 // Other miscellaneous methods.
966 //===--------------------------------------------------------------------===//
968 /// \brief Get the source location for the given file:line:col triplet.
970 /// If the source file is included multiple times, the source location will
971 /// be based upon the first inclusion.
972 SourceLocation getLocation(const FileEntry *SourceFile,
973 unsigned Line, unsigned Col);
975 /// \brief Determines the order of 2 source locations in the translation unit.
977 /// \returns true if LHS source location comes before RHS, false otherwise.
978 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
980 /// \brief Determines the order of 2 source locations in the "source location
982 static bool isBeforeInSourceLocationOffset(SourceLocation LHS,
983 SourceLocation RHS) {
984 return isBeforeInSourceLocationOffset(LHS, RHS.getOffset());
987 /// \brief Determines the order of a source location and a source location
988 /// offset in the "source location address space".
989 static bool isBeforeInSourceLocationOffset(SourceLocation LHS, unsigned RHS) {
990 return LHS.getOffset() < RHS;
993 // Iterators over FileInfos.
994 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
995 ::const_iterator fileinfo_iterator;
996 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
997 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
998 bool hasFileInfo(const FileEntry *File) const {
999 return FileInfos.find(File) != FileInfos.end();
1002 /// PrintStats - Print statistics to stderr.
1004 void PrintStats() const;
1006 unsigned sloc_entry_size() const { return SLocEntryTable.size(); }
1008 // FIXME: Exposing this is a little gross; what we want is a good way
1009 // to iterate the entries that were not defined in an AST file (or
1010 // any other external source).
1011 unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); }
1013 const SrcMgr::SLocEntry &getSLocEntry(unsigned ID, bool *Invalid = 0) const {
1014 assert(ID < SLocEntryTable.size() && "Invalid id");
1015 // If we haven't loaded this source-location entry from the external source
1017 if (ExternalSLocEntries &&
1018 ID < SLocEntryLoaded.size() &&
1019 !SLocEntryLoaded[ID] &&
1020 ExternalSLocEntries->ReadSLocEntry(ID) &&
1024 return SLocEntryTable[ID];
1027 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1028 return getSLocEntry(FID.ID, Invalid);
1031 unsigned getNextOffset() const { return NextOffset; }
1033 /// \brief Preallocate some number of source location entries, which
1034 /// will be loaded as needed from the given external source.
1035 void PreallocateSLocEntries(ExternalSLocEntrySource *Source,
1036 unsigned NumSLocEntries,
1037 unsigned NextOffset);
1039 /// \brief Clear out any preallocated source location entries that
1040 /// haven't already been loaded.
1041 void ClearPreallocatedSLocEntries();
1044 const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1046 /// createInstantiationLoc - Implements the common elements of storing an
1047 /// instantiation info struct into the SLocEntry table and producing a source
1048 /// location that refers to it.
1049 SourceLocation createInstantiationLocImpl(const SrcMgr::InstantiationInfo &II,
1051 unsigned PreallocatedID = 0,
1052 unsigned Offset = 0);
1054 /// isOffsetInFileID - Return true if the specified FileID contains the
1055 /// specified SourceLocation offset. This is a very hot method.
1056 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1057 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1058 // If the entry is after the offset, it can't contain it.
1059 if (SLocOffset < Entry.getOffset()) return false;
1061 // If this is the last entry than it does. Otherwise, the entry after it
1062 // has to not include it.
1063 if (FID.ID+1 == SLocEntryTable.size()) return true;
1065 return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1068 /// createFileID - Create a new fileID for the specified ContentCache and
1069 /// include position. This works regardless of whether the ContentCache
1070 /// corresponds to a file or some other input source.
1071 FileID createFileID(const SrcMgr::ContentCache* File,
1072 SourceLocation IncludePos,
1073 SrcMgr::CharacteristicKind DirCharacter,
1074 unsigned PreallocatedID = 0,
1075 unsigned Offset = 0);
1077 const SrcMgr::ContentCache *
1078 getOrCreateContentCache(const FileEntry *SourceFile);
1080 /// createMemBufferContentCache - Create a new ContentCache for the specified
1082 const SrcMgr::ContentCache*
1083 createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
1085 FileID getFileIDSlow(unsigned SLocOffset) const;
1087 SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
1088 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1090 std::pair<FileID, unsigned>
1091 getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E) const;
1092 std::pair<FileID, unsigned>
1093 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1094 unsigned Offset) const;
1098 } // end namespace clang