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 //===----------------------------------------------------------------------===//
11 /// \brief Defines the SourceManager interface.
13 /// There are three different types of locations in a file: a spelling
14 /// location, an expansion location, and a presumed location.
16 /// Given an example of:
18 /// #define min(x, y) x < y ? x : y
21 /// and then later on a use of min:
27 /// The expansion location is the line in the source code where the macro
28 /// was expanded (the return statement), the spelling location is the
29 /// location in the source where the macro was originally defined,
30 /// and the presumed location is where the line directive states that
31 /// the line is 17, or any other line.
33 //===----------------------------------------------------------------------===//
35 #ifndef LLVM_CLANG_SOURCEMANAGER_H
36 #define LLVM_CLANG_SOURCEMANAGER_H
38 #include "clang/Basic/LLVM.h"
39 #include "clang/Basic/FileManager.h"
40 #include "clang/Basic/SourceLocation.h"
41 #include "llvm/Support/Allocator.h"
42 #include "llvm/Support/DataTypes.h"
43 #include "llvm/ADT/ArrayRef.h"
44 #include "llvm/ADT/PointerIntPair.h"
45 #include "llvm/ADT/PointerUnion.h"
46 #include "llvm/ADT/IntrusiveRefCntPtr.h"
47 #include "llvm/ADT/OwningPtr.h"
48 #include "llvm/ADT/DenseMap.h"
49 #include "llvm/ADT/DenseSet.h"
50 #include "llvm/Support/MemoryBuffer.h"
57 class DiagnosticsEngine;
66 /// \brief Public enums and private classes that are part of the
67 /// SourceManager implementation.
70 /// \brief Indicates whether a file or directory holds normal user code,
71 /// system code, or system code which is implicitly 'extern "C"' in C++ mode.
73 /// Entire directories can be tagged with this (this is maintained by
74 /// DirectoryLookup and friends) as can specific FileInfos when a \#pragma
75 /// system_header is seen or in various other cases.
77 enum CharacteristicKind {
78 C_User, C_System, C_ExternCSystem
81 /// \brief One instance of this struct is kept for every file loaded or used.
83 /// This object owns the MemoryBuffer object.
86 /// \brief Whether the buffer is invalid.
88 /// \brief Whether the buffer should not be freed on destruction.
92 /// \brief The actual buffer containing the characters from the input
95 /// This is owned by the ContentCache object. The bits indicate
96 /// whether the buffer is invalid.
97 mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
100 /// \brief Reference to the file entry representing this ContentCache.
102 /// This reference does not own the FileEntry object.
104 /// It is possible for this to be NULL if the ContentCache encapsulates
105 /// an imaginary text buffer.
106 const FileEntry *OrigEntry;
108 /// \brief References the file which the contents were actually loaded from.
110 /// Can be different from 'Entry' if we overridden the contents of one file
111 /// with the contents of another file.
112 const FileEntry *ContentsEntry;
114 /// \brief A bump pointer allocated array of offsets for each source line.
116 /// This is lazily computed. This is owned by the SourceManager
117 /// BumpPointerAllocator object.
118 unsigned *SourceLineCache;
120 /// \brief The number of lines in this ContentCache.
122 /// This is only valid if SourceLineCache is non-null.
123 unsigned NumLines : 31;
125 /// \brief Indicates whether the buffer itself was provided to override
126 /// the actual file contents.
128 /// When true, the original entry may be a virtual file that does not
130 unsigned BufferOverridden : 1;
132 /// \brief True if this content cache was initially created for a source
133 /// file considered as a system one.
134 unsigned IsSystemFile : 1;
136 ContentCache(const FileEntry *Ent = 0)
137 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
138 SourceLineCache(0), NumLines(0), BufferOverridden(false),
139 IsSystemFile(false) {}
141 ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
142 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
143 SourceLineCache(0), NumLines(0), BufferOverridden(false),
144 IsSystemFile(false) {}
148 /// The copy ctor does not allow copies where source object has either
149 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
150 /// is not transferred, so this is a logical error.
151 ContentCache(const ContentCache &RHS)
152 : Buffer(0, false), SourceLineCache(0), BufferOverridden(false),
155 OrigEntry = RHS.OrigEntry;
156 ContentsEntry = RHS.ContentsEntry;
158 assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 &&
159 "Passed ContentCache object cannot own a buffer.");
161 NumLines = RHS.NumLines;
164 /// \brief Returns the memory buffer for the associated content.
166 /// \param Diag Object through which diagnostics will be emitted if the
167 /// buffer cannot be retrieved.
169 /// \param Loc If specified, is the location that invalid file diagnostics
170 /// will be emitted at.
172 /// \param Invalid If non-NULL, will be set \c true if an error occurred.
173 const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag,
174 const SourceManager &SM,
175 SourceLocation Loc = SourceLocation(),
176 bool *Invalid = 0) const;
178 /// \brief Returns the size of the content encapsulated by this
181 /// This can be the size of the source file or the size of an
182 /// arbitrary scratch buffer. If the ContentCache encapsulates a source
183 /// file this size is retrieved from the file's FileEntry.
184 unsigned getSize() const;
186 /// \brief Returns the number of bytes actually mapped for this
189 /// This can be 0 if the MemBuffer was not actually expanded.
190 unsigned getSizeBytesMapped() const;
192 /// Returns the kind of memory used to back the memory buffer for
193 /// this content cache. This is used for performance analysis.
194 llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
196 void setBuffer(const llvm::MemoryBuffer *B) {
197 assert(!Buffer.getPointer() && "MemoryBuffer already set.");
198 Buffer.setPointer(B);
199 Buffer.setInt(false);
202 /// \brief Get the underlying buffer, returning NULL if the buffer is not
204 const llvm::MemoryBuffer *getRawBuffer() const {
205 return Buffer.getPointer();
208 /// \brief Replace the existing buffer (which will be deleted)
209 /// with the given buffer.
210 void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
212 /// \brief Determine whether the buffer itself is invalid.
213 bool isBufferInvalid() const {
214 return Buffer.getInt() & InvalidFlag;
217 /// \brief Determine whether the buffer should be freed.
218 bool shouldFreeBuffer() const {
219 return (Buffer.getInt() & DoNotFreeFlag) == 0;
223 // Disable assignments.
224 ContentCache &operator=(const ContentCache& RHS) LLVM_DELETED_FUNCTION;
227 /// \brief Information about a FileID, basically just the logical file
228 /// that it represents and include stack information.
230 /// Each FileInfo has include stack information, indicating where it came
231 /// from. This information encodes the \#include chain that a token was
232 /// expanded from. The main include file has an invalid IncludeLoc.
234 /// FileInfos contain a "ContentCache *", with the contents of the file.
237 /// \brief The location of the \#include that brought in this file.
239 /// This is an invalid SLOC for the main file (top of the \#include chain).
240 unsigned IncludeLoc; // Really a SourceLocation
242 /// \brief Number of FileIDs (files and macros) that were created during
243 /// preprocessing of this \#include, including this SLocEntry.
245 /// Zero means the preprocessor didn't provide such info for this SLocEntry.
246 unsigned NumCreatedFIDs;
248 /// \brief Contains the ContentCache* and the bits indicating the
249 /// characteristic of the file and whether it has \#line info, all
250 /// bitmangled together.
253 friend class clang::SourceManager;
254 friend class clang::ASTWriter;
255 friend class clang::ASTReader;
257 /// \brief Return a FileInfo object.
258 static FileInfo get(SourceLocation IL, const ContentCache *Con,
259 CharacteristicKind FileCharacter) {
261 X.IncludeLoc = IL.getRawEncoding();
262 X.NumCreatedFIDs = 0;
263 X.Data = (uintptr_t)Con;
264 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
265 assert((unsigned)FileCharacter < 4 && "invalid file character");
266 X.Data |= (unsigned)FileCharacter;
270 SourceLocation getIncludeLoc() const {
271 return SourceLocation::getFromRawEncoding(IncludeLoc);
273 const ContentCache* getContentCache() const {
274 return reinterpret_cast<const ContentCache*>(Data & ~7UL);
277 /// \brief Return whether this is a system header or not.
278 CharacteristicKind getFileCharacteristic() const {
279 return (CharacteristicKind)(Data & 3);
282 /// \brief Return true if this FileID has \#line directives in it.
283 bool hasLineDirectives() const { return (Data & 4) != 0; }
285 /// \brief Set the flag that indicates that this FileID has
286 /// line table entries associated with it.
287 void setHasLineDirectives() {
292 /// \brief Each ExpansionInfo encodes the expansion location - where
293 /// the token was ultimately expanded, and the SpellingLoc - where the actual
294 /// character data for the token came from.
295 class ExpansionInfo {
296 // Really these are all SourceLocations.
298 /// \brief Where the spelling for the token can be found.
299 unsigned SpellingLoc;
301 /// In a macro expansion, ExpansionLocStart and ExpansionLocEnd
302 /// indicate the start and end of the expansion. In object-like macros,
303 /// they will be the same. In a function-like macro expansion, the start
304 /// will be the identifier and the end will be the ')'. Finally, in
305 /// macro-argument instantiations, the end will be 'SourceLocation()', an
306 /// invalid location.
307 unsigned ExpansionLocStart, ExpansionLocEnd;
310 SourceLocation getSpellingLoc() const {
311 return SourceLocation::getFromRawEncoding(SpellingLoc);
313 SourceLocation getExpansionLocStart() const {
314 return SourceLocation::getFromRawEncoding(ExpansionLocStart);
316 SourceLocation getExpansionLocEnd() const {
317 SourceLocation EndLoc =
318 SourceLocation::getFromRawEncoding(ExpansionLocEnd);
319 return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
322 std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
323 return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
326 bool isMacroArgExpansion() const {
327 // Note that this needs to return false for default constructed objects.
328 return getExpansionLocStart().isValid() &&
329 SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
332 bool isFunctionMacroExpansion() const {
333 return getExpansionLocStart().isValid() &&
334 getExpansionLocStart() != getExpansionLocEnd();
337 /// \brief Return a ExpansionInfo for an expansion.
339 /// Start and End specify the expansion range (where the macro is
340 /// expanded), and SpellingLoc specifies the spelling location (where
341 /// the characters from the token come from). All three can refer to
342 /// normal File SLocs or expansion locations.
343 static ExpansionInfo create(SourceLocation SpellingLoc,
344 SourceLocation Start, SourceLocation End) {
346 X.SpellingLoc = SpellingLoc.getRawEncoding();
347 X.ExpansionLocStart = Start.getRawEncoding();
348 X.ExpansionLocEnd = End.getRawEncoding();
352 /// \brief Return a special ExpansionInfo for the expansion of
353 /// a macro argument into a function-like macro's body.
355 /// ExpansionLoc specifies the expansion location (where the macro is
356 /// expanded). This doesn't need to be a range because a macro is always
357 /// expanded at a macro parameter reference, and macro parameters are
358 /// always exactly one token. SpellingLoc specifies the spelling location
359 /// (where the characters from the token come from). ExpansionLoc and
360 /// SpellingLoc can both refer to normal File SLocs or expansion locations.
364 /// #define F(x) f(x)
368 /// When expanding '\c F(42)', the '\c x' would call this with an
369 /// SpellingLoc pointing at '\c 42' and an ExpansionLoc pointing at its
370 /// location in the definition of '\c F'.
371 static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
372 SourceLocation ExpansionLoc) {
373 // We store an intentionally invalid source location for the end of the
374 // expansion range to mark that this is a macro argument ion rather than
376 return create(SpellingLoc, ExpansionLoc, SourceLocation());
380 /// \brief This is a discriminated union of FileInfo and ExpansionInfo.
382 /// SourceManager keeps an array of these objects, and they are uniquely
383 /// identified by the FileID datatype.
385 unsigned Offset; // low bit is set for expansion info.
388 ExpansionInfo Expansion;
391 unsigned getOffset() const { return Offset >> 1; }
393 bool isExpansion() const { return Offset & 1; }
394 bool isFile() const { return !isExpansion(); }
396 const FileInfo &getFile() const {
397 assert(isFile() && "Not a file SLocEntry!");
401 const ExpansionInfo &getExpansion() const {
402 assert(isExpansion() && "Not a macro expansion SLocEntry!");
406 static SLocEntry get(unsigned Offset, const FileInfo &FI) {
408 E.Offset = Offset << 1;
413 static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
415 E.Offset = (Offset << 1) | 1;
416 E.Expansion = Expansion;
420 } // end SrcMgr namespace.
422 /// \brief External source of source location entries.
423 class ExternalSLocEntrySource {
425 virtual ~ExternalSLocEntrySource();
427 /// \brief Read the source location entry with index ID, which will always be
430 /// \returns true if an error occurred that prevented the source-location
431 /// entry from being loaded.
432 virtual bool ReadSLocEntry(int ID) = 0;
434 /// \brief Retrieve the module import location and name for the given ID, if
435 /// in fact it was loaded from a module (rather than, say, a precompiled
437 virtual std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) = 0;
441 /// \brief Holds the cache used by isBeforeInTranslationUnit.
443 /// The cache structure is complex enough to be worth breaking out of
445 class IsBeforeInTranslationUnitCache {
446 /// \brief The FileID's of the cached query.
448 /// If these match up with a subsequent query, the result can be reused.
449 FileID LQueryFID, RQueryFID;
451 /// \brief True if LQueryFID was created before RQueryFID.
453 /// This is used to compare macro expansion locations.
454 bool IsLQFIDBeforeRQFID;
456 /// \brief The file found in common between the two \#include traces, i.e.,
457 /// the nearest common ancestor of the \#include tree.
460 /// \brief The offset of the previous query in CommonFID.
462 /// Usually, this represents the location of the \#include for QueryFID, but
463 /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a
464 /// random token in the parent.
465 unsigned LCommonOffset, RCommonOffset;
468 /// \brief Return true if the currently cached values match up with
469 /// the specified LHS/RHS query.
471 /// If not, we can't use the cache.
472 bool isCacheValid(FileID LHS, FileID RHS) const {
473 return LQueryFID == LHS && RQueryFID == RHS;
476 /// \brief If the cache is valid, compute the result given the
477 /// specified offsets in the LHS/RHS FileID's.
478 bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
479 // If one of the query files is the common file, use the offset. Otherwise,
480 // use the #include loc in the common file.
481 if (LQueryFID != CommonFID) LOffset = LCommonOffset;
482 if (RQueryFID != CommonFID) ROffset = RCommonOffset;
484 // It is common for multiple macro expansions to be "included" from the same
485 // location (expansion location), in which case use the order of the FileIDs
486 // to determine which came first. This will also take care the case where
487 // one of the locations points at the inclusion/expansion point of the other
488 // in which case its FileID will come before the other.
489 if (LOffset == ROffset)
490 return IsLQFIDBeforeRQFID;
492 return LOffset < ROffset;
495 /// \brief Set up a new query.
496 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
500 IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
504 LQueryFID = RQueryFID = FileID();
505 IsLQFIDBeforeRQFID = false;
508 void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
509 unsigned rCommonOffset) {
510 CommonFID = commonFID;
511 LCommonOffset = lCommonOffset;
512 RCommonOffset = rCommonOffset;
517 /// \brief The stack used when building modules on demand, which is used
518 /// to provide a link between the source managers of the different compiler
520 typedef llvm::ArrayRef<std::pair<std::string, FullSourceLoc> > ModuleBuildStack;
522 /// \brief This class handles loading and caching of source files into memory.
524 /// This object owns the MemoryBuffer objects for all of the loaded
525 /// files and assigns unique FileID's for each unique \#include chain.
527 /// The SourceManager can be queried for information about SourceLocation
528 /// objects, turning them into either spelling or expansion locations. Spelling
529 /// locations represent where the bytes corresponding to a token came from and
530 /// expansion locations represent where the location is in the user's view. In
531 /// the case of a macro expansion, for example, the spelling location indicates
532 /// where the expanded token came from and the expansion location specifies
533 /// where it was expanded.
534 class SourceManager : public RefCountedBase<SourceManager> {
535 /// \brief DiagnosticsEngine object.
536 DiagnosticsEngine &Diag;
538 FileManager &FileMgr;
540 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
542 /// \brief Memoized information about all of the files tracked by this
545 /// This map allows us to merge ContentCache entries based
546 /// on their FileEntry*. All ContentCache objects will thus have unique,
547 /// non-null, FileEntry pointers.
548 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
550 /// \brief True if the ContentCache for files that are overriden by other
551 /// files, should report the original file name. Defaults to true.
552 bool OverridenFilesKeepOriginalName;
554 /// \brief True if non-system source files should be treated as volatile
555 /// (likely to change while trying to use them). Defaults to false.
556 bool UserFilesAreVolatile;
558 struct OverriddenFilesInfoTy {
559 /// \brief Files that have been overriden with the contents from another
561 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
562 /// \brief Files that were overridden with a memory buffer.
563 llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer;
566 /// \brief Lazily create the object keeping overridden files info, since
567 /// it is uncommonly used.
568 OwningPtr<OverriddenFilesInfoTy> OverriddenFilesInfo;
570 OverriddenFilesInfoTy &getOverriddenFilesInfo() {
571 if (!OverriddenFilesInfo)
572 OverriddenFilesInfo.reset(new OverriddenFilesInfoTy);
573 return *OverriddenFilesInfo;
576 /// \brief Information about various memory buffers that we have read in.
578 /// All FileEntry* within the stored ContentCache objects are NULL,
579 /// as they do not refer to a file.
580 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
582 /// \brief The table of SLocEntries that are local to this module.
584 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
586 std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
588 /// \brief The table of SLocEntries that are loaded from other modules.
590 /// Negative FileIDs are indexes into this table. To get from ID to an index,
592 mutable std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
594 /// \brief The starting offset of the next local SLocEntry.
596 /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
597 unsigned NextLocalOffset;
599 /// \brief The starting offset of the latest batch of loaded SLocEntries.
601 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
602 /// not have been loaded, so that value would be unknown.
603 unsigned CurrentLoadedOffset;
605 /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
607 static const unsigned MaxLoadedOffset = 1U << 31U;
609 /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
610 /// have already been loaded from the external source.
612 /// Same indexing as LoadedSLocEntryTable.
613 std::vector<bool> SLocEntryLoaded;
615 /// \brief An external source for source location entries.
616 ExternalSLocEntrySource *ExternalSLocEntries;
618 /// \brief A one-entry cache to speed up getFileID.
620 /// LastFileIDLookup records the last FileID looked up or created, because it
621 /// is very common to look up many tokens from the same file.
622 mutable FileID LastFileIDLookup;
624 /// \brief Holds information for \#line directives.
626 /// This is referenced by indices from SLocEntryTable.
627 LineTableInfo *LineTable;
629 /// \brief These ivars serve as a cache used in the getLineNumber
630 /// method which is used to speedup getLineNumber calls to nearby locations.
631 mutable FileID LastLineNoFileIDQuery;
632 mutable SrcMgr::ContentCache *LastLineNoContentCache;
633 mutable unsigned LastLineNoFilePos;
634 mutable unsigned LastLineNoResult;
636 /// \brief The file ID for the main source file of the translation unit.
639 /// \brief The file ID for the precompiled preamble there is one.
640 FileID PreambleFileID;
642 // Statistics for -print-stats.
643 mutable unsigned NumLinearScans, NumBinaryProbes;
645 // Cache results for the isBeforeInTranslationUnit method.
646 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
648 // Cache for the "fake" buffer used for error-recovery purposes.
649 mutable llvm::MemoryBuffer *FakeBufferForRecovery;
651 mutable SrcMgr::ContentCache *FakeContentCacheForRecovery;
653 /// \brief Lazily computed map of macro argument chunks to their expanded
655 typedef std::map<unsigned, SourceLocation> MacroArgsMap;
657 mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap;
659 /// \brief The stack of modules being built, which is used to detect
660 /// cycles in the module dependency graph as modules are being built, as
661 /// well as to describe why we're rebuilding a particular module.
663 /// There is no way to set this value from the command line. If we ever need
664 /// to do so (e.g., if on-demand module construction moves out-of-process),
665 /// we can add a cc1-level option to do so.
666 SmallVector<std::pair<std::string, FullSourceLoc>, 2> StoredModuleBuildStack;
668 // SourceManager doesn't support copy construction.
669 explicit SourceManager(const SourceManager&) LLVM_DELETED_FUNCTION;
670 void operator=(const SourceManager&) LLVM_DELETED_FUNCTION;
672 SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
673 bool UserFilesAreVolatile = false);
676 void clearIDTables();
678 DiagnosticsEngine &getDiagnostics() const { return Diag; }
680 FileManager &getFileManager() const { return FileMgr; }
682 /// \brief Set true if the SourceManager should report the original file name
683 /// for contents of files that were overriden by other files.Defaults to true.
684 void setOverridenFilesKeepOriginalName(bool value) {
685 OverridenFilesKeepOriginalName = value;
688 /// \brief True if non-system source files should be treated as volatile
689 /// (likely to change while trying to use them).
690 bool userFilesAreVolatile() const { return UserFilesAreVolatile; }
692 /// \brief Retrieve the module build stack.
693 ModuleBuildStack getModuleBuildStack() const {
694 return StoredModuleBuildStack;
697 /// \brief Set the module build stack.
698 void setModuleBuildStack(ModuleBuildStack stack) {
699 StoredModuleBuildStack.clear();
700 StoredModuleBuildStack.append(stack.begin(), stack.end());
703 /// \brief Push an entry to the module build stack.
704 void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc) {
705 StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc));
708 /// \brief Create the FileID for a memory buffer that will represent the
709 /// FileID for the main source.
711 /// One example of when this would be used is when the main source is read
713 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
714 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
715 assert(MainFileID.isInvalid() && "MainFileID already set!");
716 MainFileID = createFileIDForMemBuffer(Buffer, Kind);
720 //===--------------------------------------------------------------------===//
721 // MainFileID creation and querying methods.
722 //===--------------------------------------------------------------------===//
724 /// \brief Returns the FileID of the main source file.
725 FileID getMainFileID() const { return MainFileID; }
727 /// \brief Create the FileID for the main source file.
728 FileID createMainFileID(const FileEntry *SourceFile,
729 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) {
730 assert(MainFileID.isInvalid() && "MainFileID already set!");
731 MainFileID = createFileID(SourceFile, SourceLocation(), Kind);
735 /// \brief Set the file ID for the main source file.
736 void setMainFileID(FileID FID) {
737 assert(MainFileID.isInvalid() && "MainFileID already set!");
741 /// \brief Set the file ID for the precompiled preamble.
742 void setPreambleFileID(FileID Preamble) {
743 assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
744 PreambleFileID = Preamble;
747 /// \brief Get the file ID for the precompiled preamble if there is one.
748 FileID getPreambleFileID() const { return PreambleFileID; }
750 //===--------------------------------------------------------------------===//
751 // Methods to create new FileID's and macro expansions.
752 //===--------------------------------------------------------------------===//
754 /// \brief Create a new FileID that represents the specified file
755 /// being \#included from the specified IncludePosition.
757 /// This translates NULL into standard input.
758 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
759 SrcMgr::CharacteristicKind FileCharacter,
760 int LoadedID = 0, unsigned LoadedOffset = 0) {
761 const SrcMgr::ContentCache *
762 IR = getOrCreateContentCache(SourceFile,
763 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
764 assert(IR && "getOrCreateContentCache() cannot return NULL");
765 return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
768 /// \brief Create a new FileID that represents the specified memory buffer.
770 /// This does no caching of the buffer and takes ownership of the
771 /// MemoryBuffer, so only pass a MemoryBuffer to this once.
772 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
773 SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
774 int LoadedID = 0, unsigned LoadedOffset = 0,
775 SourceLocation IncludeLoc = SourceLocation()) {
776 return createFileID(createMemBufferContentCache(Buffer), IncludeLoc,
777 FileCharacter, LoadedID, LoadedOffset);
780 /// \brief Return a new SourceLocation that encodes the
781 /// fact that a token from SpellingLoc should actually be referenced from
782 /// ExpansionLoc, and that it represents the expansion of a macro argument
783 /// into the function-like macro body.
784 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
785 SourceLocation ExpansionLoc,
788 /// \brief Return a new SourceLocation that encodes the fact
789 /// that a token from SpellingLoc should actually be referenced from
791 SourceLocation createExpansionLoc(SourceLocation Loc,
792 SourceLocation ExpansionLocStart,
793 SourceLocation ExpansionLocEnd,
796 unsigned LoadedOffset = 0);
798 /// \brief Retrieve the memory buffer associated with the given file.
800 /// \param Invalid If non-NULL, will be set \c true if an error
801 /// occurs while retrieving the memory buffer.
802 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
805 /// \brief Override the contents of the given source file by providing an
806 /// already-allocated buffer.
808 /// \param SourceFile the source file whose contents will be overriden.
810 /// \param Buffer the memory buffer whose contents will be used as the
811 /// data in the given source file.
813 /// \param DoNotFree If true, then the buffer will not be freed when the
814 /// source manager is destroyed.
815 void overrideFileContents(const FileEntry *SourceFile,
816 const llvm::MemoryBuffer *Buffer,
817 bool DoNotFree = false);
819 /// \brief Override the given source file with another one.
821 /// \param SourceFile the source file which will be overriden.
823 /// \param NewFile the file whose contents will be used as the
824 /// data instead of the contents of the given source file.
825 void overrideFileContents(const FileEntry *SourceFile,
826 const FileEntry *NewFile);
828 /// \brief Returns true if the file contents have been overridden.
829 bool isFileOverridden(const FileEntry *File) {
830 if (OverriddenFilesInfo) {
831 if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
833 if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
834 OverriddenFilesInfo->OverriddenFiles.end())
840 /// \brief Disable overridding the contents of a file, previously enabled
841 /// with #overrideFileContents.
843 /// This should be called before parsing has begun.
844 void disableFileContentsOverride(const FileEntry *File);
846 //===--------------------------------------------------------------------===//
847 // FileID manipulation methods.
848 //===--------------------------------------------------------------------===//
850 /// \brief Return the buffer for the specified FileID.
852 /// If there is an error opening this buffer the first time, this
853 /// manufactures a temporary buffer and returns a non-empty error string.
854 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
855 bool *Invalid = 0) const {
856 bool MyInvalid = false;
857 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
858 if (MyInvalid || !Entry.isFile()) {
862 return getFakeBufferForRecovery();
865 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
869 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
870 bool MyInvalid = false;
871 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
872 if (MyInvalid || !Entry.isFile()) {
876 return getFakeBufferForRecovery();
879 return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
884 /// \brief Returns the FileEntry record for the provided FileID.
885 const FileEntry *getFileEntryForID(FileID FID) const {
886 bool MyInvalid = false;
887 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
888 if (MyInvalid || !Entry.isFile())
891 const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
894 return Content->OrigEntry;
897 /// \brief Returns the FileEntry record for the provided SLocEntry.
898 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
900 const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
903 return Content->OrigEntry;
906 /// \brief Return a StringRef to the source buffer data for the
907 /// specified FileID.
909 /// \param FID The file ID whose contents will be returned.
910 /// \param Invalid If non-NULL, will be set true if an error occurred.
911 StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
913 /// \brief Get the number of FileIDs (files and macros) that were created
914 /// during preprocessing of \p FID, including it.
915 unsigned getNumCreatedFIDsForFileID(FileID FID) const {
916 bool Invalid = false;
917 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
918 if (Invalid || !Entry.isFile())
921 return Entry.getFile().NumCreatedFIDs;
924 /// \brief Set the number of FileIDs (files and macros) that were created
925 /// during preprocessing of \p FID, including it.
926 void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
927 bool Invalid = false;
928 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
929 if (Invalid || !Entry.isFile())
932 assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
933 const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
936 //===--------------------------------------------------------------------===//
937 // SourceLocation manipulation methods.
938 //===--------------------------------------------------------------------===//
940 /// \brief Return the FileID for a SourceLocation.
942 /// This is a very hot method that is used for all SourceManager queries
943 /// that start with a SourceLocation object. It is responsible for finding
944 /// the entry in SLocEntryTable which contains the specified location.
946 FileID getFileID(SourceLocation SpellingLoc) const {
947 unsigned SLocOffset = SpellingLoc.getOffset();
949 // If our one-entry cache covers this offset, just return it.
950 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
951 return LastFileIDLookup;
953 return getFileIDSlow(SLocOffset);
956 /// \brief Return the filename of the file containing a SourceLocation.
957 StringRef getFilename(SourceLocation SpellingLoc) const {
958 if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
963 /// \brief Return the source location corresponding to the first byte of
964 /// the specified file.
965 SourceLocation getLocForStartOfFile(FileID FID) const {
966 bool Invalid = false;
967 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
968 if (Invalid || !Entry.isFile())
969 return SourceLocation();
971 unsigned FileOffset = Entry.getOffset();
972 return SourceLocation::getFileLoc(FileOffset);
975 /// \brief Return the source location corresponding to the last byte of the
977 SourceLocation getLocForEndOfFile(FileID FID) const {
978 bool Invalid = false;
979 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
980 if (Invalid || !Entry.isFile())
981 return SourceLocation();
983 unsigned FileOffset = Entry.getOffset();
984 return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1);
987 /// \brief Returns the include location if \p FID is a \#include'd file
988 /// otherwise it returns an invalid location.
989 SourceLocation getIncludeLoc(FileID FID) const {
990 bool Invalid = false;
991 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
992 if (Invalid || !Entry.isFile())
993 return SourceLocation();
995 return Entry.getFile().getIncludeLoc();
998 // \brief Returns the import location if the given source location is
999 // located within a module, or an invalid location if the source location
1000 // is within the current translation unit.
1001 std::pair<SourceLocation, StringRef>
1002 getModuleImportLoc(SourceLocation Loc) const {
1003 FileID FID = getFileID(Loc);
1005 // Positive file IDs are in the current translation unit, and -1 is a
1008 return std::make_pair(SourceLocation(), "");
1010 return ExternalSLocEntries->getModuleImportLoc(FID.ID);
1013 /// \brief Given a SourceLocation object \p Loc, return the expansion
1014 /// location referenced by the ID.
1015 SourceLocation getExpansionLoc(SourceLocation Loc) const {
1016 // Handle the non-mapped case inline, defer to out of line code to handle
1018 if (Loc.isFileID()) return Loc;
1019 return getExpansionLocSlowCase(Loc);
1022 /// \brief Given \p Loc, if it is a macro location return the expansion
1023 /// location or the spelling location, depending on if it comes from a
1024 /// macro argument or not.
1025 SourceLocation getFileLoc(SourceLocation Loc) const {
1026 if (Loc.isFileID()) return Loc;
1027 return getFileLocSlowCase(Loc);
1030 /// \brief Return the start/end of the expansion information for an
1031 /// expansion location.
1033 /// \pre \p Loc is required to be an expansion location.
1034 std::pair<SourceLocation,SourceLocation>
1035 getImmediateExpansionRange(SourceLocation Loc) const;
1037 /// \brief Given a SourceLocation object, return the range of
1038 /// tokens covered by the expansion the ultimate file.
1039 std::pair<SourceLocation,SourceLocation>
1040 getExpansionRange(SourceLocation Loc) const;
1043 /// \brief Given a SourceLocation object, return the spelling
1044 /// location referenced by the ID.
1046 /// This is the place where the characters that make up the lexed token
1048 SourceLocation getSpellingLoc(SourceLocation Loc) const {
1049 // Handle the non-mapped case inline, defer to out of line code to handle
1051 if (Loc.isFileID()) return Loc;
1052 return getSpellingLocSlowCase(Loc);
1055 /// \brief Given a SourceLocation object, return the spelling location
1056 /// referenced by the ID.
1058 /// This is the first level down towards the place where the characters
1059 /// that make up the lexed token can be found. This should not generally
1060 /// be used by clients.
1061 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
1063 /// \brief Decompose the specified location into a raw FileID + Offset pair.
1065 /// The first element is the FileID, the second is the offset from the
1066 /// start of the buffer of the location.
1067 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
1068 FileID FID = getFileID(Loc);
1069 bool Invalid = false;
1070 const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
1072 return std::make_pair(FileID(), 0);
1073 return std::make_pair(FID, Loc.getOffset()-E.getOffset());
1076 /// \brief Decompose the specified location into a raw FileID + Offset pair.
1078 /// If the location is an expansion record, walk through it until we find
1079 /// the final location expanded.
1080 std::pair<FileID, unsigned>
1081 getDecomposedExpansionLoc(SourceLocation Loc) const {
1082 FileID FID = getFileID(Loc);
1083 bool Invalid = false;
1084 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1086 return std::make_pair(FileID(), 0);
1088 unsigned Offset = Loc.getOffset()-E->getOffset();
1090 return std::make_pair(FID, Offset);
1092 return getDecomposedExpansionLocSlowCase(E);
1095 /// \brief Decompose the specified location into a raw FileID + Offset pair.
1097 /// If the location is an expansion record, walk through it until we find
1098 /// its spelling record.
1099 std::pair<FileID, unsigned>
1100 getDecomposedSpellingLoc(SourceLocation Loc) const {
1101 FileID FID = getFileID(Loc);
1102 bool Invalid = false;
1103 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1105 return std::make_pair(FileID(), 0);
1107 unsigned Offset = Loc.getOffset()-E->getOffset();
1109 return std::make_pair(FID, Offset);
1110 return getDecomposedSpellingLocSlowCase(E, Offset);
1113 /// \brief Returns the offset from the start of the file that the
1114 /// specified SourceLocation represents.
1116 /// This is not very meaningful for a macro ID.
1117 unsigned getFileOffset(SourceLocation SpellingLoc) const {
1118 return getDecomposedLoc(SpellingLoc).second;
1121 /// \brief Tests whether the given source location represents a macro
1122 /// argument's expansion into the function-like macro definition.
1124 /// Such source locations only appear inside of the expansion
1125 /// locations representing where a particular function-like macro was
1127 bool isMacroArgExpansion(SourceLocation Loc) const;
1129 /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
1130 /// chunk of the source location address space.
1132 /// If it's true and \p RelativeOffset is non-null, it will be set to the
1133 /// relative offset of \p Loc inside the chunk.
1134 bool isInSLocAddrSpace(SourceLocation Loc,
1135 SourceLocation Start, unsigned Length,
1136 unsigned *RelativeOffset = 0) const {
1137 assert(((Start.getOffset() < NextLocalOffset &&
1138 Start.getOffset()+Length <= NextLocalOffset) ||
1139 (Start.getOffset() >= CurrentLoadedOffset &&
1140 Start.getOffset()+Length < MaxLoadedOffset)) &&
1141 "Chunk is not valid SLoc address space");
1142 unsigned LocOffs = Loc.getOffset();
1143 unsigned BeginOffs = Start.getOffset();
1144 unsigned EndOffs = BeginOffs + Length;
1145 if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1147 *RelativeOffset = LocOffs - BeginOffs;
1154 /// \brief Return true if both \p LHS and \p RHS are in the local source
1155 /// location address space or the loaded one.
1157 /// If it's true and \p RelativeOffset is non-null, it will be set to the
1158 /// offset of \p RHS relative to \p LHS.
1159 bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1160 int *RelativeOffset) const {
1161 unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1162 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1163 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1165 if (LHSLoaded == RHSLoaded) {
1167 *RelativeOffset = RHSOffs - LHSOffs;
1174 //===--------------------------------------------------------------------===//
1175 // Queries about the code at a SourceLocation.
1176 //===--------------------------------------------------------------------===//
1178 /// \brief Return a pointer to the start of the specified location
1179 /// in the appropriate spelling MemoryBuffer.
1181 /// \param Invalid If non-NULL, will be set \c true if an error occurs.
1182 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
1184 /// \brief Return the column # for the specified file position.
1186 /// This is significantly cheaper to compute than the line number. This
1187 /// returns zero if the column number isn't known. This may only be called
1188 /// on a file sloc, so you must choose a spelling or expansion location
1189 /// before calling this method.
1190 unsigned getColumnNumber(FileID FID, unsigned FilePos,
1191 bool *Invalid = 0) const;
1192 unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1193 unsigned getExpansionColumnNumber(SourceLocation Loc,
1194 bool *Invalid = 0) const;
1195 unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
1198 /// \brief Given a SourceLocation, return the spelling line number
1199 /// for the position indicated.
1201 /// This requires building and caching a table of line offsets for the
1202 /// MemoryBuffer, so this is not cheap: use only when about to emit a
1204 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
1205 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1206 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1207 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
1209 /// \brief Return the filename or buffer identifier of the buffer the
1212 /// Note that this name does not respect \#line directives. Use
1213 /// getPresumedLoc for normal clients.
1214 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
1216 /// \brief Return the file characteristic of the specified source
1217 /// location, indicating whether this is a normal file, a system
1218 /// header, or an "implicit extern C" system header.
1220 /// This state can be modified with flags on GNU linemarker directives like:
1224 /// which changes all source locations in the current file after that to be
1225 /// considered to be from a system header.
1226 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
1228 /// \brief Returns the "presumed" location of a SourceLocation specifies.
1230 /// A "presumed location" can be modified by \#line or GNU line marker
1231 /// directives. This provides a view on the data that a user should see
1232 /// in diagnostics, for example.
1234 /// Note that a presumed location is always given as the expansion point of
1235 /// an expansion location, not at the spelling location.
1237 /// \returns The presumed location of the specified SourceLocation. If the
1238 /// presumed location cannot be calculate (e.g., because \p Loc is invalid
1239 /// or the file containing \p Loc has changed on disk), returns an invalid
1240 /// presumed location.
1241 PresumedLoc getPresumedLoc(SourceLocation Loc,
1242 bool UseLineDirectives = true) const;
1244 /// \brief Returns true if both SourceLocations correspond to the same file.
1245 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1246 return getFileID(Loc1) == getFileID(Loc2);
1249 /// \brief Returns true if the file of provided SourceLocation is the main
1251 bool isFromMainFile(SourceLocation Loc) const {
1252 return getFileID(Loc) == getMainFileID();
1255 /// \brief Returns if a SourceLocation is in a system header.
1256 bool isInSystemHeader(SourceLocation Loc) const {
1257 return getFileCharacteristic(Loc) != SrcMgr::C_User;
1260 /// \brief Returns if a SourceLocation is in an "extern C" system header.
1261 bool isInExternCSystemHeader(SourceLocation Loc) const {
1262 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1265 /// \brief Returns whether \p Loc is expanded from a macro in a system header.
1266 bool isInSystemMacro(SourceLocation loc) {
1267 return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
1270 /// \brief The size of the SLocEnty that \p FID represents.
1271 unsigned getFileIDSize(FileID FID) const;
1273 /// \brief Given a specific FileID, returns true if \p Loc is inside that
1274 /// FileID chunk and sets relative offset (offset of \p Loc from beginning
1275 /// of FileID) to \p relativeOffset.
1276 bool isInFileID(SourceLocation Loc, FileID FID,
1277 unsigned *RelativeOffset = 0) const {
1278 unsigned Offs = Loc.getOffset();
1279 if (isOffsetInFileID(FID, Offs)) {
1281 *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1288 //===--------------------------------------------------------------------===//
1289 // Line Table Manipulation Routines
1290 //===--------------------------------------------------------------------===//
1292 /// \brief Return the uniqued ID for the specified filename.
1294 unsigned getLineTableFilenameID(StringRef Str);
1296 /// \brief Add a line note to the line table for the FileID and offset
1297 /// specified by Loc.
1299 /// If FilenameID is -1, it is considered to be unspecified.
1300 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
1301 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
1302 bool IsFileEntry, bool IsFileExit,
1303 bool IsSystemHeader, bool IsExternCHeader);
1305 /// \brief Determine if the source manager has a line table.
1306 bool hasLineTable() const { return LineTable != 0; }
1308 /// \brief Retrieve the stored line table.
1309 LineTableInfo &getLineTable();
1311 //===--------------------------------------------------------------------===//
1312 // Queries for performance analysis.
1313 //===--------------------------------------------------------------------===//
1315 /// \brief Return the total amount of physical memory allocated by the
1316 /// ContentCache allocator.
1317 size_t getContentCacheSize() const {
1318 return ContentCacheAlloc.getTotalMemory();
1321 struct MemoryBufferSizes {
1322 const size_t malloc_bytes;
1323 const size_t mmap_bytes;
1325 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1326 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1329 /// \brief Return the amount of memory used by memory buffers, breaking down
1330 /// by heap-backed versus mmap'ed memory.
1331 MemoryBufferSizes getMemoryBufferSizes() const;
1333 /// \brief Return the amount of memory used for various side tables and
1334 /// data structures in the SourceManager.
1335 size_t getDataStructureSizes() const;
1337 //===--------------------------------------------------------------------===//
1338 // Other miscellaneous methods.
1339 //===--------------------------------------------------------------------===//
1341 /// \brief Get the source location for the given file:line:col triplet.
1343 /// If the source file is included multiple times, the source location will
1344 /// be based upon the first inclusion.
1345 SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1346 unsigned Line, unsigned Col) const;
1348 /// \brief Get the FileID for the given file.
1350 /// If the source file is included multiple times, the FileID will be the
1351 /// first inclusion.
1352 FileID translateFile(const FileEntry *SourceFile) const;
1354 /// \brief Get the source location in \p FID for the given line:col.
1355 /// Returns null location if \p FID is not a file SLocEntry.
1356 SourceLocation translateLineCol(FileID FID,
1357 unsigned Line, unsigned Col) const;
1359 /// \brief If \p Loc points inside a function macro argument, the returned
1360 /// location will be the macro location in which the argument was expanded.
1361 /// If a macro argument is used multiple times, the expanded location will
1362 /// be at the first expansion of the argument.
1366 /// Passing a file location pointing at 'foo', will yield a macro location
1367 /// where 'foo' was expanded into.
1368 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
1370 /// \brief Determines the order of 2 source locations in the translation unit.
1372 /// \returns true if LHS source location comes before RHS, false otherwise.
1373 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1375 /// \brief Determines the order of 2 source locations in the "source location
1377 bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1378 return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1381 /// \brief Determines the order of a source location and a source location
1382 /// offset in the "source location address space".
1384 /// Note that we always consider source locations loaded from
1385 bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1386 unsigned LHSOffset = LHS.getOffset();
1387 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1388 bool RHSLoaded = RHS >= CurrentLoadedOffset;
1389 if (LHSLoaded == RHSLoaded)
1390 return LHSOffset < RHS;
1395 // Iterators over FileInfos.
1396 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1397 ::const_iterator fileinfo_iterator;
1398 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1399 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
1400 bool hasFileInfo(const FileEntry *File) const {
1401 return FileInfos.find(File) != FileInfos.end();
1404 /// \brief Print statistics to stderr.
1406 void PrintStats() const;
1408 /// \brief Get the number of local SLocEntries we have.
1409 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1411 /// \brief Get a local SLocEntry. This is exposed for indexing.
1412 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1413 bool *Invalid = 0) const {
1414 assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1415 return LocalSLocEntryTable[Index];
1418 /// \brief Get the number of loaded SLocEntries we have.
1419 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1421 /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1422 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
1423 bool *Invalid = 0) const {
1424 assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1425 if (SLocEntryLoaded[Index])
1426 return LoadedSLocEntryTable[Index];
1427 return loadSLocEntry(Index, Invalid);
1430 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1431 if (FID.ID == 0 || FID.ID == -1) {
1432 if (Invalid) *Invalid = true;
1433 return LocalSLocEntryTable[0];
1435 return getSLocEntryByID(FID.ID);
1438 unsigned getNextLocalOffset() const { return NextLocalOffset; }
1440 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1441 assert(LoadedSLocEntryTable.empty() &&
1442 "Invalidating existing loaded entries");
1443 ExternalSLocEntries = Source;
1446 /// \brief Allocate a number of loaded SLocEntries, which will be actually
1447 /// loaded on demand from the external source.
1449 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1450 /// in the global source view. The lowest ID and the base offset of the
1451 /// entries will be returned.
1452 std::pair<int, unsigned>
1453 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1455 /// \brief Returns true if \p Loc came from a PCH/Module.
1456 bool isLoadedSourceLocation(SourceLocation Loc) const {
1457 return Loc.getOffset() >= CurrentLoadedOffset;
1460 /// \brief Returns true if \p Loc did not come from a PCH/Module.
1461 bool isLocalSourceLocation(SourceLocation Loc) const {
1462 return Loc.getOffset() < NextLocalOffset;
1465 /// \brief Returns true if \p FID came from a PCH/Module.
1466 bool isLoadedFileID(FileID FID) const {
1467 assert(FID.ID != -1 && "Using FileID sentinel value");
1471 /// \brief Returns true if \p FID did not come from a PCH/Module.
1472 bool isLocalFileID(FileID FID) const {
1473 return !isLoadedFileID(FID);
1476 /// Get a presumed location suitable for displaying in a diagnostic message,
1477 /// taking into account macro arguments and expansions.
1478 PresumedLoc getPresumedLocForDisplay(SourceLocation Loc,
1479 bool UseLineDirectives = true) const{
1480 // This is a condensed form of the algorithm used by emitCaretDiagnostic to
1481 // walk to the top of the macro call stack.
1482 while (Loc.isMacroID()) {
1483 Loc = skipToMacroArgExpansion(Loc);
1484 Loc = getImmediateMacroCallerLoc(Loc);
1487 return getPresumedLoc(Loc, UseLineDirectives);
1490 /// Look through spelling locations for a macro argument expansion, and if
1491 /// found skip to it so that we can trace the argument rather than the macros
1492 /// in which that argument is used. If no macro argument expansion is found,
1493 /// don't skip anything and return the starting location.
1494 SourceLocation skipToMacroArgExpansion(SourceLocation StartLoc) const {
1495 for (SourceLocation L = StartLoc; L.isMacroID();
1496 L = getImmediateSpellingLoc(L)) {
1497 if (isMacroArgExpansion(L))
1500 // Otherwise just return initial location, there's nothing to skip.
1504 /// Gets the location of the immediate macro caller, one level up the stack
1505 /// toward the initial macro typed into the source.
1506 SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const {
1507 if (!Loc.isMacroID()) return Loc;
1509 // When we have the location of (part of) an expanded parameter, its
1510 // spelling location points to the argument as typed into the macro call,
1511 // and therefore is used to locate the macro caller.
1512 if (isMacroArgExpansion(Loc))
1513 return getImmediateSpellingLoc(Loc);
1515 // Otherwise, the caller of the macro is located where this macro is
1516 // expanded (while the spelling is part of the macro definition).
1517 return getImmediateExpansionRange(Loc).first;
1520 /// Gets the location of the immediate macro callee, one level down the stack
1521 /// toward the leaf macro.
1522 SourceLocation getImmediateMacroCalleeLoc(SourceLocation Loc) const {
1523 if (!Loc.isMacroID()) return Loc;
1525 // When we have the location of (part of) an expanded parameter, its
1526 // expansion location points to the unexpanded parameter reference within
1527 // the macro definition (or callee).
1528 if (isMacroArgExpansion(Loc))
1529 return getImmediateExpansionRange(Loc).first;
1531 // Otherwise, the callee of the macro is located where this location was
1532 // spelled inside the macro definition.
1533 return getImmediateSpellingLoc(Loc);
1537 const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1538 const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
1540 const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1542 /// \brief Get the entry with the given unwrapped FileID.
1543 const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1544 assert(ID != -1 && "Using FileID sentinel value");
1546 return getLoadedSLocEntryByID(ID);
1547 return getLocalSLocEntry(static_cast<unsigned>(ID));
1550 const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID,
1551 bool *Invalid = 0) const {
1552 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1555 /// Implements the common elements of storing an expansion info struct into
1556 /// the SLocEntry table and producing a source location that refers to it.
1557 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1560 unsigned LoadedOffset = 0);
1562 /// \brief Return true if the specified FileID contains the
1563 /// specified SourceLocation offset. This is a very hot method.
1564 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1565 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1566 // If the entry is after the offset, it can't contain it.
1567 if (SLocOffset < Entry.getOffset()) return false;
1569 // If this is the very last entry then it does.
1573 // If it is the last local entry, then it does if the location is local.
1574 if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size()))
1575 return SLocOffset < NextLocalOffset;
1577 // Otherwise, the entry after it has to not include it. This works for both
1578 // local and loaded entries.
1579 return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset();
1582 /// \brief Create a new fileID for the specified ContentCache and
1583 /// include position.
1585 /// This works regardless of whether the ContentCache corresponds to a
1586 /// file or some other input source.
1587 FileID createFileID(const SrcMgr::ContentCache* File,
1588 SourceLocation IncludePos,
1589 SrcMgr::CharacteristicKind DirCharacter,
1590 int LoadedID, unsigned LoadedOffset);
1592 const SrcMgr::ContentCache *
1593 getOrCreateContentCache(const FileEntry *SourceFile,
1594 bool isSystemFile = false);
1596 /// \brief Create a new ContentCache for the specified memory buffer.
1597 const SrcMgr::ContentCache*
1598 createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
1600 FileID getFileIDSlow(unsigned SLocOffset) const;
1601 FileID getFileIDLocal(unsigned SLocOffset) const;
1602 FileID getFileIDLoaded(unsigned SLocOffset) const;
1604 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1605 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1606 SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1608 std::pair<FileID, unsigned>
1609 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1610 std::pair<FileID, unsigned>
1611 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1612 unsigned Offset) const;
1613 void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const;
1614 void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
1616 SourceLocation SpellLoc,
1617 SourceLocation ExpansionLoc,
1618 unsigned ExpansionLength) const;
1619 friend class ASTReader;
1620 friend class ASTWriter;
1623 /// \brief Comparison function object.
1624 template<typename T>
1625 class BeforeThanCompare;
1627 /// \brief Compare two source locations.
1629 class BeforeThanCompare<SourceLocation> {
1633 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { }
1635 bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1636 return SM.isBeforeInTranslationUnit(LHS, RHS);
1640 /// \brief Compare two non-overlapping source ranges.
1642 class BeforeThanCompare<SourceRange> {
1646 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { }
1648 bool operator()(SourceRange LHS, SourceRange RHS) {
1649 return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin());
1653 } // end namespace clang