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