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