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