clang API Documentation
00001 //===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 /// 00010 /// \file 00011 /// \brief Defines the SourceManager interface. 00012 /// 00013 /// There are three different types of locations in a %file: a spelling 00014 /// location, an expansion location, and a presumed location. 00015 /// 00016 /// Given an example of: 00017 /// \code 00018 /// #define min(x, y) x < y ? x : y 00019 /// \endcode 00020 /// 00021 /// and then later on a use of min: 00022 /// \code 00023 /// #line 17 00024 /// return min(a, b); 00025 /// \endcode 00026 /// 00027 /// The expansion location is the line in the source code where the macro 00028 /// was expanded (the return statement), the spelling location is the 00029 /// location in the source where the macro was originally defined, 00030 /// and the presumed location is where the line directive states that 00031 /// the line is 17, or any other line. 00032 /// 00033 //===----------------------------------------------------------------------===// 00034 00035 #ifndef LLVM_CLANG_BASIC_SOURCEMANAGER_H 00036 #define LLVM_CLANG_BASIC_SOURCEMANAGER_H 00037 00038 #include "clang/Basic/FileManager.h" 00039 #include "clang/Basic/LLVM.h" 00040 #include "clang/Basic/SourceLocation.h" 00041 #include "llvm/ADT/ArrayRef.h" 00042 #include "llvm/ADT/DenseMap.h" 00043 #include "llvm/ADT/DenseSet.h" 00044 #include "llvm/ADT/IntrusiveRefCntPtr.h" 00045 #include "llvm/ADT/PointerIntPair.h" 00046 #include "llvm/ADT/PointerUnion.h" 00047 #include "llvm/Support/AlignOf.h" 00048 #include "llvm/Support/Allocator.h" 00049 #include "llvm/Support/DataTypes.h" 00050 #include "llvm/Support/MemoryBuffer.h" 00051 #include <cassert> 00052 #include <map> 00053 #include <memory> 00054 #include <vector> 00055 00056 namespace clang { 00057 00058 class DiagnosticsEngine; 00059 class SourceManager; 00060 class FileManager; 00061 class FileEntry; 00062 class LineTableInfo; 00063 class LangOptions; 00064 class ASTWriter; 00065 class ASTReader; 00066 00067 /// \brief Public enums and private classes that are part of the 00068 /// SourceManager implementation. 00069 /// 00070 namespace SrcMgr { 00071 /// \brief Indicates whether a file or directory holds normal user code, 00072 /// system code, or system code which is implicitly 'extern "C"' in C++ mode. 00073 /// 00074 /// Entire directories can be tagged with this (this is maintained by 00075 /// DirectoryLookup and friends) as can specific FileInfos when a \#pragma 00076 /// system_header is seen or in various other cases. 00077 /// 00078 enum CharacteristicKind { 00079 C_User, C_System, C_ExternCSystem 00080 }; 00081 00082 /// \brief One instance of this struct is kept for every file loaded or used. 00083 /// 00084 /// This object owns the MemoryBuffer object. 00085 class ContentCache { 00086 enum CCFlags { 00087 /// \brief Whether the buffer is invalid. 00088 InvalidFlag = 0x01, 00089 /// \brief Whether the buffer should not be freed on destruction. 00090 DoNotFreeFlag = 0x02 00091 }; 00092 00093 // Note that the first member of this class is an aligned character buffer 00094 // to ensure that this class has an alignment of 8 bytes. This wastes 00095 // 8 bytes for every ContentCache object, but each of these corresponds to 00096 // a file loaded into memory, so the 8 bytes doesn't seem terribly 00097 // important. It is quite awkward to fit this aligner into any other part 00098 // of the class due to the lack of portable ways to combine it with other 00099 // members. 00100 llvm::AlignedCharArray<8, 1> NonceAligner; 00101 00102 /// \brief The actual buffer containing the characters from the input 00103 /// file. 00104 /// 00105 /// This is owned by the ContentCache object. The bits indicate 00106 /// whether the buffer is invalid. 00107 mutable llvm::PointerIntPair<llvm::MemoryBuffer *, 2> Buffer; 00108 00109 public: 00110 /// \brief Reference to the file entry representing this ContentCache. 00111 /// 00112 /// This reference does not own the FileEntry object. 00113 /// 00114 /// It is possible for this to be NULL if the ContentCache encapsulates 00115 /// an imaginary text buffer. 00116 const FileEntry *OrigEntry; 00117 00118 /// \brief References the file which the contents were actually loaded from. 00119 /// 00120 /// Can be different from 'Entry' if we overridden the contents of one file 00121 /// with the contents of another file. 00122 const FileEntry *ContentsEntry; 00123 00124 /// \brief A bump pointer allocated array of offsets for each source line. 00125 /// 00126 /// This is lazily computed. This is owned by the SourceManager 00127 /// BumpPointerAllocator object. 00128 unsigned *SourceLineCache; 00129 00130 /// \brief The number of lines in this ContentCache. 00131 /// 00132 /// This is only valid if SourceLineCache is non-null. 00133 unsigned NumLines : 31; 00134 00135 /// \brief Indicates whether the buffer itself was provided to override 00136 /// the actual file contents. 00137 /// 00138 /// When true, the original entry may be a virtual file that does not 00139 /// exist. 00140 unsigned BufferOverridden : 1; 00141 00142 /// \brief True if this content cache was initially created for a source 00143 /// file considered as a system one. 00144 unsigned IsSystemFile : 1; 00145 00146 ContentCache(const FileEntry *Ent = nullptr) 00147 : Buffer(nullptr, false), OrigEntry(Ent), ContentsEntry(Ent), 00148 SourceLineCache(nullptr), NumLines(0), BufferOverridden(false), 00149 IsSystemFile(false) { 00150 (void)NonceAligner; // Silence warnings about unused member. 00151 } 00152 00153 ContentCache(const FileEntry *Ent, const FileEntry *contentEnt) 00154 : Buffer(nullptr, false), OrigEntry(Ent), ContentsEntry(contentEnt), 00155 SourceLineCache(nullptr), NumLines(0), BufferOverridden(false), 00156 IsSystemFile(false) {} 00157 00158 ~ContentCache(); 00159 00160 /// The copy ctor does not allow copies where source object has either 00161 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory 00162 /// is not transferred, so this is a logical error. 00163 ContentCache(const ContentCache &RHS) 00164 : Buffer(nullptr, false), SourceLineCache(nullptr), 00165 BufferOverridden(false), IsSystemFile(false) { 00166 OrigEntry = RHS.OrigEntry; 00167 ContentsEntry = RHS.ContentsEntry; 00168 00169 assert(RHS.Buffer.getPointer() == nullptr && 00170 RHS.SourceLineCache == nullptr && 00171 "Passed ContentCache object cannot own a buffer."); 00172 00173 NumLines = RHS.NumLines; 00174 } 00175 00176 /// \brief Returns the memory buffer for the associated content. 00177 /// 00178 /// \param Diag Object through which diagnostics will be emitted if the 00179 /// buffer cannot be retrieved. 00180 /// 00181 /// \param Loc If specified, is the location that invalid file diagnostics 00182 /// will be emitted at. 00183 /// 00184 /// \param Invalid If non-NULL, will be set \c true if an error occurred. 00185 llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag, 00186 const SourceManager &SM, 00187 SourceLocation Loc = SourceLocation(), 00188 bool *Invalid = nullptr) const; 00189 00190 /// \brief Returns the size of the content encapsulated by this 00191 /// ContentCache. 00192 /// 00193 /// This can be the size of the source file or the size of an 00194 /// arbitrary scratch buffer. If the ContentCache encapsulates a source 00195 /// file this size is retrieved from the file's FileEntry. 00196 unsigned getSize() const; 00197 00198 /// \brief Returns the number of bytes actually mapped for this 00199 /// ContentCache. 00200 /// 00201 /// This can be 0 if the MemBuffer was not actually expanded. 00202 unsigned getSizeBytesMapped() const; 00203 00204 /// Returns the kind of memory used to back the memory buffer for 00205 /// this content cache. This is used for performance analysis. 00206 llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const; 00207 00208 void setBuffer(std::unique_ptr<llvm::MemoryBuffer> B) { 00209 assert(!Buffer.getPointer() && "MemoryBuffer already set."); 00210 Buffer.setPointer(B.release()); 00211 Buffer.setInt(0); 00212 } 00213 00214 /// \brief Get the underlying buffer, returning NULL if the buffer is not 00215 /// yet available. 00216 llvm::MemoryBuffer *getRawBuffer() const { return Buffer.getPointer(); } 00217 00218 /// \brief Replace the existing buffer (which will be deleted) 00219 /// with the given buffer. 00220 void replaceBuffer(llvm::MemoryBuffer *B, bool DoNotFree = false); 00221 00222 /// \brief Determine whether the buffer itself is invalid. 00223 bool isBufferInvalid() const { 00224 return Buffer.getInt() & InvalidFlag; 00225 } 00226 00227 /// \brief Determine whether the buffer should be freed. 00228 bool shouldFreeBuffer() const { 00229 return (Buffer.getInt() & DoNotFreeFlag) == 0; 00230 } 00231 00232 private: 00233 // Disable assignments. 00234 ContentCache &operator=(const ContentCache& RHS) LLVM_DELETED_FUNCTION; 00235 }; 00236 00237 // Assert that the \c ContentCache objects will always be 8-byte aligned so 00238 // that we can pack 3 bits of integer into pointers to such objects. 00239 static_assert(llvm::AlignOf<ContentCache>::Alignment >= 8, 00240 "ContentCache must be 8-byte aligned."); 00241 00242 /// \brief Information about a FileID, basically just the logical file 00243 /// that it represents and include stack information. 00244 /// 00245 /// Each FileInfo has include stack information, indicating where it came 00246 /// from. This information encodes the \#include chain that a token was 00247 /// expanded from. The main include file has an invalid IncludeLoc. 00248 /// 00249 /// FileInfos contain a "ContentCache *", with the contents of the file. 00250 /// 00251 class FileInfo { 00252 /// \brief The location of the \#include that brought in this file. 00253 /// 00254 /// This is an invalid SLOC for the main file (top of the \#include chain). 00255 unsigned IncludeLoc; // Really a SourceLocation 00256 00257 /// \brief Number of FileIDs (files and macros) that were created during 00258 /// preprocessing of this \#include, including this SLocEntry. 00259 /// 00260 /// Zero means the preprocessor didn't provide such info for this SLocEntry. 00261 unsigned NumCreatedFIDs; 00262 00263 /// \brief Contains the ContentCache* and the bits indicating the 00264 /// characteristic of the file and whether it has \#line info, all 00265 /// bitmangled together. 00266 uintptr_t Data; 00267 00268 friend class clang::SourceManager; 00269 friend class clang::ASTWriter; 00270 friend class clang::ASTReader; 00271 public: 00272 /// \brief Return a FileInfo object. 00273 static FileInfo get(SourceLocation IL, const ContentCache *Con, 00274 CharacteristicKind FileCharacter) { 00275 FileInfo X; 00276 X.IncludeLoc = IL.getRawEncoding(); 00277 X.NumCreatedFIDs = 0; 00278 X.Data = (uintptr_t)Con; 00279 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned"); 00280 assert((unsigned)FileCharacter < 4 && "invalid file character"); 00281 X.Data |= (unsigned)FileCharacter; 00282 return X; 00283 } 00284 00285 SourceLocation getIncludeLoc() const { 00286 return SourceLocation::getFromRawEncoding(IncludeLoc); 00287 } 00288 const ContentCache* getContentCache() const { 00289 return reinterpret_cast<const ContentCache*>(Data & ~uintptr_t(7)); 00290 } 00291 00292 /// \brief Return whether this is a system header or not. 00293 CharacteristicKind getFileCharacteristic() const { 00294 return (CharacteristicKind)(Data & 3); 00295 } 00296 00297 /// \brief Return true if this FileID has \#line directives in it. 00298 bool hasLineDirectives() const { return (Data & 4) != 0; } 00299 00300 /// \brief Set the flag that indicates that this FileID has 00301 /// line table entries associated with it. 00302 void setHasLineDirectives() { 00303 Data |= 4; 00304 } 00305 }; 00306 00307 /// \brief Each ExpansionInfo encodes the expansion location - where 00308 /// the token was ultimately expanded, and the SpellingLoc - where the actual 00309 /// character data for the token came from. 00310 class ExpansionInfo { 00311 // Really these are all SourceLocations. 00312 00313 /// \brief Where the spelling for the token can be found. 00314 unsigned SpellingLoc; 00315 00316 /// In a macro expansion, ExpansionLocStart and ExpansionLocEnd 00317 /// indicate the start and end of the expansion. In object-like macros, 00318 /// they will be the same. In a function-like macro expansion, the start 00319 /// will be the identifier and the end will be the ')'. Finally, in 00320 /// macro-argument instantiations, the end will be 'SourceLocation()', an 00321 /// invalid location. 00322 unsigned ExpansionLocStart, ExpansionLocEnd; 00323 00324 public: 00325 SourceLocation getSpellingLoc() const { 00326 return SourceLocation::getFromRawEncoding(SpellingLoc); 00327 } 00328 SourceLocation getExpansionLocStart() const { 00329 return SourceLocation::getFromRawEncoding(ExpansionLocStart); 00330 } 00331 SourceLocation getExpansionLocEnd() const { 00332 SourceLocation EndLoc = 00333 SourceLocation::getFromRawEncoding(ExpansionLocEnd); 00334 return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc; 00335 } 00336 00337 std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const { 00338 return std::make_pair(getExpansionLocStart(), getExpansionLocEnd()); 00339 } 00340 00341 bool isMacroArgExpansion() const { 00342 // Note that this needs to return false for default constructed objects. 00343 return getExpansionLocStart().isValid() && 00344 SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid(); 00345 } 00346 00347 bool isMacroBodyExpansion() const { 00348 return getExpansionLocStart().isValid() && 00349 SourceLocation::getFromRawEncoding(ExpansionLocEnd).isValid(); 00350 } 00351 00352 bool isFunctionMacroExpansion() const { 00353 return getExpansionLocStart().isValid() && 00354 getExpansionLocStart() != getExpansionLocEnd(); 00355 } 00356 00357 /// \brief Return a ExpansionInfo for an expansion. 00358 /// 00359 /// Start and End specify the expansion range (where the macro is 00360 /// expanded), and SpellingLoc specifies the spelling location (where 00361 /// the characters from the token come from). All three can refer to 00362 /// normal File SLocs or expansion locations. 00363 static ExpansionInfo create(SourceLocation SpellingLoc, 00364 SourceLocation Start, SourceLocation End) { 00365 ExpansionInfo X; 00366 X.SpellingLoc = SpellingLoc.getRawEncoding(); 00367 X.ExpansionLocStart = Start.getRawEncoding(); 00368 X.ExpansionLocEnd = End.getRawEncoding(); 00369 return X; 00370 } 00371 00372 /// \brief Return a special ExpansionInfo for the expansion of 00373 /// a macro argument into a function-like macro's body. 00374 /// 00375 /// ExpansionLoc specifies the expansion location (where the macro is 00376 /// expanded). This doesn't need to be a range because a macro is always 00377 /// expanded at a macro parameter reference, and macro parameters are 00378 /// always exactly one token. SpellingLoc specifies the spelling location 00379 /// (where the characters from the token come from). ExpansionLoc and 00380 /// SpellingLoc can both refer to normal File SLocs or expansion locations. 00381 /// 00382 /// Given the code: 00383 /// \code 00384 /// #define F(x) f(x) 00385 /// F(42); 00386 /// \endcode 00387 /// 00388 /// When expanding '\c F(42)', the '\c x' would call this with an 00389 /// SpellingLoc pointing at '\c 42' and an ExpansionLoc pointing at its 00390 /// location in the definition of '\c F'. 00391 static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc, 00392 SourceLocation ExpansionLoc) { 00393 // We store an intentionally invalid source location for the end of the 00394 // expansion range to mark that this is a macro argument ion rather than 00395 // a normal one. 00396 return create(SpellingLoc, ExpansionLoc, SourceLocation()); 00397 } 00398 }; 00399 00400 /// \brief This is a discriminated union of FileInfo and ExpansionInfo. 00401 /// 00402 /// SourceManager keeps an array of these objects, and they are uniquely 00403 /// identified by the FileID datatype. 00404 class SLocEntry { 00405 unsigned Offset; // low bit is set for expansion info. 00406 union { 00407 FileInfo File; 00408 ExpansionInfo Expansion; 00409 }; 00410 public: 00411 unsigned getOffset() const { return Offset >> 1; } 00412 00413 bool isExpansion() const { return Offset & 1; } 00414 bool isFile() const { return !isExpansion(); } 00415 00416 const FileInfo &getFile() const { 00417 assert(isFile() && "Not a file SLocEntry!"); 00418 return File; 00419 } 00420 00421 const ExpansionInfo &getExpansion() const { 00422 assert(isExpansion() && "Not a macro expansion SLocEntry!"); 00423 return Expansion; 00424 } 00425 00426 static SLocEntry get(unsigned Offset, const FileInfo &FI) { 00427 SLocEntry E; 00428 E.Offset = Offset << 1; 00429 E.File = FI; 00430 return E; 00431 } 00432 00433 static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) { 00434 SLocEntry E; 00435 E.Offset = (Offset << 1) | 1; 00436 E.Expansion = Expansion; 00437 return E; 00438 } 00439 }; 00440 } // end SrcMgr namespace. 00441 00442 /// \brief External source of source location entries. 00443 class ExternalSLocEntrySource { 00444 public: 00445 virtual ~ExternalSLocEntrySource(); 00446 00447 /// \brief Read the source location entry with index ID, which will always be 00448 /// less than -1. 00449 /// 00450 /// \returns true if an error occurred that prevented the source-location 00451 /// entry from being loaded. 00452 virtual bool ReadSLocEntry(int ID) = 0; 00453 00454 /// \brief Retrieve the module import location and name for the given ID, if 00455 /// in fact it was loaded from a module (rather than, say, a precompiled 00456 /// header). 00457 virtual std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) = 0; 00458 }; 00459 00460 00461 /// \brief Holds the cache used by isBeforeInTranslationUnit. 00462 /// 00463 /// The cache structure is complex enough to be worth breaking out of 00464 /// SourceManager. 00465 class InBeforeInTUCacheEntry { 00466 /// \brief The FileID's of the cached query. 00467 /// 00468 /// If these match up with a subsequent query, the result can be reused. 00469 FileID LQueryFID, RQueryFID; 00470 00471 /// \brief True if LQueryFID was created before RQueryFID. 00472 /// 00473 /// This is used to compare macro expansion locations. 00474 bool IsLQFIDBeforeRQFID; 00475 00476 /// \brief The file found in common between the two \#include traces, i.e., 00477 /// the nearest common ancestor of the \#include tree. 00478 FileID CommonFID; 00479 00480 /// \brief The offset of the previous query in CommonFID. 00481 /// 00482 /// Usually, this represents the location of the \#include for QueryFID, but 00483 /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a 00484 /// random token in the parent. 00485 unsigned LCommonOffset, RCommonOffset; 00486 public: 00487 /// \brief Return true if the currently cached values match up with 00488 /// the specified LHS/RHS query. 00489 /// 00490 /// If not, we can't use the cache. 00491 bool isCacheValid(FileID LHS, FileID RHS) const { 00492 return LQueryFID == LHS && RQueryFID == RHS; 00493 } 00494 00495 /// \brief If the cache is valid, compute the result given the 00496 /// specified offsets in the LHS/RHS FileID's. 00497 bool getCachedResult(unsigned LOffset, unsigned ROffset) const { 00498 // If one of the query files is the common file, use the offset. Otherwise, 00499 // use the #include loc in the common file. 00500 if (LQueryFID != CommonFID) LOffset = LCommonOffset; 00501 if (RQueryFID != CommonFID) ROffset = RCommonOffset; 00502 00503 // It is common for multiple macro expansions to be "included" from the same 00504 // location (expansion location), in which case use the order of the FileIDs 00505 // to determine which came first. This will also take care the case where 00506 // one of the locations points at the inclusion/expansion point of the other 00507 // in which case its FileID will come before the other. 00508 if (LOffset == ROffset) 00509 return IsLQFIDBeforeRQFID; 00510 00511 return LOffset < ROffset; 00512 } 00513 00514 /// \brief Set up a new query. 00515 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) { 00516 assert(LHS != RHS); 00517 LQueryFID = LHS; 00518 RQueryFID = RHS; 00519 IsLQFIDBeforeRQFID = isLFIDBeforeRFID; 00520 } 00521 00522 void clear() { 00523 LQueryFID = RQueryFID = FileID(); 00524 IsLQFIDBeforeRQFID = false; 00525 } 00526 00527 void setCommonLoc(FileID commonFID, unsigned lCommonOffset, 00528 unsigned rCommonOffset) { 00529 CommonFID = commonFID; 00530 LCommonOffset = lCommonOffset; 00531 RCommonOffset = rCommonOffset; 00532 } 00533 00534 }; 00535 00536 /// \brief The stack used when building modules on demand, which is used 00537 /// to provide a link between the source managers of the different compiler 00538 /// instances. 00539 typedef ArrayRef<std::pair<std::string, FullSourceLoc> > ModuleBuildStack; 00540 00541 /// \brief This class handles loading and caching of source files into memory. 00542 /// 00543 /// This object owns the MemoryBuffer objects for all of the loaded 00544 /// files and assigns unique FileID's for each unique \#include chain. 00545 /// 00546 /// The SourceManager can be queried for information about SourceLocation 00547 /// objects, turning them into either spelling or expansion locations. Spelling 00548 /// locations represent where the bytes corresponding to a token came from and 00549 /// expansion locations represent where the location is in the user's view. In 00550 /// the case of a macro expansion, for example, the spelling location indicates 00551 /// where the expanded token came from and the expansion location specifies 00552 /// where it was expanded. 00553 class SourceManager : public RefCountedBase<SourceManager> { 00554 /// \brief DiagnosticsEngine object. 00555 DiagnosticsEngine &Diag; 00556 00557 FileManager &FileMgr; 00558 00559 mutable llvm::BumpPtrAllocator ContentCacheAlloc; 00560 00561 /// \brief Memoized information about all of the files tracked by this 00562 /// SourceManager. 00563 /// 00564 /// This map allows us to merge ContentCache entries based 00565 /// on their FileEntry*. All ContentCache objects will thus have unique, 00566 /// non-null, FileEntry pointers. 00567 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; 00568 00569 /// \brief True if the ContentCache for files that are overridden by other 00570 /// files, should report the original file name. Defaults to true. 00571 bool OverridenFilesKeepOriginalName; 00572 00573 /// \brief True if non-system source files should be treated as volatile 00574 /// (likely to change while trying to use them). Defaults to false. 00575 bool UserFilesAreVolatile; 00576 00577 struct OverriddenFilesInfoTy { 00578 /// \brief Files that have been overridden with the contents from another 00579 /// file. 00580 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles; 00581 /// \brief Files that were overridden with a memory buffer. 00582 llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer; 00583 }; 00584 00585 /// \brief Lazily create the object keeping overridden files info, since 00586 /// it is uncommonly used. 00587 std::unique_ptr<OverriddenFilesInfoTy> OverriddenFilesInfo; 00588 00589 OverriddenFilesInfoTy &getOverriddenFilesInfo() { 00590 if (!OverriddenFilesInfo) 00591 OverriddenFilesInfo.reset(new OverriddenFilesInfoTy); 00592 return *OverriddenFilesInfo; 00593 } 00594 00595 /// \brief Information about various memory buffers that we have read in. 00596 /// 00597 /// All FileEntry* within the stored ContentCache objects are NULL, 00598 /// as they do not refer to a file. 00599 std::vector<SrcMgr::ContentCache*> MemBufferInfos; 00600 00601 /// \brief The table of SLocEntries that are local to this module. 00602 /// 00603 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid 00604 /// expansion. 00605 SmallVector<SrcMgr::SLocEntry, 0> LocalSLocEntryTable; 00606 00607 /// \brief The table of SLocEntries that are loaded from other modules. 00608 /// 00609 /// Negative FileIDs are indexes into this table. To get from ID to an index, 00610 /// use (-ID - 2). 00611 mutable SmallVector<SrcMgr::SLocEntry, 0> LoadedSLocEntryTable; 00612 00613 /// \brief The starting offset of the next local SLocEntry. 00614 /// 00615 /// This is LocalSLocEntryTable.back().Offset + the size of that entry. 00616 unsigned NextLocalOffset; 00617 00618 /// \brief The starting offset of the latest batch of loaded SLocEntries. 00619 /// 00620 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might 00621 /// not have been loaded, so that value would be unknown. 00622 unsigned CurrentLoadedOffset; 00623 00624 /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset 00625 /// starts at 2^31. 00626 static const unsigned MaxLoadedOffset = 1U << 31U; 00627 00628 /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable 00629 /// have already been loaded from the external source. 00630 /// 00631 /// Same indexing as LoadedSLocEntryTable. 00632 std::vector<bool> SLocEntryLoaded; 00633 00634 /// \brief An external source for source location entries. 00635 ExternalSLocEntrySource *ExternalSLocEntries; 00636 00637 /// \brief A one-entry cache to speed up getFileID. 00638 /// 00639 /// LastFileIDLookup records the last FileID looked up or created, because it 00640 /// is very common to look up many tokens from the same file. 00641 mutable FileID LastFileIDLookup; 00642 00643 /// \brief Holds information for \#line directives. 00644 /// 00645 /// This is referenced by indices from SLocEntryTable. 00646 LineTableInfo *LineTable; 00647 00648 /// \brief These ivars serve as a cache used in the getLineNumber 00649 /// method which is used to speedup getLineNumber calls to nearby locations. 00650 mutable FileID LastLineNoFileIDQuery; 00651 mutable SrcMgr::ContentCache *LastLineNoContentCache; 00652 mutable unsigned LastLineNoFilePos; 00653 mutable unsigned LastLineNoResult; 00654 00655 /// \brief The file ID for the main source file of the translation unit. 00656 FileID MainFileID; 00657 00658 /// \brief The file ID for the precompiled preamble there is one. 00659 FileID PreambleFileID; 00660 00661 // Statistics for -print-stats. 00662 mutable unsigned NumLinearScans, NumBinaryProbes; 00663 00664 /// \brief Associates a FileID with its "included/expanded in" decomposed 00665 /// location. 00666 /// 00667 /// Used to cache results from and speed-up \c getDecomposedIncludedLoc 00668 /// function. 00669 mutable llvm::DenseMap<FileID, std::pair<FileID, unsigned> > IncludedLocMap; 00670 00671 /// The key value into the IsBeforeInTUCache table. 00672 typedef std::pair<FileID, FileID> IsBeforeInTUCacheKey; 00673 00674 /// The IsBeforeInTranslationUnitCache is a mapping from FileID pairs 00675 /// to cache results. 00676 typedef llvm::DenseMap<IsBeforeInTUCacheKey, InBeforeInTUCacheEntry> 00677 InBeforeInTUCache; 00678 00679 /// Cache results for the isBeforeInTranslationUnit method. 00680 mutable InBeforeInTUCache IBTUCache; 00681 mutable InBeforeInTUCacheEntry IBTUCacheOverflow; 00682 00683 /// Return the cache entry for comparing the given file IDs 00684 /// for isBeforeInTranslationUnit. 00685 InBeforeInTUCacheEntry &getInBeforeInTUCache(FileID LFID, FileID RFID) const; 00686 00687 // Cache for the "fake" buffer used for error-recovery purposes. 00688 mutable std::unique_ptr<llvm::MemoryBuffer> FakeBufferForRecovery; 00689 00690 mutable std::unique_ptr<SrcMgr::ContentCache> FakeContentCacheForRecovery; 00691 00692 /// \brief Lazily computed map of macro argument chunks to their expanded 00693 /// source location. 00694 typedef std::map<unsigned, SourceLocation> MacroArgsMap; 00695 00696 mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap; 00697 00698 /// \brief The stack of modules being built, which is used to detect 00699 /// cycles in the module dependency graph as modules are being built, as 00700 /// well as to describe why we're rebuilding a particular module. 00701 /// 00702 /// There is no way to set this value from the command line. If we ever need 00703 /// to do so (e.g., if on-demand module construction moves out-of-process), 00704 /// we can add a cc1-level option to do so. 00705 SmallVector<std::pair<std::string, FullSourceLoc>, 2> StoredModuleBuildStack; 00706 00707 // SourceManager doesn't support copy construction. 00708 explicit SourceManager(const SourceManager&) LLVM_DELETED_FUNCTION; 00709 void operator=(const SourceManager&) LLVM_DELETED_FUNCTION; 00710 public: 00711 SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr, 00712 bool UserFilesAreVolatile = false); 00713 ~SourceManager(); 00714 00715 void clearIDTables(); 00716 00717 DiagnosticsEngine &getDiagnostics() const { return Diag; } 00718 00719 FileManager &getFileManager() const { return FileMgr; } 00720 00721 /// \brief Set true if the SourceManager should report the original file name 00722 /// for contents of files that were overridden by other files. Defaults to 00723 /// true. 00724 void setOverridenFilesKeepOriginalName(bool value) { 00725 OverridenFilesKeepOriginalName = value; 00726 } 00727 00728 /// \brief True if non-system source files should be treated as volatile 00729 /// (likely to change while trying to use them). 00730 bool userFilesAreVolatile() const { return UserFilesAreVolatile; } 00731 00732 /// \brief Retrieve the module build stack. 00733 ModuleBuildStack getModuleBuildStack() const { 00734 return StoredModuleBuildStack; 00735 } 00736 00737 /// \brief Set the module build stack. 00738 void setModuleBuildStack(ModuleBuildStack stack) { 00739 StoredModuleBuildStack.clear(); 00740 StoredModuleBuildStack.append(stack.begin(), stack.end()); 00741 } 00742 00743 /// \brief Push an entry to the module build stack. 00744 void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc) { 00745 StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc)); 00746 } 00747 00748 //===--------------------------------------------------------------------===// 00749 // MainFileID creation and querying methods. 00750 //===--------------------------------------------------------------------===// 00751 00752 /// \brief Returns the FileID of the main source file. 00753 FileID getMainFileID() const { return MainFileID; } 00754 00755 /// \brief Set the file ID for the main source file. 00756 void setMainFileID(FileID FID) { 00757 MainFileID = FID; 00758 } 00759 00760 /// \brief Set the file ID for the precompiled preamble. 00761 void setPreambleFileID(FileID Preamble) { 00762 assert(PreambleFileID.isInvalid() && "PreambleFileID already set!"); 00763 PreambleFileID = Preamble; 00764 } 00765 00766 /// \brief Get the file ID for the precompiled preamble if there is one. 00767 FileID getPreambleFileID() const { return PreambleFileID; } 00768 00769 //===--------------------------------------------------------------------===// 00770 // Methods to create new FileID's and macro expansions. 00771 //===--------------------------------------------------------------------===// 00772 00773 /// \brief Create a new FileID that represents the specified file 00774 /// being \#included from the specified IncludePosition. 00775 /// 00776 /// This translates NULL into standard input. 00777 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, 00778 SrcMgr::CharacteristicKind FileCharacter, 00779 int LoadedID = 0, unsigned LoadedOffset = 0) { 00780 const SrcMgr::ContentCache * 00781 IR = getOrCreateContentCache(SourceFile, 00782 /*isSystemFile=*/FileCharacter != SrcMgr::C_User); 00783 assert(IR && "getOrCreateContentCache() cannot return NULL"); 00784 return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset); 00785 } 00786 00787 /// \brief Create a new FileID that represents the specified memory buffer. 00788 /// 00789 /// This does no caching of the buffer and takes ownership of the 00790 /// MemoryBuffer, so only pass a MemoryBuffer to this once. 00791 FileID createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer, 00792 SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User, 00793 int LoadedID = 0, unsigned LoadedOffset = 0, 00794 SourceLocation IncludeLoc = SourceLocation()) { 00795 return createFileID(createMemBufferContentCache(std::move(Buffer)), 00796 IncludeLoc, FileCharacter, LoadedID, LoadedOffset); 00797 } 00798 00799 /// \brief Return a new SourceLocation that encodes the 00800 /// fact that a token from SpellingLoc should actually be referenced from 00801 /// ExpansionLoc, and that it represents the expansion of a macro argument 00802 /// into the function-like macro body. 00803 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc, 00804 SourceLocation ExpansionLoc, 00805 unsigned TokLength); 00806 00807 /// \brief Return a new SourceLocation that encodes the fact 00808 /// that a token from SpellingLoc should actually be referenced from 00809 /// ExpansionLoc. 00810 SourceLocation createExpansionLoc(SourceLocation Loc, 00811 SourceLocation ExpansionLocStart, 00812 SourceLocation ExpansionLocEnd, 00813 unsigned TokLength, 00814 int LoadedID = 0, 00815 unsigned LoadedOffset = 0); 00816 00817 /// \brief Retrieve the memory buffer associated with the given file. 00818 /// 00819 /// \param Invalid If non-NULL, will be set \c true if an error 00820 /// occurs while retrieving the memory buffer. 00821 llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File, 00822 bool *Invalid = nullptr); 00823 00824 /// \brief Override the contents of the given source file by providing an 00825 /// already-allocated buffer. 00826 /// 00827 /// \param SourceFile the source file whose contents will be overridden. 00828 /// 00829 /// \param Buffer the memory buffer whose contents will be used as the 00830 /// data in the given source file. 00831 /// 00832 /// \param DoNotFree If true, then the buffer will not be freed when the 00833 /// source manager is destroyed. 00834 void overrideFileContents(const FileEntry *SourceFile, 00835 llvm::MemoryBuffer *Buffer, bool DoNotFree); 00836 void overrideFileContents(const FileEntry *SourceFile, 00837 std::unique_ptr<llvm::MemoryBuffer> Buffer) { 00838 overrideFileContents(SourceFile, Buffer.release(), /*DoNotFree*/ false); 00839 } 00840 00841 /// \brief Override the given source file with another one. 00842 /// 00843 /// \param SourceFile the source file which will be overridden. 00844 /// 00845 /// \param NewFile the file whose contents will be used as the 00846 /// data instead of the contents of the given source file. 00847 void overrideFileContents(const FileEntry *SourceFile, 00848 const FileEntry *NewFile); 00849 00850 /// \brief Returns true if the file contents have been overridden. 00851 bool isFileOverridden(const FileEntry *File) { 00852 if (OverriddenFilesInfo) { 00853 if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File)) 00854 return true; 00855 if (OverriddenFilesInfo->OverriddenFiles.find(File) != 00856 OverriddenFilesInfo->OverriddenFiles.end()) 00857 return true; 00858 } 00859 return false; 00860 } 00861 00862 /// \brief Disable overridding the contents of a file, previously enabled 00863 /// with #overrideFileContents. 00864 /// 00865 /// This should be called before parsing has begun. 00866 void disableFileContentsOverride(const FileEntry *File); 00867 00868 //===--------------------------------------------------------------------===// 00869 // FileID manipulation methods. 00870 //===--------------------------------------------------------------------===// 00871 00872 /// \brief Return the buffer for the specified FileID. 00873 /// 00874 /// If there is an error opening this buffer the first time, this 00875 /// manufactures a temporary buffer and returns a non-empty error string. 00876 llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc, 00877 bool *Invalid = nullptr) const { 00878 bool MyInvalid = false; 00879 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 00880 if (MyInvalid || !Entry.isFile()) { 00881 if (Invalid) 00882 *Invalid = true; 00883 00884 return getFakeBufferForRecovery(); 00885 } 00886 00887 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc, 00888 Invalid); 00889 } 00890 00891 llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = nullptr) const { 00892 bool MyInvalid = false; 00893 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 00894 if (MyInvalid || !Entry.isFile()) { 00895 if (Invalid) 00896 *Invalid = true; 00897 00898 return getFakeBufferForRecovery(); 00899 } 00900 00901 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, 00902 SourceLocation(), 00903 Invalid); 00904 } 00905 00906 /// \brief Returns the FileEntry record for the provided FileID. 00907 const FileEntry *getFileEntryForID(FileID FID) const { 00908 bool MyInvalid = false; 00909 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 00910 if (MyInvalid || !Entry.isFile()) 00911 return nullptr; 00912 00913 const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache(); 00914 if (!Content) 00915 return nullptr; 00916 return Content->OrigEntry; 00917 } 00918 00919 /// \brief Returns the FileEntry record for the provided SLocEntry. 00920 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const 00921 { 00922 const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache(); 00923 if (!Content) 00924 return nullptr; 00925 return Content->OrigEntry; 00926 } 00927 00928 /// \brief Return a StringRef to the source buffer data for the 00929 /// specified FileID. 00930 /// 00931 /// \param FID The file ID whose contents will be returned. 00932 /// \param Invalid If non-NULL, will be set true if an error occurred. 00933 StringRef getBufferData(FileID FID, bool *Invalid = nullptr) const; 00934 00935 /// \brief Get the number of FileIDs (files and macros) that were created 00936 /// during preprocessing of \p FID, including it. 00937 unsigned getNumCreatedFIDsForFileID(FileID FID) const { 00938 bool Invalid = false; 00939 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 00940 if (Invalid || !Entry.isFile()) 00941 return 0; 00942 00943 return Entry.getFile().NumCreatedFIDs; 00944 } 00945 00946 /// \brief Set the number of FileIDs (files and macros) that were created 00947 /// during preprocessing of \p FID, including it. 00948 void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const { 00949 bool Invalid = false; 00950 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 00951 if (Invalid || !Entry.isFile()) 00952 return; 00953 00954 assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!"); 00955 const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs; 00956 } 00957 00958 //===--------------------------------------------------------------------===// 00959 // SourceLocation manipulation methods. 00960 //===--------------------------------------------------------------------===// 00961 00962 /// \brief Return the FileID for a SourceLocation. 00963 /// 00964 /// This is a very hot method that is used for all SourceManager queries 00965 /// that start with a SourceLocation object. It is responsible for finding 00966 /// the entry in SLocEntryTable which contains the specified location. 00967 /// 00968 FileID getFileID(SourceLocation SpellingLoc) const { 00969 unsigned SLocOffset = SpellingLoc.getOffset(); 00970 00971 // If our one-entry cache covers this offset, just return it. 00972 if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) 00973 return LastFileIDLookup; 00974 00975 return getFileIDSlow(SLocOffset); 00976 } 00977 00978 /// \brief Return the filename of the file containing a SourceLocation. 00979 StringRef getFilename(SourceLocation SpellingLoc) const { 00980 if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc))) 00981 return F->getName(); 00982 return StringRef(); 00983 } 00984 00985 /// \brief Return the source location corresponding to the first byte of 00986 /// the specified file. 00987 SourceLocation getLocForStartOfFile(FileID FID) const { 00988 bool Invalid = false; 00989 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 00990 if (Invalid || !Entry.isFile()) 00991 return SourceLocation(); 00992 00993 unsigned FileOffset = Entry.getOffset(); 00994 return SourceLocation::getFileLoc(FileOffset); 00995 } 00996 00997 /// \brief Return the source location corresponding to the last byte of the 00998 /// specified file. 00999 SourceLocation getLocForEndOfFile(FileID FID) const { 01000 bool Invalid = false; 01001 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 01002 if (Invalid || !Entry.isFile()) 01003 return SourceLocation(); 01004 01005 unsigned FileOffset = Entry.getOffset(); 01006 return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID)); 01007 } 01008 01009 /// \brief Returns the include location if \p FID is a \#include'd file 01010 /// otherwise it returns an invalid location. 01011 SourceLocation getIncludeLoc(FileID FID) const { 01012 bool Invalid = false; 01013 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 01014 if (Invalid || !Entry.isFile()) 01015 return SourceLocation(); 01016 01017 return Entry.getFile().getIncludeLoc(); 01018 } 01019 01020 // \brief Returns the import location if the given source location is 01021 // located within a module, or an invalid location if the source location 01022 // is within the current translation unit. 01023 std::pair<SourceLocation, StringRef> 01024 getModuleImportLoc(SourceLocation Loc) const { 01025 FileID FID = getFileID(Loc); 01026 01027 // Positive file IDs are in the current translation unit, and -1 is a 01028 // placeholder. 01029 if (FID.ID >= -1) 01030 return std::make_pair(SourceLocation(), ""); 01031 01032 return ExternalSLocEntries->getModuleImportLoc(FID.ID); 01033 } 01034 01035 /// \brief Given a SourceLocation object \p Loc, return the expansion 01036 /// location referenced by the ID. 01037 SourceLocation getExpansionLoc(SourceLocation Loc) const { 01038 // Handle the non-mapped case inline, defer to out of line code to handle 01039 // expansions. 01040 if (Loc.isFileID()) return Loc; 01041 return getExpansionLocSlowCase(Loc); 01042 } 01043 01044 /// \brief Given \p Loc, if it is a macro location return the expansion 01045 /// location or the spelling location, depending on if it comes from a 01046 /// macro argument or not. 01047 SourceLocation getFileLoc(SourceLocation Loc) const { 01048 if (Loc.isFileID()) return Loc; 01049 return getFileLocSlowCase(Loc); 01050 } 01051 01052 /// \brief Return the start/end of the expansion information for an 01053 /// expansion location. 01054 /// 01055 /// \pre \p Loc is required to be an expansion location. 01056 std::pair<SourceLocation,SourceLocation> 01057 getImmediateExpansionRange(SourceLocation Loc) const; 01058 01059 /// \brief Given a SourceLocation object, return the range of 01060 /// tokens covered by the expansion the ultimate file. 01061 std::pair<SourceLocation,SourceLocation> 01062 getExpansionRange(SourceLocation Loc) const; 01063 01064 01065 /// \brief Given a SourceLocation object, return the spelling 01066 /// location referenced by the ID. 01067 /// 01068 /// This is the place where the characters that make up the lexed token 01069 /// can be found. 01070 SourceLocation getSpellingLoc(SourceLocation Loc) const { 01071 // Handle the non-mapped case inline, defer to out of line code to handle 01072 // expansions. 01073 if (Loc.isFileID()) return Loc; 01074 return getSpellingLocSlowCase(Loc); 01075 } 01076 01077 /// \brief Given a SourceLocation object, return the spelling location 01078 /// referenced by the ID. 01079 /// 01080 /// This is the first level down towards the place where the characters 01081 /// that make up the lexed token can be found. This should not generally 01082 /// be used by clients. 01083 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; 01084 01085 /// \brief Decompose the specified location into a raw FileID + Offset pair. 01086 /// 01087 /// The first element is the FileID, the second is the offset from the 01088 /// start of the buffer of the location. 01089 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { 01090 FileID FID = getFileID(Loc); 01091 bool Invalid = false; 01092 const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid); 01093 if (Invalid) 01094 return std::make_pair(FileID(), 0); 01095 return std::make_pair(FID, Loc.getOffset()-E.getOffset()); 01096 } 01097 01098 /// \brief Decompose the specified location into a raw FileID + Offset pair. 01099 /// 01100 /// If the location is an expansion record, walk through it until we find 01101 /// the final location expanded. 01102 std::pair<FileID, unsigned> 01103 getDecomposedExpansionLoc(SourceLocation Loc) const { 01104 FileID FID = getFileID(Loc); 01105 bool Invalid = false; 01106 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid); 01107 if (Invalid) 01108 return std::make_pair(FileID(), 0); 01109 01110 unsigned Offset = Loc.getOffset()-E->getOffset(); 01111 if (Loc.isFileID()) 01112 return std::make_pair(FID, Offset); 01113 01114 return getDecomposedExpansionLocSlowCase(E); 01115 } 01116 01117 /// \brief Decompose the specified location into a raw FileID + Offset pair. 01118 /// 01119 /// If the location is an expansion record, walk through it until we find 01120 /// its spelling record. 01121 std::pair<FileID, unsigned> 01122 getDecomposedSpellingLoc(SourceLocation Loc) const { 01123 FileID FID = getFileID(Loc); 01124 bool Invalid = false; 01125 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid); 01126 if (Invalid) 01127 return std::make_pair(FileID(), 0); 01128 01129 unsigned Offset = Loc.getOffset()-E->getOffset(); 01130 if (Loc.isFileID()) 01131 return std::make_pair(FID, Offset); 01132 return getDecomposedSpellingLocSlowCase(E, Offset); 01133 } 01134 01135 /// \brief Returns the "included/expanded in" decomposed location of the given 01136 /// FileID. 01137 std::pair<FileID, unsigned> getDecomposedIncludedLoc(FileID FID) const; 01138 01139 /// \brief Returns the offset from the start of the file that the 01140 /// specified SourceLocation represents. 01141 /// 01142 /// This is not very meaningful for a macro ID. 01143 unsigned getFileOffset(SourceLocation SpellingLoc) const { 01144 return getDecomposedLoc(SpellingLoc).second; 01145 } 01146 01147 /// \brief Tests whether the given source location represents a macro 01148 /// argument's expansion into the function-like macro definition. 01149 /// 01150 /// Such source locations only appear inside of the expansion 01151 /// locations representing where a particular function-like macro was 01152 /// expanded. 01153 bool isMacroArgExpansion(SourceLocation Loc) const; 01154 01155 /// \brief Tests whether the given source location represents the expansion of 01156 /// a macro body. 01157 /// 01158 /// This is equivalent to testing whether the location is part of a macro 01159 /// expansion but not the expansion of an argument to a function-like macro. 01160 bool isMacroBodyExpansion(SourceLocation Loc) const; 01161 01162 /// \brief Returns true if the given MacroID location points at the beginning 01163 /// of the immediate macro expansion. 01164 /// 01165 /// \param MacroBegin If non-null and function returns true, it is set to the 01166 /// begin location of the immediate macro expansion. 01167 bool isAtStartOfImmediateMacroExpansion(SourceLocation Loc, 01168 SourceLocation *MacroBegin = nullptr) const; 01169 01170 /// \brief Returns true if the given MacroID location points at the character 01171 /// end of the immediate macro expansion. 01172 /// 01173 /// \param MacroEnd If non-null and function returns true, it is set to the 01174 /// character end location of the immediate macro expansion. 01175 bool 01176 isAtEndOfImmediateMacroExpansion(SourceLocation Loc, 01177 SourceLocation *MacroEnd = nullptr) const; 01178 01179 /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length) 01180 /// chunk of the source location address space. 01181 /// 01182 /// If it's true and \p RelativeOffset is non-null, it will be set to the 01183 /// relative offset of \p Loc inside the chunk. 01184 bool isInSLocAddrSpace(SourceLocation Loc, 01185 SourceLocation Start, unsigned Length, 01186 unsigned *RelativeOffset = nullptr) const { 01187 assert(((Start.getOffset() < NextLocalOffset && 01188 Start.getOffset()+Length <= NextLocalOffset) || 01189 (Start.getOffset() >= CurrentLoadedOffset && 01190 Start.getOffset()+Length < MaxLoadedOffset)) && 01191 "Chunk is not valid SLoc address space"); 01192 unsigned LocOffs = Loc.getOffset(); 01193 unsigned BeginOffs = Start.getOffset(); 01194 unsigned EndOffs = BeginOffs + Length; 01195 if (LocOffs >= BeginOffs && LocOffs < EndOffs) { 01196 if (RelativeOffset) 01197 *RelativeOffset = LocOffs - BeginOffs; 01198 return true; 01199 } 01200 01201 return false; 01202 } 01203 01204 /// \brief Return true if both \p LHS and \p RHS are in the local source 01205 /// location address space or the loaded one. 01206 /// 01207 /// If it's true and \p RelativeOffset is non-null, it will be set to the 01208 /// offset of \p RHS relative to \p LHS. 01209 bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS, 01210 int *RelativeOffset) const { 01211 unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset(); 01212 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset; 01213 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset; 01214 01215 if (LHSLoaded == RHSLoaded) { 01216 if (RelativeOffset) 01217 *RelativeOffset = RHSOffs - LHSOffs; 01218 return true; 01219 } 01220 01221 return false; 01222 } 01223 01224 //===--------------------------------------------------------------------===// 01225 // Queries about the code at a SourceLocation. 01226 //===--------------------------------------------------------------------===// 01227 01228 /// \brief Return a pointer to the start of the specified location 01229 /// in the appropriate spelling MemoryBuffer. 01230 /// 01231 /// \param Invalid If non-NULL, will be set \c true if an error occurs. 01232 const char *getCharacterData(SourceLocation SL, 01233 bool *Invalid = nullptr) const; 01234 01235 /// \brief Return the column # for the specified file position. 01236 /// 01237 /// This is significantly cheaper to compute than the line number. This 01238 /// returns zero if the column number isn't known. This may only be called 01239 /// on a file sloc, so you must choose a spelling or expansion location 01240 /// before calling this method. 01241 unsigned getColumnNumber(FileID FID, unsigned FilePos, 01242 bool *Invalid = nullptr) const; 01243 unsigned getSpellingColumnNumber(SourceLocation Loc, 01244 bool *Invalid = nullptr) const; 01245 unsigned getExpansionColumnNumber(SourceLocation Loc, 01246 bool *Invalid = nullptr) const; 01247 unsigned getPresumedColumnNumber(SourceLocation Loc, 01248 bool *Invalid = nullptr) const; 01249 01250 /// \brief Given a SourceLocation, return the spelling line number 01251 /// for the position indicated. 01252 /// 01253 /// This requires building and caching a table of line offsets for the 01254 /// MemoryBuffer, so this is not cheap: use only when about to emit a 01255 /// diagnostic. 01256 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = nullptr) const; 01257 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const; 01258 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const; 01259 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const; 01260 01261 /// \brief Return the filename or buffer identifier of the buffer the 01262 /// location is in. 01263 /// 01264 /// Note that this name does not respect \#line directives. Use 01265 /// getPresumedLoc for normal clients. 01266 const char *getBufferName(SourceLocation Loc, bool *Invalid = nullptr) const; 01267 01268 /// \brief Return the file characteristic of the specified source 01269 /// location, indicating whether this is a normal file, a system 01270 /// header, or an "implicit extern C" system header. 01271 /// 01272 /// This state can be modified with flags on GNU linemarker directives like: 01273 /// \code 01274 /// # 4 "foo.h" 3 01275 /// \endcode 01276 /// which changes all source locations in the current file after that to be 01277 /// considered to be from a system header. 01278 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; 01279 01280 /// \brief Returns the "presumed" location of a SourceLocation specifies. 01281 /// 01282 /// A "presumed location" can be modified by \#line or GNU line marker 01283 /// directives. This provides a view on the data that a user should see 01284 /// in diagnostics, for example. 01285 /// 01286 /// Note that a presumed location is always given as the expansion point of 01287 /// an expansion location, not at the spelling location. 01288 /// 01289 /// \returns The presumed location of the specified SourceLocation. If the 01290 /// presumed location cannot be calculated (e.g., because \p Loc is invalid 01291 /// or the file containing \p Loc has changed on disk), returns an invalid 01292 /// presumed location. 01293 PresumedLoc getPresumedLoc(SourceLocation Loc, 01294 bool UseLineDirectives = true) const; 01295 01296 /// \brief Returns whether the PresumedLoc for a given SourceLocation is 01297 /// in the main file. 01298 /// 01299 /// This computes the "presumed" location for a SourceLocation, then checks 01300 /// whether it came from a file other than the main file. This is different 01301 /// from isWrittenInMainFile() because it takes line marker directives into 01302 /// account. 01303 bool isInMainFile(SourceLocation Loc) const; 01304 01305 /// \brief Returns true if the spelling locations for both SourceLocations 01306 /// are part of the same file buffer. 01307 /// 01308 /// This check ignores line marker directives. 01309 bool isWrittenInSameFile(SourceLocation Loc1, SourceLocation Loc2) const { 01310 return getFileID(Loc1) == getFileID(Loc2); 01311 } 01312 01313 /// \brief Returns true if the spelling location for the given location 01314 /// is in the main file buffer. 01315 /// 01316 /// This check ignores line marker directives. 01317 bool isWrittenInMainFile(SourceLocation Loc) const { 01318 return getFileID(Loc) == getMainFileID(); 01319 } 01320 01321 /// \brief Returns if a SourceLocation is in a system header. 01322 bool isInSystemHeader(SourceLocation Loc) const { 01323 return getFileCharacteristic(Loc) != SrcMgr::C_User; 01324 } 01325 01326 /// \brief Returns if a SourceLocation is in an "extern C" system header. 01327 bool isInExternCSystemHeader(SourceLocation Loc) const { 01328 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; 01329 } 01330 01331 /// \brief Returns whether \p Loc is expanded from a macro in a system header. 01332 bool isInSystemMacro(SourceLocation loc) { 01333 return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc)); 01334 } 01335 01336 /// \brief The size of the SLocEntry that \p FID represents. 01337 unsigned getFileIDSize(FileID FID) const; 01338 01339 /// \brief Given a specific FileID, returns true if \p Loc is inside that 01340 /// FileID chunk and sets relative offset (offset of \p Loc from beginning 01341 /// of FileID) to \p relativeOffset. 01342 bool isInFileID(SourceLocation Loc, FileID FID, 01343 unsigned *RelativeOffset = nullptr) const { 01344 unsigned Offs = Loc.getOffset(); 01345 if (isOffsetInFileID(FID, Offs)) { 01346 if (RelativeOffset) 01347 *RelativeOffset = Offs - getSLocEntry(FID).getOffset(); 01348 return true; 01349 } 01350 01351 return false; 01352 } 01353 01354 //===--------------------------------------------------------------------===// 01355 // Line Table Manipulation Routines 01356 //===--------------------------------------------------------------------===// 01357 01358 /// \brief Return the uniqued ID for the specified filename. 01359 /// 01360 unsigned getLineTableFilenameID(StringRef Str); 01361 01362 /// \brief Add a line note to the line table for the FileID and offset 01363 /// specified by Loc. 01364 /// 01365 /// If FilenameID is -1, it is considered to be unspecified. 01366 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); 01367 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, 01368 bool IsFileEntry, bool IsFileExit, 01369 bool IsSystemHeader, bool IsExternCHeader); 01370 01371 /// \brief Determine if the source manager has a line table. 01372 bool hasLineTable() const { return LineTable != nullptr; } 01373 01374 /// \brief Retrieve the stored line table. 01375 LineTableInfo &getLineTable(); 01376 01377 //===--------------------------------------------------------------------===// 01378 // Queries for performance analysis. 01379 //===--------------------------------------------------------------------===// 01380 01381 /// \brief Return the total amount of physical memory allocated by the 01382 /// ContentCache allocator. 01383 size_t getContentCacheSize() const { 01384 return ContentCacheAlloc.getTotalMemory(); 01385 } 01386 01387 struct MemoryBufferSizes { 01388 const size_t malloc_bytes; 01389 const size_t mmap_bytes; 01390 01391 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes) 01392 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {} 01393 }; 01394 01395 /// \brief Return the amount of memory used by memory buffers, breaking down 01396 /// by heap-backed versus mmap'ed memory. 01397 MemoryBufferSizes getMemoryBufferSizes() const; 01398 01399 /// \brief Return the amount of memory used for various side tables and 01400 /// data structures in the SourceManager. 01401 size_t getDataStructureSizes() const; 01402 01403 //===--------------------------------------------------------------------===// 01404 // Other miscellaneous methods. 01405 //===--------------------------------------------------------------------===// 01406 01407 /// \brief Get the source location for the given file:line:col triplet. 01408 /// 01409 /// If the source file is included multiple times, the source location will 01410 /// be based upon the first inclusion. 01411 SourceLocation translateFileLineCol(const FileEntry *SourceFile, 01412 unsigned Line, unsigned Col) const; 01413 01414 /// \brief Get the FileID for the given file. 01415 /// 01416 /// If the source file is included multiple times, the FileID will be the 01417 /// first inclusion. 01418 FileID translateFile(const FileEntry *SourceFile) const; 01419 01420 /// \brief Get the source location in \p FID for the given line:col. 01421 /// Returns null location if \p FID is not a file SLocEntry. 01422 SourceLocation translateLineCol(FileID FID, 01423 unsigned Line, unsigned Col) const; 01424 01425 /// \brief If \p Loc points inside a function macro argument, the returned 01426 /// location will be the macro location in which the argument was expanded. 01427 /// If a macro argument is used multiple times, the expanded location will 01428 /// be at the first expansion of the argument. 01429 /// e.g. 01430 /// MY_MACRO(foo); 01431 /// ^ 01432 /// Passing a file location pointing at 'foo', will yield a macro location 01433 /// where 'foo' was expanded into. 01434 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const; 01435 01436 /// \brief Determines the order of 2 source locations in the translation unit. 01437 /// 01438 /// \returns true if LHS source location comes before RHS, false otherwise. 01439 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; 01440 01441 /// \brief Determines the order of 2 source locations in the "source location 01442 /// address space". 01443 bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const { 01444 return isBeforeInSLocAddrSpace(LHS, RHS.getOffset()); 01445 } 01446 01447 /// \brief Determines the order of a source location and a source location 01448 /// offset in the "source location address space". 01449 /// 01450 /// Note that we always consider source locations loaded from 01451 bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const { 01452 unsigned LHSOffset = LHS.getOffset(); 01453 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset; 01454 bool RHSLoaded = RHS >= CurrentLoadedOffset; 01455 if (LHSLoaded == RHSLoaded) 01456 return LHSOffset < RHS; 01457 01458 return LHSLoaded; 01459 } 01460 01461 // Iterators over FileInfos. 01462 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> 01463 ::const_iterator fileinfo_iterator; 01464 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } 01465 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } 01466 bool hasFileInfo(const FileEntry *File) const { 01467 return FileInfos.find(File) != FileInfos.end(); 01468 } 01469 01470 /// \brief Print statistics to stderr. 01471 /// 01472 void PrintStats() const; 01473 01474 /// \brief Get the number of local SLocEntries we have. 01475 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); } 01476 01477 /// \brief Get a local SLocEntry. This is exposed for indexing. 01478 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index, 01479 bool *Invalid = nullptr) const { 01480 assert(Index < LocalSLocEntryTable.size() && "Invalid index"); 01481 return LocalSLocEntryTable[Index]; 01482 } 01483 01484 /// \brief Get the number of loaded SLocEntries we have. 01485 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();} 01486 01487 /// \brief Get a loaded SLocEntry. This is exposed for indexing. 01488 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, 01489 bool *Invalid = nullptr) const { 01490 assert(Index < LoadedSLocEntryTable.size() && "Invalid index"); 01491 if (SLocEntryLoaded[Index]) 01492 return LoadedSLocEntryTable[Index]; 01493 return loadSLocEntry(Index, Invalid); 01494 } 01495 01496 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, 01497 bool *Invalid = nullptr) const { 01498 if (FID.ID == 0 || FID.ID == -1) { 01499 if (Invalid) *Invalid = true; 01500 return LocalSLocEntryTable[0]; 01501 } 01502 return getSLocEntryByID(FID.ID, Invalid); 01503 } 01504 01505 unsigned getNextLocalOffset() const { return NextLocalOffset; } 01506 01507 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) { 01508 assert(LoadedSLocEntryTable.empty() && 01509 "Invalidating existing loaded entries"); 01510 ExternalSLocEntries = Source; 01511 } 01512 01513 /// \brief Allocate a number of loaded SLocEntries, which will be actually 01514 /// loaded on demand from the external source. 01515 /// 01516 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space 01517 /// in the global source view. The lowest ID and the base offset of the 01518 /// entries will be returned. 01519 std::pair<int, unsigned> 01520 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize); 01521 01522 /// \brief Returns true if \p Loc came from a PCH/Module. 01523 bool isLoadedSourceLocation(SourceLocation Loc) const { 01524 return Loc.getOffset() >= CurrentLoadedOffset; 01525 } 01526 01527 /// \brief Returns true if \p Loc did not come from a PCH/Module. 01528 bool isLocalSourceLocation(SourceLocation Loc) const { 01529 return Loc.getOffset() < NextLocalOffset; 01530 } 01531 01532 /// \brief Returns true if \p FID came from a PCH/Module. 01533 bool isLoadedFileID(FileID FID) const { 01534 assert(FID.ID != -1 && "Using FileID sentinel value"); 01535 return FID.ID < 0; 01536 } 01537 01538 /// \brief Returns true if \p FID did not come from a PCH/Module. 01539 bool isLocalFileID(FileID FID) const { 01540 return !isLoadedFileID(FID); 01541 } 01542 01543 /// Gets the location of the immediate macro caller, one level up the stack 01544 /// toward the initial macro typed into the source. 01545 SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const { 01546 if (!Loc.isMacroID()) return Loc; 01547 01548 // When we have the location of (part of) an expanded parameter, its 01549 // spelling location points to the argument as expanded in the macro call, 01550 // and therefore is used to locate the macro caller. 01551 if (isMacroArgExpansion(Loc)) 01552 return getImmediateSpellingLoc(Loc); 01553 01554 // Otherwise, the caller of the macro is located where this macro is 01555 // expanded (while the spelling is part of the macro definition). 01556 return getImmediateExpansionRange(Loc).first; 01557 } 01558 01559 private: 01560 llvm::MemoryBuffer *getFakeBufferForRecovery() const; 01561 const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const; 01562 01563 const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const; 01564 01565 /// \brief Get the entry with the given unwrapped FileID. 01566 const SrcMgr::SLocEntry &getSLocEntryByID(int ID, 01567 bool *Invalid = nullptr) const { 01568 assert(ID != -1 && "Using FileID sentinel value"); 01569 if (ID < 0) 01570 return getLoadedSLocEntryByID(ID, Invalid); 01571 return getLocalSLocEntry(static_cast<unsigned>(ID), Invalid); 01572 } 01573 01574 const SrcMgr::SLocEntry & 01575 getLoadedSLocEntryByID(int ID, bool *Invalid = nullptr) const { 01576 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid); 01577 } 01578 01579 /// Implements the common elements of storing an expansion info struct into 01580 /// the SLocEntry table and producing a source location that refers to it. 01581 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion, 01582 unsigned TokLength, 01583 int LoadedID = 0, 01584 unsigned LoadedOffset = 0); 01585 01586 /// \brief Return true if the specified FileID contains the 01587 /// specified SourceLocation offset. This is a very hot method. 01588 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { 01589 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); 01590 // If the entry is after the offset, it can't contain it. 01591 if (SLocOffset < Entry.getOffset()) return false; 01592 01593 // If this is the very last entry then it does. 01594 if (FID.ID == -2) 01595 return true; 01596 01597 // If it is the last local entry, then it does if the location is local. 01598 if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size())) 01599 return SLocOffset < NextLocalOffset; 01600 01601 // Otherwise, the entry after it has to not include it. This works for both 01602 // local and loaded entries. 01603 return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset(); 01604 } 01605 01606 /// \brief Returns the previous in-order FileID or an invalid FileID if there 01607 /// is no previous one. 01608 FileID getPreviousFileID(FileID FID) const; 01609 01610 /// \brief Returns the next in-order FileID or an invalid FileID if there is 01611 /// no next one. 01612 FileID getNextFileID(FileID FID) const; 01613 01614 /// \brief Create a new fileID for the specified ContentCache and 01615 /// include position. 01616 /// 01617 /// This works regardless of whether the ContentCache corresponds to a 01618 /// file or some other input source. 01619 FileID createFileID(const SrcMgr::ContentCache* File, 01620 SourceLocation IncludePos, 01621 SrcMgr::CharacteristicKind DirCharacter, 01622 int LoadedID, unsigned LoadedOffset); 01623 01624 const SrcMgr::ContentCache * 01625 getOrCreateContentCache(const FileEntry *SourceFile, 01626 bool isSystemFile = false); 01627 01628 /// \brief Create a new ContentCache for the specified memory buffer. 01629 const SrcMgr::ContentCache * 01630 createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf); 01631 01632 FileID getFileIDSlow(unsigned SLocOffset) const; 01633 FileID getFileIDLocal(unsigned SLocOffset) const; 01634 FileID getFileIDLoaded(unsigned SLocOffset) const; 01635 01636 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; 01637 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; 01638 SourceLocation getFileLocSlowCase(SourceLocation Loc) const; 01639 01640 std::pair<FileID, unsigned> 01641 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const; 01642 std::pair<FileID, unsigned> 01643 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 01644 unsigned Offset) const; 01645 void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const; 01646 void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache, 01647 FileID FID, 01648 SourceLocation SpellLoc, 01649 SourceLocation ExpansionLoc, 01650 unsigned ExpansionLength) const; 01651 friend class ASTReader; 01652 friend class ASTWriter; 01653 }; 01654 01655 /// \brief Comparison function object. 01656 template<typename T> 01657 class BeforeThanCompare; 01658 01659 /// \brief Compare two source locations. 01660 template<> 01661 class BeforeThanCompare<SourceLocation> { 01662 SourceManager &SM; 01663 01664 public: 01665 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { } 01666 01667 bool operator()(SourceLocation LHS, SourceLocation RHS) const { 01668 return SM.isBeforeInTranslationUnit(LHS, RHS); 01669 } 01670 }; 01671 01672 /// \brief Compare two non-overlapping source ranges. 01673 template<> 01674 class BeforeThanCompare<SourceRange> { 01675 SourceManager &SM; 01676 01677 public: 01678 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { } 01679 01680 bool operator()(SourceRange LHS, SourceRange RHS) const { 01681 return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin()); 01682 } 01683 }; 01684 01685 } // end namespace clang 01686 01687 01688 #endif