SourceManager.h revision a1f1fad8b60e1cb9d21a40a37f2e03150bcbeb6f
1d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===// 2d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// 3d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// The LLVM Compiler Infrastructure 4d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// 5d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// This file is distributed under the University of Illinois Open Source 6d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// License. See LICENSE.TXT for details. 7d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===----------------------------------------------------------------------===// 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This file defines the SourceManager interface. 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 12d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch//===----------------------------------------------------------------------===// 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef LLVM_CLANG_SOURCEMANAGER_H 151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LLVM_CLANG_SOURCEMANAGER_H 16d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "clang/Basic/LLVM.h" 18d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "clang/Basic/SourceLocation.h" 19d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "llvm/Support/Allocator.h" 20d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "llvm/Support/DataTypes.h" 21d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "llvm/ADT/PointerIntPair.h" 22d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "llvm/ADT/PointerUnion.h" 23d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "llvm/ADT/IntrusiveRefCntPtr.h" 24d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "llvm/ADT/DenseMap.h" 25d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "llvm/Support/MemoryBuffer.h" 26d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include <map> 27d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <vector> 28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <cassert> 291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 30d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochnamespace clang { 31d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class DiagnosticsEngine; 334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class SourceManager; 344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class FileManager; 354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class FileEntry; 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class LineTableInfo; 374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class LangOptions; 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ASTWriter; 39d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass ASTReader; 40d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 41d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// There are three different types of locations in a file: a spelling 42d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// location, an expansion location, and a presumed location. 43d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// 44d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// Given an example of: 45d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// #define min(x, y) x < y ? x : y 46d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// 47d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// and then later on a use of min: 48d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// #line 17 49d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// return min(a, b); 50d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// 51d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// The expansion location is the line in the source code where the macro 52d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// was expanded (the return statement), the spelling location is the 536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)/// location in the source where the macro was originally defined, 546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)/// and the presumed location is where the line directive states that 55d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch/// the line is 17, or any other line. 56d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 57d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)/// SrcMgr - Public enums and private classes that are part of the 58ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch/// SourceManager implementation. 59ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch/// 60ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochnamespace SrcMgr { 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// CharacteristicKind - This is used to represent whether a file or directory 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// holds normal user code, system code, or system code which is implicitly 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// 'extern "C"' in C++ mode. Entire directories can be tagged with this 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// (this is maintained by DirectoryLookup and friends) as can specific 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// FileInfos when a #pragma system_header is seen or various other cases. 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) enum CharacteristicKind { 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) C_User, C_System, C_ExternCSystem 69ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch }; 70ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch 71ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch /// ContentCache - One instance of this struct is kept for every file 72d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// loaded or used. This object owns the MemoryBuffer object. 73d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch class ContentCache { 74d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch enum CCFlags { 75d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// \brief Whether the buffer is invalid. 76d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch InvalidFlag = 0x01, 77d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// \brief Whether the buffer should not be freed on destruction. 78d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch DoNotFreeFlag = 0x02 79d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch }; 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// Buffer - The actual buffer containing the characters from the input 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// file. This is owned by the ContentCache object. 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// The bits indicate indicates whether the buffer is invalid. 84d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer; 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public: 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// Reference to the file entry representing this ContentCache. 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// This reference does not own the FileEntry object. 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// It is possible for this to be NULL if 90d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// the ContentCache encapsulates an imaginary text buffer. 91d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch const FileEntry *OrigEntry; 92d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 93d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// \brief References the file which the contents were actually loaded from. 94d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// Can be different from 'Entry' if we overridden the contents of one file 956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) /// with the contents of another file. 96d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch const FileEntry *ContentsEntry; 97d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 98d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// SourceLineCache - A bump pointer allocated array of offsets for each 99d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// source line. This is lazily computed. This is owned by the 100d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) /// SourceManager BumpPointerAllocator object. 101d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) unsigned *SourceLineCache; 102ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// NumLines - The number of lines in this ContentCache. This is only valid 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// if SourceLineCache is non-null. 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned NumLines : 31; 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// \brief Indicates whether the buffer itself was provided to override 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// the actual file contents. 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// When true, the original entry may be a virtual file that does not 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// exist. 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned BufferOverridden : 1; 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ContentCache(const FileEntry *Ent = 0) 115d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent), 116d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch SourceLineCache(0), NumLines(0), BufferOverridden(false) {} 117d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci ContentCache(const FileEntry *Ent, const FileEntry *contentEnt) 1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt), 1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci SourceLineCache(0), NumLines(0), BufferOverridden(false) {} 1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci ~ContentCache(); 1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 124d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// The copy ctor does not allow copies where source object has either 125d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory 126d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// is not transferred, so this is a logical error. 1270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) ContentCache(const ContentCache &RHS) 128d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch : Buffer(0, false), SourceLineCache(0), BufferOverridden(false) 129d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch { 130d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch OrigEntry = RHS.OrigEntry; 1310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) ContentsEntry = RHS.ContentsEntry; 1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 && 134d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch "Passed ContentCache object cannot own a buffer."); 1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) NumLines = RHS.NumLines; 1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// getBuffer - Returns the memory buffer for the associated content. 140d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) /// 141d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch /// \param Diag Object through which diagnostics will be emitted if the 142 /// buffer cannot be retrieved. 143 /// 144 /// \param Loc If specified, is the location that invalid file diagnostics 145 /// will be emitted at. 146 /// 147 /// \param Invalid If non-NULL, will be set \c true if an error occurred. 148 const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag, 149 const SourceManager &SM, 150 SourceLocation Loc = SourceLocation(), 151 bool *Invalid = 0) const; 152 153 /// getSize - Returns the size of the content encapsulated by this 154 /// ContentCache. This can be the size of the source file or the size of an 155 /// arbitrary scratch buffer. If the ContentCache encapsulates a source 156 /// file this size is retrieved from the file's FileEntry. 157 unsigned getSize() const; 158 159 /// getSizeBytesMapped - Returns the number of bytes actually mapped for 160 /// this ContentCache. This can be 0 if the MemBuffer was not actually 161 /// expanded. 162 unsigned getSizeBytesMapped() const; 163 164 /// Returns the kind of memory used to back the memory buffer for 165 /// this content cache. This is used for performance analysis. 166 llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const; 167 168 void setBuffer(const llvm::MemoryBuffer *B) { 169 assert(!Buffer.getPointer() && "MemoryBuffer already set."); 170 Buffer.setPointer(B); 171 Buffer.setInt(false); 172 } 173 174 /// \brief Get the underlying buffer, returning NULL if the buffer is not 175 /// yet available. 176 const llvm::MemoryBuffer *getRawBuffer() const { 177 return Buffer.getPointer(); 178 } 179 180 /// \brief Replace the existing buffer (which will be deleted) 181 /// with the given buffer. 182 void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false); 183 184 /// \brief Determine whether the buffer itself is invalid. 185 bool isBufferInvalid() const { 186 return Buffer.getInt() & InvalidFlag; 187 } 188 189 /// \brief Determine whether the buffer should be freed. 190 bool shouldFreeBuffer() const { 191 return (Buffer.getInt() & DoNotFreeFlag) == 0; 192 } 193 194 private: 195 // Disable assignments. 196 ContentCache &operator=(const ContentCache& RHS); 197 }; 198 199 /// FileInfo - Information about a FileID, basically just the logical file 200 /// that it represents and include stack information. 201 /// 202 /// Each FileInfo has include stack information, indicating where it came 203 /// from. This information encodes the #include chain that a token was 204 /// expanded from. The main include file has an invalid IncludeLoc. 205 /// 206 /// FileInfos contain a "ContentCache *", with the contents of the file. 207 /// 208 class FileInfo { 209 /// IncludeLoc - The location of the #include that brought in this file. 210 /// This is an invalid SLOC for the main file (top of the #include chain). 211 unsigned IncludeLoc; // Really a SourceLocation 212 213 /// \brief Number of FileIDs (files and macros) that were created during 214 /// preprocessing of this #include, including this SLocEntry. 215 /// Zero means the preprocessor didn't provide such info for this SLocEntry. 216 unsigned NumCreatedFIDs; 217 218 /// Data - This contains the ContentCache* and the bits indicating the 219 /// characteristic of the file and whether it has #line info, all bitmangled 220 /// together. 221 uintptr_t Data; 222 223 friend class clang::SourceManager; 224 friend class clang::ASTWriter; 225 friend class clang::ASTReader; 226 public: 227 /// get - Return a FileInfo object. 228 static FileInfo get(SourceLocation IL, const ContentCache *Con, 229 CharacteristicKind FileCharacter) { 230 FileInfo X; 231 X.IncludeLoc = IL.getRawEncoding(); 232 X.NumCreatedFIDs = 0; 233 X.Data = (uintptr_t)Con; 234 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned"); 235 assert((unsigned)FileCharacter < 4 && "invalid file character"); 236 X.Data |= (unsigned)FileCharacter; 237 return X; 238 } 239 240 SourceLocation getIncludeLoc() const { 241 return SourceLocation::getFromRawEncoding(IncludeLoc); 242 } 243 const ContentCache* getContentCache() const { 244 return reinterpret_cast<const ContentCache*>(Data & ~7UL); 245 } 246 247 /// getCharacteristic - Return whether this is a system header or not. 248 CharacteristicKind getFileCharacteristic() const { 249 return (CharacteristicKind)(Data & 3); 250 } 251 252 /// hasLineDirectives - Return true if this FileID has #line directives in 253 /// it. 254 bool hasLineDirectives() const { return (Data & 4) != 0; } 255 256 /// setHasLineDirectives - Set the flag that indicates that this FileID has 257 /// line table entries associated with it. 258 void setHasLineDirectives() { 259 Data |= 4; 260 } 261 }; 262 263 /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where 264 /// the token was ultimately expanded, and the SpellingLoc - where the actual 265 /// character data for the token came from. 266 class ExpansionInfo { 267 // Really these are all SourceLocations. 268 269 /// SpellingLoc - Where the spelling for the token can be found. 270 unsigned SpellingLoc; 271 272 /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these 273 /// indicate the start and end of the expansion. In object-like macros, 274 /// these will be the same. In a function-like macro expansion, the start 275 /// will be the identifier and the end will be the ')'. Finally, in 276 /// macro-argument instantitions, the end will be 'SourceLocation()', an 277 /// invalid location. 278 unsigned ExpansionLocStart, ExpansionLocEnd; 279 280 public: 281 SourceLocation getSpellingLoc() const { 282 return SourceLocation::getFromRawEncoding(SpellingLoc); 283 } 284 SourceLocation getExpansionLocStart() const { 285 return SourceLocation::getFromRawEncoding(ExpansionLocStart); 286 } 287 SourceLocation getExpansionLocEnd() const { 288 SourceLocation EndLoc = 289 SourceLocation::getFromRawEncoding(ExpansionLocEnd); 290 return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc; 291 } 292 293 std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const { 294 return std::make_pair(getExpansionLocStart(), getExpansionLocEnd()); 295 } 296 297 bool isMacroArgExpansion() const { 298 // Note that this needs to return false for default constructed objects. 299 return getExpansionLocStart().isValid() && 300 SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid(); 301 } 302 303 bool isFunctionMacroExpansion() const { 304 return getExpansionLocStart().isValid() && 305 getExpansionLocStart() != getExpansionLocEnd(); 306 } 307 308 /// create - Return a ExpansionInfo for an expansion. Start and End specify 309 /// the expansion range (where the macro is expanded), and SpellingLoc 310 /// specifies the spelling location (where the characters from the token 311 /// come from). All three can refer to normal File SLocs or expansion 312 /// locations. 313 static ExpansionInfo create(SourceLocation SpellingLoc, 314 SourceLocation Start, SourceLocation End) { 315 ExpansionInfo X; 316 X.SpellingLoc = SpellingLoc.getRawEncoding(); 317 X.ExpansionLocStart = Start.getRawEncoding(); 318 X.ExpansionLocEnd = End.getRawEncoding(); 319 return X; 320 } 321 322 /// createForMacroArg - Return a special ExpansionInfo for the expansion of 323 /// a macro argument into a function-like macro's body. ExpansionLoc 324 /// specifies the expansion location (where the macro is expanded). This 325 /// doesn't need to be a range because a macro is always expanded at 326 /// a macro parameter reference, and macro parameters are always exactly 327 /// one token. SpellingLoc specifies the spelling location (where the 328 /// characters from the token come from). ExpansionLoc and SpellingLoc can 329 /// both refer to normal File SLocs or expansion locations. 330 /// 331 /// Given the code: 332 /// \code 333 /// #define F(x) f(x) 334 /// F(42); 335 /// \endcode 336 /// 337 /// When expanding '\c F(42)', the '\c x' would call this with an 338 /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its 339 /// location in the definition of '\c F'. 340 static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc, 341 SourceLocation ExpansionLoc) { 342 // We store an intentionally invalid source location for the end of the 343 // expansion range to mark that this is a macro argument ion rather than 344 // a normal one. 345 return create(SpellingLoc, ExpansionLoc, SourceLocation()); 346 } 347 }; 348 349 /// SLocEntry - This is a discriminated union of FileInfo and 350 /// ExpansionInfo. SourceManager keeps an array of these objects, and 351 /// they are uniquely identified by the FileID datatype. 352 class SLocEntry { 353 unsigned Offset; // low bit is set for expansion info. 354 union { 355 FileInfo File; 356 ExpansionInfo Expansion; 357 }; 358 public: 359 unsigned getOffset() const { return Offset >> 1; } 360 361 bool isExpansion() const { return Offset & 1; } 362 bool isFile() const { return !isExpansion(); } 363 364 const FileInfo &getFile() const { 365 assert(isFile() && "Not a file SLocEntry!"); 366 return File; 367 } 368 369 const ExpansionInfo &getExpansion() const { 370 assert(isExpansion() && "Not a macro expansion SLocEntry!"); 371 return Expansion; 372 } 373 374 static SLocEntry get(unsigned Offset, const FileInfo &FI) { 375 SLocEntry E; 376 E.Offset = Offset << 1; 377 E.File = FI; 378 return E; 379 } 380 381 static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) { 382 SLocEntry E; 383 E.Offset = (Offset << 1) | 1; 384 E.Expansion = Expansion; 385 return E; 386 } 387 }; 388} // end SrcMgr namespace. 389 390/// \brief External source of source location entries. 391class ExternalSLocEntrySource { 392public: 393 virtual ~ExternalSLocEntrySource(); 394 395 /// \brief Read the source location entry with index ID, which will always be 396 /// less than -1. 397 /// 398 /// \returns true if an error occurred that prevented the source-location 399 /// entry from being loaded. 400 virtual bool ReadSLocEntry(int ID) = 0; 401}; 402 403 404/// IsBeforeInTranslationUnitCache - This class holds the cache used by 405/// isBeforeInTranslationUnit. The cache structure is complex enough to be 406/// worth breaking out of SourceManager. 407class IsBeforeInTranslationUnitCache { 408 /// L/R QueryFID - These are the FID's of the cached query. If these match up 409 /// with a subsequent query, the result can be reused. 410 FileID LQueryFID, RQueryFID; 411 412 /// \brief True if LQueryFID was created before RQueryFID. This is used 413 /// to compare macro expansion locations. 414 bool IsLQFIDBeforeRQFID; 415 416 /// CommonFID - This is the file found in common between the two #include 417 /// traces. It is the nearest common ancestor of the #include tree. 418 FileID CommonFID; 419 420 /// L/R CommonOffset - This is the offset of the previous query in CommonFID. 421 /// Usually, this represents the location of the #include for QueryFID, but if 422 /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a 423 /// random token in the parent. 424 unsigned LCommonOffset, RCommonOffset; 425public: 426 427 /// isCacheValid - Return true if the currently cached values match up with 428 /// the specified LHS/RHS query. If not, we can't use the cache. 429 bool isCacheValid(FileID LHS, FileID RHS) const { 430 return LQueryFID == LHS && RQueryFID == RHS; 431 } 432 433 /// getCachedResult - If the cache is valid, compute the result given the 434 /// specified offsets in the LHS/RHS FID's. 435 bool getCachedResult(unsigned LOffset, unsigned ROffset) const { 436 // If one of the query files is the common file, use the offset. Otherwise, 437 // use the #include loc in the common file. 438 if (LQueryFID != CommonFID) LOffset = LCommonOffset; 439 if (RQueryFID != CommonFID) ROffset = RCommonOffset; 440 441 // It is common for multiple macro expansions to be "included" from the same 442 // location (expansion location), in which case use the order of the FileIDs 443 // to determine which came first. This will also take care the case where 444 // one of the locations points at the inclusion/expansion point of the other 445 // in which case its FileID will come before the other. 446 if (LOffset == ROffset) 447 return IsLQFIDBeforeRQFID; 448 449 return LOffset < ROffset; 450 } 451 452 // Set up a new query. 453 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) { 454 assert(LHS != RHS); 455 LQueryFID = LHS; 456 RQueryFID = RHS; 457 IsLQFIDBeforeRQFID = isLFIDBeforeRFID; 458 } 459 460 void clear() { 461 LQueryFID = RQueryFID = FileID(); 462 IsLQFIDBeforeRQFID = false; 463 } 464 465 void setCommonLoc(FileID commonFID, unsigned lCommonOffset, 466 unsigned rCommonOffset) { 467 CommonFID = commonFID; 468 LCommonOffset = lCommonOffset; 469 RCommonOffset = rCommonOffset; 470 } 471 472}; 473 474/// \brief This class handles loading and caching of source files into memory. 475/// 476/// This object owns the MemoryBuffer objects for all of the loaded 477/// files and assigns unique FileID's for each unique #include chain. 478/// 479/// The SourceManager can be queried for information about SourceLocation 480/// objects, turning them into either spelling or expansion locations. Spelling 481/// locations represent where the bytes corresponding to a token came from and 482/// expansion locations represent where the location is in the user's view. In 483/// the case of a macro expansion, for example, the spelling location indicates 484/// where the expanded token came from and the expansion location specifies 485/// where it was expanded. 486class SourceManager : public llvm::RefCountedBase<SourceManager> { 487 /// \brief DiagnosticsEngine object. 488 DiagnosticsEngine &Diag; 489 490 FileManager &FileMgr; 491 492 mutable llvm::BumpPtrAllocator ContentCacheAlloc; 493 494 /// FileInfos - Memoized information about all of the files tracked by this 495 /// SourceManager. This set allows us to merge ContentCache entries based 496 /// on their FileEntry*. All ContentCache objects will thus have unique, 497 /// non-null, FileEntry pointers. 498 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; 499 500 /// \brief True if the ContentCache for files that are overriden by other 501 /// files, should report the original file name. Defaults to true. 502 bool OverridenFilesKeepOriginalName; 503 504 /// \brief Files that have been overriden with the contents from another file. 505 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles; 506 507 /// MemBufferInfos - Information about various memory buffers that we have 508 /// read in. All FileEntry* within the stored ContentCache objects are NULL, 509 /// as they do not refer to a file. 510 std::vector<SrcMgr::ContentCache*> MemBufferInfos; 511 512 /// \brief The table of SLocEntries that are local to this module. 513 /// 514 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid 515 /// expansion. 516 std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable; 517 518 /// \brief The table of SLocEntries that are loaded from other modules. 519 /// 520 /// Negative FileIDs are indexes into this table. To get from ID to an index, 521 /// use (-ID - 2). 522 std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable; 523 524 /// \brief The starting offset of the next local SLocEntry. 525 /// 526 /// This is LocalSLocEntryTable.back().Offset + the size of that entry. 527 unsigned NextLocalOffset; 528 529 /// \brief The starting offset of the latest batch of loaded SLocEntries. 530 /// 531 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might 532 /// not have been loaded, so that value would be unknown. 533 unsigned CurrentLoadedOffset; 534 535 /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset 536 /// starts at 2^31. 537 static const unsigned MaxLoadedOffset = 1U << 31U; 538 539 /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable 540 /// have already been loaded from the external source. 541 /// 542 /// Same indexing as LoadedSLocEntryTable. 543 std::vector<bool> SLocEntryLoaded; 544 545 /// \brief An external source for source location entries. 546 ExternalSLocEntrySource *ExternalSLocEntries; 547 548 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID. 549 /// LastFileIDLookup records the last FileID looked up or created, because it 550 /// is very common to look up many tokens from the same file. 551 mutable FileID LastFileIDLookup; 552 553 /// LineTable - This holds information for #line directives. It is referenced 554 /// by indices from SLocEntryTable. 555 LineTableInfo *LineTable; 556 557 /// LastLineNo - These ivars serve as a cache used in the getLineNumber 558 /// method which is used to speedup getLineNumber calls to nearby locations. 559 mutable FileID LastLineNoFileIDQuery; 560 mutable SrcMgr::ContentCache *LastLineNoContentCache; 561 mutable unsigned LastLineNoFilePos; 562 mutable unsigned LastLineNoResult; 563 564 /// MainFileID - The file ID for the main source file of the translation unit. 565 FileID MainFileID; 566 567 /// \brief The file ID for the precompiled preamble there is one. 568 FileID PreambleFileID; 569 570 // Statistics for -print-stats. 571 mutable unsigned NumLinearScans, NumBinaryProbes; 572 573 // Cache results for the isBeforeInTranslationUnit method. 574 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache; 575 576 // Cache for the "fake" buffer used for error-recovery purposes. 577 mutable llvm::MemoryBuffer *FakeBufferForRecovery; 578 579 /// \brief Lazily computed map of macro argument chunks to their expanded 580 /// source location. 581 typedef std::map<unsigned, SourceLocation> MacroArgsMap; 582 583 mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap; 584 585 // SourceManager doesn't support copy construction. 586 explicit SourceManager(const SourceManager&); 587 void operator=(const SourceManager&); 588public: 589 SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr); 590 ~SourceManager(); 591 592 void clearIDTables(); 593 594 DiagnosticsEngine &getDiagnostics() const { return Diag; } 595 596 FileManager &getFileManager() const { return FileMgr; } 597 598 /// \brief Set true if the SourceManager should report the original file name 599 /// for contents of files that were overriden by other files.Defaults to true. 600 void setOverridenFilesKeepOriginalName(bool value) { 601 OverridenFilesKeepOriginalName = value; 602 } 603 604 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer 605 /// that will represent the FileID for the main source. One example 606 /// of when this would be used is when the main source is read from STDIN. 607 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { 608 assert(MainFileID.isInvalid() && "MainFileID already set!"); 609 MainFileID = createFileIDForMemBuffer(Buffer); 610 return MainFileID; 611 } 612 613 //===--------------------------------------------------------------------===// 614 // MainFileID creation and querying methods. 615 //===--------------------------------------------------------------------===// 616 617 /// getMainFileID - Returns the FileID of the main source file. 618 FileID getMainFileID() const { return MainFileID; } 619 620 /// createMainFileID - Create the FileID for the main source file. 621 FileID createMainFileID(const FileEntry *SourceFile, 622 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) { 623 assert(MainFileID.isInvalid() && "MainFileID already set!"); 624 MainFileID = createFileID(SourceFile, SourceLocation(), Kind); 625 return MainFileID; 626 } 627 628 /// \brief Set the file ID for the main source file. 629 void setMainFileID(FileID FID) { 630 assert(MainFileID.isInvalid() && "MainFileID already set!"); 631 MainFileID = FID; 632 } 633 634 /// \brief Set the file ID for the precompiled preamble. 635 void setPreambleFileID(FileID Preamble) { 636 assert(PreambleFileID.isInvalid() && "PreambleFileID already set!"); 637 PreambleFileID = Preamble; 638 } 639 640 /// \brief Get the file ID for the precompiled preamble if there is one. 641 FileID getPreambleFileID() const { return PreambleFileID; } 642 643 //===--------------------------------------------------------------------===// 644 // Methods to create new FileID's and macro expansions. 645 //===--------------------------------------------------------------------===// 646 647 /// createFileID - Create a new FileID that represents the specified file 648 /// being #included from the specified IncludePosition. This translates NULL 649 /// into standard input. 650 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, 651 SrcMgr::CharacteristicKind FileCharacter, 652 int LoadedID = 0, unsigned LoadedOffset = 0) { 653 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); 654 assert(IR && "getOrCreateContentCache() cannot return NULL"); 655 return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset); 656 } 657 658 /// createFileIDForMemBuffer - Create a new FileID that represents the 659 /// specified memory buffer. This does no caching of the buffer and takes 660 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. 661 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer, 662 int LoadedID = 0, unsigned LoadedOffset = 0, 663 SourceLocation IncludeLoc = SourceLocation()) { 664 return createFileID(createMemBufferContentCache(Buffer), IncludeLoc, 665 SrcMgr::C_User, LoadedID, LoadedOffset); 666 } 667 668 /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the 669 /// fact that a token from SpellingLoc should actually be referenced from 670 /// ExpansionLoc, and that it represents the expansion of a macro argument 671 /// into the function-like macro body. 672 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc, 673 SourceLocation ExpansionLoc, 674 unsigned TokLength); 675 676 /// createExpansionLoc - Return a new SourceLocation that encodes the fact 677 /// that a token from SpellingLoc should actually be referenced from 678 /// ExpansionLoc. 679 SourceLocation createExpansionLoc(SourceLocation Loc, 680 SourceLocation ExpansionLocStart, 681 SourceLocation ExpansionLocEnd, 682 unsigned TokLength, 683 int LoadedID = 0, 684 unsigned LoadedOffset = 0); 685 686 /// \brief Retrieve the memory buffer associated with the given file. 687 /// 688 /// \param Invalid If non-NULL, will be set \c true if an error 689 /// occurs while retrieving the memory buffer. 690 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File, 691 bool *Invalid = 0); 692 693 /// \brief Override the contents of the given source file by providing an 694 /// already-allocated buffer. 695 /// 696 /// \param SourceFile the source file whose contents will be overriden. 697 /// 698 /// \param Buffer the memory buffer whose contents will be used as the 699 /// data in the given source file. 700 /// 701 /// \param DoNotFree If true, then the buffer will not be freed when the 702 /// source manager is destroyed. 703 void overrideFileContents(const FileEntry *SourceFile, 704 const llvm::MemoryBuffer *Buffer, 705 bool DoNotFree = false); 706 707 /// \brief Override the the given source file with another one. 708 /// 709 /// \param SourceFile the source file which will be overriden. 710 /// 711 /// \param NewFile the file whose contents will be used as the 712 /// data instead of the contents of the given source file. 713 void overrideFileContents(const FileEntry *SourceFile, 714 const FileEntry *NewFile); 715 716 //===--------------------------------------------------------------------===// 717 // FileID manipulation methods. 718 //===--------------------------------------------------------------------===// 719 720 /// getBuffer - Return the buffer for the specified FileID. If there is an 721 /// error opening this buffer the first time, this manufactures a temporary 722 /// buffer and returns a non-empty error string. 723 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc, 724 bool *Invalid = 0) const { 725 bool MyInvalid = false; 726 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 727 if (MyInvalid || !Entry.isFile()) { 728 if (Invalid) 729 *Invalid = true; 730 731 return getFakeBufferForRecovery(); 732 } 733 734 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc, 735 Invalid); 736 } 737 738 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const { 739 bool MyInvalid = false; 740 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 741 if (MyInvalid || !Entry.isFile()) { 742 if (Invalid) 743 *Invalid = true; 744 745 return getFakeBufferForRecovery(); 746 } 747 748 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, 749 SourceLocation(), 750 Invalid); 751 } 752 753 /// getFileEntryForID - Returns the FileEntry record for the provided FileID. 754 const FileEntry *getFileEntryForID(FileID FID) const { 755 bool MyInvalid = false; 756 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 757 if (MyInvalid || !Entry.isFile()) 758 return 0; 759 760 const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache(); 761 if (!Content) 762 return 0; 763 return Content->OrigEntry; 764 } 765 766 /// Returns the FileEntry record for the provided SLocEntry. 767 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const 768 { 769 const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache(); 770 if (!Content) 771 return 0; 772 return Content->OrigEntry; 773 } 774 775 /// getBufferData - Return a StringRef to the source buffer data for the 776 /// specified FileID. 777 /// 778 /// \param FID The file ID whose contents will be returned. 779 /// \param Invalid If non-NULL, will be set true if an error occurred. 780 StringRef getBufferData(FileID FID, bool *Invalid = 0) const; 781 782 /// \brief Get the number of FileIDs (files and macros) that were created 783 /// during preprocessing of \p FID, including it. 784 unsigned getNumCreatedFIDsForFileID(FileID FID) const { 785 bool Invalid = false; 786 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 787 if (Invalid || !Entry.isFile()) 788 return 0; 789 790 return Entry.getFile().NumCreatedFIDs; 791 } 792 793 /// \brief Set the number of FileIDs (files and macros) that were created 794 /// during preprocessing of \p FID, including it. 795 void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const { 796 bool Invalid = false; 797 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 798 if (Invalid || !Entry.isFile()) 799 return; 800 801 assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!"); 802 const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs; 803 } 804 805 //===--------------------------------------------------------------------===// 806 // SourceLocation manipulation methods. 807 //===--------------------------------------------------------------------===// 808 809 /// getFileID - Return the FileID for a SourceLocation. This is a very 810 /// hot method that is used for all SourceManager queries that start with a 811 /// SourceLocation object. It is responsible for finding the entry in 812 /// SLocEntryTable which contains the specified location. 813 /// 814 FileID getFileID(SourceLocation SpellingLoc) const { 815 unsigned SLocOffset = SpellingLoc.getOffset(); 816 817 // If our one-entry cache covers this offset, just return it. 818 if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) 819 return LastFileIDLookup; 820 821 return getFileIDSlow(SLocOffset); 822 } 823 824 /// getLocForStartOfFile - Return the source location corresponding to the 825 /// first byte of the specified file. 826 SourceLocation getLocForStartOfFile(FileID FID) const { 827 bool Invalid = false; 828 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 829 if (Invalid || !Entry.isFile()) 830 return SourceLocation(); 831 832 unsigned FileOffset = Entry.getOffset(); 833 return SourceLocation::getFileLoc(FileOffset); 834 } 835 836 /// \brief Return the source location corresponding to the last byte of the 837 /// specified file. 838 SourceLocation getLocForEndOfFile(FileID FID) const { 839 bool Invalid = false; 840 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 841 if (Invalid || !Entry.isFile()) 842 return SourceLocation(); 843 844 unsigned FileOffset = Entry.getOffset(); 845 return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1); 846 } 847 848 /// \brief Returns the include location if \p FID is a #include'd file 849 /// otherwise it returns an invalid location. 850 SourceLocation getIncludeLoc(FileID FID) const { 851 bool Invalid = false; 852 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 853 if (Invalid || !Entry.isFile()) 854 return SourceLocation(); 855 856 return Entry.getFile().getIncludeLoc(); 857 } 858 859 /// getExpansionLoc - Given a SourceLocation object, return the expansion 860 /// location referenced by the ID. 861 SourceLocation getExpansionLoc(SourceLocation Loc) const { 862 // Handle the non-mapped case inline, defer to out of line code to handle 863 // expansions. 864 if (Loc.isFileID()) return Loc; 865 return getExpansionLocSlowCase(Loc); 866 } 867 868 /// \brief Given \p Loc, if it is a macro location return the expansion 869 /// location or the spelling location, depending on if it comes from a 870 /// macro argument or not. 871 SourceLocation getFileLoc(SourceLocation Loc) const { 872 if (Loc.isFileID()) return Loc; 873 return getFileLocSlowCase(Loc); 874 } 875 876 /// getImmediateExpansionRange - Loc is required to be an expansion location. 877 /// Return the start/end of the expansion information. 878 std::pair<SourceLocation,SourceLocation> 879 getImmediateExpansionRange(SourceLocation Loc) const; 880 881 /// getExpansionRange - Given a SourceLocation object, return the range of 882 /// tokens covered by the expansion the ultimate file. 883 std::pair<SourceLocation,SourceLocation> 884 getExpansionRange(SourceLocation Loc) const; 885 886 887 /// getSpellingLoc - Given a SourceLocation object, return the spelling 888 /// location referenced by the ID. This is the place where the characters 889 /// that make up the lexed token can be found. 890 SourceLocation getSpellingLoc(SourceLocation Loc) const { 891 // Handle the non-mapped case inline, defer to out of line code to handle 892 // expansions. 893 if (Loc.isFileID()) return Loc; 894 return getSpellingLocSlowCase(Loc); 895 } 896 897 /// getImmediateSpellingLoc - Given a SourceLocation object, return the 898 /// spelling location referenced by the ID. This is the first level down 899 /// towards the place where the characters that make up the lexed token can be 900 /// found. This should not generally be used by clients. 901 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; 902 903 /// getDecomposedLoc - Decompose the specified location into a raw FileID + 904 /// Offset pair. The first element is the FileID, the second is the 905 /// offset from the start of the buffer of the location. 906 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { 907 FileID FID = getFileID(Loc); 908 bool Invalid = false; 909 const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid); 910 if (Invalid) 911 return std::make_pair(FileID(), 0); 912 return std::make_pair(FID, Loc.getOffset()-E.getOffset()); 913 } 914 915 /// getDecomposedExpansionLoc - Decompose the specified location into a raw 916 /// FileID + Offset pair. If the location is an expansion record, walk 917 /// through it until we find the final location expanded. 918 std::pair<FileID, unsigned> 919 getDecomposedExpansionLoc(SourceLocation Loc) const { 920 FileID FID = getFileID(Loc); 921 bool Invalid = false; 922 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid); 923 if (Invalid) 924 return std::make_pair(FileID(), 0); 925 926 unsigned Offset = Loc.getOffset()-E->getOffset(); 927 if (Loc.isFileID()) 928 return std::make_pair(FID, Offset); 929 930 return getDecomposedExpansionLocSlowCase(E); 931 } 932 933 /// getDecomposedSpellingLoc - Decompose the specified location into a raw 934 /// FileID + Offset pair. If the location is an expansion record, walk 935 /// through it until we find its spelling record. 936 std::pair<FileID, unsigned> 937 getDecomposedSpellingLoc(SourceLocation Loc) const { 938 FileID FID = getFileID(Loc); 939 bool Invalid = false; 940 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid); 941 if (Invalid) 942 return std::make_pair(FileID(), 0); 943 944 unsigned Offset = Loc.getOffset()-E->getOffset(); 945 if (Loc.isFileID()) 946 return std::make_pair(FID, Offset); 947 return getDecomposedSpellingLocSlowCase(E, Offset); 948 } 949 950 /// getFileOffset - This method returns the offset from the start 951 /// of the file that the specified SourceLocation represents. This is not very 952 /// meaningful for a macro ID. 953 unsigned getFileOffset(SourceLocation SpellingLoc) const { 954 return getDecomposedLoc(SpellingLoc).second; 955 } 956 957 /// isMacroArgExpansion - This method tests whether the given source location 958 /// represents a macro argument's expansion into the function-like macro 959 /// definition. Such source locations only appear inside of the expansion 960 /// locations representing where a particular function-like macro was 961 /// expanded. 962 bool isMacroArgExpansion(SourceLocation Loc) const; 963 964 /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length) 965 /// chunk of the source location address space. 966 /// If it's true and \p RelativeOffset is non-null, it will be set to the 967 /// relative offset of \p Loc inside the chunk. 968 bool isInSLocAddrSpace(SourceLocation Loc, 969 SourceLocation Start, unsigned Length, 970 unsigned *RelativeOffset = 0) const { 971 assert(((Start.getOffset() < NextLocalOffset && 972 Start.getOffset()+Length <= NextLocalOffset) || 973 (Start.getOffset() >= CurrentLoadedOffset && 974 Start.getOffset()+Length < MaxLoadedOffset)) && 975 "Chunk is not valid SLoc address space"); 976 unsigned LocOffs = Loc.getOffset(); 977 unsigned BeginOffs = Start.getOffset(); 978 unsigned EndOffs = BeginOffs + Length; 979 if (LocOffs >= BeginOffs && LocOffs < EndOffs) { 980 if (RelativeOffset) 981 *RelativeOffset = LocOffs - BeginOffs; 982 return true; 983 } 984 985 return false; 986 } 987 988 /// \brief Return true if both \p LHS and \p RHS are in the local source 989 /// location address space or the loaded one. If it's true and \p 990 /// RelativeOffset is non-null, it will be set to the offset of \p RHS 991 /// relative to \p LHS. 992 bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS, 993 int *RelativeOffset) const { 994 unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset(); 995 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset; 996 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset; 997 998 if (LHSLoaded == RHSLoaded) { 999 if (RelativeOffset) 1000 *RelativeOffset = RHSOffs - LHSOffs; 1001 return true; 1002 } 1003 1004 return false; 1005 } 1006 1007 //===--------------------------------------------------------------------===// 1008 // Queries about the code at a SourceLocation. 1009 //===--------------------------------------------------------------------===// 1010 1011 /// getCharacterData - Return a pointer to the start of the specified location 1012 /// in the appropriate spelling MemoryBuffer. 1013 /// 1014 /// \param Invalid If non-NULL, will be set \c true if an error occurs. 1015 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const; 1016 1017 /// getColumnNumber - Return the column # for the specified file position. 1018 /// This is significantly cheaper to compute than the line number. This 1019 /// returns zero if the column number isn't known. This may only be called 1020 /// on a file sloc, so you must choose a spelling or expansion location 1021 /// before calling this method. 1022 unsigned getColumnNumber(FileID FID, unsigned FilePos, 1023 bool *Invalid = 0) const; 1024 unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; 1025 unsigned getExpansionColumnNumber(SourceLocation Loc, 1026 bool *Invalid = 0) const; 1027 unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; 1028 1029 1030 /// getLineNumber - Given a SourceLocation, return the spelling line number 1031 /// for the position indicated. This requires building and caching a table of 1032 /// line offsets for the MemoryBuffer, so this is not cheap: use only when 1033 /// about to emit a diagnostic. 1034 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const; 1035 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 1036 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 1037 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 1038 1039 /// Return the filename or buffer identifier of the buffer the location is in. 1040 /// Note that this name does not respect #line directives. Use getPresumedLoc 1041 /// for normal clients. 1042 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const; 1043 1044 /// getFileCharacteristic - return the file characteristic of the specified 1045 /// source location, indicating whether this is a normal file, a system 1046 /// header, or an "implicit extern C" system header. 1047 /// 1048 /// This state can be modified with flags on GNU linemarker directives like: 1049 /// # 4 "foo.h" 3 1050 /// which changes all source locations in the current file after that to be 1051 /// considered to be from a system header. 1052 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; 1053 1054 /// getPresumedLoc - This method returns the "presumed" location of a 1055 /// SourceLocation specifies. A "presumed location" can be modified by #line 1056 /// or GNU line marker directives. This provides a view on the data that a 1057 /// user should see in diagnostics, for example. 1058 /// 1059 /// Note that a presumed location is always given as the expansion point of 1060 /// an expansion location, not at the spelling location. 1061 /// 1062 /// \returns The presumed location of the specified SourceLocation. If the 1063 /// presumed location cannot be calculate (e.g., because \p Loc is invalid 1064 /// or the file containing \p Loc has changed on disk), returns an invalid 1065 /// presumed location. 1066 PresumedLoc getPresumedLoc(SourceLocation Loc) const; 1067 1068 /// isFromSameFile - Returns true if both SourceLocations correspond to 1069 /// the same file. 1070 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { 1071 return getFileID(Loc1) == getFileID(Loc2); 1072 } 1073 1074 /// isFromMainFile - Returns true if the file of provided SourceLocation is 1075 /// the main file. 1076 bool isFromMainFile(SourceLocation Loc) const { 1077 return getFileID(Loc) == getMainFileID(); 1078 } 1079 1080 /// isInSystemHeader - Returns if a SourceLocation is in a system header. 1081 bool isInSystemHeader(SourceLocation Loc) const { 1082 return getFileCharacteristic(Loc) != SrcMgr::C_User; 1083 } 1084 1085 /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C" 1086 /// system header. 1087 bool isInExternCSystemHeader(SourceLocation Loc) const { 1088 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; 1089 } 1090 1091 /// \brief Returns whether \p Loc is expanded from a macro in a system header. 1092 bool isInSystemMacro(SourceLocation loc) { 1093 return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc)); 1094 } 1095 1096 /// \brief The size of the SLocEnty that \p FID represents. 1097 unsigned getFileIDSize(FileID FID) const; 1098 1099 /// \brief Given a specific FileID, returns true if \p Loc is inside that 1100 /// FileID chunk and sets relative offset (offset of \p Loc from beginning 1101 /// of FileID) to \p relativeOffset. 1102 bool isInFileID(SourceLocation Loc, FileID FID, 1103 unsigned *RelativeOffset = 0) const { 1104 unsigned Offs = Loc.getOffset(); 1105 if (isOffsetInFileID(FID, Offs)) { 1106 if (RelativeOffset) 1107 *RelativeOffset = Offs - getSLocEntry(FID).getOffset(); 1108 return true; 1109 } 1110 1111 return false; 1112 } 1113 1114 //===--------------------------------------------------------------------===// 1115 // Line Table Manipulation Routines 1116 //===--------------------------------------------------------------------===// 1117 1118 /// getLineTableFilenameID - Return the uniqued ID for the specified filename. 1119 /// 1120 unsigned getLineTableFilenameID(StringRef Str); 1121 1122 /// AddLineNote - Add a line note to the line table for the FileID and offset 1123 /// specified by Loc. If FilenameID is -1, it is considered to be 1124 /// unspecified. 1125 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); 1126 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, 1127 bool IsFileEntry, bool IsFileExit, 1128 bool IsSystemHeader, bool IsExternCHeader); 1129 1130 /// \brief Determine if the source manager has a line table. 1131 bool hasLineTable() const { return LineTable != 0; } 1132 1133 /// \brief Retrieve the stored line table. 1134 LineTableInfo &getLineTable(); 1135 1136 //===--------------------------------------------------------------------===// 1137 // Queries for performance analysis. 1138 //===--------------------------------------------------------------------===// 1139 1140 /// Return the total amount of physical memory allocated by the 1141 /// ContentCache allocator. 1142 size_t getContentCacheSize() const { 1143 return ContentCacheAlloc.getTotalMemory(); 1144 } 1145 1146 struct MemoryBufferSizes { 1147 const size_t malloc_bytes; 1148 const size_t mmap_bytes; 1149 1150 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes) 1151 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {} 1152 }; 1153 1154 /// Return the amount of memory used by memory buffers, breaking down 1155 /// by heap-backed versus mmap'ed memory. 1156 MemoryBufferSizes getMemoryBufferSizes() const; 1157 1158 // Return the amount of memory used for various side tables and 1159 // data structures in the SourceManager. 1160 size_t getDataStructureSizes() const; 1161 1162 //===--------------------------------------------------------------------===// 1163 // Other miscellaneous methods. 1164 //===--------------------------------------------------------------------===// 1165 1166 /// \brief Get the source location for the given file:line:col triplet. 1167 /// 1168 /// If the source file is included multiple times, the source location will 1169 /// be based upon the first inclusion. 1170 SourceLocation translateFileLineCol(const FileEntry *SourceFile, 1171 unsigned Line, unsigned Col) const; 1172 1173 /// \brief Get the FileID for the given file. 1174 /// 1175 /// If the source file is included multiple times, the FileID will be the 1176 /// first inclusion. 1177 FileID translateFile(const FileEntry *SourceFile) const; 1178 1179 /// \brief Get the source location in \p FID for the given line:col. 1180 /// Returns null location if \p FID is not a file SLocEntry. 1181 SourceLocation translateLineCol(FileID FID, 1182 unsigned Line, unsigned Col) const; 1183 1184 /// \brief If \p Loc points inside a function macro argument, the returned 1185 /// location will be the macro location in which the argument was expanded. 1186 /// If a macro argument is used multiple times, the expanded location will 1187 /// be at the first expansion of the argument. 1188 /// e.g. 1189 /// MY_MACRO(foo); 1190 /// ^ 1191 /// Passing a file location pointing at 'foo', will yield a macro location 1192 /// where 'foo' was expanded into. 1193 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const; 1194 1195 /// \brief Determines the order of 2 source locations in the translation unit. 1196 /// 1197 /// \returns true if LHS source location comes before RHS, false otherwise. 1198 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; 1199 1200 /// \brief Comparison function class. 1201 class LocBeforeThanCompare : public std::binary_function<SourceLocation, 1202 SourceLocation, bool> { 1203 SourceManager &SM; 1204 1205 public: 1206 explicit LocBeforeThanCompare(SourceManager &SM) : SM(SM) { } 1207 1208 bool operator()(SourceLocation LHS, SourceLocation RHS) const { 1209 return SM.isBeforeInTranslationUnit(LHS, RHS); 1210 } 1211 }; 1212 1213 /// \brief Determines the order of 2 source locations in the "source location 1214 /// address space". 1215 bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const { 1216 return isBeforeInSLocAddrSpace(LHS, RHS.getOffset()); 1217 } 1218 1219 /// \brief Determines the order of a source location and a source location 1220 /// offset in the "source location address space". 1221 /// 1222 /// Note that we always consider source locations loaded from 1223 bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const { 1224 unsigned LHSOffset = LHS.getOffset(); 1225 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset; 1226 bool RHSLoaded = RHS >= CurrentLoadedOffset; 1227 if (LHSLoaded == RHSLoaded) 1228 return LHSOffset < RHS; 1229 1230 return LHSLoaded; 1231 } 1232 1233 // Iterators over FileInfos. 1234 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> 1235 ::const_iterator fileinfo_iterator; 1236 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } 1237 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } 1238 bool hasFileInfo(const FileEntry *File) const { 1239 return FileInfos.find(File) != FileInfos.end(); 1240 } 1241 1242 /// PrintStats - Print statistics to stderr. 1243 /// 1244 void PrintStats() const; 1245 1246 /// \brief Get the number of local SLocEntries we have. 1247 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); } 1248 1249 /// \brief Get a local SLocEntry. This is exposed for indexing. 1250 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index, 1251 bool *Invalid = 0) const { 1252 assert(Index < LocalSLocEntryTable.size() && "Invalid index"); 1253 return LocalSLocEntryTable[Index]; 1254 } 1255 1256 /// \brief Get the number of loaded SLocEntries we have. 1257 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();} 1258 1259 /// \brief Get a loaded SLocEntry. This is exposed for indexing. 1260 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, 1261 bool *Invalid = 0) const { 1262 assert(Index < LoadedSLocEntryTable.size() && "Invalid index"); 1263 if (!SLocEntryLoaded[Index]) 1264 ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2)); 1265 return LoadedSLocEntryTable[Index]; 1266 } 1267 1268 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const { 1269 if (FID.ID == 0 || FID.ID == -1) { 1270 if (Invalid) *Invalid = true; 1271 return LocalSLocEntryTable[0]; 1272 } 1273 return getSLocEntryByID(FID.ID); 1274 } 1275 1276 unsigned getNextLocalOffset() const { return NextLocalOffset; } 1277 1278 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) { 1279 assert(LoadedSLocEntryTable.empty() && 1280 "Invalidating existing loaded entries"); 1281 ExternalSLocEntries = Source; 1282 } 1283 1284 /// \brief Allocate a number of loaded SLocEntries, which will be actually 1285 /// loaded on demand from the external source. 1286 /// 1287 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space 1288 /// in the global source view. The lowest ID and the base offset of the 1289 /// entries will be returned. 1290 std::pair<int, unsigned> 1291 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize); 1292 1293 /// \brief Returns true if \p Loc came from a PCH/Module. 1294 bool isLoadedSourceLocation(SourceLocation Loc) const { 1295 return Loc.getOffset() >= CurrentLoadedOffset; 1296 } 1297 1298 /// \brief Returns true if \p Loc did not come from a PCH/Module. 1299 bool isLocalSourceLocation(SourceLocation Loc) const { 1300 return Loc.getOffset() < NextLocalOffset; 1301 } 1302 1303 /// \brief Returns true if \p FID came from a PCH/Module. 1304 bool isLoadedFileID(FileID FID) const { 1305 assert(FID.ID != -1 && "Using FileID sentinel value"); 1306 return FID.ID < 0; 1307 } 1308 1309 /// \brief Returns true if \p FID did not come from a PCH/Module. 1310 bool isLocalFileID(FileID FID) const { 1311 return !isLoadedFileID(FID); 1312 } 1313 1314private: 1315 const llvm::MemoryBuffer *getFakeBufferForRecovery() const; 1316 1317 /// \brief Get the entry with the given unwrapped FileID. 1318 const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const { 1319 assert(ID != -1 && "Using FileID sentinel value"); 1320 if (ID < 0) 1321 return getLoadedSLocEntryByID(ID); 1322 return getLocalSLocEntry(static_cast<unsigned>(ID)); 1323 } 1324 1325 const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const { 1326 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2)); 1327 } 1328 1329 /// createExpansionLoc - Implements the common elements of storing an 1330 /// expansion info struct into the SLocEntry table and producing a source 1331 /// location that refers to it. 1332 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion, 1333 unsigned TokLength, 1334 int LoadedID = 0, 1335 unsigned LoadedOffset = 0); 1336 1337 /// isOffsetInFileID - Return true if the specified FileID contains the 1338 /// specified SourceLocation offset. This is a very hot method. 1339 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { 1340 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); 1341 // If the entry is after the offset, it can't contain it. 1342 if (SLocOffset < Entry.getOffset()) return false; 1343 1344 // If this is the very last entry then it does. 1345 if (FID.ID == -2) 1346 return true; 1347 1348 // If it is the last local entry, then it does if the location is local. 1349 if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) { 1350 return SLocOffset < NextLocalOffset; 1351 } 1352 1353 // Otherwise, the entry after it has to not include it. This works for both 1354 // local and loaded entries. 1355 return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset(); 1356 } 1357 1358 /// createFileID - Create a new fileID for the specified ContentCache and 1359 /// include position. This works regardless of whether the ContentCache 1360 /// corresponds to a file or some other input source. 1361 FileID createFileID(const SrcMgr::ContentCache* File, 1362 SourceLocation IncludePos, 1363 SrcMgr::CharacteristicKind DirCharacter, 1364 int LoadedID, unsigned LoadedOffset); 1365 1366 const SrcMgr::ContentCache * 1367 getOrCreateContentCache(const FileEntry *SourceFile); 1368 1369 /// createMemBufferContentCache - Create a new ContentCache for the specified 1370 /// memory buffer. 1371 const SrcMgr::ContentCache* 1372 createMemBufferContentCache(const llvm::MemoryBuffer *Buf); 1373 1374 FileID getFileIDSlow(unsigned SLocOffset) const; 1375 FileID getFileIDLocal(unsigned SLocOffset) const; 1376 FileID getFileIDLoaded(unsigned SLocOffset) const; 1377 1378 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; 1379 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; 1380 SourceLocation getFileLocSlowCase(SourceLocation Loc) const; 1381 1382 std::pair<FileID, unsigned> 1383 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const; 1384 std::pair<FileID, unsigned> 1385 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 1386 unsigned Offset) const; 1387 void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const; 1388 1389 friend class ASTReader; 1390 friend class ASTWriter; 1391}; 1392 1393 1394} // end namespace clang 1395 1396#endif 1397