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