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