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