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" |
Douglas Gregor | d4f77aa | 2009-04-13 15:31:25 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManagerInternals.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
| 19 | #include "llvm/System/Path.h" |
Ted Kremenek | 665dd4a | 2007-12-05 22:21:13 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Streams.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Douglas Gregor | 370187c | 2009-04-22 21:45:53 +0000 | [diff] [blame] | 22 | #include <iostream> |
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 | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 60 | unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) { |
| 61 | // Look up the filename in the string table, returning the pre-existing value |
| 62 | // if it exists. |
| 63 | llvm::StringMapEntry<unsigned> &Entry = |
| 64 | FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U); |
| 65 | if (Entry.getValue() != ~0U) |
| 66 | return Entry.getValue(); |
| 67 | |
| 68 | // Otherwise, assign this the next available ID. |
| 69 | Entry.setValue(FilenamesByID.size()); |
| 70 | FilenamesByID.push_back(&Entry); |
| 71 | return FilenamesByID.size()-1; |
| 72 | } |
| 73 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 74 | /// AddLineNote - Add a line note to the line table that indicates that there |
| 75 | /// is a #line at the specified FID/Offset location which changes the presumed |
| 76 | /// location to LineNo/FilenameID. |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 77 | void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset, |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 78 | unsigned LineNo, int FilenameID) { |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 79 | std::vector<LineEntry> &Entries = LineEntries[FID]; |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 81 | assert((Entries.empty() || Entries.back().FileOffset < Offset) && |
| 82 | "Adding line entries out of order!"); |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 84 | SrcMgr::CharacteristicKind Kind = SrcMgr::C_User; |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 85 | unsigned IncludeOffset = 0; |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 87 | if (!Entries.empty()) { |
| 88 | // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember |
| 89 | // that we are still in "foo.h". |
| 90 | if (FilenameID == -1) |
| 91 | FilenameID = Entries.back().FilenameID; |
| 92 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 93 | // If we are after a line marker that switched us to system header mode, or |
| 94 | // that set #include information, preserve it. |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 95 | Kind = Entries.back().FileKind; |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 96 | IncludeOffset = Entries.back().IncludeOffset; |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 99 | Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind, |
| 100 | IncludeOffset)); |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 103 | /// AddLineNote This is the same as the previous version of AddLineNote, but is |
| 104 | /// used for GNU line markers. If EntryExit is 0, then this doesn't change the |
| 105 | /// presumed #include stack. If it is 1, this is a file entry, if it is 2 then |
| 106 | /// this is a file exit. FileKind specifies whether this is a system header or |
| 107 | /// extern C system header. |
| 108 | void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset, |
| 109 | unsigned LineNo, int FilenameID, |
| 110 | unsigned EntryExit, |
| 111 | SrcMgr::CharacteristicKind FileKind) { |
| 112 | assert(FilenameID != -1 && "Unspecified filename should use other accessor"); |
| 113 | |
| 114 | std::vector<LineEntry> &Entries = LineEntries[FID]; |
| 115 | |
| 116 | assert((Entries.empty() || Entries.back().FileOffset < Offset) && |
| 117 | "Adding line entries out of order!"); |
| 118 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 119 | unsigned IncludeOffset = 0; |
| 120 | if (EntryExit == 0) { // No #include stack change. |
| 121 | IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset; |
| 122 | } else if (EntryExit == 1) { |
| 123 | IncludeOffset = Offset-1; |
| 124 | } else if (EntryExit == 2) { |
| 125 | assert(!Entries.empty() && Entries.back().IncludeOffset && |
| 126 | "PPDirectives should have caught case when popping empty include stack"); |
| 127 | |
| 128 | // Get the include loc of the last entries' include loc as our include loc. |
| 129 | IncludeOffset = 0; |
| 130 | if (const LineEntry *PrevEntry = |
| 131 | FindNearestLineEntry(FID, Entries.back().IncludeOffset)) |
| 132 | IncludeOffset = PrevEntry->IncludeOffset; |
| 133 | } |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 135 | Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind, |
| 136 | IncludeOffset)); |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 140 | /// FindNearestLineEntry - Find the line entry nearest to FID that is before |
| 141 | /// it. If there is no line entry before Offset in FID, return null. |
| 142 | const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID, |
| 143 | unsigned Offset) { |
| 144 | const std::vector<LineEntry> &Entries = LineEntries[FID]; |
| 145 | assert(!Entries.empty() && "No #line entries for this FID after all!"); |
| 146 | |
Chris Lattner | 6c1fbe0 | 2009-02-04 04:46:59 +0000 | [diff] [blame] | 147 | // It is very common for the query to be after the last #line, check this |
| 148 | // first. |
| 149 | if (Entries.back().FileOffset <= Offset) |
| 150 | return &Entries.back(); |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 6c1fbe0 | 2009-02-04 04:46:59 +0000 | [diff] [blame] | 152 | // Do a binary search to find the maximal element that is still before Offset. |
| 153 | std::vector<LineEntry>::const_iterator I = |
| 154 | std::upper_bound(Entries.begin(), Entries.end(), Offset); |
| 155 | if (I == Entries.begin()) return 0; |
| 156 | return &*--I; |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 157 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 158 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 159 | /// \brief Add a new line entry that has already been encoded into |
| 160 | /// the internal representation of the line table. |
| 161 | void LineTableInfo::AddEntry(unsigned FID, |
| 162 | const std::vector<LineEntry> &Entries) { |
| 163 | LineEntries[FID] = Entries; |
| 164 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 166 | /// getLineTableFilenameID - Return the uniqued ID for the specified filename. |
| 167 | /// |
| 168 | unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) { |
| 169 | if (LineTable == 0) |
| 170 | LineTable = new LineTableInfo(); |
| 171 | return LineTable->getLineTableFilenameID(Ptr, Len); |
| 172 | } |
| 173 | |
| 174 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 175 | /// AddLineNote - Add a line note to the line table for the FileID and offset |
| 176 | /// specified by Loc. If FilenameID is -1, it is considered to be |
| 177 | /// unspecified. |
| 178 | void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, |
| 179 | int FilenameID) { |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 180 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 181 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 182 | const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile(); |
| 183 | |
| 184 | // Remember that this file has #line directives now if it doesn't already. |
| 185 | const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); |
| 186 | |
| 187 | if (LineTable == 0) |
| 188 | LineTable = new LineTableInfo(); |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 189 | LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID); |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 192 | /// AddLineNote - Add a GNU line marker to the line table. |
| 193 | void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, |
| 194 | int FilenameID, bool IsFileEntry, |
| 195 | bool IsFileExit, bool IsSystemHeader, |
| 196 | bool IsExternCHeader) { |
| 197 | // If there is no filename and no flags, this is treated just like a #line, |
| 198 | // which does not change the flags of the previous line marker. |
| 199 | if (FilenameID == -1) { |
| 200 | assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader && |
| 201 | "Can't set flags without setting the filename!"); |
| 202 | return AddLineNote(Loc, LineNo, FilenameID); |
| 203 | } |
| 204 | |
| 205 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
| 206 | const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile(); |
| 207 | |
| 208 | // Remember that this file has #line directives now if it doesn't already. |
| 209 | const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); |
| 210 | |
| 211 | if (LineTable == 0) |
| 212 | LineTable = new LineTableInfo(); |
| 213 | |
| 214 | SrcMgr::CharacteristicKind FileKind; |
| 215 | if (IsExternCHeader) |
| 216 | FileKind = SrcMgr::C_ExternCSystem; |
| 217 | else if (IsSystemHeader) |
| 218 | FileKind = SrcMgr::C_System; |
| 219 | else |
| 220 | FileKind = SrcMgr::C_User; |
| 221 | |
| 222 | unsigned EntryExit = 0; |
| 223 | if (IsFileEntry) |
| 224 | EntryExit = 1; |
| 225 | else if (IsFileExit) |
| 226 | EntryExit = 2; |
| 227 | |
| 228 | LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID, |
| 229 | EntryExit, FileKind); |
| 230 | } |
| 231 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 232 | LineTableInfo &SourceManager::getLineTable() { |
| 233 | if (LineTable == 0) |
| 234 | LineTable = new LineTableInfo(); |
| 235 | return *LineTable; |
| 236 | } |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 238 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 239 | // Private 'Create' methods. |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 240 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 242 | SourceManager::~SourceManager() { |
| 243 | delete LineTable; |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 244 | |
| 245 | // Delete FileEntry objects corresponding to content caches. Since the actual |
| 246 | // content cache objects are bump pointer allocated, we just have to run the |
| 247 | // dtors, but we call the deallocate method for completeness. |
| 248 | for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) { |
| 249 | MemBufferInfos[i]->~ContentCache(); |
| 250 | ContentCacheAlloc.Deallocate(MemBufferInfos[i]); |
| 251 | } |
| 252 | for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator |
| 253 | I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { |
| 254 | I->second->~ContentCache(); |
| 255 | ContentCacheAlloc.Deallocate(I->second); |
| 256 | } |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void SourceManager::clearIDTables() { |
| 260 | MainFileID = FileID(); |
| 261 | SLocEntryTable.clear(); |
| 262 | LastLineNoFileIDQuery = FileID(); |
| 263 | LastLineNoContentCache = 0; |
| 264 | LastFileIDLookup = FileID(); |
| 265 | |
| 266 | if (LineTable) |
| 267 | LineTable->clear(); |
| 268 | |
| 269 | // Use up FileID #0 as an invalid instantiation. |
| 270 | NextOffset = 0; |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 271 | createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 274 | /// getOrCreateContentCache - Create or return a cached ContentCache for the |
| 275 | /// specified file. |
| 276 | const ContentCache * |
| 277 | SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | assert(FileEnt && "Didn't specify a file entry to use?"); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 279 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | // Do we already have information about this file? |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 281 | ContentCache *&Entry = FileInfos[FileEnt]; |
| 282 | if (Entry) return Entry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 284 | // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned |
| 285 | // so that FileInfo can use the low 3 bits of the pointer for its own |
| 286 | // nefarious purposes. |
| 287 | unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; |
| 288 | EntryAlign = std::max(8U, EntryAlign); |
| 289 | Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 290 | new (Entry) ContentCache(FileEnt); |
| 291 | return Entry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
Ted Kremenek | d1c0eee | 2007-10-31 17:53:38 +0000 | [diff] [blame] | 295 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 296 | /// memory buffer. This does no caching. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 297 | const ContentCache* |
| 298 | SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) { |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 299 | // Add a new ContentCache to the MemBufferInfos list and return it. Make sure |
| 300 | // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of |
| 301 | // the pointer for its own nefarious purposes. |
| 302 | unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; |
| 303 | EntryAlign = std::max(8U, EntryAlign); |
| 304 | ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 305 | new (Entry) ContentCache(); |
| 306 | MemBufferInfos.push_back(Entry); |
| 307 | Entry->setBuffer(Buffer); |
| 308 | return Entry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 311 | void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source, |
| 312 | unsigned NumSLocEntries, |
| 313 | unsigned NextOffset) { |
| 314 | ExternalSLocEntries = Source; |
| 315 | this->NextOffset = NextOffset; |
| 316 | SLocEntryLoaded.resize(NumSLocEntries + 1); |
| 317 | SLocEntryLoaded[0] = true; |
| 318 | SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries); |
| 319 | } |
| 320 | |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 321 | void SourceManager::ClearPreallocatedSLocEntries() { |
| 322 | unsigned I = 0; |
| 323 | for (unsigned N = SLocEntryLoaded.size(); I != N; ++I) |
| 324 | if (!SLocEntryLoaded[I]) |
| 325 | break; |
| 326 | |
| 327 | // We've already loaded all preallocated source location entries. |
| 328 | if (I == SLocEntryLoaded.size()) |
| 329 | return; |
| 330 | |
| 331 | // Remove everything from location I onward. |
| 332 | SLocEntryTable.resize(I); |
| 333 | SLocEntryLoaded.clear(); |
| 334 | ExternalSLocEntries = 0; |
| 335 | } |
| 336 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 337 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 338 | //===----------------------------------------------------------------------===// |
| 339 | // Methods to create new FileID's and instantiations. |
| 340 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | |
Nico Weber | 48002c8 | 2008-09-29 00:25:48 +0000 | [diff] [blame] | 342 | /// createFileID - Create a new fileID for the specified ContentCache and |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 343 | /// include position. This works regardless of whether the ContentCache |
| 344 | /// corresponds to a file or some other input source. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 345 | FileID SourceManager::createFileID(const ContentCache *File, |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 346 | SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 347 | SrcMgr::CharacteristicKind FileCharacter, |
| 348 | unsigned PreallocatedID, |
| 349 | unsigned Offset) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 350 | if (PreallocatedID) { |
| 351 | // If we're filling in a preallocated ID, just load in the file |
| 352 | // entry and return. |
| 353 | assert(PreallocatedID < SLocEntryLoaded.size() && |
| 354 | "Preallocate ID out-of-range"); |
| 355 | assert(!SLocEntryLoaded[PreallocatedID] && |
| 356 | "Source location entry already loaded"); |
| 357 | assert(Offset && "Preallocate source location cannot have zero offset"); |
| 358 | SLocEntryTable[PreallocatedID] |
| 359 | = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter)); |
| 360 | SLocEntryLoaded[PreallocatedID] = true; |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 361 | FileID FID = FileID::get(PreallocatedID); |
| 362 | if (File->FirstFID.isInvalid()) |
| 363 | File->FirstFID = FID; |
| 364 | return LastFileIDLookup = FID; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 367 | SLocEntryTable.push_back(SLocEntry::get(NextOffset, |
| 368 | FileInfo::get(IncludePos, File, |
| 369 | FileCharacter))); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 370 | unsigned FileSize = File->getSize(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 371 | assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!"); |
| 372 | NextOffset += FileSize+1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 373 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 374 | // Set LastFileIDLookup to the newly created file. The next getFileID call is |
| 375 | // almost guaranteed to be from that file. |
Argyrios Kyrtzidis | ea703f1 | 2009-06-23 00:42:06 +0000 | [diff] [blame] | 376 | FileID FID = FileID::get(SLocEntryTable.size()-1); |
| 377 | if (File->FirstFID.isInvalid()) |
| 378 | File->FirstFID = FID; |
| 379 | return LastFileIDLookup = FID; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 382 | /// createInstantiationLoc - Return a new SourceLocation that encodes the fact |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 383 | /// that a token from SpellingLoc should actually be referenced from |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 384 | /// InstantiationLoc. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 385 | SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 386 | SourceLocation ILocStart, |
| 387 | SourceLocation ILocEnd, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 388 | unsigned TokLength, |
| 389 | unsigned PreallocatedID, |
| 390 | unsigned Offset) { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 391 | InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 392 | if (PreallocatedID) { |
| 393 | // If we're filling in a preallocated ID, just load in the |
| 394 | // instantiation entry and return. |
| 395 | assert(PreallocatedID < SLocEntryLoaded.size() && |
| 396 | "Preallocate ID out-of-range"); |
| 397 | assert(!SLocEntryLoaded[PreallocatedID] && |
| 398 | "Source location entry already loaded"); |
| 399 | assert(Offset && "Preallocate source location cannot have zero offset"); |
| 400 | SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II); |
| 401 | SLocEntryLoaded[PreallocatedID] = true; |
| 402 | return SourceLocation::getMacroLoc(Offset); |
| 403 | } |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 404 | SLocEntryTable.push_back(SLocEntry::get(NextOffset, II)); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 405 | assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!"); |
| 406 | NextOffset += TokLength+1; |
| 407 | return SourceLocation::getMacroLoc(NextOffset-(TokLength+1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Chris Lattner | 31530ba | 2009-01-19 07:32:13 +0000 | [diff] [blame] | 410 | /// getBufferData - Return a pointer to the start and end of the source buffer |
| 411 | /// data for the specified FileID. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 412 | std::pair<const char*, const char*> |
| 413 | SourceManager::getBufferData(FileID FID) const { |
| 414 | const llvm::MemoryBuffer *Buf = getBuffer(FID); |
| 415 | return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd()); |
| 416 | } |
| 417 | |
| 418 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 419 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 420 | // SourceLocation manipulation methods. |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 421 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 422 | |
| 423 | /// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot |
| 424 | /// method that is used for all SourceManager queries that start with a |
| 425 | /// SourceLocation object. It is responsible for finding the entry in |
| 426 | /// SLocEntryTable which contains the specified location. |
| 427 | /// |
| 428 | FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { |
| 429 | assert(SLocOffset && "Invalid FileID"); |
| 430 | |
| 431 | // After the first and second level caches, I see two common sorts of |
| 432 | // behavior: 1) a lot of searched FileID's are "near" the cached file location |
| 433 | // or are "near" the cached instantiation location. 2) others are just |
| 434 | // completely random and may be a very long way away. |
| 435 | // |
| 436 | // To handle this, we do a linear search for up to 8 steps to catch #1 quickly |
| 437 | // then we fall back to a less cache efficient, but more scalable, binary |
| 438 | // search to find the location. |
| 439 | |
| 440 | // See if this is near the file point - worst case we start scanning from the |
| 441 | // most newly created FileID. |
| 442 | std::vector<SrcMgr::SLocEntry>::const_iterator I; |
| 443 | |
| 444 | if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { |
| 445 | // Neither loc prunes our search. |
| 446 | I = SLocEntryTable.end(); |
| 447 | } else { |
| 448 | // Perhaps it is near the file point. |
| 449 | I = SLocEntryTable.begin()+LastFileIDLookup.ID; |
| 450 | } |
| 451 | |
| 452 | // Find the FileID that contains this. "I" is an iterator that points to a |
| 453 | // FileID whose offset is known to be larger than SLocOffset. |
| 454 | unsigned NumProbes = 0; |
| 455 | while (1) { |
| 456 | --I; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 457 | if (ExternalSLocEntries) |
| 458 | getSLocEntry(FileID::get(I - SLocEntryTable.begin())); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 459 | if (I->getOffset() <= SLocOffset) { |
| 460 | #if 0 |
| 461 | printf("lin %d -> %d [%s] %d %d\n", SLocOffset, |
| 462 | I-SLocEntryTable.begin(), |
| 463 | I->isInstantiation() ? "inst" : "file", |
| 464 | LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); |
| 465 | #endif |
| 466 | FileID Res = FileID::get(I-SLocEntryTable.begin()); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 467 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 468 | // If this isn't an instantiation, remember it. We have good locality |
| 469 | // across FileID lookups. |
| 470 | if (!I->isInstantiation()) |
| 471 | LastFileIDLookup = Res; |
| 472 | NumLinearScans += NumProbes+1; |
| 473 | return Res; |
| 474 | } |
| 475 | if (++NumProbes == 8) |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | // Convert "I" back into an index. We know that it is an entry whose index is |
| 480 | // larger than the offset we are looking for. |
| 481 | unsigned GreaterIndex = I-SLocEntryTable.begin(); |
| 482 | // LessIndex - This is the lower bound of the range that we're searching. |
| 483 | // We know that the offset corresponding to the FileID is is less than |
| 484 | // SLocOffset. |
| 485 | unsigned LessIndex = 0; |
| 486 | NumProbes = 0; |
| 487 | while (1) { |
| 488 | unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 489 | unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 490 | |
| 491 | ++NumProbes; |
| 492 | |
| 493 | // If the offset of the midpoint is too large, chop the high side of the |
| 494 | // range to the midpoint. |
| 495 | if (MidOffset > SLocOffset) { |
| 496 | GreaterIndex = MiddleIndex; |
| 497 | continue; |
| 498 | } |
| 499 | |
| 500 | // If the middle index contains the value, succeed and return. |
| 501 | if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { |
| 502 | #if 0 |
| 503 | printf("bin %d -> %d [%s] %d %d\n", SLocOffset, |
| 504 | I-SLocEntryTable.begin(), |
| 505 | I->isInstantiation() ? "inst" : "file", |
| 506 | LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); |
| 507 | #endif |
| 508 | FileID Res = FileID::get(MiddleIndex); |
| 509 | |
| 510 | // If this isn't an instantiation, remember it. We have good locality |
| 511 | // across FileID lookups. |
| 512 | if (!I->isInstantiation()) |
| 513 | LastFileIDLookup = Res; |
| 514 | NumBinaryProbes += NumProbes; |
| 515 | return Res; |
| 516 | } |
| 517 | |
| 518 | // Otherwise, move the low-side up to the middle index. |
| 519 | LessIndex = MiddleIndex; |
| 520 | } |
| 521 | } |
| 522 | |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 523 | SourceLocation SourceManager:: |
| 524 | getInstantiationLocSlowCase(SourceLocation Loc) const { |
| 525 | do { |
| 526 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 527 | Loc = getSLocEntry(LocInfo.first).getInstantiation() |
| 528 | .getInstantiationLocStart(); |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 529 | Loc = Loc.getFileLocWithOffset(LocInfo.second); |
| 530 | } while (!Loc.isFileID()); |
| 531 | |
| 532 | return Loc; |
| 533 | } |
| 534 | |
| 535 | SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { |
| 536 | do { |
| 537 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
| 538 | Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); |
| 539 | Loc = Loc.getFileLocWithOffset(LocInfo.second); |
| 540 | } while (!Loc.isFileID()); |
| 541 | return Loc; |
| 542 | } |
| 543 | |
| 544 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 545 | std::pair<FileID, unsigned> |
| 546 | SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, |
| 547 | unsigned Offset) const { |
| 548 | // If this is an instantiation record, walk through all the instantiation |
| 549 | // points. |
| 550 | FileID FID; |
| 551 | SourceLocation Loc; |
| 552 | do { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 553 | Loc = E->getInstantiation().getInstantiationLocStart(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 554 | |
| 555 | FID = getFileID(Loc); |
| 556 | E = &getSLocEntry(FID); |
| 557 | Offset += Loc.getOffset()-E->getOffset(); |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 558 | } while (!Loc.isFileID()); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 559 | |
| 560 | return std::make_pair(FID, Offset); |
| 561 | } |
| 562 | |
| 563 | std::pair<FileID, unsigned> |
| 564 | SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, |
| 565 | unsigned Offset) const { |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 566 | // If this is an instantiation record, walk through all the instantiation |
| 567 | // points. |
| 568 | FileID FID; |
| 569 | SourceLocation Loc; |
| 570 | do { |
| 571 | Loc = E->getInstantiation().getSpellingLoc(); |
| 572 | |
| 573 | FID = getFileID(Loc); |
| 574 | E = &getSLocEntry(FID); |
| 575 | Offset += Loc.getOffset()-E->getOffset(); |
| 576 | } while (!Loc.isFileID()); |
| 577 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 578 | return std::make_pair(FID, Offset); |
| 579 | } |
| 580 | |
Chris Lattner | 387616e | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 581 | /// getImmediateSpellingLoc - Given a SourceLocation object, return the |
| 582 | /// spelling location referenced by the ID. This is the first level down |
| 583 | /// towards the place where the characters that make up the lexed token can be |
| 584 | /// found. This should not generally be used by clients. |
| 585 | SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{ |
| 586 | if (Loc.isFileID()) return Loc; |
| 587 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
| 588 | Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); |
| 589 | return Loc.getFileLocWithOffset(LocInfo.second); |
| 590 | } |
| 591 | |
| 592 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 593 | /// getImmediateInstantiationRange - Loc is required to be an instantiation |
| 594 | /// location. Return the start/end of the instantiation information. |
| 595 | std::pair<SourceLocation,SourceLocation> |
| 596 | SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const { |
| 597 | assert(Loc.isMacroID() && "Not an instantiation loc!"); |
| 598 | const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation(); |
| 599 | return II.getInstantiationLocRange(); |
| 600 | } |
| 601 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 602 | /// getInstantiationRange - Given a SourceLocation object, return the |
| 603 | /// range of tokens covered by the instantiation in the ultimate file. |
| 604 | std::pair<SourceLocation,SourceLocation> |
| 605 | SourceManager::getInstantiationRange(SourceLocation Loc) const { |
| 606 | if (Loc.isFileID()) return std::make_pair(Loc, Loc); |
| 607 | |
| 608 | std::pair<SourceLocation,SourceLocation> Res = |
| 609 | getImmediateInstantiationRange(Loc); |
| 610 | |
| 611 | // Fully resolve the start and end locations to their ultimate instantiation |
| 612 | // points. |
| 613 | while (!Res.first.isFileID()) |
| 614 | Res.first = getImmediateInstantiationRange(Res.first).first; |
| 615 | while (!Res.second.isFileID()) |
| 616 | Res.second = getImmediateInstantiationRange(Res.second).second; |
| 617 | return Res; |
| 618 | } |
| 619 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 620 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 621 | |
| 622 | //===----------------------------------------------------------------------===// |
| 623 | // Queries about the code at a SourceLocation. |
| 624 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 625 | |
| 626 | /// getCharacterData - Return a pointer to the start of the specified location |
| 627 | /// in the appropriate MemoryBuffer. |
| 628 | const char *SourceManager::getCharacterData(SourceLocation SL) const { |
| 629 | // Note that this is a hot function in the getSpelling() path, which is |
| 630 | // heavily used by -E mode. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 631 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 632 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 633 | // Note that calling 'getBuffer()' may lazily page in a source file. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 634 | return getSLocEntry(LocInfo.first).getFile().getContentCache() |
| 635 | ->getBuffer()->getBufferStart() + LocInfo.second; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 638 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 639 | /// getColumnNumber - Return the column # for the specified file position. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 640 | /// this is significantly cheaper to compute than the line number. |
| 641 | unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const { |
| 642 | const char *Buf = getBuffer(FID)->getBufferStart(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 643 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 644 | unsigned LineStart = FilePos; |
| 645 | while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') |
| 646 | --LineStart; |
| 647 | return FilePos-LineStart+1; |
| 648 | } |
| 649 | |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 650 | unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const { |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 651 | if (Loc.isInvalid()) return 0; |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 652 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); |
| 653 | return getColumnNumber(LocInfo.first, LocInfo.second); |
| 654 | } |
| 655 | |
| 656 | unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const { |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 657 | if (Loc.isInvalid()) return 0; |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 658 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
| 659 | return getColumnNumber(LocInfo.first, LocInfo.second); |
| 660 | } |
| 661 | |
| 662 | |
| 663 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 664 | static void ComputeLineNumbers(ContentCache* FI, |
| 665 | llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE; |
| 666 | static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){ |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 667 | // Note that calling 'getBuffer()' may lazily page in the file. |
| 668 | const MemoryBuffer *Buffer = FI->getBuffer(); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 669 | |
| 670 | // Find the file offsets of all of the *physical* source lines. This does |
| 671 | // not look at trigraphs, escaped newlines, or anything else tricky. |
| 672 | std::vector<unsigned> LineOffsets; |
| 673 | |
| 674 | // Line #1 starts at char 0. |
| 675 | LineOffsets.push_back(0); |
| 676 | |
| 677 | const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); |
| 678 | const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); |
| 679 | unsigned Offs = 0; |
| 680 | while (1) { |
| 681 | // Skip over the contents of the line. |
| 682 | // TODO: Vectorize this? This is very performance sensitive for programs |
| 683 | // with lots of diagnostics and in -E mode. |
| 684 | const unsigned char *NextBuf = (const unsigned char *)Buf; |
| 685 | while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') |
| 686 | ++NextBuf; |
| 687 | Offs += NextBuf-Buf; |
| 688 | Buf = NextBuf; |
| 689 | |
| 690 | if (Buf[0] == '\n' || Buf[0] == '\r') { |
| 691 | // If this is \n\r or \r\n, skip both characters. |
| 692 | if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) |
| 693 | ++Offs, ++Buf; |
| 694 | ++Offs, ++Buf; |
| 695 | LineOffsets.push_back(Offs); |
| 696 | } else { |
| 697 | // Otherwise, this is a null. If end of file, exit. |
| 698 | if (Buf == End) break; |
| 699 | // Otherwise, skip the null. |
| 700 | ++Offs, ++Buf; |
| 701 | } |
| 702 | } |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 703 | |
| 704 | // Copy the offsets into the FileInfo structure. |
| 705 | FI->NumLines = LineOffsets.size(); |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 706 | FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size()); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 707 | std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); |
| 708 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 709 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 710 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 711 | /// for the position indicated. This requires building and caching a table of |
| 712 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 713 | /// about to emit a diagnostic. |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 714 | unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 715 | ContentCache *Content; |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 716 | if (LastLineNoFileIDQuery == FID) |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 717 | Content = LastLineNoContentCache; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 718 | else |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 719 | Content = const_cast<ContentCache*>(getSLocEntry(FID) |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 720 | .getFile().getContentCache()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 721 | |
| 722 | // 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] | 723 | /// SourceLineCache for it on demand. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 724 | if (Content->SourceLineCache == 0) |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 725 | ComputeLineNumbers(Content, ContentCacheAlloc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 726 | |
| 727 | // Okay, we know we have a line number table. Do a binary search to find the |
| 728 | // line number that this character position lands on. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 729 | unsigned *SourceLineCache = Content->SourceLineCache; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 730 | unsigned *SourceLineCacheStart = SourceLineCache; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 731 | unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 732 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 733 | unsigned QueriedFilePos = FilePos+1; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 734 | |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 735 | // FIXME: I would like to be convinced that this code is worth being as |
| 736 | // complicated as it is, binary search isn't that slow. |
| 737 | // |
| 738 | // If it is worth being optimized, then in my opinion it could be more |
| 739 | // performant, simpler, and more obviously correct by just "galloping" outward |
| 740 | // from the queried file position. In fact, this could be incorporated into a |
| 741 | // generic algorithm such as lower_bound_with_hint. |
| 742 | // |
| 743 | // If someone gives me a test case where this matters, and I will do it! - DWD |
| 744 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 745 | // If the previous query was to the same file, we know both the file pos from |
| 746 | // that query and the line number returned. This allows us to narrow the |
| 747 | // search space from the entire file to something near the match. |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 748 | if (LastLineNoFileIDQuery == FID) { |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 749 | if (QueriedFilePos >= LastLineNoFilePos) { |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 750 | // FIXME: Potential overflow? |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 751 | SourceLineCache = SourceLineCache+LastLineNoResult-1; |
| 752 | |
| 753 | // The query is likely to be nearby the previous one. Here we check to |
| 754 | // see if it is within 5, 10 or 20 lines. It can be far away in cases |
| 755 | // where big comment blocks and vertical whitespace eat up lines but |
| 756 | // contribute no tokens. |
| 757 | if (SourceLineCache+5 < SourceLineCacheEnd) { |
| 758 | if (SourceLineCache[5] > QueriedFilePos) |
| 759 | SourceLineCacheEnd = SourceLineCache+5; |
| 760 | else if (SourceLineCache+10 < SourceLineCacheEnd) { |
| 761 | if (SourceLineCache[10] > QueriedFilePos) |
| 762 | SourceLineCacheEnd = SourceLineCache+10; |
| 763 | else if (SourceLineCache+20 < SourceLineCacheEnd) { |
| 764 | if (SourceLineCache[20] > QueriedFilePos) |
| 765 | SourceLineCacheEnd = SourceLineCache+20; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | } else { |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 770 | if (LastLineNoResult < Content->NumLines) |
| 771 | SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 775 | // If the spread is large, do a "radix" test as our initial guess, based on |
| 776 | // the assumption that lines average to approximately the same length. |
| 777 | // NOTE: This is currently disabled, as it does not appear to be profitable in |
| 778 | // initial measurements. |
| 779 | if (0 && SourceLineCacheEnd-SourceLineCache > 20) { |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 780 | unsigned FileLen = Content->SourceLineCache[Content->NumLines-1]; |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 781 | |
| 782 | // Take a stab at guessing where it is. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 783 | unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen; |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 784 | |
| 785 | // Check for -10 and +10 lines. |
| 786 | unsigned LowerBound = std::max(int(ApproxPos-10), 0); |
| 787 | unsigned UpperBound = std::min(ApproxPos+10, FileLen); |
| 788 | |
| 789 | // If the computed lower bound is less than the query location, move it in. |
| 790 | if (SourceLineCache < SourceLineCacheStart+LowerBound && |
| 791 | SourceLineCacheStart[LowerBound] < QueriedFilePos) |
| 792 | SourceLineCache = SourceLineCacheStart+LowerBound; |
| 793 | |
| 794 | // If the computed upper bound is greater than the query location, move it. |
| 795 | if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound && |
| 796 | SourceLineCacheStart[UpperBound] >= QueriedFilePos) |
| 797 | SourceLineCacheEnd = SourceLineCacheStart+UpperBound; |
| 798 | } |
| 799 | |
| 800 | unsigned *Pos |
| 801 | = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 802 | unsigned LineNo = Pos-SourceLineCacheStart; |
| 803 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 804 | LastLineNoFileIDQuery = FID; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 805 | LastLineNoContentCache = Content; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 806 | LastLineNoFilePos = QueriedFilePos; |
| 807 | LastLineNoResult = LineNo; |
| 808 | return LineNo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 811 | unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const { |
| 812 | if (Loc.isInvalid()) return 0; |
| 813 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
| 814 | return getLineNumber(LocInfo.first, LocInfo.second); |
| 815 | } |
| 816 | unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const { |
| 817 | if (Loc.isInvalid()) return 0; |
| 818 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); |
| 819 | return getLineNumber(LocInfo.first, LocInfo.second); |
| 820 | } |
| 821 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 822 | /// getFileCharacteristic - return the file characteristic of the specified |
| 823 | /// source location, indicating whether this is a normal file, a system |
| 824 | /// header, or an "implicit extern C" system header. |
| 825 | /// |
| 826 | /// This state can be modified with flags on GNU linemarker directives like: |
| 827 | /// # 4 "foo.h" 3 |
| 828 | /// which changes all source locations in the current file after that to be |
| 829 | /// considered to be from a system header. |
| 830 | SrcMgr::CharacteristicKind |
| 831 | SourceManager::getFileCharacteristic(SourceLocation Loc) const { |
| 832 | assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!"); |
| 833 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
| 834 | const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile(); |
| 835 | |
| 836 | // If there are no #line directives in this file, just return the whole-file |
| 837 | // state. |
| 838 | if (!FI.hasLineDirectives()) |
| 839 | return FI.getFileCharacteristic(); |
| 840 | |
| 841 | assert(LineTable && "Can't have linetable entries without a LineTable!"); |
| 842 | // See if there is a #line directive before the location. |
| 843 | const LineEntry *Entry = |
| 844 | LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second); |
| 845 | |
| 846 | // If this is before the first line marker, use the file characteristic. |
| 847 | if (!Entry) |
| 848 | return FI.getFileCharacteristic(); |
| 849 | |
| 850 | return Entry->FileKind; |
| 851 | } |
| 852 | |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 853 | /// Return the filename or buffer identifier of the buffer the location is in. |
| 854 | /// Note that this name does not respect #line directives. Use getPresumedLoc |
| 855 | /// for normal clients. |
| 856 | const char *SourceManager::getBufferName(SourceLocation Loc) const { |
| 857 | if (Loc.isInvalid()) return "<invalid loc>"; |
| 858 | |
| 859 | return getBuffer(getFileID(Loc))->getBufferIdentifier(); |
| 860 | } |
| 861 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 862 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 863 | /// getPresumedLoc - This method returns the "presumed" location of a |
| 864 | /// SourceLocation specifies. A "presumed location" can be modified by #line |
| 865 | /// or GNU line marker directives. This provides a view on the data that a |
| 866 | /// user should see in diagnostics, for example. |
| 867 | /// |
| 868 | /// Note that a presumed location is always given as the instantiation point |
| 869 | /// of an instantiation location, not at the spelling location. |
| 870 | PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { |
| 871 | if (Loc.isInvalid()) return PresumedLoc(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 872 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 873 | // Presumed locations are always for instantiation points. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 874 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 875 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 876 | const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile(); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 877 | const SrcMgr::ContentCache *C = FI.getContentCache(); |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 878 | |
| 879 | // To get the source name, first consult the FileEntry (if one exists) |
| 880 | // before the MemBuffer as this will avoid unnecessarily paging in the |
| 881 | // MemBuffer. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 882 | const char *Filename = |
| 883 | C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier(); |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 884 | unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second); |
| 885 | unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second); |
| 886 | SourceLocation IncludeLoc = FI.getIncludeLoc(); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 887 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 888 | // If we have #line directives in this file, update and overwrite the physical |
| 889 | // location info if appropriate. |
| 890 | if (FI.hasLineDirectives()) { |
| 891 | assert(LineTable && "Can't have linetable entries without a LineTable!"); |
| 892 | // See if there is a #line directive before this. If so, get it. |
| 893 | if (const LineEntry *Entry = |
| 894 | LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) { |
Chris Lattner | fc39133 | 2009-02-04 02:00:59 +0000 | [diff] [blame] | 895 | // If the LineEntry indicates a filename, use it. |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 896 | if (Entry->FilenameID != -1) |
| 897 | Filename = LineTable->getFilename(Entry->FilenameID); |
Chris Lattner | fc39133 | 2009-02-04 02:00:59 +0000 | [diff] [blame] | 898 | |
| 899 | // Use the line number specified by the LineEntry. This line number may |
| 900 | // be multiple lines down from the line entry. Add the difference in |
| 901 | // physical line numbers from the query point and the line marker to the |
| 902 | // total. |
| 903 | unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset); |
| 904 | LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1); |
| 905 | |
Chris Lattner | 0e0e5da | 2009-02-04 02:15:40 +0000 | [diff] [blame] | 906 | // Note that column numbers are not molested by line markers. |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 907 | |
| 908 | // Handle virtual #include manipulation. |
| 909 | if (Entry->IncludeOffset) { |
| 910 | IncludeLoc = getLocForStartOfFile(LocInfo.first); |
| 911 | IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset); |
| 912 | } |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| 916 | return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | //===----------------------------------------------------------------------===// |
| 920 | // Other miscellaneous methods. |
| 921 | //===----------------------------------------------------------------------===// |
| 922 | |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 923 | /// \brief Get the source location for the given file:line:col triplet. |
| 924 | /// |
| 925 | /// If the source file is included multiple times, the source location will |
| 926 | /// be based upon the first inclusion. |
| 927 | SourceLocation SourceManager::getLocation(const FileEntry *SourceFile, |
| 928 | unsigned Line, unsigned Col) const { |
| 929 | assert(SourceFile && "Null source file!"); |
| 930 | assert(Line && Col && "Line and column should start from 1!"); |
| 931 | |
| 932 | fileinfo_iterator FI = FileInfos.find(SourceFile); |
| 933 | if (FI == FileInfos.end()) |
| 934 | return SourceLocation(); |
| 935 | ContentCache *Content = FI->second; |
| 936 | |
| 937 | // If this is the first use of line information for this buffer, compute the |
| 938 | /// SourceLineCache for it on demand. |
| 939 | if (Content->SourceLineCache == 0) |
| 940 | ComputeLineNumbers(Content, ContentCacheAlloc); |
| 941 | |
| 942 | if (Line > Content->NumLines) |
| 943 | return SourceLocation(); |
| 944 | |
| 945 | unsigned FilePos = Content->SourceLineCache[Line - 1]; |
Argyrios Kyrtzidis | 081445c | 2009-06-25 18:22:16 +0000 | [diff] [blame] | 946 | const char *Buf = Content->getBuffer()->getBufferStart() + FilePos; |
Argyrios Kyrtzidis | 93edc3c | 2009-06-20 08:40:15 +0000 | [diff] [blame] | 947 | unsigned BufLength = Content->getBuffer()->getBufferEnd() - Buf; |
| 948 | unsigned i = 0; |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 949 | |
| 950 | // Check that the given column is valid. |
Argyrios Kyrtzidis | 93edc3c | 2009-06-20 08:40:15 +0000 | [diff] [blame] | 951 | while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r') |
| 952 | ++i; |
| 953 | if (i < Col-1) |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 954 | return SourceLocation(); |
| 955 | |
| 956 | return getLocForStartOfFile(Content->FirstFID). |
| 957 | getFileLocWithOffset(FilePos + Col - 1); |
| 958 | } |
| 959 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 960 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 961 | /// |
| 962 | /// \returns true if LHS source location comes before RHS, false otherwise. |
| 963 | bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS, |
| 964 | SourceLocation RHS) const { |
| 965 | assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!"); |
| 966 | if (LHS == RHS) |
| 967 | return false; |
| 968 | |
| 969 | std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS); |
| 970 | std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS); |
| 971 | |
| 972 | // If the source locations are in the same file, just compare offsets. |
| 973 | if (LOffs.first == ROffs.first) |
| 974 | return LOffs.second < ROffs.second; |
| 975 | |
| 976 | // If we are comparing a source location with multiple locations in the same |
| 977 | // file, we get a big win by caching the result. |
| 978 | |
| 979 | if (LastLFIDForBeforeTUCheck == LOffs.first && |
| 980 | LastRFIDForBeforeTUCheck == ROffs.first) |
| 981 | return LastResForBeforeTUCheck; |
| 982 | |
| 983 | LastLFIDForBeforeTUCheck = LOffs.first; |
| 984 | LastRFIDForBeforeTUCheck = ROffs.first; |
| 985 | |
| 986 | // "Traverse" the include/instantiation stacks of both locations and try to |
| 987 | // find a common "ancestor". |
| 988 | // |
| 989 | // First we traverse the stack of the right location and check each level |
| 990 | // against the level of the left location, while collecting all levels in a |
| 991 | // "stack map". |
| 992 | |
| 993 | std::map<FileID, unsigned> ROffsMap; |
| 994 | ROffsMap[ROffs.first] = ROffs.second; |
| 995 | |
| 996 | while (1) { |
| 997 | SourceLocation UpperLoc; |
| 998 | const SrcMgr::SLocEntry &Entry = getSLocEntry(ROffs.first); |
| 999 | if (Entry.isInstantiation()) |
| 1000 | UpperLoc = Entry.getInstantiation().getInstantiationLocStart(); |
| 1001 | else |
| 1002 | UpperLoc = Entry.getFile().getIncludeLoc(); |
| 1003 | |
| 1004 | if (UpperLoc.isInvalid()) |
| 1005 | break; // We reached the top. |
| 1006 | |
| 1007 | ROffs = getDecomposedLoc(UpperLoc); |
| 1008 | |
| 1009 | if (LOffs.first == ROffs.first) |
| 1010 | return LastResForBeforeTUCheck = LOffs.second < ROffs.second; |
| 1011 | |
| 1012 | ROffsMap[ROffs.first] = ROffs.second; |
| 1013 | } |
| 1014 | |
| 1015 | // We didn't find a common ancestor. Now traverse the stack of the left |
| 1016 | // location, checking against the stack map of the right location. |
| 1017 | |
| 1018 | while (1) { |
| 1019 | SourceLocation UpperLoc; |
| 1020 | const SrcMgr::SLocEntry &Entry = getSLocEntry(LOffs.first); |
| 1021 | if (Entry.isInstantiation()) |
| 1022 | UpperLoc = Entry.getInstantiation().getInstantiationLocStart(); |
| 1023 | else |
| 1024 | UpperLoc = Entry.getFile().getIncludeLoc(); |
| 1025 | |
| 1026 | if (UpperLoc.isInvalid()) |
| 1027 | break; // We reached the top. |
| 1028 | |
| 1029 | LOffs = getDecomposedLoc(UpperLoc); |
| 1030 | |
| 1031 | std::map<FileID, unsigned>::iterator I = ROffsMap.find(LOffs.first); |
| 1032 | if (I != ROffsMap.end()) |
| 1033 | return LastResForBeforeTUCheck = LOffs.second < I->second; |
| 1034 | } |
| 1035 | |
| 1036 | // No common ancestor. |
| 1037 | // Now we are getting into murky waters. Most probably this is because one |
| 1038 | // location is in the predefines buffer. |
| 1039 | |
| 1040 | const FileEntry *LEntry = |
| 1041 | getSLocEntry(LOffs.first).getFile().getContentCache()->Entry; |
| 1042 | const FileEntry *REntry = |
| 1043 | getSLocEntry(ROffs.first).getFile().getContentCache()->Entry; |
| 1044 | |
| 1045 | // If the locations are in two memory buffers we give up, we can't answer |
| 1046 | // which one should be considered first. |
| 1047 | // FIXME: Should there be a way to "include" memory buffers in the translation |
| 1048 | // unit ? |
| 1049 | assert((LEntry != 0 || REntry != 0) && "Locations in memory buffers."); |
Daniel Dunbar | 5eee090 | 2009-06-23 23:09:58 +0000 | [diff] [blame] | 1050 | (void) REntry; |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1051 | |
| 1052 | // Consider the memory buffer as coming before the file in the translation |
| 1053 | // unit. |
| 1054 | if (LEntry == 0) |
| 1055 | return LastResForBeforeTUCheck = true; |
| 1056 | else { |
| 1057 | assert(REntry == 0 && "Locations in not #included files ?"); |
| 1058 | return LastResForBeforeTUCheck = false; |
| 1059 | } |
| 1060 | } |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1061 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1062 | /// PrintStats - Print statistics to stderr. |
| 1063 | /// |
| 1064 | void SourceManager::PrintStats() const { |
Ted Kremenek | 665dd4a | 2007-12-05 22:21:13 +0000 | [diff] [blame] | 1065 | llvm::cerr << "\n*** Source Manager Stats:\n"; |
| 1066 | llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size() |
Chris Lattner | 08c375c | 2009-01-27 05:22:43 +0000 | [diff] [blame] | 1067 | << " mem buffers mapped.\n"; |
| 1068 | llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, " |
| 1069 | << NextOffset << "B of Sloc address space used.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1070 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1071 | unsigned NumLineNumsComputed = 0; |
| 1072 | unsigned NumFileBytesMapped = 0; |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 1073 | for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){ |
| 1074 | NumLineNumsComputed += I->second->SourceLineCache != 0; |
| 1075 | NumFileBytesMapped += I->second->getSizeBytesMapped(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1076 | } |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1077 | |
Ted Kremenek | 665dd4a | 2007-12-05 22:21:13 +0000 | [diff] [blame] | 1078 | llvm::cerr << NumFileBytesMapped << " bytes of files mapped, " |
| 1079 | << NumLineNumsComputed << " files with line #'s computed.\n"; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1080 | llvm::cerr << "FileID scans: " << NumLinearScans << " linear, " |
| 1081 | << NumBinaryProbes << " binary.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1082 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1083 | |
| 1084 | ExternalSLocEntrySource::~ExternalSLocEntrySource() { } |