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