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