Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SourceManager.cpp - Track and cache source files -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SourceManager interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | d4f77aa | 2009-04-13 15:31:25 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/FileManager.h" |
Benjamin Kramer | 5807d9c | 2010-11-18 12:46:39 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Optional.h" |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Path.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 25 | #include <string> |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 26 | #include <cstring> |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 27 | #include <sys/stat.h> |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 28 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | using namespace SrcMgr; |
| 31 | using llvm::MemoryBuffer; |
| 32 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 34 | // SourceManager Helper Classes |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 37 | ContentCache::~ContentCache() { |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 38 | if (shouldFreeBuffer()) |
| 39 | delete Buffer.getPointer(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Ted Kremenek | c16c208 | 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 | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 46 | return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0; |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Ted Kremenek | f61b831 | 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 | c16c208 | 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 | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 65 | /// file is not lazily brought in from disk to satisfy this query. |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 66 | unsigned ContentCache::getSize() const { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 67 | return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize() |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 68 | : (unsigned) ContentsEntry->getSize(); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 71 | void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B, |
| 72 | bool DoNotFree) { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 73 | assert(B != Buffer.getPointer()); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 75 | if (shouldFreeBuffer()) |
| 76 | delete Buffer.getPointer(); |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 77 | Buffer.setPointer(B); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 78 | Buffer.setInt(DoNotFree? DoNotFreeFlag : 0); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 81 | const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag, |
Chris Lattner | 5c5db4e | 2010-04-20 20:49:23 +0000 | [diff] [blame] | 82 | const SourceManager &SM, |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 83 | SourceLocation Loc, |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 84 | bool *Invalid) const { |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 85 | // Lazily create the Buffer for ContentCaches that wrap files. If we already |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 86 | // computed it, just return what we have. |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 87 | if (Buffer.getPointer() || ContentsEntry == 0) { |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 88 | if (Invalid) |
| 89 | *Invalid = isBufferInvalid(); |
Chris Lattner | 38caec4 | 2010-04-20 18:14:03 +0000 | [diff] [blame] | 90 | |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 91 | return Buffer.getPointer(); |
| 92 | } |
Benjamin Kramer | 5807d9c | 2010-11-18 12:46:39 +0000 | [diff] [blame] | 93 | |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 94 | std::string ErrorStr; |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 95 | Buffer.setPointer(SM.getFileManager().getBufferForFile(ContentsEntry, &ErrorStr)); |
Chris Lattner | b088cd3 | 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 | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 109 | Buffer.setPointer(MemoryBuffer::getNewMemBuffer(ContentsEntry->getSize(), |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 110 | "<invalid>")); |
| 111 | char *Ptr = const_cast<char*>(Buffer.getPointer()->getBufferStart()); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 112 | for (unsigned i = 0, e = ContentsEntry->getSize(); i != e; ++i) |
Chris Lattner | b088cd3 | 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 | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 117 | ContentsEntry->getName(), ErrorStr); |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 118 | else |
| 119 | Diag.Report(Loc, diag::err_cannot_open_file) |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 120 | << ContentsEntry->getName() << ErrorStr; |
Chris Lattner | b088cd3 | 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 | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 130 | if (getRawBuffer()->getBufferSize() != (size_t)ContentsEntry->getSize()) { |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 131 | if (Diag.isDiagnosticInFlight()) |
| 132 | Diag.SetDelayedDiagnostic(diag::err_file_modified, |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 133 | ContentsEntry->getName()); |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 134 | else |
| 135 | Diag.Report(Loc, diag::err_file_modified) |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 136 | << ContentsEntry->getName(); |
Chris Lattner | b088cd3 | 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 | 156119d | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 142 | |
Chris Lattner | b088cd3 | 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 | 156119d | 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 | b088cd3 | 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 | 156119d | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 147 | const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr) |
Chris Lattner | b088cd3 | 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 | 156119d | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 160 | if (InvalidBOM) { |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 161 | Diag.Report(Loc, diag::err_unsupported_bom) |
Eric Christopher | 156119d | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 162 | << InvalidBOM << ContentsEntry->getName(); |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 163 | Buffer.setInt(Buffer.getInt() | InvalidFlag); |
Ted Kremenek | 5b034ad | 2009-01-06 22:43:04 +0000 | [diff] [blame] | 164 | } |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 165 | |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 166 | if (Invalid) |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 167 | *Invalid = isBufferInvalid(); |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 168 | |
| 169 | return Buffer.getPointer(); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame^] | 172 | unsigned LineTableInfo::getLineTableFilenameID(llvm::StringRef Name) { |
Chris Lattner | 5b9a504 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | llvm::StringMapEntry<unsigned> &Entry = |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame^] | 176 | FilenameIDs.GetOrCreateValue(Name, ~0U); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 177 | if (Entry.getValue() != ~0U) |
| 178 | return Entry.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 5b9a504 | 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 | ac50e34 | 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 | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 189 | void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset, |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 190 | unsigned LineNo, int FilenameID) { |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 191 | std::vector<LineEntry> &Entries = LineEntries[FID]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 23b5dc6 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 196 | SrcMgr::CharacteristicKind Kind = SrcMgr::C_User; |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 197 | unsigned IncludeOffset = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 9d79eba | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 137b6a6 | 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 | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 207 | Kind = Entries.back().FileKind; |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 208 | IncludeOffset = Entries.back().IncludeOffset; |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 211 | Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind, |
| 212 | IncludeOffset)); |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | 9d79eba | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 226 | std::vector<LineEntry> &Entries = LineEntries[FID]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 9d79eba | 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 | 137b6a6 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 137b6a6 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 247 | Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind, |
| 248 | IncludeOffset)); |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
Chris Lattner | 3cd949c | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID, |
Chris Lattner | 3cd949c | 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 | 6c1fbe0 | 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 | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 6c1fbe0 | 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 | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 269 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 270 | |
Douglas Gregor | bd94500 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | void LineTableInfo::AddEntry(unsigned FID, |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 274 | const std::vector<LineEntry> &Entries) { |
| 275 | LineEntries[FID] = Entries; |
| 276 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 278 | /// getLineTableFilenameID - Return the uniqued ID for the specified filename. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | /// |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame^] | 280 | unsigned SourceManager::getLineTableFilenameID(llvm::StringRef Name) { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 281 | if (LineTable == 0) |
| 282 | LineTable = new LineTableInfo(); |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame^] | 283 | return LineTable->getLineTableFilenameID(Name); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
Chris Lattner | 4c4ea17 | 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 | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 292 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Douglas Gregor | e23ac65 | 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 | ac50e34 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 304 | if (LineTable == 0) |
| 305 | LineTable = new LineTableInfo(); |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 306 | LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID); |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Chris Lattner | 9d79eba | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 322 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
Douglas Gregor | e23ac65 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 9d79eba | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 334 | if (LineTable == 0) |
| 335 | LineTable = new LineTableInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 9d79eba | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Chris Lattner | 9d79eba | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Chris Lattner | 9d79eba | 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 | bd94500 | 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 | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 361 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 362 | // Private 'Create' methods. |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 363 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 365 | SourceManager::SourceManager(Diagnostic &Diag, FileManager &FileMgr) |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 366 | : Diag(Diag), FileMgr(FileMgr), OverridenFilesKeepOriginalName(true), |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 367 | ExternalSLocEntries(0), LineTable(0), NumLinearScans(0), |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 368 | NumBinaryProbes(0), FakeBufferForRecovery(0) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 369 | clearIDTables(); |
| 370 | Diag.setSourceManager(this); |
| 371 | } |
| 372 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 373 | SourceManager::~SourceManager() { |
| 374 | delete LineTable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 0d0bf8c | 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 | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 388 | |
| 389 | delete FakeBufferForRecovery; |
Chris Lattner | 5b9a504 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 399 | if (LineTable) |
| 400 | LineTable->clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 402 | // Use up FileID #0 as an invalid instantiation. |
| 403 | NextOffset = 0; |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 404 | createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Chris Lattner | de7aeef | 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) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | assert(FileEnt && "Didn't specify a file entry to use?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 413 | // Do we already have information about this file? |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 414 | ContentCache *&Entry = FileInfos[FileEnt]; |
| 415 | if (Entry) return Entry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 00282d6 | 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 | b1c8649 | 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 | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 431 | new (Entry) ContentCache(OverridenFilesKeepOriginalName ? FileEnt |
| 432 | : overI->second, |
| 433 | overI->second); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 435 | return Entry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | |
Ted Kremenek | d1c0eee | 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 | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 441 | const ContentCache* |
| 442 | SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) { |
Chris Lattner | 00282d6 | 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 | 0d0bf8c | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Douglas Gregor | 7f94b0b | 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 | b86238d | 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 | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 463 | SLocEntryLoaded.resize(NumSLocEntries + 1); |
| 464 | SLocEntryLoaded[0] = true; |
Sebastian Redl | b86238d | 2010-07-28 21:07:02 +0000 | [diff] [blame] | 465 | SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries - CurPrealloc); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Douglas Gregor | 2bf1eb0 | 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 | e23ac65 | 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 | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 493 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 494 | //===----------------------------------------------------------------------===// |
| 495 | // Methods to create new FileID's and instantiations. |
| 496 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 497 | |
Dan Gohman | 3f86b78 | 2010-08-26 21:27:06 +0000 | [diff] [blame] | 498 | /// createFileID - Create a new FileID for the specified ContentCache and |
Ted Kremenek | 0d892d8 | 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 | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 501 | FileID SourceManager::createFileID(const ContentCache *File, |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 502 | SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 503 | SrcMgr::CharacteristicKind FileCharacter, |
| 504 | unsigned PreallocatedID, |
| 505 | unsigned Offset) { |
Douglas Gregor | 7f94b0b | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | assert(PreallocatedID < SLocEntryLoaded.size() && |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 510 | "Preallocate ID out-of-range"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | assert(!SLocEntryLoaded[PreallocatedID] && |
Douglas Gregor | 7f94b0b | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | SLocEntryTable[PreallocatedID] |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 515 | = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter)); |
| 516 | SLocEntryLoaded[PreallocatedID] = true; |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 517 | FileID FID = FileID::get(PreallocatedID); |
Douglas Gregor | 5de6572 | 2010-03-19 06:12:06 +0000 | [diff] [blame] | 518 | return FID; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | SLocEntryTable.push_back(SLocEntry::get(NextOffset, |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 522 | FileInfo::get(IncludePos, File, |
| 523 | FileCharacter))); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 524 | unsigned FileSize = File->getSize(); |
Chris Lattner | de7aeef | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Chris Lattner | de7aeef | 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 | ea703f1 | 2009-06-23 00:42:06 +0000 | [diff] [blame] | 530 | FileID FID = FileID::get(SLocEntryTable.size()-1); |
Argyrios Kyrtzidis | ea703f1 | 2009-06-23 00:42:06 +0000 | [diff] [blame] | 531 | return LastFileIDLookup = FID; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 534 | /// createInstantiationLoc - Return a new SourceLocation that encodes the fact |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 535 | /// that a token from SpellingLoc should actually be referenced from |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 536 | /// InstantiationLoc. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 537 | SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 538 | SourceLocation ILocStart, |
| 539 | SourceLocation ILocEnd, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 540 | unsigned TokLength, |
| 541 | unsigned PreallocatedID, |
| 542 | unsigned Offset) { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 543 | InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 544 | if (PreallocatedID) { |
| 545 | // If we're filling in a preallocated ID, just load in the |
| 546 | // instantiation entry and return. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | assert(PreallocatedID < SLocEntryLoaded.size() && |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 548 | "Preallocate ID out-of-range"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | assert(!SLocEntryLoaded[PreallocatedID] && |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 550 | "Source location entry already loaded"); |
| 551 | assert(Offset && "Preallocate source location cannot have zero offset"); |
| 552 | SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II); |
| 553 | SLocEntryLoaded[PreallocatedID] = true; |
| 554 | return SourceLocation::getMacroLoc(Offset); |
| 555 | } |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 556 | SLocEntryTable.push_back(SLocEntry::get(NextOffset, II)); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 557 | assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!"); |
| 558 | NextOffset += TokLength+1; |
| 559 | return SourceLocation::getMacroLoc(NextOffset-(TokLength+1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 562 | const llvm::MemoryBuffer * |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 563 | SourceManager::getMemoryBufferForFile(const FileEntry *File, |
| 564 | bool *Invalid) { |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 565 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(File); |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 566 | assert(IR && "getOrCreateContentCache() cannot return NULL"); |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 567 | return IR->getBuffer(Diag, *this, SourceLocation(), Invalid); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Dan Gohman | 0d06e99 | 2010-10-26 20:47:28 +0000 | [diff] [blame] | 570 | void SourceManager::overrideFileContents(const FileEntry *SourceFile, |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 571 | const llvm::MemoryBuffer *Buffer, |
| 572 | bool DoNotFree) { |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 573 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); |
Dan Gohman | 0d06e99 | 2010-10-26 20:47:28 +0000 | [diff] [blame] | 574 | assert(IR && "getOrCreateContentCache() cannot return NULL"); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 575 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 576 | const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 579 | void SourceManager::overrideFileContents(const FileEntry *SourceFile, |
| 580 | const FileEntry *NewFile) { |
| 581 | assert(SourceFile->getSize() == NewFile->getSize() && |
| 582 | "Different sizes, use the FileManager to create a virtual file with " |
| 583 | "the correct size"); |
| 584 | assert(FileInfos.count(SourceFile) == 0 && |
| 585 | "This function should be called at the initialization stage, before " |
| 586 | "any parsing occurs."); |
| 587 | OverriddenFiles[SourceFile] = NewFile; |
| 588 | } |
| 589 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 590 | llvm::StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const { |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 591 | bool MyInvalid = false; |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 592 | const SLocEntry &SLoc = getSLocEntry(FID.ID, &MyInvalid); |
| 593 | if (!SLoc.isFile() || MyInvalid) { |
Douglas Gregor | 3de8424 | 2011-01-31 22:42:36 +0000 | [diff] [blame] | 594 | if (Invalid) |
| 595 | *Invalid = true; |
| 596 | return "<<<<<INVALID SOURCE LOCATION>>>>>"; |
| 597 | } |
| 598 | |
| 599 | const llvm::MemoryBuffer *Buf |
| 600 | = SLoc.getFile().getContentCache()->getBuffer(Diag, *this, SourceLocation(), |
| 601 | &MyInvalid); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 602 | if (Invalid) |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 603 | *Invalid = MyInvalid; |
| 604 | |
| 605 | if (MyInvalid) |
Douglas Gregor | 3de8424 | 2011-01-31 22:42:36 +0000 | [diff] [blame] | 606 | return "<<<<<INVALID SOURCE LOCATION>>>>>"; |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 607 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 608 | return Buf->getBuffer(); |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 609 | } |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 611 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 612 | // SourceLocation manipulation methods. |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 613 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 614 | |
| 615 | /// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot |
| 616 | /// method that is used for all SourceManager queries that start with a |
| 617 | /// SourceLocation object. It is responsible for finding the entry in |
| 618 | /// SLocEntryTable which contains the specified location. |
| 619 | /// |
| 620 | FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 621 | if (!SLocOffset) |
| 622 | return FileID::get(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 624 | // After the first and second level caches, I see two common sorts of |
| 625 | // behavior: 1) a lot of searched FileID's are "near" the cached file location |
| 626 | // or are "near" the cached instantiation location. 2) others are just |
| 627 | // completely random and may be a very long way away. |
| 628 | // |
| 629 | // To handle this, we do a linear search for up to 8 steps to catch #1 quickly |
| 630 | // then we fall back to a less cache efficient, but more scalable, binary |
| 631 | // search to find the location. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 633 | // See if this is near the file point - worst case we start scanning from the |
| 634 | // most newly created FileID. |
| 635 | std::vector<SrcMgr::SLocEntry>::const_iterator I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 637 | if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { |
| 638 | // Neither loc prunes our search. |
| 639 | I = SLocEntryTable.end(); |
| 640 | } else { |
| 641 | // Perhaps it is near the file point. |
| 642 | I = SLocEntryTable.begin()+LastFileIDLookup.ID; |
| 643 | } |
| 644 | |
| 645 | // Find the FileID that contains this. "I" is an iterator that points to a |
| 646 | // FileID whose offset is known to be larger than SLocOffset. |
| 647 | unsigned NumProbes = 0; |
| 648 | while (1) { |
| 649 | --I; |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 650 | if (ExternalSLocEntries) { |
| 651 | bool Invalid = false; |
| 652 | getSLocEntry(FileID::get(I - SLocEntryTable.begin()), &Invalid); |
| 653 | if (Invalid) |
| 654 | return FileID::get(0); |
| 655 | } |
| 656 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 657 | if (I->getOffset() <= SLocOffset) { |
| 658 | #if 0 |
| 659 | printf("lin %d -> %d [%s] %d %d\n", SLocOffset, |
| 660 | I-SLocEntryTable.begin(), |
| 661 | I->isInstantiation() ? "inst" : "file", |
| 662 | LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); |
| 663 | #endif |
| 664 | FileID Res = FileID::get(I-SLocEntryTable.begin()); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 665 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 666 | // If this isn't an instantiation, remember it. We have good locality |
| 667 | // across FileID lookups. |
| 668 | if (!I->isInstantiation()) |
| 669 | LastFileIDLookup = Res; |
| 670 | NumLinearScans += NumProbes+1; |
| 671 | return Res; |
| 672 | } |
| 673 | if (++NumProbes == 8) |
| 674 | break; |
| 675 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 677 | // Convert "I" back into an index. We know that it is an entry whose index is |
| 678 | // larger than the offset we are looking for. |
| 679 | unsigned GreaterIndex = I-SLocEntryTable.begin(); |
| 680 | // LessIndex - This is the lower bound of the range that we're searching. |
| 681 | // We know that the offset corresponding to the FileID is is less than |
| 682 | // SLocOffset. |
| 683 | unsigned LessIndex = 0; |
| 684 | NumProbes = 0; |
| 685 | while (1) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 686 | bool Invalid = false; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 687 | unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 688 | unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex), &Invalid) |
| 689 | .getOffset(); |
| 690 | if (Invalid) |
| 691 | return FileID::get(0); |
| 692 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 693 | ++NumProbes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 695 | // If the offset of the midpoint is too large, chop the high side of the |
| 696 | // range to the midpoint. |
| 697 | if (MidOffset > SLocOffset) { |
| 698 | GreaterIndex = MiddleIndex; |
| 699 | continue; |
| 700 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 702 | // If the middle index contains the value, succeed and return. |
| 703 | if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { |
| 704 | #if 0 |
| 705 | printf("bin %d -> %d [%s] %d %d\n", SLocOffset, |
| 706 | I-SLocEntryTable.begin(), |
| 707 | I->isInstantiation() ? "inst" : "file", |
| 708 | LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); |
| 709 | #endif |
| 710 | FileID Res = FileID::get(MiddleIndex); |
| 711 | |
| 712 | // If this isn't an instantiation, remember it. We have good locality |
| 713 | // across FileID lookups. |
| 714 | if (!I->isInstantiation()) |
| 715 | LastFileIDLookup = Res; |
| 716 | NumBinaryProbes += NumProbes; |
| 717 | return Res; |
| 718 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 720 | // Otherwise, move the low-side up to the middle index. |
| 721 | LessIndex = MiddleIndex; |
| 722 | } |
| 723 | } |
| 724 | |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 725 | SourceLocation SourceManager:: |
| 726 | getInstantiationLocSlowCase(SourceLocation Loc) const { |
| 727 | do { |
Chris Lattner | a5c6c58 | 2010-02-12 19:31:35 +0000 | [diff] [blame] | 728 | // Note: If Loc indicates an offset into a token that came from a macro |
| 729 | // expansion (e.g. the 5th character of the token) we do not want to add |
| 730 | // this offset when going to the instantiation location. The instatiation |
| 731 | // location is the macro invocation, which the offset has nothing to do |
| 732 | // with. This is unlike when we get the spelling loc, because the offset |
| 733 | // directly correspond to the token whose spelling we're inspecting. |
| 734 | Loc = getSLocEntry(getFileID(Loc)).getInstantiation() |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 735 | .getInstantiationLocStart(); |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 736 | } while (!Loc.isFileID()); |
| 737 | |
| 738 | return Loc; |
| 739 | } |
| 740 | |
| 741 | SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { |
| 742 | do { |
| 743 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
| 744 | Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); |
| 745 | Loc = Loc.getFileLocWithOffset(LocInfo.second); |
| 746 | } while (!Loc.isFileID()); |
| 747 | return Loc; |
| 748 | } |
| 749 | |
| 750 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 751 | std::pair<FileID, unsigned> |
| 752 | SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, |
| 753 | unsigned Offset) const { |
| 754 | // If this is an instantiation record, walk through all the instantiation |
| 755 | // points. |
| 756 | FileID FID; |
| 757 | SourceLocation Loc; |
| 758 | do { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 759 | Loc = E->getInstantiation().getInstantiationLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 761 | FID = getFileID(Loc); |
| 762 | E = &getSLocEntry(FID); |
| 763 | Offset += Loc.getOffset()-E->getOffset(); |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 764 | } while (!Loc.isFileID()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 765 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 766 | return std::make_pair(FID, Offset); |
| 767 | } |
| 768 | |
| 769 | std::pair<FileID, unsigned> |
| 770 | SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, |
| 771 | unsigned Offset) const { |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 772 | // If this is an instantiation record, walk through all the instantiation |
| 773 | // points. |
| 774 | FileID FID; |
| 775 | SourceLocation Loc; |
| 776 | do { |
| 777 | Loc = E->getInstantiation().getSpellingLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 | |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 779 | FID = getFileID(Loc); |
| 780 | E = &getSLocEntry(FID); |
| 781 | Offset += Loc.getOffset()-E->getOffset(); |
| 782 | } while (!Loc.isFileID()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 784 | return std::make_pair(FID, Offset); |
| 785 | } |
| 786 | |
Chris Lattner | 387616e | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 787 | /// getImmediateSpellingLoc - Given a SourceLocation object, return the |
| 788 | /// spelling location referenced by the ID. This is the first level down |
| 789 | /// towards the place where the characters that make up the lexed token can be |
| 790 | /// found. This should not generally be used by clients. |
| 791 | SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{ |
| 792 | if (Loc.isFileID()) return Loc; |
| 793 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
| 794 | Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); |
| 795 | return Loc.getFileLocWithOffset(LocInfo.second); |
| 796 | } |
| 797 | |
| 798 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 799 | /// getImmediateInstantiationRange - Loc is required to be an instantiation |
| 800 | /// location. Return the start/end of the instantiation information. |
| 801 | std::pair<SourceLocation,SourceLocation> |
| 802 | SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const { |
| 803 | assert(Loc.isMacroID() && "Not an instantiation loc!"); |
| 804 | const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation(); |
| 805 | return II.getInstantiationLocRange(); |
| 806 | } |
| 807 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 808 | /// getInstantiationRange - Given a SourceLocation object, return the |
| 809 | /// range of tokens covered by the instantiation in the ultimate file. |
| 810 | std::pair<SourceLocation,SourceLocation> |
| 811 | SourceManager::getInstantiationRange(SourceLocation Loc) const { |
| 812 | if (Loc.isFileID()) return std::make_pair(Loc, Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 814 | std::pair<SourceLocation,SourceLocation> Res = |
| 815 | getImmediateInstantiationRange(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 817 | // Fully resolve the start and end locations to their ultimate instantiation |
| 818 | // points. |
| 819 | while (!Res.first.isFileID()) |
| 820 | Res.first = getImmediateInstantiationRange(Res.first).first; |
| 821 | while (!Res.second.isFileID()) |
| 822 | Res.second = getImmediateInstantiationRange(Res.second).second; |
| 823 | return Res; |
| 824 | } |
| 825 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 826 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 827 | |
| 828 | //===----------------------------------------------------------------------===// |
| 829 | // Queries about the code at a SourceLocation. |
| 830 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 831 | |
| 832 | /// getCharacterData - Return a pointer to the start of the specified location |
| 833 | /// in the appropriate MemoryBuffer. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 834 | const char *SourceManager::getCharacterData(SourceLocation SL, |
| 835 | bool *Invalid) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 836 | // Note that this is a hot function in the getSpelling() path, which is |
| 837 | // heavily used by -E mode. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 838 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 840 | // Note that calling 'getBuffer()' may lazily page in a source file. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 841 | bool CharDataInvalid = false; |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 842 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &CharDataInvalid); |
| 843 | if (CharDataInvalid || !Entry.isFile()) { |
| 844 | if (Invalid) |
| 845 | *Invalid = true; |
| 846 | |
| 847 | return "<<<<INVALID BUFFER>>>>"; |
| 848 | } |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 849 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 850 | = Entry.getFile().getContentCache() |
| 851 | ->getBuffer(Diag, *this, SourceLocation(), &CharDataInvalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 852 | if (Invalid) |
| 853 | *Invalid = CharDataInvalid; |
| 854 | return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 858 | /// getColumnNumber - Return the column # for the specified file position. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 859 | /// this is significantly cheaper to compute than the line number. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 860 | unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos, |
| 861 | bool *Invalid) const { |
| 862 | bool MyInvalid = false; |
| 863 | const char *Buf = getBuffer(FID, &MyInvalid)->getBufferStart(); |
| 864 | if (Invalid) |
| 865 | *Invalid = MyInvalid; |
| 866 | |
| 867 | if (MyInvalid) |
| 868 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | unsigned LineStart = FilePos; |
| 871 | while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') |
| 872 | --LineStart; |
| 873 | return FilePos-LineStart+1; |
| 874 | } |
| 875 | |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 876 | // isInvalid - Return the result of calling loc.isInvalid(), and |
| 877 | // if Invalid is not null, set its value to same. |
| 878 | static bool isInvalid(SourceLocation Loc, bool *Invalid) { |
| 879 | bool MyInvalid = Loc.isInvalid(); |
| 880 | if (Invalid) |
| 881 | *Invalid = MyInvalid; |
| 882 | return MyInvalid; |
| 883 | } |
| 884 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 885 | unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc, |
| 886 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 887 | if (isInvalid(Loc, Invalid)) return 0; |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 888 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 889 | return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 892 | unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc, |
| 893 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 894 | if (isInvalid(Loc, Invalid)) return 0; |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 895 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 896 | return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 899 | unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc, |
| 900 | bool *Invalid) const { |
| 901 | if (isInvalid(Loc, Invalid)) return 0; |
| 902 | return getPresumedLoc(Loc).getColumn(); |
| 903 | } |
| 904 | |
Chandler Carruth | 14bd965 | 2010-10-23 08:44:57 +0000 | [diff] [blame] | 905 | static LLVM_ATTRIBUTE_NOINLINE void |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 906 | ComputeLineNumbers(Diagnostic &Diag, ContentCache *FI, |
| 907 | llvm::BumpPtrAllocator &Alloc, |
| 908 | const SourceManager &SM, bool &Invalid); |
| 909 | static void ComputeLineNumbers(Diagnostic &Diag, ContentCache *FI, |
| 910 | llvm::BumpPtrAllocator &Alloc, |
| 911 | const SourceManager &SM, bool &Invalid) { |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 912 | // Note that calling 'getBuffer()' may lazily page in the file. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 913 | const MemoryBuffer *Buffer = FI->getBuffer(Diag, SM, SourceLocation(), |
| 914 | &Invalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 915 | if (Invalid) |
| 916 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 918 | // Find the file offsets of all of the *physical* source lines. This does |
| 919 | // not look at trigraphs, escaped newlines, or anything else tricky. |
| 920 | std::vector<unsigned> LineOffsets; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 921 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 922 | // Line #1 starts at char 0. |
| 923 | LineOffsets.push_back(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 924 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 925 | const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); |
| 926 | const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); |
| 927 | unsigned Offs = 0; |
| 928 | while (1) { |
| 929 | // Skip over the contents of the line. |
| 930 | // TODO: Vectorize this? This is very performance sensitive for programs |
| 931 | // with lots of diagnostics and in -E mode. |
| 932 | const unsigned char *NextBuf = (const unsigned char *)Buf; |
| 933 | while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') |
| 934 | ++NextBuf; |
| 935 | Offs += NextBuf-Buf; |
| 936 | Buf = NextBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 938 | if (Buf[0] == '\n' || Buf[0] == '\r') { |
| 939 | // If this is \n\r or \r\n, skip both characters. |
| 940 | if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) |
| 941 | ++Offs, ++Buf; |
| 942 | ++Offs, ++Buf; |
| 943 | LineOffsets.push_back(Offs); |
| 944 | } else { |
| 945 | // Otherwise, this is a null. If end of file, exit. |
| 946 | if (Buf == End) break; |
| 947 | // Otherwise, skip the null. |
| 948 | ++Offs, ++Buf; |
| 949 | } |
| 950 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 952 | // Copy the offsets into the FileInfo structure. |
| 953 | FI->NumLines = LineOffsets.size(); |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 954 | FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size()); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 955 | std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); |
| 956 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 957 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 958 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 959 | /// for the position indicated. This requires building and caching a table of |
| 960 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 961 | /// about to emit a diagnostic. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 962 | unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos, |
| 963 | bool *Invalid) const { |
Argyrios Kyrtzidis | 5adc051 | 2011-05-17 22:09:53 +0000 | [diff] [blame] | 964 | if (FID.isInvalid()) { |
| 965 | if (Invalid) |
| 966 | *Invalid = true; |
| 967 | return 1; |
| 968 | } |
| 969 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 970 | ContentCache *Content; |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 971 | if (LastLineNoFileIDQuery == FID) |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 972 | Content = LastLineNoContentCache; |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 973 | else { |
| 974 | bool MyInvalid = false; |
| 975 | const SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); |
| 976 | if (MyInvalid || !Entry.isFile()) { |
| 977 | if (Invalid) |
| 978 | *Invalid = true; |
| 979 | return 1; |
| 980 | } |
| 981 | |
| 982 | Content = const_cast<ContentCache*>(Entry.getFile().getContentCache()); |
| 983 | } |
| 984 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 985 | // If this is the first use of line information for this buffer, compute the |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 986 | /// SourceLineCache for it on demand. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 987 | if (Content->SourceLineCache == 0) { |
| 988 | bool MyInvalid = false; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 989 | ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 990 | if (Invalid) |
| 991 | *Invalid = MyInvalid; |
| 992 | if (MyInvalid) |
| 993 | return 1; |
| 994 | } else if (Invalid) |
| 995 | *Invalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 996 | |
| 997 | // Okay, we know we have a line number table. Do a binary search to find the |
| 998 | // line number that this character position lands on. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 999 | unsigned *SourceLineCache = Content->SourceLineCache; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1000 | unsigned *SourceLineCacheStart = SourceLineCache; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1001 | unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1003 | unsigned QueriedFilePos = FilePos+1; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1004 | |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1005 | // FIXME: I would like to be convinced that this code is worth being as |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | // complicated as it is, binary search isn't that slow. |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1007 | // |
| 1008 | // If it is worth being optimized, then in my opinion it could be more |
| 1009 | // performant, simpler, and more obviously correct by just "galloping" outward |
| 1010 | // from the queried file position. In fact, this could be incorporated into a |
| 1011 | // generic algorithm such as lower_bound_with_hint. |
| 1012 | // |
| 1013 | // If someone gives me a test case where this matters, and I will do it! - DWD |
| 1014 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1015 | // If the previous query was to the same file, we know both the file pos from |
| 1016 | // that query and the line number returned. This allows us to narrow the |
| 1017 | // search space from the entire file to something near the match. |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1018 | if (LastLineNoFileIDQuery == FID) { |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1019 | if (QueriedFilePos >= LastLineNoFilePos) { |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1020 | // FIXME: Potential overflow? |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1021 | SourceLineCache = SourceLineCache+LastLineNoResult-1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1023 | // The query is likely to be nearby the previous one. Here we check to |
| 1024 | // see if it is within 5, 10 or 20 lines. It can be far away in cases |
| 1025 | // where big comment blocks and vertical whitespace eat up lines but |
| 1026 | // contribute no tokens. |
| 1027 | if (SourceLineCache+5 < SourceLineCacheEnd) { |
| 1028 | if (SourceLineCache[5] > QueriedFilePos) |
| 1029 | SourceLineCacheEnd = SourceLineCache+5; |
| 1030 | else if (SourceLineCache+10 < SourceLineCacheEnd) { |
| 1031 | if (SourceLineCache[10] > QueriedFilePos) |
| 1032 | SourceLineCacheEnd = SourceLineCache+10; |
| 1033 | else if (SourceLineCache+20 < SourceLineCacheEnd) { |
| 1034 | if (SourceLineCache[20] > QueriedFilePos) |
| 1035 | SourceLineCacheEnd = SourceLineCache+20; |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | } else { |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1040 | if (LastLineNoResult < Content->NumLines) |
| 1041 | SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1042 | } |
| 1043 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1045 | // If the spread is large, do a "radix" test as our initial guess, based on |
| 1046 | // the assumption that lines average to approximately the same length. |
| 1047 | // NOTE: This is currently disabled, as it does not appear to be profitable in |
| 1048 | // initial measurements. |
| 1049 | if (0 && SourceLineCacheEnd-SourceLineCache > 20) { |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1050 | unsigned FileLen = Content->SourceLineCache[Content->NumLines-1]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1052 | // Take a stab at guessing where it is. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1053 | unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1055 | // Check for -10 and +10 lines. |
| 1056 | unsigned LowerBound = std::max(int(ApproxPos-10), 0); |
| 1057 | unsigned UpperBound = std::min(ApproxPos+10, FileLen); |
| 1058 | |
| 1059 | // If the computed lower bound is less than the query location, move it in. |
| 1060 | if (SourceLineCache < SourceLineCacheStart+LowerBound && |
| 1061 | SourceLineCacheStart[LowerBound] < QueriedFilePos) |
| 1062 | SourceLineCache = SourceLineCacheStart+LowerBound; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1063 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1064 | // If the computed upper bound is greater than the query location, move it. |
| 1065 | if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound && |
| 1066 | SourceLineCacheStart[UpperBound] >= QueriedFilePos) |
| 1067 | SourceLineCacheEnd = SourceLineCacheStart+UpperBound; |
| 1068 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1070 | unsigned *Pos |
| 1071 | = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1072 | unsigned LineNo = Pos-SourceLineCacheStart; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1074 | LastLineNoFileIDQuery = FID; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1075 | LastLineNoContentCache = Content; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1076 | LastLineNoFilePos = QueriedFilePos; |
| 1077 | LastLineNoResult = LineNo; |
| 1078 | return LineNo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1081 | unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc, |
| 1082 | bool *Invalid) const { |
| 1083 | if (isInvalid(Loc, Invalid)) return 0; |
| 1084 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); |
| 1085 | return getLineNumber(LocInfo.first, LocInfo.second); |
| 1086 | } |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1087 | unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc, |
| 1088 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1089 | if (isInvalid(Loc, Invalid)) return 0; |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1090 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
| 1091 | return getLineNumber(LocInfo.first, LocInfo.second); |
| 1092 | } |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1093 | unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc, |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1094 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1095 | if (isInvalid(Loc, Invalid)) return 0; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1096 | return getPresumedLoc(Loc).getLine(); |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1099 | /// getFileCharacteristic - return the file characteristic of the specified |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | /// source location, indicating whether this is a normal file, a system |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1101 | /// header, or an "implicit extern C" system header. |
| 1102 | /// |
| 1103 | /// This state can be modified with flags on GNU linemarker directives like: |
| 1104 | /// # 4 "foo.h" 3 |
| 1105 | /// which changes all source locations in the current file after that to be |
| 1106 | /// considered to be from a system header. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | SrcMgr::CharacteristicKind |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1108 | SourceManager::getFileCharacteristic(SourceLocation Loc) const { |
| 1109 | assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!"); |
| 1110 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1111 | bool Invalid = false; |
| 1112 | const SLocEntry &SEntry = getSLocEntry(LocInfo.first, &Invalid); |
| 1113 | if (Invalid || !SEntry.isFile()) |
| 1114 | return C_User; |
| 1115 | |
| 1116 | const SrcMgr::FileInfo &FI = SEntry.getFile(); |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1117 | |
| 1118 | // If there are no #line directives in this file, just return the whole-file |
| 1119 | // state. |
| 1120 | if (!FI.hasLineDirectives()) |
| 1121 | return FI.getFileCharacteristic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1123 | assert(LineTable && "Can't have linetable entries without a LineTable!"); |
| 1124 | // See if there is a #line directive before the location. |
| 1125 | const LineEntry *Entry = |
| 1126 | LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1128 | // If this is before the first line marker, use the file characteristic. |
| 1129 | if (!Entry) |
| 1130 | return FI.getFileCharacteristic(); |
| 1131 | |
| 1132 | return Entry->FileKind; |
| 1133 | } |
| 1134 | |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 1135 | /// Return the filename or buffer identifier of the buffer the location is in. |
| 1136 | /// Note that this name does not respect #line directives. Use getPresumedLoc |
| 1137 | /// for normal clients. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1138 | const char *SourceManager::getBufferName(SourceLocation Loc, |
| 1139 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1140 | if (isInvalid(Loc, Invalid)) return "<invalid loc>"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1142 | return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier(); |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1145 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1146 | /// getPresumedLoc - This method returns the "presumed" location of a |
| 1147 | /// SourceLocation specifies. A "presumed location" can be modified by #line |
| 1148 | /// or GNU line marker directives. This provides a view on the data that a |
| 1149 | /// user should see in diagnostics, for example. |
| 1150 | /// |
| 1151 | /// Note that a presumed location is always given as the instantiation point |
| 1152 | /// of an instantiation location, not at the spelling location. |
| 1153 | PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { |
| 1154 | if (Loc.isInvalid()) return PresumedLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1156 | // Presumed locations are always for instantiation points. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 1157 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1158 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1159 | bool Invalid = false; |
| 1160 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); |
| 1161 | if (Invalid || !Entry.isFile()) |
| 1162 | return PresumedLoc(); |
| 1163 | |
| 1164 | const SrcMgr::FileInfo &FI = Entry.getFile(); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1165 | const SrcMgr::ContentCache *C = FI.getContentCache(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1167 | // To get the source name, first consult the FileEntry (if one exists) |
| 1168 | // before the MemBuffer as this will avoid unnecessarily paging in the |
| 1169 | // MemBuffer. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1170 | const char *Filename; |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1171 | if (C->OrigEntry) |
| 1172 | Filename = C->OrigEntry->getName(); |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1173 | else |
| 1174 | Filename = C->getBuffer(Diag, *this)->getBufferIdentifier(); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1175 | |
Douglas Gregor | c417fa0 | 2010-11-02 00:39:22 +0000 | [diff] [blame] | 1176 | unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid); |
| 1177 | if (Invalid) |
| 1178 | return PresumedLoc(); |
| 1179 | unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid); |
| 1180 | if (Invalid) |
| 1181 | return PresumedLoc(); |
| 1182 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1183 | SourceLocation IncludeLoc = FI.getIncludeLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1185 | // If we have #line directives in this file, update and overwrite the physical |
| 1186 | // location info if appropriate. |
| 1187 | if (FI.hasLineDirectives()) { |
| 1188 | assert(LineTable && "Can't have linetable entries without a LineTable!"); |
| 1189 | // See if there is a #line directive before this. If so, get it. |
| 1190 | if (const LineEntry *Entry = |
| 1191 | LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) { |
Chris Lattner | fc39133 | 2009-02-04 02:00:59 +0000 | [diff] [blame] | 1192 | // If the LineEntry indicates a filename, use it. |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1193 | if (Entry->FilenameID != -1) |
| 1194 | Filename = LineTable->getFilename(Entry->FilenameID); |
Chris Lattner | fc39133 | 2009-02-04 02:00:59 +0000 | [diff] [blame] | 1195 | |
| 1196 | // Use the line number specified by the LineEntry. This line number may |
| 1197 | // be multiple lines down from the line entry. Add the difference in |
| 1198 | // physical line numbers from the query point and the line marker to the |
| 1199 | // total. |
| 1200 | unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset); |
| 1201 | LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | |
Chris Lattner | 0e0e5da | 2009-02-04 02:15:40 +0000 | [diff] [blame] | 1203 | // Note that column numbers are not molested by line markers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 1205 | // Handle virtual #include manipulation. |
| 1206 | if (Entry->IncludeOffset) { |
| 1207 | IncludeLoc = getLocForStartOfFile(LocInfo.first); |
| 1208 | IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset); |
| 1209 | } |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Argyrios Kyrtzidis | 469244a | 2011-05-28 03:56:11 +0000 | [diff] [blame] | 1216 | /// \brief Returns true if the given MacroID location points at the first |
| 1217 | /// token of the macro instantiation. |
| 1218 | bool SourceManager::isAtStartOfMacroInstantiation(SourceLocation loc) const { |
| 1219 | assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); |
| 1220 | |
| 1221 | unsigned FID = getFileID(loc).ID; |
| 1222 | assert(FID > 1); |
| 1223 | std::pair<SourceLocation, SourceLocation> |
| 1224 | instRange = getImmediateInstantiationRange(loc); |
| 1225 | |
| 1226 | bool invalid = false; |
| 1227 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID-1, &invalid); |
| 1228 | if (invalid) |
| 1229 | return false; |
| 1230 | |
| 1231 | // If the FileID immediately before it is a file then this is the first token |
| 1232 | // in the macro. |
| 1233 | if (Entry.isFile()) |
| 1234 | return true; |
| 1235 | |
| 1236 | // If the FileID immediately before it (which is a macro token) is the |
| 1237 | // immediate instantiated macro, check this macro token's location. |
| 1238 | if (getFileID(instRange.second).ID == FID-1) |
| 1239 | return isAtStartOfMacroInstantiation(instRange.first); |
| 1240 | |
| 1241 | // If the FileID immediately before it (which is a macro token) came from a |
| 1242 | // different instantiation, then this is the first token in the macro. |
| 1243 | if (getInstantiationLoc(Entry.getInstantiation().getInstantiationLocStart()) |
| 1244 | != getInstantiationLoc(loc)) |
| 1245 | return true; |
| 1246 | |
| 1247 | // It is inside the macro or the last token in the macro. |
| 1248 | return false; |
| 1249 | } |
| 1250 | |
| 1251 | /// \brief Returns true if the given MacroID location points at the last |
| 1252 | /// token of the macro instantiation. |
| 1253 | bool SourceManager::isAtEndOfMacroInstantiation(SourceLocation loc) const { |
| 1254 | assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); |
| 1255 | |
| 1256 | unsigned FID = getFileID(loc).ID; |
| 1257 | assert(FID > 1); |
| 1258 | std::pair<SourceLocation, SourceLocation> |
| 1259 | instRange = getInstantiationRange(loc); |
| 1260 | |
| 1261 | // If there's no FileID after it, it is the last token in the macro. |
| 1262 | if (FID+1 == sloc_entry_size()) |
| 1263 | return true; |
| 1264 | |
| 1265 | bool invalid = false; |
| 1266 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID+1, &invalid); |
| 1267 | if (invalid) |
| 1268 | return false; |
| 1269 | |
| 1270 | // If the FileID immediately after it is a file or a macro token which |
| 1271 | // came from a different instantiation, then this is the last token in the |
| 1272 | // macro. |
| 1273 | if (Entry.isFile()) |
| 1274 | return true; |
| 1275 | if (getInstantiationLoc(Entry.getInstantiation().getInstantiationLocStart()) |
| 1276 | != instRange.first) |
| 1277 | return true; |
| 1278 | |
| 1279 | // It is inside the macro or the first token in the macro. |
| 1280 | return false; |
| 1281 | } |
| 1282 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1283 | //===----------------------------------------------------------------------===// |
| 1284 | // Other miscellaneous methods. |
| 1285 | //===----------------------------------------------------------------------===// |
| 1286 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1287 | /// \brief Retrieve the inode for the given file entry, if possible. |
| 1288 | /// |
| 1289 | /// This routine involves a system call, and therefore should only be used |
| 1290 | /// in non-performance-critical code. |
| 1291 | static llvm::Optional<ino_t> getActualFileInode(const FileEntry *File) { |
| 1292 | if (!File) |
| 1293 | return llvm::Optional<ino_t>(); |
| 1294 | |
| 1295 | struct stat StatBuf; |
| 1296 | if (::stat(File->getName(), &StatBuf)) |
| 1297 | return llvm::Optional<ino_t>(); |
| 1298 | |
| 1299 | return StatBuf.st_ino; |
| 1300 | } |
| 1301 | |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1302 | /// \brief Get the source location for the given file:line:col triplet. |
| 1303 | /// |
| 1304 | /// If the source file is included multiple times, the source location will |
| 1305 | /// be based upon the first inclusion. |
| 1306 | SourceLocation SourceManager::getLocation(const FileEntry *SourceFile, |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1307 | unsigned Line, unsigned Col) { |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1308 | assert(SourceFile && "Null source file!"); |
| 1309 | assert(Line && Col && "Line and column should start from 1!"); |
| 1310 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1311 | // Find the first file ID that corresponds to the given file. |
| 1312 | FileID FirstFID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1314 | // First, check the main file ID, since it is common to look for a |
| 1315 | // location in the main file. |
| 1316 | llvm::Optional<ino_t> SourceFileInode; |
| 1317 | llvm::Optional<llvm::StringRef> SourceFileName; |
| 1318 | if (!MainFileID.isInvalid()) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1319 | bool Invalid = false; |
| 1320 | const SLocEntry &MainSLoc = getSLocEntry(MainFileID, &Invalid); |
| 1321 | if (Invalid) |
| 1322 | return SourceLocation(); |
| 1323 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1324 | if (MainSLoc.isFile()) { |
| 1325 | const ContentCache *MainContentCache |
| 1326 | = MainSLoc.getFile().getContentCache(); |
Douglas Gregor | b7a1841 | 2011-02-11 18:08:15 +0000 | [diff] [blame] | 1327 | if (!MainContentCache) { |
| 1328 | // Can't do anything |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1329 | } else if (MainContentCache->OrigEntry == SourceFile) { |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1330 | FirstFID = MainFileID; |
Douglas Gregor | b7a1841 | 2011-02-11 18:08:15 +0000 | [diff] [blame] | 1331 | } else { |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1332 | // Fall back: check whether we have the same base name and inode |
| 1333 | // as the main file. |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1334 | const FileEntry *MainFile = MainContentCache->OrigEntry; |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1335 | SourceFileName = llvm::sys::path::filename(SourceFile->getName()); |
| 1336 | if (*SourceFileName == llvm::sys::path::filename(MainFile->getName())) { |
| 1337 | SourceFileInode = getActualFileInode(SourceFile); |
Douglas Gregor | 37c02bf | 2011-02-16 19:09:24 +0000 | [diff] [blame] | 1338 | if (SourceFileInode) { |
| 1339 | if (llvm::Optional<ino_t> MainFileInode |
| 1340 | = getActualFileInode(MainFile)) { |
| 1341 | if (*SourceFileInode == *MainFileInode) { |
| 1342 | FirstFID = MainFileID; |
| 1343 | SourceFile = MainFile; |
| 1344 | } |
| 1345 | } |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | if (FirstFID.isInvalid()) { |
| 1353 | // The location we're looking for isn't in the main file; look |
| 1354 | // through all of the source locations. |
| 1355 | for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1356 | bool Invalid = false; |
| 1357 | const SLocEntry &SLoc = getSLocEntry(I, &Invalid); |
| 1358 | if (Invalid) |
| 1359 | return SourceLocation(); |
| 1360 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1361 | if (SLoc.isFile() && |
| 1362 | SLoc.getFile().getContentCache() && |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1363 | SLoc.getFile().getContentCache()->OrigEntry == SourceFile) { |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1364 | FirstFID = FileID::get(I); |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | // If we haven't found what we want yet, try again, but this time stat() |
| 1371 | // each of the files in case the files have changed since we originally |
| 1372 | // parsed the file. |
| 1373 | if (FirstFID.isInvalid() && |
| 1374 | (SourceFileName || |
| 1375 | (SourceFileName = llvm::sys::path::filename(SourceFile->getName()))) && |
| 1376 | (SourceFileInode || |
| 1377 | (SourceFileInode = getActualFileInode(SourceFile)))) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1378 | bool Invalid = false; |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1379 | for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1380 | const SLocEntry &SLoc = getSLocEntry(I, &Invalid); |
| 1381 | if (Invalid) |
| 1382 | return SourceLocation(); |
| 1383 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1384 | if (SLoc.isFile()) { |
| 1385 | const ContentCache *FileContentCache |
| 1386 | = SLoc.getFile().getContentCache(); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1387 | const FileEntry *Entry =FileContentCache? FileContentCache->OrigEntry : 0; |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1388 | if (Entry && |
Douglas Gregor | b7a1841 | 2011-02-11 18:08:15 +0000 | [diff] [blame] | 1389 | *SourceFileName == llvm::sys::path::filename(Entry->getName())) { |
| 1390 | if (llvm::Optional<ino_t> EntryInode = getActualFileInode(Entry)) { |
| 1391 | if (*SourceFileInode == *EntryInode) { |
| 1392 | FirstFID = FileID::get(I); |
| 1393 | SourceFile = Entry; |
| 1394 | break; |
| 1395 | } |
| 1396 | } |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | if (FirstFID.isInvalid()) |
| 1403 | return SourceLocation(); |
| 1404 | |
| 1405 | if (Line == 1 && Col == 1) |
| 1406 | return getLocForStartOfFile(FirstFID); |
| 1407 | |
| 1408 | ContentCache *Content |
| 1409 | = const_cast<ContentCache *>(getOrCreateContentCache(SourceFile)); |
| 1410 | if (!Content) |
| 1411 | return SourceLocation(); |
| 1412 | |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1413 | // If this is the first use of line information for this buffer, compute the |
| 1414 | /// SourceLineCache for it on demand. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1415 | if (Content->SourceLineCache == 0) { |
| 1416 | bool MyInvalid = false; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1417 | ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1418 | if (MyInvalid) |
| 1419 | return SourceLocation(); |
| 1420 | } |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1421 | |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1422 | if (Line > Content->NumLines) { |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1423 | unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize(); |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1424 | if (Size > 0) |
| 1425 | --Size; |
| 1426 | return getLocForStartOfFile(FirstFID).getFileLocWithOffset(Size); |
| 1427 | } |
| 1428 | |
| 1429 | unsigned FilePos = Content->SourceLineCache[Line - 1]; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1430 | const char *Buf = Content->getBuffer(Diag, *this)->getBufferStart() + FilePos; |
| 1431 | unsigned BufLength = Content->getBuffer(Diag, *this)->getBufferEnd() - Buf; |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1432 | unsigned i = 0; |
| 1433 | |
| 1434 | // Check that the given column is valid. |
| 1435 | while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r') |
| 1436 | ++i; |
| 1437 | if (i < Col-1) |
| 1438 | return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + i); |
| 1439 | |
Douglas Gregor | 4a160e1 | 2009-12-02 05:34:39 +0000 | [diff] [blame] | 1440 | return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + Col - 1); |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1443 | /// Given a decomposed source location, move it up the include/instantiation |
| 1444 | /// stack to the parent source location. If this is possible, return the |
| 1445 | /// decomposed version of the parent in Loc and return false. If Loc is the |
| 1446 | /// top-level entry, return true and don't modify it. |
| 1447 | static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc, |
| 1448 | const SourceManager &SM) { |
| 1449 | SourceLocation UpperLoc; |
| 1450 | const SrcMgr::SLocEntry &Entry = SM.getSLocEntry(Loc.first); |
| 1451 | if (Entry.isInstantiation()) |
| 1452 | UpperLoc = Entry.getInstantiation().getInstantiationLocStart(); |
| 1453 | else |
| 1454 | UpperLoc = Entry.getFile().getIncludeLoc(); |
| 1455 | |
| 1456 | if (UpperLoc.isInvalid()) |
| 1457 | return true; // We reached the top. |
| 1458 | |
| 1459 | Loc = SM.getDecomposedLoc(UpperLoc); |
| 1460 | return false; |
| 1461 | } |
| 1462 | |
| 1463 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1464 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 1465 | /// |
| 1466 | /// \returns true if LHS source location comes before RHS, false otherwise. |
| 1467 | bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS, |
| 1468 | SourceLocation RHS) const { |
| 1469 | assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!"); |
| 1470 | if (LHS == RHS) |
| 1471 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
Argyrios Kyrtzidis | ee933e1 | 2010-12-24 02:53:53 +0000 | [diff] [blame] | 1473 | // If both locations are macro instantiations, the order of their offsets |
| 1474 | // reflect the order that the tokens, pointed to by these locations, were |
| 1475 | // instantiated (during parsing each token that is instantiated by a macro, |
| 1476 | // expands the SLocEntries). |
| 1477 | if (LHS.isMacroID() && RHS.isMacroID()) |
| 1478 | return LHS.getOffset() < RHS.getOffset(); |
| 1479 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1480 | std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS); |
| 1481 | std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1483 | // If the source locations are in the same file, just compare offsets. |
| 1484 | if (LOffs.first == ROffs.first) |
| 1485 | return LOffs.second < ROffs.second; |
| 1486 | |
| 1487 | // If we are comparing a source location with multiple locations in the same |
| 1488 | // file, we get a big win by caching the result. |
Chris Lattner | 66a915f | 2010-05-07 05:10:46 +0000 | [diff] [blame] | 1489 | if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first)) |
| 1490 | return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 1492 | // Okay, we missed in the cache, start updating the cache for this query. |
| 1493 | IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1495 | // "Traverse" the include/instantiation stacks of both locations and try to |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1496 | // find a common "ancestor". FileIDs build a tree-like structure that |
| 1497 | // reflects the #include hierarchy, and this algorithm needs to find the |
| 1498 | // nearest common ancestor between the two locations. For example, if you |
| 1499 | // have a.c that includes b.h and c.h, and are comparing a location in b.h to |
| 1500 | // a location in c.h, we need to find that their nearest common ancestor is |
| 1501 | // a.c, and compare the locations of the two #includes to find their relative |
| 1502 | // ordering. |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1503 | // |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1504 | // SourceManager assigns FileIDs in order of parsing. This means that an |
| 1505 | // includee always has a larger FileID than an includer. While you might |
| 1506 | // think that we could just compare the FileID's here, that doesn't work to |
| 1507 | // compare a point at the end of a.c with a point within c.h. Though c.h has |
| 1508 | // a larger FileID, we have to compare the include point of c.h to the |
| 1509 | // location in a.c. |
| 1510 | // |
| 1511 | // Despite not being able to directly compare FileID's, we can tell that a |
| 1512 | // larger FileID is necessarily more deeply nested than a lower one and use |
| 1513 | // this information to walk up the tree to the nearest common ancestor. |
| 1514 | do { |
| 1515 | // If LOffs is larger than ROffs, then LOffs must be more deeply nested than |
| 1516 | // ROffs, walk up the #include chain. |
| 1517 | if (LOffs.first.ID > ROffs.first.ID) { |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1518 | if (MoveUpIncludeHierarchy(LOffs, *this)) |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1519 | break; // We reached the top. |
| 1520 | |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1521 | } else { |
| 1522 | // Otherwise, ROffs is larger than LOffs, so ROffs must be more deeply |
| 1523 | // nested than LOffs, walk up the #include chain. |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1524 | if (MoveUpIncludeHierarchy(ROffs, *this)) |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1525 | break; // We reached the top. |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 1526 | } |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1527 | } while (LOffs.first != ROffs.first); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1529 | // If we exited because we found a nearest common ancestor, compare the |
| 1530 | // locations within the common file and cache them. |
| 1531 | if (LOffs.first == ROffs.first) { |
| 1532 | IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second); |
| 1533 | return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second); |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1534 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | |
Daniel Dunbar | fbcc7be | 2009-12-01 23:07:57 +0000 | [diff] [blame] | 1536 | // There is no common ancestor, most probably because one location is in the |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 1537 | // predefines buffer or an AST file. |
Daniel Dunbar | fbcc7be | 2009-12-01 23:07:57 +0000 | [diff] [blame] | 1538 | // FIXME: We should rearrange the external interface so this simply never |
| 1539 | // happens; it can't conceptually happen. Also see PR5662. |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1540 | IsBeforeInTUCache.setQueryFIDs(FileID(), FileID()); // Don't try caching. |
| 1541 | |
| 1542 | // Zip both entries up to the top level record. |
| 1543 | while (!MoveUpIncludeHierarchy(LOffs, *this)) /*empty*/; |
| 1544 | while (!MoveUpIncludeHierarchy(ROffs, *this)) /*empty*/; |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1546 | // If exactly one location is a memory buffer, assume it precedes the other. |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Strip off macro instantation locations, going up to the top-level File |
| 1549 | // SLocEntry. |
| 1550 | bool LIsMB = getFileEntryForID(LOffs.first) == 0; |
| 1551 | bool RIsMB = getFileEntryForID(ROffs.first) == 0; |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1552 | if (LIsMB != RIsMB) |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 1553 | return LIsMB; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | |
Daniel Dunbar | fbcc7be | 2009-12-01 23:07:57 +0000 | [diff] [blame] | 1555 | // Otherwise, just assume FileIDs were created in order. |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 1556 | return LOffs.first < ROffs.first; |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1557 | } |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1558 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | /// PrintStats - Print statistics to stderr. |
| 1560 | /// |
| 1561 | void SourceManager::PrintStats() const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 1562 | llvm::errs() << "\n*** Source Manager Stats:\n"; |
| 1563 | llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size() |
| 1564 | << " mem buffers mapped.\n"; |
| 1565 | llvm::errs() << SLocEntryTable.size() << " SLocEntry's allocated, " |
| 1566 | << NextOffset << "B of Sloc address space used.\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1568 | unsigned NumLineNumsComputed = 0; |
| 1569 | unsigned NumFileBytesMapped = 0; |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 1570 | for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){ |
| 1571 | NumLineNumsComputed += I->second->SourceLineCache != 0; |
| 1572 | NumFileBytesMapped += I->second->getSizeBytesMapped(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1573 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 1575 | llvm::errs() << NumFileBytesMapped << " bytes of files mapped, " |
| 1576 | << NumLineNumsComputed << " files with line #'s computed.\n"; |
| 1577 | llvm::errs() << "FileID scans: " << NumLinearScans << " linear, " |
| 1578 | << NumBinaryProbes << " binary.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1579 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1580 | |
| 1581 | ExternalSLocEntrySource::~ExternalSLocEntrySource() { } |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 1582 | |
| 1583 | /// Return the amount of memory used by memory buffers, breaking down |
| 1584 | /// by heap-backed versus mmap'ed memory. |
| 1585 | SourceManager::MemoryBufferSizes SourceManager::getMemoryBufferSizes() const { |
| 1586 | size_t malloc_bytes = 0; |
| 1587 | size_t mmap_bytes = 0; |
| 1588 | |
| 1589 | for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) |
| 1590 | if (size_t sized_mapped = MemBufferInfos[i]->getSizeBytesMapped()) |
| 1591 | switch (MemBufferInfos[i]->getMemoryBufferKind()) { |
| 1592 | case llvm::MemoryBuffer::MemoryBuffer_MMap: |
| 1593 | mmap_bytes += sized_mapped; |
| 1594 | break; |
| 1595 | case llvm::MemoryBuffer::MemoryBuffer_Malloc: |
| 1596 | malloc_bytes += sized_mapped; |
| 1597 | break; |
| 1598 | } |
| 1599 | |
| 1600 | return MemoryBufferSizes(malloc_bytes, mmap_bytes); |
| 1601 | } |
| 1602 | |