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