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