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" |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
Ted Kremenek | 6e36c12 | 2011-07-27 18:41:16 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Capacity.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 27 | #include <string> |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 28 | #include <cstring> |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 29 | #include <sys/stat.h> |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 30 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace SrcMgr; |
| 33 | using llvm::MemoryBuffer; |
| 34 | |
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 | // SourceManager Helper Classes |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 39 | ContentCache::~ContentCache() { |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 40 | if (shouldFreeBuffer()) |
| 41 | delete Buffer.getPointer(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 44 | /// getSizeBytesMapped - Returns the number of bytes actually mapped for this |
| 45 | /// ContentCache. This can be 0 if the MemBuffer was not actually expanded. |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 46 | unsigned ContentCache::getSizeBytesMapped() const { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 47 | return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0; |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 50 | /// Returns the kind of memory used to back the memory buffer for |
| 51 | /// this content cache. This is used for performance analysis. |
| 52 | llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind() const { |
| 53 | assert(Buffer.getPointer()); |
| 54 | |
| 55 | // Should be unreachable, but keep for sanity. |
| 56 | if (!Buffer.getPointer()) |
| 57 | return llvm::MemoryBuffer::MemoryBuffer_Malloc; |
| 58 | |
| 59 | const llvm::MemoryBuffer *buf = Buffer.getPointer(); |
| 60 | return buf->getBufferKind(); |
| 61 | } |
| 62 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 63 | /// getSize - Returns the size of the content encapsulated by this ContentCache. |
| 64 | /// This can be the size of the source file or the size of an arbitrary |
| 65 | /// scratch buffer. If the ContentCache encapsulates a source file, that |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 66 | /// file is not lazily brought in from disk to satisfy this query. |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 67 | unsigned ContentCache::getSize() const { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 68 | return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize() |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 69 | : (unsigned) ContentsEntry->getSize(); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 72 | void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B, |
| 73 | bool DoNotFree) { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 74 | assert(B != Buffer.getPointer()); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 76 | if (shouldFreeBuffer()) |
| 77 | delete Buffer.getPointer(); |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 78 | Buffer.setPointer(B); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 79 | Buffer.setInt(DoNotFree? DoNotFreeFlag : 0); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 80 | } |
| 81 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 82 | const llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag, |
Chris Lattner | 5c5db4e | 2010-04-20 20:49:23 +0000 | [diff] [blame] | 83 | const SourceManager &SM, |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 84 | SourceLocation Loc, |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 85 | bool *Invalid) const { |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 86 | // Lazily create the Buffer for ContentCaches that wrap files. If we already |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 87 | // computed it, just return what we have. |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 88 | if (Buffer.getPointer() || ContentsEntry == 0) { |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 89 | if (Invalid) |
| 90 | *Invalid = isBufferInvalid(); |
Chris Lattner | 38caec4 | 2010-04-20 18:14:03 +0000 | [diff] [blame] | 91 | |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 92 | return Buffer.getPointer(); |
| 93 | } |
Benjamin Kramer | 5807d9c | 2010-11-18 12:46:39 +0000 | [diff] [blame] | 94 | |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 95 | std::string ErrorStr; |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 96 | Buffer.setPointer(SM.getFileManager().getBufferForFile(ContentsEntry, &ErrorStr)); |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 97 | |
| 98 | // If we were unable to open the file, then we are in an inconsistent |
| 99 | // situation where the content cache referenced a file which no longer |
| 100 | // exists. Most likely, we were using a stat cache with an invalid entry but |
| 101 | // the file could also have been removed during processing. Since we can't |
| 102 | // really deal with this situation, just create an empty buffer. |
| 103 | // |
| 104 | // FIXME: This is definitely not ideal, but our immediate clients can't |
| 105 | // currently handle returning a null entry here. Ideally we should detect |
| 106 | // that we are in an inconsistent situation and error out as quickly as |
| 107 | // possible. |
| 108 | if (!Buffer.getPointer()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 109 | const StringRef FillStr("<<<MISSING SOURCE FILE>>>\n"); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 110 | Buffer.setPointer(MemoryBuffer::getNewMemBuffer(ContentsEntry->getSize(), |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 111 | "<invalid>")); |
| 112 | char *Ptr = const_cast<char*>(Buffer.getPointer()->getBufferStart()); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 113 | for (unsigned i = 0, e = ContentsEntry->getSize(); i != e; ++i) |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 114 | Ptr[i] = FillStr[i % FillStr.size()]; |
| 115 | |
| 116 | if (Diag.isDiagnosticInFlight()) |
| 117 | Diag.SetDelayedDiagnostic(diag::err_cannot_open_file, |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 118 | ContentsEntry->getName(), ErrorStr); |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 119 | else |
| 120 | Diag.Report(Loc, diag::err_cannot_open_file) |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 121 | << ContentsEntry->getName() << ErrorStr; |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 122 | |
| 123 | Buffer.setInt(Buffer.getInt() | InvalidFlag); |
| 124 | |
| 125 | if (Invalid) *Invalid = true; |
| 126 | return Buffer.getPointer(); |
| 127 | } |
| 128 | |
| 129 | // Check that the file's size is the same as in the file entry (which may |
| 130 | // have come from a stat cache). |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 131 | if (getRawBuffer()->getBufferSize() != (size_t)ContentsEntry->getSize()) { |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 132 | if (Diag.isDiagnosticInFlight()) |
| 133 | Diag.SetDelayedDiagnostic(diag::err_file_modified, |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 134 | ContentsEntry->getName()); |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 135 | else |
| 136 | Diag.Report(Loc, diag::err_file_modified) |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 137 | << ContentsEntry->getName(); |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 138 | |
| 139 | Buffer.setInt(Buffer.getInt() | InvalidFlag); |
| 140 | if (Invalid) *Invalid = true; |
| 141 | return Buffer.getPointer(); |
| 142 | } |
Eric Christopher | 156119d | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 143 | |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 144 | // 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] | 145 | // (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] | 146 | // http://en.wikipedia.org/wiki/Byte_order_mark for more information. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 147 | StringRef BufStr = Buffer.getPointer()->getBuffer(); |
Eric Christopher | 156119d | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 148 | const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr) |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 149 | .StartsWith("\xFE\xFF", "UTF-16 (BE)") |
| 150 | .StartsWith("\xFF\xFE", "UTF-16 (LE)") |
| 151 | .StartsWith("\x00\x00\xFE\xFF", "UTF-32 (BE)") |
| 152 | .StartsWith("\xFF\xFE\x00\x00", "UTF-32 (LE)") |
| 153 | .StartsWith("\x2B\x2F\x76", "UTF-7") |
| 154 | .StartsWith("\xF7\x64\x4C", "UTF-1") |
| 155 | .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC") |
| 156 | .StartsWith("\x0E\xFE\xFF", "SDSU") |
| 157 | .StartsWith("\xFB\xEE\x28", "BOCU-1") |
| 158 | .StartsWith("\x84\x31\x95\x33", "GB-18030") |
| 159 | .Default(0); |
| 160 | |
Eric Christopher | 156119d | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 161 | if (InvalidBOM) { |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 162 | Diag.Report(Loc, diag::err_unsupported_bom) |
Eric Christopher | 156119d | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 163 | << InvalidBOM << ContentsEntry->getName(); |
Chris Lattner | b088cd3 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 164 | Buffer.setInt(Buffer.getInt() | InvalidFlag); |
Ted Kremenek | 5b034ad | 2009-01-06 22:43:04 +0000 | [diff] [blame] | 165 | } |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 166 | |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 167 | if (Invalid) |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 168 | *Invalid = isBufferInvalid(); |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 169 | |
| 170 | return Buffer.getPointer(); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 173 | unsigned LineTableInfo::getLineTableFilenameID(StringRef Name) { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 174 | // Look up the filename in the string table, returning the pre-existing value |
| 175 | // if it exists. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | llvm::StringMapEntry<unsigned> &Entry = |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 177 | FilenameIDs.GetOrCreateValue(Name, ~0U); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 178 | if (Entry.getValue() != ~0U) |
| 179 | return Entry.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 181 | // Otherwise, assign this the next available ID. |
| 182 | Entry.setValue(FilenamesByID.size()); |
| 183 | FilenamesByID.push_back(&Entry); |
| 184 | return FilenamesByID.size()-1; |
| 185 | } |
| 186 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 187 | /// AddLineNote - Add a line note to the line table that indicates that there |
| 188 | /// is a #line at the specified FID/Offset location which changes the presumed |
| 189 | /// location to LineNo/FilenameID. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 190 | void LineTableInfo::AddLineNote(int FID, unsigned Offset, |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 191 | unsigned LineNo, int FilenameID) { |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 192 | std::vector<LineEntry> &Entries = LineEntries[FID]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 194 | assert((Entries.empty() || Entries.back().FileOffset < Offset) && |
| 195 | "Adding line entries out of order!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 197 | SrcMgr::CharacteristicKind Kind = SrcMgr::C_User; |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 198 | unsigned IncludeOffset = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 200 | if (!Entries.empty()) { |
| 201 | // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember |
| 202 | // that we are still in "foo.h". |
| 203 | if (FilenameID == -1) |
| 204 | FilenameID = Entries.back().FilenameID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 206 | // If we are after a line marker that switched us to system header mode, or |
| 207 | // that set #include information, preserve it. |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 208 | Kind = Entries.back().FileKind; |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 209 | IncludeOffset = Entries.back().IncludeOffset; |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 210 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 212 | Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind, |
| 213 | IncludeOffset)); |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 216 | /// AddLineNote This is the same as the previous version of AddLineNote, but is |
| 217 | /// used for GNU line markers. If EntryExit is 0, then this doesn't change the |
| 218 | /// presumed #include stack. If it is 1, this is a file entry, if it is 2 then |
| 219 | /// this is a file exit. FileKind specifies whether this is a system header or |
| 220 | /// extern C system header. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 221 | void LineTableInfo::AddLineNote(int FID, unsigned Offset, |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 222 | unsigned LineNo, int FilenameID, |
| 223 | unsigned EntryExit, |
| 224 | SrcMgr::CharacteristicKind FileKind) { |
| 225 | assert(FilenameID != -1 && "Unspecified filename should use other accessor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 227 | std::vector<LineEntry> &Entries = LineEntries[FID]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 229 | assert((Entries.empty() || Entries.back().FileOffset < Offset) && |
| 230 | "Adding line entries out of order!"); |
| 231 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 232 | unsigned IncludeOffset = 0; |
| 233 | if (EntryExit == 0) { // No #include stack change. |
| 234 | IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset; |
| 235 | } else if (EntryExit == 1) { |
| 236 | IncludeOffset = Offset-1; |
| 237 | } else if (EntryExit == 2) { |
| 238 | assert(!Entries.empty() && Entries.back().IncludeOffset && |
| 239 | "PPDirectives should have caught case when popping empty include stack"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 241 | // Get the include loc of the last entries' include loc as our include loc. |
| 242 | IncludeOffset = 0; |
| 243 | if (const LineEntry *PrevEntry = |
| 244 | FindNearestLineEntry(FID, Entries.back().IncludeOffset)) |
| 245 | IncludeOffset = PrevEntry->IncludeOffset; |
| 246 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 248 | Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind, |
| 249 | IncludeOffset)); |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 253 | /// FindNearestLineEntry - Find the line entry nearest to FID that is before |
| 254 | /// it. If there is no line entry before Offset in FID, return null. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 255 | const LineEntry *LineTableInfo::FindNearestLineEntry(int FID, |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 256 | unsigned Offset) { |
| 257 | const std::vector<LineEntry> &Entries = LineEntries[FID]; |
| 258 | assert(!Entries.empty() && "No #line entries for this FID after all!"); |
| 259 | |
Chris Lattner | 6c1fbe0 | 2009-02-04 04:46:59 +0000 | [diff] [blame] | 260 | // It is very common for the query to be after the last #line, check this |
| 261 | // first. |
| 262 | if (Entries.back().FileOffset <= Offset) |
| 263 | return &Entries.back(); |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 6c1fbe0 | 2009-02-04 04:46:59 +0000 | [diff] [blame] | 265 | // Do a binary search to find the maximal element that is still before Offset. |
| 266 | std::vector<LineEntry>::const_iterator I = |
| 267 | std::upper_bound(Entries.begin(), Entries.end(), Offset); |
| 268 | if (I == Entries.begin()) return 0; |
| 269 | return &*--I; |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 270 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 272 | /// \brief Add a new line entry that has already been encoded into |
| 273 | /// the internal representation of the line table. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 274 | void LineTableInfo::AddEntry(int FID, |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 275 | const std::vector<LineEntry> &Entries) { |
| 276 | LineEntries[FID] = Entries; |
| 277 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 279 | /// getLineTableFilenameID - Return the uniqued ID for the specified filename. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | /// |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 281 | unsigned SourceManager::getLineTableFilenameID(StringRef Name) { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 282 | if (LineTable == 0) |
| 283 | LineTable = new LineTableInfo(); |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 284 | return LineTable->getLineTableFilenameID(Name); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 288 | /// AddLineNote - Add a line note to the line table for the FileID and offset |
| 289 | /// specified by Loc. If FilenameID is -1, it is considered to be |
| 290 | /// unspecified. |
| 291 | void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, |
| 292 | int FilenameID) { |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 293 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 295 | bool Invalid = false; |
| 296 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); |
| 297 | if (!Entry.isFile() || Invalid) |
| 298 | return; |
| 299 | |
| 300 | const SrcMgr::FileInfo &FileInfo = Entry.getFile(); |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 301 | |
| 302 | // Remember that this file has #line directives now if it doesn't already. |
| 303 | const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 305 | if (LineTable == 0) |
| 306 | LineTable = new LineTableInfo(); |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 307 | LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID); |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 310 | /// AddLineNote - Add a GNU line marker to the line table. |
| 311 | void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, |
| 312 | int FilenameID, bool IsFileEntry, |
| 313 | bool IsFileExit, bool IsSystemHeader, |
| 314 | bool IsExternCHeader) { |
| 315 | // If there is no filename and no flags, this is treated just like a #line, |
| 316 | // which does not change the flags of the previous line marker. |
| 317 | if (FilenameID == -1) { |
| 318 | assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader && |
| 319 | "Can't set flags without setting the filename!"); |
| 320 | return AddLineNote(Loc, LineNo, FilenameID); |
| 321 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 323 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 324 | |
| 325 | bool Invalid = false; |
| 326 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); |
| 327 | if (!Entry.isFile() || Invalid) |
| 328 | return; |
| 329 | |
| 330 | const SrcMgr::FileInfo &FileInfo = Entry.getFile(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 332 | // Remember that this file has #line directives now if it doesn't already. |
| 333 | const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 335 | if (LineTable == 0) |
| 336 | LineTable = new LineTableInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 338 | SrcMgr::CharacteristicKind FileKind; |
| 339 | if (IsExternCHeader) |
| 340 | FileKind = SrcMgr::C_ExternCSystem; |
| 341 | else if (IsSystemHeader) |
| 342 | FileKind = SrcMgr::C_System; |
| 343 | else |
| 344 | FileKind = SrcMgr::C_User; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 346 | unsigned EntryExit = 0; |
| 347 | if (IsFileEntry) |
| 348 | EntryExit = 1; |
| 349 | else if (IsFileExit) |
| 350 | EntryExit = 2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 352 | LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID, |
| 353 | EntryExit, FileKind); |
| 354 | } |
| 355 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 356 | LineTableInfo &SourceManager::getLineTable() { |
| 357 | if (LineTable == 0) |
| 358 | LineTable = new LineTableInfo(); |
| 359 | return *LineTable; |
| 360 | } |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 362 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 363 | // Private 'Create' methods. |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 364 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 365 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 366 | SourceManager::SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr) |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 367 | : Diag(Diag), FileMgr(FileMgr), OverridenFilesKeepOriginalName(true), |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 368 | ExternalSLocEntries(0), LineTable(0), NumLinearScans(0), |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 369 | NumBinaryProbes(0), FakeBufferForRecovery(0) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 370 | clearIDTables(); |
| 371 | Diag.setSourceManager(this); |
| 372 | } |
| 373 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 374 | SourceManager::~SourceManager() { |
| 375 | delete LineTable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 377 | // Delete FileEntry objects corresponding to content caches. Since the actual |
| 378 | // content cache objects are bump pointer allocated, we just have to run the |
| 379 | // dtors, but we call the deallocate method for completeness. |
| 380 | for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) { |
| 381 | MemBufferInfos[i]->~ContentCache(); |
| 382 | ContentCacheAlloc.Deallocate(MemBufferInfos[i]); |
| 383 | } |
| 384 | for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator |
| 385 | I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { |
| 386 | I->second->~ContentCache(); |
| 387 | ContentCacheAlloc.Deallocate(I->second); |
| 388 | } |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 389 | |
| 390 | delete FakeBufferForRecovery; |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 391 | |
| 392 | for (llvm::DenseMap<FileID, MacroArgsMap *>::iterator |
| 393 | I = MacroArgsCacheMap.begin(),E = MacroArgsCacheMap.end(); I!=E; ++I) { |
| 394 | delete I->second; |
| 395 | } |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void SourceManager::clearIDTables() { |
| 399 | MainFileID = FileID(); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 400 | LocalSLocEntryTable.clear(); |
| 401 | LoadedSLocEntryTable.clear(); |
| 402 | SLocEntryLoaded.clear(); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 403 | LastLineNoFileIDQuery = FileID(); |
| 404 | LastLineNoContentCache = 0; |
| 405 | LastFileIDLookup = FileID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 407 | if (LineTable) |
| 408 | LineTable->clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 410 | // Use up FileID #0 as an invalid expansion. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 411 | NextLocalOffset = 0; |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 412 | CurrentLoadedOffset = MaxLoadedOffset; |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 413 | createExpansionLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 416 | /// getOrCreateContentCache - Create or return a cached ContentCache for the |
| 417 | /// specified file. |
| 418 | const ContentCache * |
| 419 | SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 420 | assert(FileEnt && "Didn't specify a file entry to use?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | // Do we already have information about this file? |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 423 | ContentCache *&Entry = FileInfos[FileEnt]; |
| 424 | if (Entry) return Entry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 426 | // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned |
| 427 | // so that FileInfo can use the low 3 bits of the pointer for its own |
| 428 | // nefarious purposes. |
| 429 | unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; |
| 430 | EntryAlign = std::max(8U, EntryAlign); |
| 431 | Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 432 | |
| 433 | // If the file contents are overridden with contents from another file, |
| 434 | // pass that file to ContentCache. |
| 435 | llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator |
| 436 | overI = OverriddenFiles.find(FileEnt); |
| 437 | if (overI == OverriddenFiles.end()) |
| 438 | new (Entry) ContentCache(FileEnt); |
| 439 | else |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 440 | new (Entry) ContentCache(OverridenFilesKeepOriginalName ? FileEnt |
| 441 | : overI->second, |
| 442 | overI->second); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 444 | return Entry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | |
Ted Kremenek | d1c0eee | 2007-10-31 17:53:38 +0000 | [diff] [blame] | 448 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 449 | /// memory buffer. This does no caching. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 450 | const ContentCache* |
| 451 | SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) { |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 452 | // Add a new ContentCache to the MemBufferInfos list and return it. Make sure |
| 453 | // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of |
| 454 | // the pointer for its own nefarious purposes. |
| 455 | unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; |
| 456 | EntryAlign = std::max(8U, EntryAlign); |
| 457 | ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 458 | new (Entry) ContentCache(); |
| 459 | MemBufferInfos.push_back(Entry); |
| 460 | Entry->setBuffer(Buffer); |
| 461 | return Entry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 464 | std::pair<int, unsigned> |
| 465 | SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries, |
| 466 | unsigned TotalSize) { |
| 467 | assert(ExternalSLocEntries && "Don't have an external sloc source"); |
| 468 | LoadedSLocEntryTable.resize(LoadedSLocEntryTable.size() + NumSLocEntries); |
| 469 | SLocEntryLoaded.resize(LoadedSLocEntryTable.size()); |
| 470 | CurrentLoadedOffset -= TotalSize; |
| 471 | assert(CurrentLoadedOffset >= NextLocalOffset && "Out of source locations"); |
| 472 | int ID = LoadedSLocEntryTable.size(); |
| 473 | return std::make_pair(-ID - 1, CurrentLoadedOffset); |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 476 | /// \brief As part of recovering from missing or changed content, produce a |
| 477 | /// fake, non-empty buffer. |
| 478 | const llvm::MemoryBuffer *SourceManager::getFakeBufferForRecovery() const { |
| 479 | if (!FakeBufferForRecovery) |
| 480 | FakeBufferForRecovery |
| 481 | = llvm::MemoryBuffer::getMemBuffer("<<<INVALID BUFFER>>"); |
| 482 | |
| 483 | return FakeBufferForRecovery; |
| 484 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 485 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 486 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 487 | // Methods to create new FileID's and macro expansions. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 488 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 489 | |
Dan Gohman | 3f86b78 | 2010-08-26 21:27:06 +0000 | [diff] [blame] | 490 | /// createFileID - Create a new FileID for the specified ContentCache and |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 491 | /// include position. This works regardless of whether the ContentCache |
| 492 | /// corresponds to a file or some other input source. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 493 | FileID SourceManager::createFileID(const ContentCache *File, |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 494 | SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 495 | SrcMgr::CharacteristicKind FileCharacter, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 496 | int LoadedID, unsigned LoadedOffset) { |
| 497 | if (LoadedID < 0) { |
| 498 | assert(LoadedID != -1 && "Loading sentinel FileID"); |
| 499 | unsigned Index = unsigned(-LoadedID) - 2; |
| 500 | assert(Index < LoadedSLocEntryTable.size() && "FileID out of range"); |
| 501 | assert(!SLocEntryLoaded[Index] && "FileID already loaded"); |
| 502 | LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset, |
| 503 | FileInfo::get(IncludePos, File, FileCharacter)); |
| 504 | SLocEntryLoaded[Index] = true; |
| 505 | return FileID::get(LoadedID); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 506 | } |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 507 | LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, |
| 508 | FileInfo::get(IncludePos, File, |
| 509 | FileCharacter))); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 510 | unsigned FileSize = File->getSize(); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 511 | assert(NextLocalOffset + FileSize + 1 > NextLocalOffset && |
| 512 | NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset && |
| 513 | "Ran out of source locations!"); |
| 514 | // We do a +1 here because we want a SourceLocation that means "the end of the |
| 515 | // file", e.g. for the "no newline at the end of the file" diagnostic. |
| 516 | NextLocalOffset += FileSize + 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 518 | // Set LastFileIDLookup to the newly created file. The next getFileID call is |
| 519 | // almost guaranteed to be from that file. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 520 | FileID FID = FileID::get(LocalSLocEntryTable.size()-1); |
Argyrios Kyrtzidis | ea703f1 | 2009-06-23 00:42:06 +0000 | [diff] [blame] | 521 | return LastFileIDLookup = FID; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 524 | SourceLocation |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 525 | SourceManager::createMacroArgExpansionLoc(SourceLocation SpellingLoc, |
| 526 | SourceLocation ExpansionLoc, |
| 527 | unsigned TokLength) { |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 528 | ExpansionInfo Info = ExpansionInfo::createForMacroArg(SpellingLoc, |
| 529 | ExpansionLoc); |
| 530 | return createExpansionLocImpl(Info, TokLength); |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | SourceLocation |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 534 | SourceManager::createExpansionLoc(SourceLocation SpellingLoc, |
| 535 | SourceLocation ExpansionLocStart, |
| 536 | SourceLocation ExpansionLocEnd, |
| 537 | unsigned TokLength, |
| 538 | int LoadedID, |
| 539 | unsigned LoadedOffset) { |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 540 | ExpansionInfo Info = ExpansionInfo::create(SpellingLoc, ExpansionLocStart, |
| 541 | ExpansionLocEnd); |
| 542 | return createExpansionLocImpl(Info, TokLength, LoadedID, LoadedOffset); |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | SourceLocation |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 546 | SourceManager::createExpansionLocImpl(const ExpansionInfo &Info, |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 547 | unsigned TokLength, |
| 548 | int LoadedID, |
| 549 | unsigned LoadedOffset) { |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 550 | if (LoadedID < 0) { |
| 551 | assert(LoadedID != -1 && "Loading sentinel FileID"); |
| 552 | unsigned Index = unsigned(-LoadedID) - 2; |
| 553 | assert(Index < LoadedSLocEntryTable.size() && "FileID out of range"); |
| 554 | assert(!SLocEntryLoaded[Index] && "FileID already loaded"); |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 555 | LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset, Info); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 556 | SLocEntryLoaded[Index] = true; |
| 557 | return SourceLocation::getMacroLoc(LoadedOffset); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 558 | } |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 559 | LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, Info)); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 560 | assert(NextLocalOffset + TokLength + 1 > NextLocalOffset && |
| 561 | NextLocalOffset + TokLength + 1 <= CurrentLoadedOffset && |
| 562 | "Ran out of source locations!"); |
| 563 | // See createFileID for that +1. |
| 564 | NextLocalOffset += TokLength + 1; |
| 565 | return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 568 | const llvm::MemoryBuffer * |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 569 | SourceManager::getMemoryBufferForFile(const FileEntry *File, |
| 570 | bool *Invalid) { |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 571 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(File); |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 572 | assert(IR && "getOrCreateContentCache() cannot return NULL"); |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 573 | return IR->getBuffer(Diag, *this, SourceLocation(), Invalid); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Dan Gohman | 0d06e99 | 2010-10-26 20:47:28 +0000 | [diff] [blame] | 576 | void SourceManager::overrideFileContents(const FileEntry *SourceFile, |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 577 | const llvm::MemoryBuffer *Buffer, |
| 578 | bool DoNotFree) { |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 579 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); |
Dan Gohman | 0d06e99 | 2010-10-26 20:47:28 +0000 | [diff] [blame] | 580 | assert(IR && "getOrCreateContentCache() cannot return NULL"); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 581 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 582 | const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 585 | void SourceManager::overrideFileContents(const FileEntry *SourceFile, |
| 586 | const FileEntry *NewFile) { |
| 587 | assert(SourceFile->getSize() == NewFile->getSize() && |
| 588 | "Different sizes, use the FileManager to create a virtual file with " |
| 589 | "the correct size"); |
| 590 | assert(FileInfos.count(SourceFile) == 0 && |
| 591 | "This function should be called at the initialization stage, before " |
| 592 | "any parsing occurs."); |
| 593 | OverriddenFiles[SourceFile] = NewFile; |
| 594 | } |
| 595 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 596 | StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const { |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 597 | bool MyInvalid = false; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 598 | const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 599 | if (!SLoc.isFile() || MyInvalid) { |
Douglas Gregor | 3de8424 | 2011-01-31 22:42:36 +0000 | [diff] [blame] | 600 | if (Invalid) |
| 601 | *Invalid = true; |
| 602 | return "<<<<<INVALID SOURCE LOCATION>>>>>"; |
| 603 | } |
| 604 | |
| 605 | const llvm::MemoryBuffer *Buf |
| 606 | = SLoc.getFile().getContentCache()->getBuffer(Diag, *this, SourceLocation(), |
| 607 | &MyInvalid); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 608 | if (Invalid) |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 609 | *Invalid = MyInvalid; |
| 610 | |
| 611 | if (MyInvalid) |
Douglas Gregor | 3de8424 | 2011-01-31 22:42:36 +0000 | [diff] [blame] | 612 | return "<<<<<INVALID SOURCE LOCATION>>>>>"; |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 613 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 614 | return Buf->getBuffer(); |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 615 | } |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 616 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 617 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 618 | // SourceLocation manipulation methods. |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 619 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 620 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 621 | /// \brief Return the FileID for a SourceLocation. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 622 | /// |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 623 | /// This is the cache-miss path of getFileID. Not as hot as that function, but |
| 624 | /// still very important. It is responsible for finding the entry in the |
| 625 | /// SLocEntry tables that contains the specified location. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 626 | FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 627 | if (!SLocOffset) |
| 628 | return FileID::get(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 630 | // Now it is time to search for the correct file. See where the SLocOffset |
| 631 | // sits in the global view and consult local or loaded buffers for it. |
| 632 | if (SLocOffset < NextLocalOffset) |
| 633 | return getFileIDLocal(SLocOffset); |
| 634 | return getFileIDLoaded(SLocOffset); |
| 635 | } |
| 636 | |
| 637 | /// \brief Return the FileID for a SourceLocation with a low offset. |
| 638 | /// |
| 639 | /// This function knows that the SourceLocation is in a local buffer, not a |
| 640 | /// loaded one. |
| 641 | FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const { |
| 642 | assert(SLocOffset < NextLocalOffset && "Bad function choice"); |
| 643 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 644 | // After the first and second level caches, I see two common sorts of |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 645 | // behavior: 1) a lot of searched FileID's are "near" the cached file |
| 646 | // location or are "near" the cached expansion location. 2) others are just |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 647 | // completely random and may be a very long way away. |
| 648 | // |
| 649 | // To handle this, we do a linear search for up to 8 steps to catch #1 quickly |
| 650 | // then we fall back to a less cache efficient, but more scalable, binary |
| 651 | // search to find the location. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 653 | // See if this is near the file point - worst case we start scanning from the |
| 654 | // most newly created FileID. |
| 655 | std::vector<SrcMgr::SLocEntry>::const_iterator I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 657 | if (LastFileIDLookup.ID < 0 || |
| 658 | LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 659 | // Neither loc prunes our search. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 660 | I = LocalSLocEntryTable.end(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 661 | } else { |
| 662 | // Perhaps it is near the file point. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 663 | I = LocalSLocEntryTable.begin()+LastFileIDLookup.ID; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | // Find the FileID that contains this. "I" is an iterator that points to a |
| 667 | // FileID whose offset is known to be larger than SLocOffset. |
| 668 | unsigned NumProbes = 0; |
| 669 | while (1) { |
| 670 | --I; |
| 671 | if (I->getOffset() <= SLocOffset) { |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 672 | FileID Res = FileID::get(int(I - LocalSLocEntryTable.begin())); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 673 | |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 674 | // If this isn't an expansion, remember it. We have good locality across |
| 675 | // FileID lookups. |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 676 | if (!I->isExpansion()) |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 677 | LastFileIDLookup = Res; |
| 678 | NumLinearScans += NumProbes+1; |
| 679 | return Res; |
| 680 | } |
| 681 | if (++NumProbes == 8) |
| 682 | break; |
| 683 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 685 | // Convert "I" back into an index. We know that it is an entry whose index is |
| 686 | // larger than the offset we are looking for. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 687 | unsigned GreaterIndex = I - LocalSLocEntryTable.begin(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 688 | // LessIndex - This is the lower bound of the range that we're searching. |
| 689 | // We know that the offset corresponding to the FileID is is less than |
| 690 | // SLocOffset. |
| 691 | unsigned LessIndex = 0; |
| 692 | NumProbes = 0; |
| 693 | while (1) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 694 | bool Invalid = false; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 695 | unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 696 | unsigned MidOffset = getLocalSLocEntry(MiddleIndex, &Invalid).getOffset(); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 697 | if (Invalid) |
| 698 | return FileID::get(0); |
| 699 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 700 | ++NumProbes; |
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 offset of the midpoint is too large, chop the high side of the |
| 703 | // range to the midpoint. |
| 704 | if (MidOffset > SLocOffset) { |
| 705 | GreaterIndex = MiddleIndex; |
| 706 | continue; |
| 707 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 709 | // If the middle index contains the value, succeed and return. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 710 | // FIXME: This could be made faster by using a function that's aware of |
| 711 | // being in the local area. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 712 | if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 713 | FileID Res = FileID::get(MiddleIndex); |
| 714 | |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 715 | // If this isn't a macro expansion, remember it. We have good locality |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 716 | // across FileID lookups. |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 717 | if (!LocalSLocEntryTable[MiddleIndex].isExpansion()) |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 718 | LastFileIDLookup = Res; |
| 719 | NumBinaryProbes += NumProbes; |
| 720 | return Res; |
| 721 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 723 | // Otherwise, move the low-side up to the middle index. |
| 724 | LessIndex = MiddleIndex; |
| 725 | } |
| 726 | } |
| 727 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 728 | /// \brief Return the FileID for a SourceLocation with a high offset. |
| 729 | /// |
| 730 | /// This function knows that the SourceLocation is in a loaded buffer, not a |
| 731 | /// local one. |
| 732 | FileID SourceManager::getFileIDLoaded(unsigned SLocOffset) const { |
Argyrios Kyrtzidis | c3b4575 | 2011-10-03 23:43:01 +0000 | [diff] [blame] | 733 | // Sanity checking, otherwise a bug may lead to hanging in release build. |
Argyrios Kyrtzidis | 527de44 | 2011-10-25 00:29:44 +0000 | [diff] [blame] | 734 | if (SLocOffset < CurrentLoadedOffset) { |
| 735 | assert(0 && "Invalid SLocOffset or bad function choice"); |
Argyrios Kyrtzidis | c3b4575 | 2011-10-03 23:43:01 +0000 | [diff] [blame] | 736 | return FileID(); |
Argyrios Kyrtzidis | 527de44 | 2011-10-25 00:29:44 +0000 | [diff] [blame] | 737 | } |
Argyrios Kyrtzidis | c3b4575 | 2011-10-03 23:43:01 +0000 | [diff] [blame] | 738 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 739 | // Essentially the same as the local case, but the loaded array is sorted |
| 740 | // in the other direction. |
| 741 | |
| 742 | // First do a linear scan from the last lookup position, if possible. |
| 743 | unsigned I; |
| 744 | int LastID = LastFileIDLookup.ID; |
| 745 | if (LastID >= 0 || getLoadedSLocEntryByID(LastID).getOffset() < SLocOffset) |
| 746 | I = 0; |
| 747 | else |
| 748 | I = (-LastID - 2) + 1; |
| 749 | |
| 750 | unsigned NumProbes; |
| 751 | for (NumProbes = 0; NumProbes < 8; ++NumProbes, ++I) { |
| 752 | // Make sure the entry is loaded! |
| 753 | const SrcMgr::SLocEntry &E = getLoadedSLocEntry(I); |
| 754 | if (E.getOffset() <= SLocOffset) { |
| 755 | FileID Res = FileID::get(-int(I) - 2); |
| 756 | |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 757 | if (!E.isExpansion()) |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 758 | LastFileIDLookup = Res; |
| 759 | NumLinearScans += NumProbes + 1; |
| 760 | return Res; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | // Linear scan failed. Do the binary search. Note the reverse sorting of the |
| 765 | // table: GreaterIndex is the one where the offset is greater, which is |
| 766 | // actually a lower index! |
| 767 | unsigned GreaterIndex = I; |
| 768 | unsigned LessIndex = LoadedSLocEntryTable.size(); |
| 769 | NumProbes = 0; |
| 770 | while (1) { |
| 771 | ++NumProbes; |
| 772 | unsigned MiddleIndex = (LessIndex - GreaterIndex) / 2 + GreaterIndex; |
| 773 | const SrcMgr::SLocEntry &E = getLoadedSLocEntry(MiddleIndex); |
| 774 | |
| 775 | ++NumProbes; |
| 776 | |
| 777 | if (E.getOffset() > SLocOffset) { |
| 778 | GreaterIndex = MiddleIndex; |
| 779 | continue; |
| 780 | } |
| 781 | |
| 782 | if (isOffsetInFileID(FileID::get(-int(MiddleIndex) - 2), SLocOffset)) { |
| 783 | FileID Res = FileID::get(-int(MiddleIndex) - 2); |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 784 | if (!E.isExpansion()) |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 785 | LastFileIDLookup = Res; |
| 786 | NumBinaryProbes += NumProbes; |
| 787 | return Res; |
| 788 | } |
| 789 | |
| 790 | LessIndex = MiddleIndex; |
| 791 | } |
| 792 | } |
| 793 | |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 794 | SourceLocation SourceManager:: |
Chandler Carruth | f84ef95 | 2011-07-25 20:52:26 +0000 | [diff] [blame] | 795 | getExpansionLocSlowCase(SourceLocation Loc) const { |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 796 | do { |
Chris Lattner | a5c6c58 | 2010-02-12 19:31:35 +0000 | [diff] [blame] | 797 | // Note: If Loc indicates an offset into a token that came from a macro |
| 798 | // expansion (e.g. the 5th character of the token) we do not want to add |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 799 | // this offset when going to the expansion location. The expansion |
Chris Lattner | a5c6c58 | 2010-02-12 19:31:35 +0000 | [diff] [blame] | 800 | // location is the macro invocation, which the offset has nothing to do |
| 801 | // with. This is unlike when we get the spelling loc, because the offset |
| 802 | // directly correspond to the token whose spelling we're inspecting. |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 803 | Loc = getSLocEntry(getFileID(Loc)).getExpansion().getExpansionLocStart(); |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 804 | } while (!Loc.isFileID()); |
| 805 | |
| 806 | return Loc; |
| 807 | } |
| 808 | |
| 809 | SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { |
| 810 | do { |
| 811 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 812 | Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc(); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 813 | Loc = Loc.getLocWithOffset(LocInfo.second); |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 814 | } while (!Loc.isFileID()); |
| 815 | return Loc; |
| 816 | } |
| 817 | |
Argyrios Kyrtzidis | 796dbfb | 2011-10-12 07:07:40 +0000 | [diff] [blame] | 818 | SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const { |
| 819 | do { |
| 820 | if (isMacroArgExpansion(Loc)) |
| 821 | Loc = getImmediateSpellingLoc(Loc); |
| 822 | else |
| 823 | Loc = getImmediateExpansionRange(Loc).first; |
| 824 | } while (!Loc.isFileID()); |
| 825 | return Loc; |
| 826 | } |
| 827 | |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 828 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 829 | std::pair<FileID, unsigned> |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 830 | SourceManager::getDecomposedExpansionLocSlowCase( |
Argyrios Kyrtzidis | 8b86ef0 | 2011-07-07 03:40:27 +0000 | [diff] [blame] | 831 | const SrcMgr::SLocEntry *E) const { |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 832 | // If this is an expansion record, walk through all the expansion points. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 833 | FileID FID; |
| 834 | SourceLocation Loc; |
Argyrios Kyrtzidis | 8b86ef0 | 2011-07-07 03:40:27 +0000 | [diff] [blame] | 835 | unsigned Offset; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 836 | do { |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 837 | Loc = E->getExpansion().getExpansionLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 839 | FID = getFileID(Loc); |
| 840 | E = &getSLocEntry(FID); |
Argyrios Kyrtzidis | 8b86ef0 | 2011-07-07 03:40:27 +0000 | [diff] [blame] | 841 | Offset = Loc.getOffset()-E->getOffset(); |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 842 | } while (!Loc.isFileID()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 844 | return std::make_pair(FID, Offset); |
| 845 | } |
| 846 | |
| 847 | std::pair<FileID, unsigned> |
| 848 | SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, |
| 849 | unsigned Offset) const { |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 850 | // If this is an expansion record, walk through all the expansion points. |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 851 | FileID FID; |
| 852 | SourceLocation Loc; |
| 853 | do { |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 854 | Loc = E->getExpansion().getSpellingLoc(); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 855 | Loc = Loc.getLocWithOffset(Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 857 | FID = getFileID(Loc); |
| 858 | E = &getSLocEntry(FID); |
Argyrios Kyrtzidis | b6c465e | 2011-08-23 21:02:41 +0000 | [diff] [blame] | 859 | Offset = Loc.getOffset()-E->getOffset(); |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 860 | } while (!Loc.isFileID()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 862 | return std::make_pair(FID, Offset); |
| 863 | } |
| 864 | |
Chris Lattner | 387616e | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 865 | /// getImmediateSpellingLoc - Given a SourceLocation object, return the |
| 866 | /// spelling location referenced by the ID. This is the first level down |
| 867 | /// towards the place where the characters that make up the lexed token can be |
| 868 | /// found. This should not generally be used by clients. |
| 869 | SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{ |
| 870 | if (Loc.isFileID()) return Loc; |
| 871 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 872 | Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc(); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 873 | return Loc.getLocWithOffset(LocInfo.second); |
Chris Lattner | 387616e | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 877 | /// getImmediateExpansionRange - Loc is required to be an expansion location. |
| 878 | /// Return the start/end of the expansion information. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 879 | std::pair<SourceLocation,SourceLocation> |
Chandler Carruth | 999f739 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 880 | SourceManager::getImmediateExpansionRange(SourceLocation Loc) const { |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 881 | assert(Loc.isMacroID() && "Not a macro expansion loc!"); |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 882 | const ExpansionInfo &Expansion = getSLocEntry(getFileID(Loc)).getExpansion(); |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 883 | return Expansion.getExpansionLocRange(); |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 886 | /// getExpansionRange - Given a SourceLocation object, return the range of |
| 887 | /// tokens covered by the expansion in the ultimate file. |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 888 | std::pair<SourceLocation,SourceLocation> |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 889 | SourceManager::getExpansionRange(SourceLocation Loc) const { |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 890 | if (Loc.isFileID()) return std::make_pair(Loc, Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 892 | std::pair<SourceLocation,SourceLocation> Res = |
Chandler Carruth | 999f739 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 893 | getImmediateExpansionRange(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 895 | // Fully resolve the start and end locations to their ultimate expansion |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 896 | // points. |
| 897 | while (!Res.first.isFileID()) |
Chandler Carruth | 999f739 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 898 | Res.first = getImmediateExpansionRange(Res.first).first; |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 899 | while (!Res.second.isFileID()) |
Chandler Carruth | 999f739 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 900 | Res.second = getImmediateExpansionRange(Res.second).second; |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 901 | return Res; |
| 902 | } |
| 903 | |
Chandler Carruth | 96d3589 | 2011-07-26 03:03:00 +0000 | [diff] [blame] | 904 | bool SourceManager::isMacroArgExpansion(SourceLocation Loc) const { |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 905 | if (!Loc.isMacroID()) return false; |
| 906 | |
| 907 | FileID FID = getFileID(Loc); |
| 908 | const SrcMgr::SLocEntry *E = &getSLocEntry(FID); |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 909 | const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); |
Chandler Carruth | 78df836 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 910 | return Expansion.isMacroArgExpansion(); |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 911 | } |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 912 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 913 | |
| 914 | //===----------------------------------------------------------------------===// |
| 915 | // Queries about the code at a SourceLocation. |
| 916 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 917 | |
| 918 | /// getCharacterData - Return a pointer to the start of the specified location |
| 919 | /// in the appropriate MemoryBuffer. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 920 | const char *SourceManager::getCharacterData(SourceLocation SL, |
| 921 | bool *Invalid) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 922 | // Note that this is a hot function in the getSpelling() path, which is |
| 923 | // heavily used by -E mode. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 924 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 926 | // Note that calling 'getBuffer()' may lazily page in a source file. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 927 | bool CharDataInvalid = false; |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 928 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &CharDataInvalid); |
| 929 | if (CharDataInvalid || !Entry.isFile()) { |
| 930 | if (Invalid) |
| 931 | *Invalid = true; |
| 932 | |
| 933 | return "<<<<INVALID BUFFER>>>>"; |
| 934 | } |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 935 | const llvm::MemoryBuffer *Buffer |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 936 | = Entry.getFile().getContentCache() |
| 937 | ->getBuffer(Diag, *this, SourceLocation(), &CharDataInvalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 938 | if (Invalid) |
| 939 | *Invalid = CharDataInvalid; |
| 940 | return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 943 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 944 | /// getColumnNumber - Return the column # for the specified file position. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 945 | /// this is significantly cheaper to compute than the line number. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 946 | unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos, |
| 947 | bool *Invalid) const { |
| 948 | bool MyInvalid = false; |
| 949 | const char *Buf = getBuffer(FID, &MyInvalid)->getBufferStart(); |
| 950 | if (Invalid) |
| 951 | *Invalid = MyInvalid; |
| 952 | |
| 953 | if (MyInvalid) |
| 954 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 956 | unsigned LineStart = FilePos; |
| 957 | while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') |
| 958 | --LineStart; |
| 959 | return FilePos-LineStart+1; |
| 960 | } |
| 961 | |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 962 | // isInvalid - Return the result of calling loc.isInvalid(), and |
| 963 | // if Invalid is not null, set its value to same. |
| 964 | static bool isInvalid(SourceLocation Loc, bool *Invalid) { |
| 965 | bool MyInvalid = Loc.isInvalid(); |
| 966 | if (Invalid) |
| 967 | *Invalid = MyInvalid; |
| 968 | return MyInvalid; |
| 969 | } |
| 970 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 971 | unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc, |
| 972 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 973 | if (isInvalid(Loc, Invalid)) return 0; |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 974 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 975 | return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Chandler Carruth | a77c031 | 2011-07-25 20:57:57 +0000 | [diff] [blame] | 978 | unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc, |
| 979 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 980 | if (isInvalid(Loc, Invalid)) return 0; |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 981 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 982 | return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 985 | unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc, |
| 986 | bool *Invalid) const { |
| 987 | if (isInvalid(Loc, Invalid)) return 0; |
| 988 | return getPresumedLoc(Loc).getColumn(); |
| 989 | } |
| 990 | |
Chandler Carruth | 14bd965 | 2010-10-23 08:44:57 +0000 | [diff] [blame] | 991 | static LLVM_ATTRIBUTE_NOINLINE void |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 992 | ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI, |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 993 | llvm::BumpPtrAllocator &Alloc, |
| 994 | const SourceManager &SM, bool &Invalid); |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 995 | static void ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI, |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 996 | llvm::BumpPtrAllocator &Alloc, |
| 997 | const SourceManager &SM, bool &Invalid) { |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 998 | // Note that calling 'getBuffer()' may lazily page in the file. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 999 | const MemoryBuffer *Buffer = FI->getBuffer(Diag, SM, SourceLocation(), |
| 1000 | &Invalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1001 | if (Invalid) |
| 1002 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1004 | // Find the file offsets of all of the *physical* source lines. This does |
| 1005 | // not look at trigraphs, escaped newlines, or anything else tricky. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1006 | SmallVector<unsigned, 256> LineOffsets; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1008 | // Line #1 starts at char 0. |
| 1009 | LineOffsets.push_back(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1011 | const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); |
| 1012 | const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); |
| 1013 | unsigned Offs = 0; |
| 1014 | while (1) { |
| 1015 | // Skip over the contents of the line. |
| 1016 | // TODO: Vectorize this? This is very performance sensitive for programs |
| 1017 | // with lots of diagnostics and in -E mode. |
| 1018 | const unsigned char *NextBuf = (const unsigned char *)Buf; |
| 1019 | while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') |
| 1020 | ++NextBuf; |
| 1021 | Offs += NextBuf-Buf; |
| 1022 | Buf = NextBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1023 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1024 | if (Buf[0] == '\n' || Buf[0] == '\r') { |
| 1025 | // If this is \n\r or \r\n, skip both characters. |
| 1026 | if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) |
| 1027 | ++Offs, ++Buf; |
| 1028 | ++Offs, ++Buf; |
| 1029 | LineOffsets.push_back(Offs); |
| 1030 | } else { |
| 1031 | // Otherwise, this is a null. If end of file, exit. |
| 1032 | if (Buf == End) break; |
| 1033 | // Otherwise, skip the null. |
| 1034 | ++Offs, ++Buf; |
| 1035 | } |
| 1036 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1038 | // Copy the offsets into the FileInfo structure. |
| 1039 | FI->NumLines = LineOffsets.size(); |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 1040 | FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size()); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1041 | std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); |
| 1042 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 1044 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1045 | /// for the position indicated. This requires building and caching a table of |
| 1046 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 1047 | /// about to emit a diagnostic. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1048 | unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos, |
| 1049 | bool *Invalid) const { |
Argyrios Kyrtzidis | 5adc051 | 2011-05-17 22:09:53 +0000 | [diff] [blame] | 1050 | if (FID.isInvalid()) { |
| 1051 | if (Invalid) |
| 1052 | *Invalid = true; |
| 1053 | return 1; |
| 1054 | } |
| 1055 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1056 | ContentCache *Content; |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1057 | if (LastLineNoFileIDQuery == FID) |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1058 | Content = LastLineNoContentCache; |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1059 | else { |
| 1060 | bool MyInvalid = false; |
| 1061 | const SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); |
| 1062 | if (MyInvalid || !Entry.isFile()) { |
| 1063 | if (Invalid) |
| 1064 | *Invalid = true; |
| 1065 | return 1; |
| 1066 | } |
| 1067 | |
| 1068 | Content = const_cast<ContentCache*>(Entry.getFile().getContentCache()); |
| 1069 | } |
| 1070 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1071 | // 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] | 1072 | /// SourceLineCache for it on demand. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1073 | if (Content->SourceLineCache == 0) { |
| 1074 | bool MyInvalid = false; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1075 | ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1076 | if (Invalid) |
| 1077 | *Invalid = MyInvalid; |
| 1078 | if (MyInvalid) |
| 1079 | return 1; |
| 1080 | } else if (Invalid) |
| 1081 | *Invalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1082 | |
| 1083 | // Okay, we know we have a line number table. Do a binary search to find the |
| 1084 | // line number that this character position lands on. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1085 | unsigned *SourceLineCache = Content->SourceLineCache; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1086 | unsigned *SourceLineCacheStart = SourceLineCache; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1087 | unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1089 | unsigned QueriedFilePos = FilePos+1; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1090 | |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1091 | // 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] | 1092 | // complicated as it is, binary search isn't that slow. |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1093 | // |
| 1094 | // If it is worth being optimized, then in my opinion it could be more |
| 1095 | // performant, simpler, and more obviously correct by just "galloping" outward |
| 1096 | // from the queried file position. In fact, this could be incorporated into a |
| 1097 | // generic algorithm such as lower_bound_with_hint. |
| 1098 | // |
| 1099 | // If someone gives me a test case where this matters, and I will do it! - DWD |
| 1100 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1101 | // If the previous query was to the same file, we know both the file pos from |
| 1102 | // that query and the line number returned. This allows us to narrow the |
| 1103 | // search space from the entire file to something near the match. |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1104 | if (LastLineNoFileIDQuery == FID) { |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1105 | if (QueriedFilePos >= LastLineNoFilePos) { |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1106 | // FIXME: Potential overflow? |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1107 | SourceLineCache = SourceLineCache+LastLineNoResult-1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1109 | // The query is likely to be nearby the previous one. Here we check to |
| 1110 | // see if it is within 5, 10 or 20 lines. It can be far away in cases |
| 1111 | // where big comment blocks and vertical whitespace eat up lines but |
| 1112 | // contribute no tokens. |
| 1113 | if (SourceLineCache+5 < SourceLineCacheEnd) { |
| 1114 | if (SourceLineCache[5] > QueriedFilePos) |
| 1115 | SourceLineCacheEnd = SourceLineCache+5; |
| 1116 | else if (SourceLineCache+10 < SourceLineCacheEnd) { |
| 1117 | if (SourceLineCache[10] > QueriedFilePos) |
| 1118 | SourceLineCacheEnd = SourceLineCache+10; |
| 1119 | else if (SourceLineCache+20 < SourceLineCacheEnd) { |
| 1120 | if (SourceLineCache[20] > QueriedFilePos) |
| 1121 | SourceLineCacheEnd = SourceLineCache+20; |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | } else { |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1126 | if (LastLineNoResult < Content->NumLines) |
| 1127 | SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1131 | // If the spread is large, do a "radix" test as our initial guess, based on |
| 1132 | // the assumption that lines average to approximately the same length. |
| 1133 | // NOTE: This is currently disabled, as it does not appear to be profitable in |
| 1134 | // initial measurements. |
| 1135 | if (0 && SourceLineCacheEnd-SourceLineCache > 20) { |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1136 | unsigned FileLen = Content->SourceLineCache[Content->NumLines-1]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1138 | // Take a stab at guessing where it is. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1139 | unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1140 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1141 | // Check for -10 and +10 lines. |
| 1142 | unsigned LowerBound = std::max(int(ApproxPos-10), 0); |
| 1143 | unsigned UpperBound = std::min(ApproxPos+10, FileLen); |
| 1144 | |
| 1145 | // If the computed lower bound is less than the query location, move it in. |
| 1146 | if (SourceLineCache < SourceLineCacheStart+LowerBound && |
| 1147 | SourceLineCacheStart[LowerBound] < QueriedFilePos) |
| 1148 | SourceLineCache = SourceLineCacheStart+LowerBound; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1150 | // If the computed upper bound is greater than the query location, move it. |
| 1151 | if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound && |
| 1152 | SourceLineCacheStart[UpperBound] >= QueriedFilePos) |
| 1153 | SourceLineCacheEnd = SourceLineCacheStart+UpperBound; |
| 1154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1156 | unsigned *Pos |
| 1157 | = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1158 | unsigned LineNo = Pos-SourceLineCacheStart; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1160 | LastLineNoFileIDQuery = FID; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1161 | LastLineNoContentCache = Content; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1162 | LastLineNoFilePos = QueriedFilePos; |
| 1163 | LastLineNoResult = LineNo; |
| 1164 | return LineNo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1167 | unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc, |
| 1168 | bool *Invalid) const { |
| 1169 | if (isInvalid(Loc, Invalid)) return 0; |
| 1170 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); |
| 1171 | return getLineNumber(LocInfo.first, LocInfo.second); |
| 1172 | } |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 1173 | unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc, |
| 1174 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1175 | if (isInvalid(Loc, Invalid)) return 0; |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 1176 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1177 | return getLineNumber(LocInfo.first, LocInfo.second); |
| 1178 | } |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1179 | unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc, |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1180 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1181 | if (isInvalid(Loc, Invalid)) return 0; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1182 | return getPresumedLoc(Loc).getLine(); |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1185 | /// getFileCharacteristic - return the file characteristic of the specified |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | /// source location, indicating whether this is a normal file, a system |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1187 | /// header, or an "implicit extern C" system header. |
| 1188 | /// |
| 1189 | /// This state can be modified with flags on GNU linemarker directives like: |
| 1190 | /// # 4 "foo.h" 3 |
| 1191 | /// which changes all source locations in the current file after that to be |
| 1192 | /// considered to be from a system header. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | SrcMgr::CharacteristicKind |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1194 | SourceManager::getFileCharacteristic(SourceLocation Loc) const { |
| 1195 | assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!"); |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 1196 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1197 | bool Invalid = false; |
| 1198 | const SLocEntry &SEntry = getSLocEntry(LocInfo.first, &Invalid); |
| 1199 | if (Invalid || !SEntry.isFile()) |
| 1200 | return C_User; |
| 1201 | |
| 1202 | const SrcMgr::FileInfo &FI = SEntry.getFile(); |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1203 | |
| 1204 | // If there are no #line directives in this file, just return the whole-file |
| 1205 | // state. |
| 1206 | if (!FI.hasLineDirectives()) |
| 1207 | return FI.getFileCharacteristic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1209 | assert(LineTable && "Can't have linetable entries without a LineTable!"); |
| 1210 | // See if there is a #line directive before the location. |
| 1211 | const LineEntry *Entry = |
| 1212 | LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1214 | // If this is before the first line marker, use the file characteristic. |
| 1215 | if (!Entry) |
| 1216 | return FI.getFileCharacteristic(); |
| 1217 | |
| 1218 | return Entry->FileKind; |
| 1219 | } |
| 1220 | |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 1221 | /// Return the filename or buffer identifier of the buffer the location is in. |
| 1222 | /// Note that this name does not respect #line directives. Use getPresumedLoc |
| 1223 | /// for normal clients. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1224 | const char *SourceManager::getBufferName(SourceLocation Loc, |
| 1225 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1226 | if (isInvalid(Loc, Invalid)) return "<invalid loc>"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1228 | return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier(); |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1232 | /// getPresumedLoc - This method returns the "presumed" location of a |
| 1233 | /// SourceLocation specifies. A "presumed location" can be modified by #line |
| 1234 | /// or GNU line marker directives. This provides a view on the data that a |
| 1235 | /// user should see in diagnostics, for example. |
| 1236 | /// |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 1237 | /// Note that a presumed location is always given as the expansion point of an |
| 1238 | /// expansion location, not at the spelling location. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1239 | PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { |
| 1240 | if (Loc.isInvalid()) return PresumedLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 1242 | // Presumed locations are always for expansion points. |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 1243 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1245 | bool Invalid = false; |
| 1246 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); |
| 1247 | if (Invalid || !Entry.isFile()) |
| 1248 | return PresumedLoc(); |
| 1249 | |
| 1250 | const SrcMgr::FileInfo &FI = Entry.getFile(); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1251 | const SrcMgr::ContentCache *C = FI.getContentCache(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1253 | // To get the source name, first consult the FileEntry (if one exists) |
| 1254 | // before the MemBuffer as this will avoid unnecessarily paging in the |
| 1255 | // MemBuffer. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1256 | const char *Filename; |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1257 | if (C->OrigEntry) |
| 1258 | Filename = C->OrigEntry->getName(); |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1259 | else |
| 1260 | Filename = C->getBuffer(Diag, *this)->getBufferIdentifier(); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1261 | |
Douglas Gregor | c417fa0 | 2010-11-02 00:39:22 +0000 | [diff] [blame] | 1262 | unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid); |
| 1263 | if (Invalid) |
| 1264 | return PresumedLoc(); |
| 1265 | unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid); |
| 1266 | if (Invalid) |
| 1267 | return PresumedLoc(); |
| 1268 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1269 | SourceLocation IncludeLoc = FI.getIncludeLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1271 | // If we have #line directives in this file, update and overwrite the physical |
| 1272 | // location info if appropriate. |
| 1273 | if (FI.hasLineDirectives()) { |
| 1274 | assert(LineTable && "Can't have linetable entries without a LineTable!"); |
| 1275 | // See if there is a #line directive before this. If so, get it. |
| 1276 | if (const LineEntry *Entry = |
| 1277 | LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) { |
Chris Lattner | fc39133 | 2009-02-04 02:00:59 +0000 | [diff] [blame] | 1278 | // If the LineEntry indicates a filename, use it. |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1279 | if (Entry->FilenameID != -1) |
| 1280 | Filename = LineTable->getFilename(Entry->FilenameID); |
Chris Lattner | fc39133 | 2009-02-04 02:00:59 +0000 | [diff] [blame] | 1281 | |
| 1282 | // Use the line number specified by the LineEntry. This line number may |
| 1283 | // be multiple lines down from the line entry. Add the difference in |
| 1284 | // physical line numbers from the query point and the line marker to the |
| 1285 | // total. |
| 1286 | unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset); |
| 1287 | LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | |
Chris Lattner | 0e0e5da | 2009-02-04 02:15:40 +0000 | [diff] [blame] | 1289 | // Note that column numbers are not molested by line markers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 1291 | // Handle virtual #include manipulation. |
| 1292 | if (Entry->IncludeOffset) { |
| 1293 | IncludeLoc = getLocForStartOfFile(LocInfo.first); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1294 | IncludeLoc = IncludeLoc.getLocWithOffset(Entry->IncludeOffset); |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 1295 | } |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Argyrios Kyrtzidis | 984e42c | 2011-08-23 21:02:28 +0000 | [diff] [blame] | 1302 | /// \brief The size of the SLocEnty that \arg FID represents. |
| 1303 | unsigned SourceManager::getFileIDSize(FileID FID) const { |
| 1304 | bool Invalid = false; |
| 1305 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 1306 | if (Invalid) |
| 1307 | return 0; |
| 1308 | |
| 1309 | int ID = FID.ID; |
| 1310 | unsigned NextOffset; |
| 1311 | if ((ID > 0 && unsigned(ID+1) == local_sloc_entry_size())) |
| 1312 | NextOffset = getNextLocalOffset(); |
| 1313 | else if (ID+1 == -1) |
| 1314 | NextOffset = MaxLoadedOffset; |
| 1315 | else |
| 1316 | NextOffset = getSLocEntry(FileID::get(ID+1)).getOffset(); |
| 1317 | |
| 1318 | return NextOffset - Entry.getOffset() - 1; |
| 1319 | } |
| 1320 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1321 | //===----------------------------------------------------------------------===// |
| 1322 | // Other miscellaneous methods. |
| 1323 | //===----------------------------------------------------------------------===// |
| 1324 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1325 | /// \brief Retrieve the inode for the given file entry, if possible. |
| 1326 | /// |
| 1327 | /// This routine involves a system call, and therefore should only be used |
| 1328 | /// in non-performance-critical code. |
| 1329 | static llvm::Optional<ino_t> getActualFileInode(const FileEntry *File) { |
| 1330 | if (!File) |
| 1331 | return llvm::Optional<ino_t>(); |
| 1332 | |
| 1333 | struct stat StatBuf; |
| 1334 | if (::stat(File->getName(), &StatBuf)) |
| 1335 | return llvm::Optional<ino_t>(); |
| 1336 | |
| 1337 | return StatBuf.st_ino; |
| 1338 | } |
| 1339 | |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1340 | /// \brief Get the source location for the given file:line:col triplet. |
| 1341 | /// |
| 1342 | /// If the source file is included multiple times, the source location will |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1343 | /// be based upon an arbitrary inclusion. |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1344 | SourceLocation SourceManager::translateFileLineCol(const FileEntry *SourceFile, |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1345 | unsigned Line, |
| 1346 | unsigned Col) const { |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1347 | assert(SourceFile && "Null source file!"); |
| 1348 | assert(Line && Col && "Line and column should start from 1!"); |
| 1349 | |
Argyrios Kyrtzidis | b201e16 | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1350 | FileID FirstFID = translateFile(SourceFile); |
| 1351 | return translateLineCol(FirstFID, Line, Col); |
| 1352 | } |
| 1353 | |
| 1354 | /// \brief Get the FileID for the given file. |
| 1355 | /// |
| 1356 | /// If the source file is included multiple times, the FileID will be the |
| 1357 | /// first inclusion. |
| 1358 | FileID SourceManager::translateFile(const FileEntry *SourceFile) const { |
| 1359 | assert(SourceFile && "Null source file!"); |
| 1360 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1361 | // Find the first file ID that corresponds to the given file. |
| 1362 | FileID FirstFID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1364 | // First, check the main file ID, since it is common to look for a |
| 1365 | // location in the main file. |
| 1366 | llvm::Optional<ino_t> SourceFileInode; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1367 | llvm::Optional<StringRef> SourceFileName; |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1368 | if (!MainFileID.isInvalid()) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1369 | bool Invalid = false; |
| 1370 | const SLocEntry &MainSLoc = getSLocEntry(MainFileID, &Invalid); |
| 1371 | if (Invalid) |
Argyrios Kyrtzidis | b201e16 | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1372 | return FileID(); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1373 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1374 | if (MainSLoc.isFile()) { |
| 1375 | const ContentCache *MainContentCache |
| 1376 | = MainSLoc.getFile().getContentCache(); |
Douglas Gregor | b7a1841 | 2011-02-11 18:08:15 +0000 | [diff] [blame] | 1377 | if (!MainContentCache) { |
| 1378 | // Can't do anything |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1379 | } else if (MainContentCache->OrigEntry == SourceFile) { |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1380 | FirstFID = MainFileID; |
Douglas Gregor | b7a1841 | 2011-02-11 18:08:15 +0000 | [diff] [blame] | 1381 | } else { |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1382 | // Fall back: check whether we have the same base name and inode |
| 1383 | // as the main file. |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1384 | const FileEntry *MainFile = MainContentCache->OrigEntry; |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1385 | SourceFileName = llvm::sys::path::filename(SourceFile->getName()); |
| 1386 | if (*SourceFileName == llvm::sys::path::filename(MainFile->getName())) { |
| 1387 | SourceFileInode = getActualFileInode(SourceFile); |
Douglas Gregor | 37c02bf | 2011-02-16 19:09:24 +0000 | [diff] [blame] | 1388 | if (SourceFileInode) { |
| 1389 | if (llvm::Optional<ino_t> MainFileInode |
| 1390 | = getActualFileInode(MainFile)) { |
| 1391 | if (*SourceFileInode == *MainFileInode) { |
| 1392 | FirstFID = MainFileID; |
| 1393 | SourceFile = MainFile; |
| 1394 | } |
| 1395 | } |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | if (FirstFID.isInvalid()) { |
| 1403 | // The location we're looking for isn't in the main file; look |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1404 | // through all of the local source locations. |
| 1405 | for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1406 | bool Invalid = false; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1407 | const SLocEntry &SLoc = getLocalSLocEntry(I, &Invalid); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1408 | if (Invalid) |
Argyrios Kyrtzidis | b201e16 | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1409 | return FileID(); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1411 | if (SLoc.isFile() && |
| 1412 | SLoc.getFile().getContentCache() && |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1413 | SLoc.getFile().getContentCache()->OrigEntry == SourceFile) { |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1414 | FirstFID = FileID::get(I); |
| 1415 | break; |
| 1416 | } |
| 1417 | } |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1418 | // If that still didn't help, try the modules. |
| 1419 | if (FirstFID.isInvalid()) { |
| 1420 | for (unsigned I = 0, N = loaded_sloc_entry_size(); I != N; ++I) { |
| 1421 | const SLocEntry &SLoc = getLoadedSLocEntry(I); |
| 1422 | if (SLoc.isFile() && |
| 1423 | SLoc.getFile().getContentCache() && |
| 1424 | SLoc.getFile().getContentCache()->OrigEntry == SourceFile) { |
| 1425 | FirstFID = FileID::get(-int(I) - 2); |
| 1426 | break; |
| 1427 | } |
| 1428 | } |
| 1429 | } |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | // If we haven't found what we want yet, try again, but this time stat() |
| 1433 | // each of the files in case the files have changed since we originally |
| 1434 | // parsed the file. |
| 1435 | if (FirstFID.isInvalid() && |
| 1436 | (SourceFileName || |
| 1437 | (SourceFileName = llvm::sys::path::filename(SourceFile->getName()))) && |
| 1438 | (SourceFileInode || |
| 1439 | (SourceFileInode = getActualFileInode(SourceFile)))) { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1440 | bool Invalid = false; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1441 | for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) { |
| 1442 | FileID IFileID; |
| 1443 | IFileID.ID = I; |
| 1444 | const SLocEntry &SLoc = getSLocEntry(IFileID, &Invalid); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1445 | if (Invalid) |
Argyrios Kyrtzidis | b201e16 | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1446 | return FileID(); |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1447 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1448 | if (SLoc.isFile()) { |
| 1449 | const ContentCache *FileContentCache |
| 1450 | = SLoc.getFile().getContentCache(); |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1451 | const FileEntry *Entry =FileContentCache? FileContentCache->OrigEntry : 0; |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1452 | if (Entry && |
Douglas Gregor | b7a1841 | 2011-02-11 18:08:15 +0000 | [diff] [blame] | 1453 | *SourceFileName == llvm::sys::path::filename(Entry->getName())) { |
| 1454 | if (llvm::Optional<ino_t> EntryInode = getActualFileInode(Entry)) { |
| 1455 | if (*SourceFileInode == *EntryInode) { |
| 1456 | FirstFID = FileID::get(I); |
| 1457 | SourceFile = Entry; |
| 1458 | break; |
| 1459 | } |
| 1460 | } |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | } |
Argyrios Kyrtzidis | b201e16 | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1465 | |
| 1466 | return FirstFID; |
Argyrios Kyrtzidis | efa2ff8 | 2011-09-19 20:40:29 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /// \brief Get the source location in \arg FID for the given line:col. |
| 1470 | /// Returns null location if \arg FID is not a file SLocEntry. |
| 1471 | SourceLocation SourceManager::translateLineCol(FileID FID, |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1472 | unsigned Line, |
| 1473 | unsigned Col) const { |
Argyrios Kyrtzidis | efa2ff8 | 2011-09-19 20:40:29 +0000 | [diff] [blame] | 1474 | if (FID.isInvalid()) |
| 1475 | return SourceLocation(); |
| 1476 | |
| 1477 | bool Invalid = false; |
| 1478 | const SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 1479 | if (Invalid) |
| 1480 | return SourceLocation(); |
| 1481 | |
| 1482 | if (!Entry.isFile()) |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1483 | return SourceLocation(); |
| 1484 | |
Argyrios Kyrtzidis | 5e5e95d | 2011-09-20 22:14:54 +0000 | [diff] [blame] | 1485 | SourceLocation FileLoc = SourceLocation::getFileLoc(Entry.getOffset()); |
| 1486 | |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1487 | if (Line == 1 && Col == 1) |
Argyrios Kyrtzidis | 5e5e95d | 2011-09-20 22:14:54 +0000 | [diff] [blame] | 1488 | return FileLoc; |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1489 | |
| 1490 | ContentCache *Content |
Argyrios Kyrtzidis | efa2ff8 | 2011-09-19 20:40:29 +0000 | [diff] [blame] | 1491 | = const_cast<ContentCache *>(Entry.getFile().getContentCache()); |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1492 | if (!Content) |
| 1493 | return SourceLocation(); |
| 1494 | |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1495 | // If this is the first use of line information for this buffer, compute the |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1496 | // SourceLineCache for it on demand. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1497 | if (Content->SourceLineCache == 0) { |
| 1498 | bool MyInvalid = false; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1499 | ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1500 | if (MyInvalid) |
| 1501 | return SourceLocation(); |
| 1502 | } |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1503 | |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1504 | if (Line > Content->NumLines) { |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1505 | unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize(); |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1506 | if (Size > 0) |
| 1507 | --Size; |
Argyrios Kyrtzidis | 5e5e95d | 2011-09-20 22:14:54 +0000 | [diff] [blame] | 1508 | return FileLoc.getLocWithOffset(Size); |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | unsigned FilePos = Content->SourceLineCache[Line - 1]; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1512 | const char *Buf = Content->getBuffer(Diag, *this)->getBufferStart() + FilePos; |
| 1513 | unsigned BufLength = Content->getBuffer(Diag, *this)->getBufferEnd() - Buf; |
Argyrios Kyrtzidis | 5e5e95d | 2011-09-20 22:14:54 +0000 | [diff] [blame] | 1514 | if (BufLength == 0) |
| 1515 | return FileLoc.getLocWithOffset(FilePos); |
| 1516 | |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1517 | unsigned i = 0; |
| 1518 | |
| 1519 | // Check that the given column is valid. |
| 1520 | while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r') |
| 1521 | ++i; |
| 1522 | if (i < Col-1) |
Argyrios Kyrtzidis | 5e5e95d | 2011-09-20 22:14:54 +0000 | [diff] [blame] | 1523 | return FileLoc.getLocWithOffset(FilePos + i); |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1524 | |
Argyrios Kyrtzidis | 5e5e95d | 2011-09-20 22:14:54 +0000 | [diff] [blame] | 1525 | return FileLoc.getLocWithOffset(FilePos + Col - 1); |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1528 | /// \brief Compute a map of macro argument chunks to their expanded source |
| 1529 | /// location. Chunks that are not part of a macro argument will map to an |
| 1530 | /// invalid source location. e.g. if a file contains one macro argument at |
| 1531 | /// offset 100 with length 10, this is how the map will be formed: |
| 1532 | /// 0 -> SourceLocation() |
| 1533 | /// 100 -> Expanded macro arg location |
| 1534 | /// 110 -> SourceLocation() |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1535 | void SourceManager::computeMacroArgsCache(MacroArgsMap *&CachePtr, |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1536 | FileID FID) const { |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1537 | assert(!FID.isInvalid()); |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1538 | assert(!CachePtr); |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1539 | |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1540 | CachePtr = new MacroArgsMap(); |
| 1541 | MacroArgsMap &MacroArgsCache = *CachePtr; |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1542 | // Initially no macro argument chunk is present. |
| 1543 | MacroArgsCache.insert(std::make_pair(0, SourceLocation())); |
| 1544 | |
| 1545 | int ID = FID.ID; |
| 1546 | while (1) { |
| 1547 | ++ID; |
| 1548 | // Stop if there are no more FileIDs to check. |
| 1549 | if (ID > 0) { |
| 1550 | if (unsigned(ID) >= local_sloc_entry_size()) |
| 1551 | return; |
| 1552 | } else if (ID == -1) { |
| 1553 | return; |
| 1554 | } |
| 1555 | |
| 1556 | const SrcMgr::SLocEntry &Entry = getSLocEntryByID(ID); |
| 1557 | if (Entry.isFile()) { |
| 1558 | SourceLocation IncludeLoc = Entry.getFile().getIncludeLoc(); |
| 1559 | if (IncludeLoc.isInvalid()) |
| 1560 | continue; |
| 1561 | if (!isInFileID(IncludeLoc, FID)) |
| 1562 | return; // No more files/macros that may be "contained" in this file. |
| 1563 | |
| 1564 | // Skip the files/macros of the #include'd file, we only care about macros |
| 1565 | // that lexed macro arguments from our file. |
| 1566 | if (Entry.getFile().NumCreatedFIDs) |
| 1567 | ID += Entry.getFile().NumCreatedFIDs - 1/*because of next ++ID*/; |
| 1568 | continue; |
| 1569 | } |
| 1570 | |
| 1571 | if (!Entry.getExpansion().isMacroArgExpansion()) |
| 1572 | continue; |
| 1573 | |
| 1574 | SourceLocation SpellLoc = |
| 1575 | getSpellingLoc(Entry.getExpansion().getSpellingLoc()); |
| 1576 | unsigned BeginOffs; |
| 1577 | if (!isInFileID(SpellLoc, FID, &BeginOffs)) |
| 1578 | return; // No more files/macros that may be "contained" in this file. |
| 1579 | unsigned EndOffs = BeginOffs + getFileIDSize(FileID::get(ID)); |
| 1580 | |
| 1581 | // Add a new chunk for this macro argument. A previous macro argument chunk |
| 1582 | // may have been lexed again, so e.g. if the map is |
| 1583 | // 0 -> SourceLocation() |
| 1584 | // 100 -> Expanded loc #1 |
| 1585 | // 110 -> SourceLocation() |
| 1586 | // and we found a new macro FileID that lexed from offet 105 with length 3, |
| 1587 | // the new map will be: |
| 1588 | // 0 -> SourceLocation() |
| 1589 | // 100 -> Expanded loc #1 |
| 1590 | // 105 -> Expanded loc #2 |
| 1591 | // 108 -> Expanded loc #1 |
| 1592 | // 110 -> SourceLocation() |
| 1593 | // |
| 1594 | // Since re-lexed macro chunks will always be the same size or less of |
| 1595 | // previous chunks, we only need to find where the ending of the new macro |
| 1596 | // chunk is mapped to and update the map with new begin/end mappings. |
| 1597 | |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1598 | MacroArgsMap::iterator I = MacroArgsCache.upper_bound(EndOffs); |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1599 | --I; |
| 1600 | SourceLocation EndOffsMappedLoc = I->second; |
| 1601 | MacroArgsCache[BeginOffs] = SourceLocation::getMacroLoc(Entry.getOffset()); |
| 1602 | MacroArgsCache[EndOffs] = EndOffsMappedLoc; |
| 1603 | } |
| 1604 | } |
| 1605 | |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1606 | /// \brief If \arg Loc points inside a function macro argument, the returned |
| 1607 | /// location will be the macro location in which the argument was expanded. |
| 1608 | /// If a macro argument is used multiple times, the expanded location will |
| 1609 | /// be at the first expansion of the argument. |
| 1610 | /// e.g. |
| 1611 | /// MY_MACRO(foo); |
| 1612 | /// ^ |
| 1613 | /// Passing a file location pointing at 'foo', will yield a macro location |
| 1614 | /// where 'foo' was expanded into. |
Argyrios Kyrtzidis | 507097e | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1615 | SourceLocation |
| 1616 | SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) const { |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1617 | if (Loc.isInvalid() || !Loc.isFileID()) |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1618 | return Loc; |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1619 | |
| 1620 | FileID FID; |
| 1621 | unsigned Offset; |
| 1622 | llvm::tie(FID, Offset) = getDecomposedLoc(Loc); |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1623 | if (FID.isInvalid()) |
| 1624 | return Loc; |
| 1625 | |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1626 | MacroArgsMap *&MacroArgsCache = MacroArgsCacheMap[FID]; |
| 1627 | if (!MacroArgsCache) |
| 1628 | computeMacroArgsCache(MacroArgsCache, FID); |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1629 | |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1630 | assert(!MacroArgsCache->empty()); |
| 1631 | MacroArgsMap::iterator I = MacroArgsCache->upper_bound(Offset); |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1632 | --I; |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1633 | |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1634 | unsigned MacroArgBeginOffs = I->first; |
| 1635 | SourceLocation MacroArgExpandedLoc = I->second; |
| 1636 | if (MacroArgExpandedLoc.isValid()) |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1637 | return MacroArgExpandedLoc.getLocWithOffset(Offset - MacroArgBeginOffs); |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1638 | |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1639 | return Loc; |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 1642 | /// Given a decomposed source location, move it up the include/expansion stack |
| 1643 | /// to the parent source location. If this is possible, return the decomposed |
| 1644 | /// version of the parent in Loc and return false. If Loc is the top-level |
| 1645 | /// entry, return true and don't modify it. |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1646 | static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc, |
| 1647 | const SourceManager &SM) { |
| 1648 | SourceLocation UpperLoc; |
| 1649 | const SrcMgr::SLocEntry &Entry = SM.getSLocEntry(Loc.first); |
Chandler Carruth | 1728762 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 1650 | if (Entry.isExpansion()) |
Argyrios Kyrtzidis | 5040247 | 2011-09-19 20:39:57 +0000 | [diff] [blame] | 1651 | UpperLoc = Entry.getExpansion().getExpansionLocEnd(); |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1652 | else |
| 1653 | UpperLoc = Entry.getFile().getIncludeLoc(); |
| 1654 | |
| 1655 | if (UpperLoc.isInvalid()) |
| 1656 | return true; // We reached the top. |
| 1657 | |
| 1658 | Loc = SM.getDecomposedLoc(UpperLoc); |
| 1659 | return false; |
| 1660 | } |
| 1661 | |
| 1662 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1663 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 1664 | /// |
| 1665 | /// \returns true if LHS source location comes before RHS, false otherwise. |
| 1666 | bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS, |
| 1667 | SourceLocation RHS) const { |
| 1668 | assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!"); |
| 1669 | if (LHS == RHS) |
| 1670 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1672 | std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS); |
| 1673 | std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1675 | // If the source locations are in the same file, just compare offsets. |
| 1676 | if (LOffs.first == ROffs.first) |
| 1677 | return LOffs.second < ROffs.second; |
| 1678 | |
| 1679 | // If we are comparing a source location with multiple locations in the same |
| 1680 | // file, we get a big win by caching the result. |
Chris Lattner | 66a915f | 2010-05-07 05:10:46 +0000 | [diff] [blame] | 1681 | if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first)) |
| 1682 | return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 1684 | // Okay, we missed in the cache, start updating the cache for this query. |
Argyrios Kyrtzidis | 37e59a1 | 2011-08-17 00:31:18 +0000 | [diff] [blame] | 1685 | IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first, |
| 1686 | /*isLFIDBeforeRFID=*/LOffs.first.ID < ROffs.first.ID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1688 | // We need to find the common ancestor. The only way of doing this is to |
| 1689 | // build the complete include chain for one and then walking up the chain |
| 1690 | // of the other looking for a match. |
| 1691 | // We use a map from FileID to Offset to store the chain. Easier than writing |
| 1692 | // a custom set hash info that only depends on the first part of a pair. |
| 1693 | typedef llvm::DenseMap<FileID, unsigned> LocSet; |
| 1694 | LocSet LChain; |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1695 | do { |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1696 | LChain.insert(LOffs); |
| 1697 | // We catch the case where LOffs is in a file included by ROffs and |
| 1698 | // quit early. The other way round unfortunately remains suboptimal. |
| 1699 | } while (LOffs.first != ROffs.first && !MoveUpIncludeHierarchy(LOffs, *this)); |
| 1700 | LocSet::iterator I; |
| 1701 | while((I = LChain.find(ROffs.first)) == LChain.end()) { |
| 1702 | if (MoveUpIncludeHierarchy(ROffs, *this)) |
| 1703 | break; // Met at topmost file. |
| 1704 | } |
| 1705 | if (I != LChain.end()) |
| 1706 | LOffs = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1708 | // If we exited because we found a nearest common ancestor, compare the |
| 1709 | // locations within the common file and cache them. |
| 1710 | if (LOffs.first == ROffs.first) { |
| 1711 | IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second); |
| 1712 | return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second); |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1713 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1715 | // This can happen if a location is in a built-ins buffer. |
| 1716 | // But see PR5662. |
| 1717 | // Clear the lookup cache, it depends on a common location. |
Argyrios Kyrtzidis | 37e59a1 | 2011-08-17 00:31:18 +0000 | [diff] [blame] | 1718 | IsBeforeInTUCache.clear(); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1719 | bool LIsBuiltins = strcmp("<built-in>", |
| 1720 | getBuffer(LOffs.first)->getBufferIdentifier()) == 0; |
| 1721 | bool RIsBuiltins = strcmp("<built-in>", |
| 1722 | getBuffer(ROffs.first)->getBufferIdentifier()) == 0; |
| 1723 | // built-in is before non-built-in |
| 1724 | if (LIsBuiltins != RIsBuiltins) |
| 1725 | return LIsBuiltins; |
| 1726 | assert(LIsBuiltins && RIsBuiltins && |
| 1727 | "Non-built-in locations must be rooted in the main file"); |
| 1728 | // Both are in built-in buffers, but from different files. We just claim that |
| 1729 | // lower IDs come first. |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 1730 | return LOffs.first < ROffs.first; |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1731 | } |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1732 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1733 | /// PrintStats - Print statistics to stderr. |
| 1734 | /// |
| 1735 | void SourceManager::PrintStats() const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 1736 | llvm::errs() << "\n*** Source Manager Stats:\n"; |
| 1737 | llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size() |
| 1738 | << " mem buffers mapped.\n"; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1739 | llvm::errs() << LocalSLocEntryTable.size() << " local SLocEntry's allocated (" |
Ted Kremenek | 6e36c12 | 2011-07-27 18:41:16 +0000 | [diff] [blame] | 1740 | << llvm::capacity_in_bytes(LocalSLocEntryTable) |
Argyrios Kyrtzidis | d410e74 | 2011-07-07 03:40:24 +0000 | [diff] [blame] | 1741 | << " bytes of capacity), " |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1742 | << NextLocalOffset << "B of Sloc address space used.\n"; |
| 1743 | llvm::errs() << LoadedSLocEntryTable.size() |
| 1744 | << " loaded SLocEntries allocated, " |
Argyrios Kyrtzidis | ac836e4 | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1745 | << MaxLoadedOffset - CurrentLoadedOffset |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1746 | << "B of Sloc address space used.\n"; |
| 1747 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1748 | unsigned NumLineNumsComputed = 0; |
| 1749 | unsigned NumFileBytesMapped = 0; |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 1750 | for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){ |
| 1751 | NumLineNumsComputed += I->second->SourceLineCache != 0; |
| 1752 | NumFileBytesMapped += I->second->getSizeBytesMapped(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1753 | } |
Argyrios Kyrtzidis | fb3612e | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1754 | unsigned NumMacroArgsComputed = MacroArgsCacheMap.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 1756 | llvm::errs() << NumFileBytesMapped << " bytes of files mapped, " |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1757 | << NumLineNumsComputed << " files with line #'s computed, " |
| 1758 | << NumMacroArgsComputed << " files with macro args computed.\n"; |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 1759 | llvm::errs() << "FileID scans: " << NumLinearScans << " linear, " |
| 1760 | << NumBinaryProbes << " binary.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1761 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1762 | |
| 1763 | ExternalSLocEntrySource::~ExternalSLocEntrySource() { } |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 1764 | |
| 1765 | /// Return the amount of memory used by memory buffers, breaking down |
| 1766 | /// by heap-backed versus mmap'ed memory. |
| 1767 | SourceManager::MemoryBufferSizes SourceManager::getMemoryBufferSizes() const { |
| 1768 | size_t malloc_bytes = 0; |
| 1769 | size_t mmap_bytes = 0; |
| 1770 | |
| 1771 | for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) |
| 1772 | if (size_t sized_mapped = MemBufferInfos[i]->getSizeBytesMapped()) |
| 1773 | switch (MemBufferInfos[i]->getMemoryBufferKind()) { |
| 1774 | case llvm::MemoryBuffer::MemoryBuffer_MMap: |
| 1775 | mmap_bytes += sized_mapped; |
| 1776 | break; |
| 1777 | case llvm::MemoryBuffer::MemoryBuffer_Malloc: |
| 1778 | malloc_bytes += sized_mapped; |
| 1779 | break; |
| 1780 | } |
| 1781 | |
| 1782 | return MemoryBufferSizes(malloc_bytes, mmap_bytes); |
| 1783 | } |
| 1784 | |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 1785 | size_t SourceManager::getDataStructureSizes() const { |
Ted Kremenek | 6e36c12 | 2011-07-27 18:41:16 +0000 | [diff] [blame] | 1786 | return llvm::capacity_in_bytes(MemBufferInfos) |
| 1787 | + llvm::capacity_in_bytes(LocalSLocEntryTable) |
| 1788 | + llvm::capacity_in_bytes(LoadedSLocEntryTable) |
| 1789 | + llvm::capacity_in_bytes(SLocEntryLoaded) |
| 1790 | + llvm::capacity_in_bytes(FileInfos) |
| 1791 | + llvm::capacity_in_bytes(OverriddenFiles); |
Ted Kremenek | ca7dc2b | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 1792 | } |