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