Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SourceManager.cpp - Track and cache source files -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SourceManager interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/SourceManager.h" |
| 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "llvm/Support/Compiler.h" |
| 17 | #include "llvm/Support/MemoryBuffer.h" |
| 18 | #include "llvm/System/Path.h" |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 19 | #include "llvm/Bitcode/Serialize.h" |
| 20 | #include "llvm/Bitcode/Deserialize.h" |
Ted Kremenek | da29d8c | 2007-12-05 22:21:13 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Streams.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace SrcMgr; |
| 25 | using llvm::MemoryBuffer; |
| 26 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 27 | //===--------------------------------------------------------------------===// |
| 28 | // SourceManager Helper Classes |
| 29 | //===--------------------------------------------------------------------===// |
| 30 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 31 | ContentCache::~ContentCache() { |
| 32 | delete Buffer; |
| 33 | delete [] SourceLineCache; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 36 | /// getSizeBytesMapped - Returns the number of bytes actually mapped for |
| 37 | /// this ContentCache. This can be 0 if the MemBuffer was not actually |
| 38 | /// instantiated. |
| 39 | unsigned ContentCache::getSizeBytesMapped() const { |
| 40 | return Buffer ? Buffer->getBufferSize() : 0; |
| 41 | } |
| 42 | |
| 43 | /// getSize - Returns the size of the content encapsulated by this ContentCache. |
| 44 | /// This can be the size of the source file or the size of an arbitrary |
| 45 | /// scratch buffer. If the ContentCache encapsulates a source file, that |
| 46 | /// file is not lazily brought in from disk to satisfy this query. |
| 47 | unsigned ContentCache::getSize() const { |
| 48 | return Entry ? Entry->getSize() : Buffer->getBufferSize(); |
| 49 | } |
| 50 | |
Chris Lattner | 68b28ff | 2009-01-26 07:37:49 +0000 | [diff] [blame^] | 51 | const llvm::MemoryBuffer *ContentCache::getBuffer() const { |
Ted Kremenek | 2bb9e6c | 2009-01-06 22:43:04 +0000 | [diff] [blame] | 52 | // Lazily create the Buffer for ContentCaches that wrap files. |
| 53 | if (!Buffer && Entry) { |
| 54 | // FIXME: Should we support a way to not have to do this check over |
| 55 | // and over if we cannot open the file? |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 56 | Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize()); |
Ted Kremenek | 2bb9e6c | 2009-01-06 22:43:04 +0000 | [diff] [blame] | 57 | } |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 58 | return Buffer; |
| 59 | } |
| 60 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 61 | //===--------------------------------------------------------------------===// |
| 62 | // Private 'Create' methods. |
| 63 | //===--------------------------------------------------------------------===// |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 65 | /// getOrCreateContentCache - Create or return a cached ContentCache for the |
| 66 | /// specified file. |
| 67 | const ContentCache * |
| 68 | SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 69 | assert(FileEnt && "Didn't specify a file entry to use?"); |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 71 | // Do we already have information about this file? |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 72 | std::set<ContentCache>::iterator I = |
| 73 | FileInfos.lower_bound(ContentCache(FileEnt)); |
| 74 | |
| 75 | if (I != FileInfos.end() && I->Entry == FileEnt) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 76 | return &*I; |
| 77 | |
Chris Lattner | 68b28ff | 2009-01-26 07:37:49 +0000 | [diff] [blame^] | 78 | // Nope, create a new Cache entry. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 79 | ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt)); |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 80 | Entry.SourceLineCache = 0; |
| 81 | Entry.NumLines = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 82 | return &Entry; |
| 83 | } |
| 84 | |
| 85 | |
Ted Kremenek | 27f9c9b | 2007-10-31 17:53:38 +0000 | [diff] [blame] | 86 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 87 | /// memory buffer. This does no caching. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 88 | const ContentCache* |
| 89 | SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) { |
Ted Kremenek | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 90 | // Add a new ContentCache to the MemBufferInfos list and return it. We |
| 91 | // must default construct the object first that the instance actually |
| 92 | // stored within MemBufferInfos actually owns the Buffer, and not any |
| 93 | // temporary we would use in the call to "push_back". |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 94 | MemBufferInfos.push_back(ContentCache()); |
| 95 | ContentCache& Entry = const_cast<ContentCache&>(MemBufferInfos.back()); |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 96 | Entry.setBuffer(Buffer); |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 97 | return &Entry; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 100 | //===----------------------------------------------------------------------===// |
| 101 | // Methods to create new FileID's and instantiations. |
| 102 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 103 | |
Nico Weber | 630347d | 2008-09-29 00:25:48 +0000 | [diff] [blame] | 104 | /// createFileID - Create a new fileID for the specified ContentCache and |
Ted Kremenek | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 105 | /// include position. This works regardless of whether the ContentCache |
| 106 | /// corresponds to a file or some other input source. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 107 | FileID SourceManager::createFileID(const ContentCache *File, |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 108 | SourceLocation IncludePos, |
| 109 | SrcMgr::CharacteristicKind FileCharacter) { |
| 110 | SLocEntryTable.push_back(SLocEntry::get(NextOffset, |
| 111 | FileInfo::get(IncludePos, File, |
| 112 | FileCharacter))); |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 113 | unsigned FileSize = File->getSize(); |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 114 | assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!"); |
| 115 | NextOffset += FileSize+1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 117 | // Set LastFileIDLookup to the newly created file. The next getFileID call is |
| 118 | // almost guaranteed to be from that file. |
| 119 | return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 122 | /// createInstantiationLoc - Return a new SourceLocation that encodes the fact |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 123 | /// that a token from SpellingLoc should actually be referenced from |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 124 | /// InstantiationLoc. |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 125 | SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc, |
| 126 | SourceLocation InstantLoc, |
| 127 | unsigned TokLength) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 128 | // The specified source location may be a mapped location, due to a macro |
| 129 | // instantiation or #line directive. Strip off this information to find out |
| 130 | // where the characters are actually located. |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 131 | SpellingLoc = getSpellingLoc(SpellingLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 133 | // Resolve InstantLoc down to a real instantiation location. |
| 134 | InstantLoc = getInstantiationLoc(InstantLoc); |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 135 | |
| 136 | SLocEntryTable.push_back(SLocEntry::get(NextOffset, |
| 137 | InstantiationInfo::get(InstantLoc, |
| 138 | SpellingLoc))); |
| 139 | assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!"); |
| 140 | NextOffset += TokLength+1; |
| 141 | return SourceLocation::getMacroLoc(NextOffset-(TokLength+1)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | 71e443a | 2009-01-19 07:32:13 +0000 | [diff] [blame] | 144 | /// getBufferData - Return a pointer to the start and end of the source buffer |
| 145 | /// data for the specified FileID. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 146 | std::pair<const char*, const char*> |
| 147 | SourceManager::getBufferData(FileID FID) const { |
| 148 | const llvm::MemoryBuffer *Buf = getBuffer(FID); |
| 149 | return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd()); |
| 150 | } |
| 151 | |
| 152 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 153 | //===--------------------------------------------------------------------===// |
| 154 | // SourceLocation manipulation methods. |
| 155 | //===--------------------------------------------------------------------===// |
| 156 | |
| 157 | /// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot |
| 158 | /// method that is used for all SourceManager queries that start with a |
| 159 | /// SourceLocation object. It is responsible for finding the entry in |
| 160 | /// SLocEntryTable which contains the specified location. |
| 161 | /// |
| 162 | FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { |
| 163 | assert(SLocOffset && "Invalid FileID"); |
| 164 | |
| 165 | // After the first and second level caches, I see two common sorts of |
| 166 | // behavior: 1) a lot of searched FileID's are "near" the cached file location |
| 167 | // or are "near" the cached instantiation location. 2) others are just |
| 168 | // completely random and may be a very long way away. |
| 169 | // |
| 170 | // To handle this, we do a linear search for up to 8 steps to catch #1 quickly |
| 171 | // then we fall back to a less cache efficient, but more scalable, binary |
| 172 | // search to find the location. |
| 173 | |
| 174 | // See if this is near the file point - worst case we start scanning from the |
| 175 | // most newly created FileID. |
| 176 | std::vector<SrcMgr::SLocEntry>::const_iterator I; |
| 177 | |
| 178 | if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { |
| 179 | // Neither loc prunes our search. |
| 180 | I = SLocEntryTable.end(); |
| 181 | } else { |
| 182 | // Perhaps it is near the file point. |
| 183 | I = SLocEntryTable.begin()+LastFileIDLookup.ID; |
| 184 | } |
| 185 | |
| 186 | // Find the FileID that contains this. "I" is an iterator that points to a |
| 187 | // FileID whose offset is known to be larger than SLocOffset. |
| 188 | unsigned NumProbes = 0; |
| 189 | while (1) { |
| 190 | --I; |
| 191 | if (I->getOffset() <= SLocOffset) { |
| 192 | #if 0 |
| 193 | printf("lin %d -> %d [%s] %d %d\n", SLocOffset, |
| 194 | I-SLocEntryTable.begin(), |
| 195 | I->isInstantiation() ? "inst" : "file", |
| 196 | LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); |
| 197 | #endif |
| 198 | FileID Res = FileID::get(I-SLocEntryTable.begin()); |
| 199 | |
| 200 | // If this isn't an instantiation, remember it. We have good locality |
| 201 | // across FileID lookups. |
| 202 | if (!I->isInstantiation()) |
| 203 | LastFileIDLookup = Res; |
| 204 | NumLinearScans += NumProbes+1; |
| 205 | return Res; |
| 206 | } |
| 207 | if (++NumProbes == 8) |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | // Convert "I" back into an index. We know that it is an entry whose index is |
| 212 | // larger than the offset we are looking for. |
| 213 | unsigned GreaterIndex = I-SLocEntryTable.begin(); |
| 214 | // LessIndex - This is the lower bound of the range that we're searching. |
| 215 | // We know that the offset corresponding to the FileID is is less than |
| 216 | // SLocOffset. |
| 217 | unsigned LessIndex = 0; |
| 218 | NumProbes = 0; |
| 219 | while (1) { |
| 220 | unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; |
| 221 | unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset(); |
| 222 | |
| 223 | ++NumProbes; |
| 224 | |
| 225 | // If the offset of the midpoint is too large, chop the high side of the |
| 226 | // range to the midpoint. |
| 227 | if (MidOffset > SLocOffset) { |
| 228 | GreaterIndex = MiddleIndex; |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | // If the middle index contains the value, succeed and return. |
| 233 | if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { |
| 234 | #if 0 |
| 235 | printf("bin %d -> %d [%s] %d %d\n", SLocOffset, |
| 236 | I-SLocEntryTable.begin(), |
| 237 | I->isInstantiation() ? "inst" : "file", |
| 238 | LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); |
| 239 | #endif |
| 240 | FileID Res = FileID::get(MiddleIndex); |
| 241 | |
| 242 | // If this isn't an instantiation, remember it. We have good locality |
| 243 | // across FileID lookups. |
| 244 | if (!I->isInstantiation()) |
| 245 | LastFileIDLookup = Res; |
| 246 | NumBinaryProbes += NumProbes; |
| 247 | return Res; |
| 248 | } |
| 249 | |
| 250 | // Otherwise, move the low-side up to the middle index. |
| 251 | LessIndex = MiddleIndex; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | std::pair<FileID, unsigned> |
| 256 | SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, |
| 257 | unsigned Offset) const { |
| 258 | // If this is an instantiation record, walk through all the instantiation |
| 259 | // points. |
| 260 | FileID FID; |
| 261 | SourceLocation Loc; |
| 262 | do { |
| 263 | Loc = E->getInstantiation().getInstantiationLoc(); |
| 264 | |
| 265 | FID = getFileID(Loc); |
| 266 | E = &getSLocEntry(FID); |
| 267 | Offset += Loc.getOffset()-E->getOffset(); |
| 268 | } while (Loc.isFileID()); |
| 269 | |
| 270 | return std::make_pair(FID, Offset); |
| 271 | } |
| 272 | |
| 273 | std::pair<FileID, unsigned> |
| 274 | SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, |
| 275 | unsigned Offset) const { |
| 276 | // If this is an instantiation record, get and return the spelling. |
| 277 | SourceLocation Loc = E->getInstantiation().getSpellingLoc(); |
| 278 | FileID FID = getFileID(Loc); |
| 279 | E = &getSLocEntry(FID); |
| 280 | Offset += Loc.getOffset()-E->getOffset(); |
| 281 | assert(Loc.isFileID() && "Should only have one spelling link"); |
| 282 | return std::make_pair(FID, Offset); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | // Queries about the code at a SourceLocation. |
| 288 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 289 | |
| 290 | /// getCharacterData - Return a pointer to the start of the specified location |
| 291 | /// in the appropriate MemoryBuffer. |
| 292 | const char *SourceManager::getCharacterData(SourceLocation SL) const { |
| 293 | // Note that this is a hot function in the getSpelling() path, which is |
| 294 | // heavily used by -E mode. |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 295 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 297 | // Note that calling 'getBuffer()' may lazily page in a source file. |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 298 | return getSLocEntry(LocInfo.first).getFile().getContentCache() |
| 299 | ->getBuffer()->getBufferStart() + LocInfo.second; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | |
| 303 | /// getColumnNumber - Return the column # for the specified file position. |
| 304 | /// this is significantly cheaper to compute than the line number. This returns |
| 305 | /// zero if the column number isn't known. |
| 306 | unsigned SourceManager::getColumnNumber(SourceLocation Loc) const { |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 307 | if (Loc.isInvalid()) return 0; |
| 308 | assert(Loc.isFileID() && "Don't know what part of instantiation loc to get"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 310 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 311 | unsigned FilePos = LocInfo.second; |
| 312 | |
| 313 | const char *Buf = getBuffer(LocInfo.first)->getBufferStart(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 314 | |
| 315 | unsigned LineStart = FilePos; |
| 316 | while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') |
| 317 | --LineStart; |
| 318 | return FilePos-LineStart+1; |
| 319 | } |
| 320 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 321 | static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE; |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 322 | static void ComputeLineNumbers(ContentCache* FI) { |
| 323 | // Note that calling 'getBuffer()' may lazily page in the file. |
| 324 | const MemoryBuffer *Buffer = FI->getBuffer(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 325 | |
| 326 | // Find the file offsets of all of the *physical* source lines. This does |
| 327 | // not look at trigraphs, escaped newlines, or anything else tricky. |
| 328 | std::vector<unsigned> LineOffsets; |
| 329 | |
| 330 | // Line #1 starts at char 0. |
| 331 | LineOffsets.push_back(0); |
| 332 | |
| 333 | const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); |
| 334 | const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); |
| 335 | unsigned Offs = 0; |
| 336 | while (1) { |
| 337 | // Skip over the contents of the line. |
| 338 | // TODO: Vectorize this? This is very performance sensitive for programs |
| 339 | // with lots of diagnostics and in -E mode. |
| 340 | const unsigned char *NextBuf = (const unsigned char *)Buf; |
| 341 | while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') |
| 342 | ++NextBuf; |
| 343 | Offs += NextBuf-Buf; |
| 344 | Buf = NextBuf; |
| 345 | |
| 346 | if (Buf[0] == '\n' || Buf[0] == '\r') { |
| 347 | // If this is \n\r or \r\n, skip both characters. |
| 348 | if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) |
| 349 | ++Offs, ++Buf; |
| 350 | ++Offs, ++Buf; |
| 351 | LineOffsets.push_back(Offs); |
| 352 | } else { |
| 353 | // Otherwise, this is a null. If end of file, exit. |
| 354 | if (Buf == End) break; |
| 355 | // Otherwise, skip the null. |
| 356 | ++Offs, ++Buf; |
| 357 | } |
| 358 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 359 | |
| 360 | // Copy the offsets into the FileInfo structure. |
| 361 | FI->NumLines = LineOffsets.size(); |
| 362 | FI->SourceLineCache = new unsigned[LineOffsets.size()]; |
| 363 | std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); |
| 364 | } |
| 365 | |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 366 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 367 | /// for the position indicated. This requires building and caching a table of |
| 368 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 369 | /// about to emit a diagnostic. |
Chris Lattner | e9bf3e3 | 2008-11-18 06:51:15 +0000 | [diff] [blame] | 370 | unsigned SourceManager::getLineNumber(SourceLocation Loc) const { |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 371 | if (Loc.isInvalid()) return 0; |
| 372 | assert(Loc.isFileID() && "Don't know what part of instantiation loc to get"); |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 374 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
| 375 | |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 376 | ContentCache *Content; |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 377 | if (LastLineNoFileIDQuery == LocInfo.first) |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 378 | Content = LastLineNoContentCache; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 379 | else |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 380 | Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first) |
| 381 | .getFile().getContentCache()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 382 | |
| 383 | // If this is the first use of line information for this buffer, compute the |
| 384 | /// SourceLineCache for it on demand. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 385 | if (Content->SourceLineCache == 0) |
| 386 | ComputeLineNumbers(Content); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 387 | |
| 388 | // Okay, we know we have a line number table. Do a binary search to find the |
| 389 | // line number that this character position lands on. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 390 | unsigned *SourceLineCache = Content->SourceLineCache; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 391 | unsigned *SourceLineCacheStart = SourceLineCache; |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 392 | unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 393 | |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 394 | unsigned QueriedFilePos = LocInfo.second+1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 395 | |
| 396 | // If the previous query was to the same file, we know both the file pos from |
| 397 | // that query and the line number returned. This allows us to narrow the |
| 398 | // search space from the entire file to something near the match. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 399 | if (LastLineNoFileIDQuery == LocInfo.first) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 400 | if (QueriedFilePos >= LastLineNoFilePos) { |
| 401 | SourceLineCache = SourceLineCache+LastLineNoResult-1; |
| 402 | |
| 403 | // The query is likely to be nearby the previous one. Here we check to |
| 404 | // see if it is within 5, 10 or 20 lines. It can be far away in cases |
| 405 | // where big comment blocks and vertical whitespace eat up lines but |
| 406 | // contribute no tokens. |
| 407 | if (SourceLineCache+5 < SourceLineCacheEnd) { |
| 408 | if (SourceLineCache[5] > QueriedFilePos) |
| 409 | SourceLineCacheEnd = SourceLineCache+5; |
| 410 | else if (SourceLineCache+10 < SourceLineCacheEnd) { |
| 411 | if (SourceLineCache[10] > QueriedFilePos) |
| 412 | SourceLineCacheEnd = SourceLineCache+10; |
| 413 | else if (SourceLineCache+20 < SourceLineCacheEnd) { |
| 414 | if (SourceLineCache[20] > QueriedFilePos) |
| 415 | SourceLineCacheEnd = SourceLineCache+20; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } else { |
| 420 | SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // If the spread is large, do a "radix" test as our initial guess, based on |
| 425 | // the assumption that lines average to approximately the same length. |
| 426 | // NOTE: This is currently disabled, as it does not appear to be profitable in |
| 427 | // initial measurements. |
| 428 | if (0 && SourceLineCacheEnd-SourceLineCache > 20) { |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 429 | unsigned FileLen = Content->SourceLineCache[Content->NumLines-1]; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 430 | |
| 431 | // Take a stab at guessing where it is. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 432 | unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 433 | |
| 434 | // Check for -10 and +10 lines. |
| 435 | unsigned LowerBound = std::max(int(ApproxPos-10), 0); |
| 436 | unsigned UpperBound = std::min(ApproxPos+10, FileLen); |
| 437 | |
| 438 | // If the computed lower bound is less than the query location, move it in. |
| 439 | if (SourceLineCache < SourceLineCacheStart+LowerBound && |
| 440 | SourceLineCacheStart[LowerBound] < QueriedFilePos) |
| 441 | SourceLineCache = SourceLineCacheStart+LowerBound; |
| 442 | |
| 443 | // If the computed upper bound is greater than the query location, move it. |
| 444 | if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound && |
| 445 | SourceLineCacheStart[UpperBound] >= QueriedFilePos) |
| 446 | SourceLineCacheEnd = SourceLineCacheStart+UpperBound; |
| 447 | } |
| 448 | |
| 449 | unsigned *Pos |
| 450 | = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); |
| 451 | unsigned LineNo = Pos-SourceLineCacheStart; |
| 452 | |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 453 | LastLineNoFileIDQuery = LocInfo.first; |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 454 | LastLineNoContentCache = Content; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 455 | LastLineNoFilePos = QueriedFilePos; |
| 456 | LastLineNoResult = LineNo; |
| 457 | return LineNo; |
| 458 | } |
| 459 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 460 | /// getSourceName - This method returns the name of the file or buffer that |
| 461 | /// the SourceLocation specifies. This can be modified with #line directives, |
| 462 | /// etc. |
| 463 | const char *SourceManager::getSourceName(SourceLocation Loc) const { |
| 464 | if (Loc.isInvalid()) return ""; |
| 465 | |
| 466 | const SrcMgr::ContentCache *C = |
| 467 | getSLocEntry(getFileID(getSpellingLoc(Loc))).getFile().getContentCache(); |
| 468 | |
| 469 | // To get the source name, first consult the FileEntry (if one exists) before |
| 470 | // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer. |
| 471 | return C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier(); |
| 472 | } |
| 473 | |
| 474 | //===----------------------------------------------------------------------===// |
| 475 | // Other miscellaneous methods. |
| 476 | //===----------------------------------------------------------------------===// |
| 477 | |
| 478 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 479 | /// PrintStats - Print statistics to stderr. |
| 480 | /// |
| 481 | void SourceManager::PrintStats() const { |
Ted Kremenek | da29d8c | 2007-12-05 22:21:13 +0000 | [diff] [blame] | 482 | llvm::cerr << "\n*** Source Manager Stats:\n"; |
| 483 | llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size() |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 484 | << " mem buffers mapped, " << SLocEntryTable.size() |
| 485 | << " SLocEntry's allocated.\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 486 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 487 | unsigned NumLineNumsComputed = 0; |
| 488 | unsigned NumFileBytesMapped = 0; |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 489 | for (std::set<ContentCache>::const_iterator I = |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 490 | FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 491 | NumLineNumsComputed += I->SourceLineCache != 0; |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 492 | NumFileBytesMapped += I->getSizeBytesMapped(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 493 | } |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 494 | |
Ted Kremenek | da29d8c | 2007-12-05 22:21:13 +0000 | [diff] [blame] | 495 | llvm::cerr << NumFileBytesMapped << " bytes of files mapped, " |
| 496 | << NumLineNumsComputed << " files with line #'s computed.\n"; |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 497 | llvm::cerr << "FileID scans: " << NumLinearScans << " linear, " |
| 498 | << NumBinaryProbes << " binary.\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 499 | } |
Ted Kremenek | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 500 | |
| 501 | //===----------------------------------------------------------------------===// |
| 502 | // Serialization. |
| 503 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 504 | |
| 505 | void ContentCache::Emit(llvm::Serializer& S) const { |
Ted Kremenek | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 506 | S.FlushRecord(); |
| 507 | S.EmitPtr(this); |
Ted Kremenek | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 508 | |
Ted Kremenek | 1b6dd6f | 2007-12-18 22:12:19 +0000 | [diff] [blame] | 509 | if (Entry) { |
| 510 | llvm::sys::Path Fname(Buffer->getBufferIdentifier()); |
| 511 | |
| 512 | if (Fname.isAbsolute()) |
| 513 | S.EmitCStr(Fname.c_str()); |
| 514 | else { |
| 515 | // Create an absolute path. |
| 516 | // FIXME: This will potentially contain ".." and "." in the path. |
| 517 | llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory(); |
| 518 | path.appendComponent(Fname.c_str()); |
| 519 | S.EmitCStr(path.c_str()); |
| 520 | } |
| 521 | } |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 522 | else { |
Ted Kremenek | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 523 | const char* p = Buffer->getBufferStart(); |
| 524 | const char* e = Buffer->getBufferEnd(); |
| 525 | |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 526 | S.EmitInt(e-p); |
| 527 | |
Ted Kremenek | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 528 | for ( ; p != e; ++p) |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 529 | S.EmitInt(*p); |
Ted Kremenek | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 532 | S.FlushRecord(); |
Ted Kremenek | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 533 | } |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 534 | |
| 535 | void ContentCache::ReadToSourceManager(llvm::Deserializer& D, |
| 536 | SourceManager& SMgr, |
| 537 | FileManager* FMgr, |
| 538 | std::vector<char>& Buf) { |
| 539 | if (FMgr) { |
| 540 | llvm::SerializedPtrID PtrID = D.ReadPtrID(); |
| 541 | D.ReadCStr(Buf,false); |
| 542 | |
| 543 | // Create/fetch the FileEntry. |
| 544 | const char* start = &Buf[0]; |
| 545 | const FileEntry* E = FMgr->getFile(start,start+Buf.size()); |
| 546 | |
Ted Kremenek | b92cd87 | 2007-12-13 18:12:10 +0000 | [diff] [blame] | 547 | // FIXME: Ideally we want a lazy materialization of the ContentCache |
| 548 | // anyway, because we don't want to read in source files unless this |
| 549 | // is absolutely needed. |
| 550 | if (!E) |
| 551 | D.RegisterPtr(PtrID,NULL); |
Nico Weber | 630347d | 2008-09-29 00:25:48 +0000 | [diff] [blame] | 552 | else |
Ted Kremenek | b92cd87 | 2007-12-13 18:12:10 +0000 | [diff] [blame] | 553 | // Get the ContextCache object and register it with the deserializer. |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 554 | D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E)); |
| 555 | return; |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 556 | } |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 557 | |
| 558 | // Register the ContextCache object with the deserializer. |
| 559 | SMgr.MemBufferInfos.push_back(ContentCache()); |
| 560 | ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back()); |
| 561 | D.RegisterPtr(&Entry); |
| 562 | |
| 563 | // Create the buffer. |
| 564 | unsigned Size = D.ReadInt(); |
| 565 | Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size); |
| 566 | |
| 567 | // Read the contents of the buffer. |
| 568 | char* p = const_cast<char*>(Entry.Buffer->getBufferStart()); |
| 569 | for (unsigned i = 0; i < Size ; ++i) |
| 570 | p[i] = D.ReadInt(); |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | void SourceManager::Emit(llvm::Serializer& S) const { |
Ted Kremenek | bc54abf | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 574 | S.EnterBlock(); |
| 575 | S.EmitPtr(this); |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 576 | S.EmitInt(MainFileID.getOpaqueValue()); |
Ted Kremenek | bc54abf | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 577 | |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 578 | // Emit: FileInfos. Just emit the file name. |
| 579 | S.EnterBlock(); |
| 580 | |
| 581 | std::for_each(FileInfos.begin(),FileInfos.end(), |
| 582 | S.MakeEmitter<ContentCache>()); |
| 583 | |
| 584 | S.ExitBlock(); |
| 585 | |
| 586 | // Emit: MemBufferInfos |
| 587 | S.EnterBlock(); |
| 588 | |
| 589 | std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(), |
| 590 | S.MakeEmitter<ContentCache>()); |
| 591 | |
| 592 | S.ExitBlock(); |
| 593 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 594 | // FIXME: Emit SLocEntryTable. |
Ted Kremenek | bc54abf | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 595 | |
| 596 | S.ExitBlock(); |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Ted Kremenek | bc54abf | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 599 | SourceManager* |
| 600 | SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){ |
| 601 | SourceManager *M = new SourceManager(); |
| 602 | D.RegisterPtr(M); |
| 603 | |
Ted Kremenek | 2578dd0 | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 604 | // Read: the FileID of the main source file of the translation unit. |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 605 | M->MainFileID = FileID::get(D.ReadInt()); |
Ted Kremenek | 2578dd0 | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 606 | |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 607 | std::vector<char> Buf; |
| 608 | |
| 609 | { // Read: FileInfos. |
| 610 | llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation(); |
| 611 | while (!D.FinishedBlock(BLoc)) |
Ted Kremenek | bc54abf | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 612 | ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf); |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | { // Read: MemBufferInfos. |
| 616 | llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation(); |
| 617 | while (!D.FinishedBlock(BLoc)) |
Ted Kremenek | bc54abf | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 618 | ContentCache::ReadToSourceManager(D,*M,NULL,Buf); |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Chris Lattner | 27c0ced | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 621 | // FIXME: Read SLocEntryTable. |
Ted Kremenek | bc54abf | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 622 | |
| 623 | return M; |
Ted Kremenek | 12206af | 2007-12-10 18:01:25 +0000 | [diff] [blame] | 624 | } |