Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1 | //===--- SourceManager.cpp - Track and cache source files -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SourceManager interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 802b776 | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Optional.h" |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringSwitch.h" |
| 21 | #include "llvm/Support/Capacity.h" |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Douglas Gregor | e0fbb83 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 27 | #include <cstring> |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 28 | #include <string> |
Douglas Gregor | 802b776 | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 30 | using namespace clang; |
Chris Lattner | 5f4b1ff | 2006-06-20 05:02:40 +0000 | [diff] [blame] | 31 | using namespace SrcMgr; |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 32 | using llvm::MemoryBuffer; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 153a0f1 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 35 | // SourceManager Helper Classes |
Chris Lattner | 153a0f1 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 37 | |
Ted Kremenek | c08bca6 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 38 | ContentCache::~ContentCache() { |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 39 | if (shouldFreeBuffer()) |
| 40 | delete Buffer.getPointer(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 43 | /// getSizeBytesMapped - Returns the number of bytes actually mapped for this |
| 44 | /// ContentCache. This can be 0 if the MemBuffer was not actually expanded. |
Ted Kremenek | 12c2af4 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 45 | unsigned ContentCache::getSizeBytesMapped() const { |
Douglas Gregor | 82752ec | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 46 | return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0; |
Ted Kremenek | 12c2af4 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Ted Kremenek | 8d58790 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 49 | /// Returns the kind of memory used to back the memory buffer for |
| 50 | /// this content cache. This is used for performance analysis. |
| 51 | llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind() const { |
| 52 | assert(Buffer.getPointer()); |
| 53 | |
| 54 | // Should be unreachable, but keep for sanity. |
| 55 | if (!Buffer.getPointer()) |
| 56 | return llvm::MemoryBuffer::MemoryBuffer_Malloc; |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 57 | |
| 58 | llvm::MemoryBuffer *buf = Buffer.getPointer(); |
Ted Kremenek | 8d58790 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 59 | return buf->getBufferKind(); |
| 60 | } |
| 61 | |
Ted Kremenek | 12c2af4 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 62 | /// getSize - Returns the size of the content encapsulated by this ContentCache. |
| 63 | /// This can be the size of the source file or the size of an arbitrary |
| 64 | /// scratch buffer. If the ContentCache encapsulates a source file, that |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 65 | /// file is not lazily brought in from disk to satisfy this query. |
Ted Kremenek | 12c2af4 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 66 | unsigned ContentCache::getSize() const { |
Douglas Gregor | 82752ec | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 67 | return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize() |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 68 | : (unsigned) ContentsEntry->getSize(); |
Ted Kremenek | 12c2af4 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 69 | } |
| 70 | |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 71 | void ContentCache::replaceBuffer(llvm::MemoryBuffer *B, bool DoNotFree) { |
Argyrios Kyrtzidis | 6eec06d | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 72 | if (B && B == Buffer.getPointer()) { |
Argyrios Kyrtzidis | cc6107d | 2011-12-10 01:38:26 +0000 | [diff] [blame] | 73 | assert(0 && "Replacing with the same buffer"); |
| 74 | Buffer.setInt(DoNotFree? DoNotFreeFlag : 0); |
| 75 | return; |
| 76 | } |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 78 | if (shouldFreeBuffer()) |
| 79 | delete Buffer.getPointer(); |
Douglas Gregor | 82752ec | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 80 | Buffer.setPointer(B); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 81 | Buffer.setInt(DoNotFree? DoNotFreeFlag : 0); |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 82 | } |
| 83 | |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 84 | llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag, |
| 85 | const SourceManager &SM, |
| 86 | SourceLocation Loc, |
| 87 | bool *Invalid) const { |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 88 | // Lazily create the Buffer for ContentCaches that wrap files. If we already |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 89 | // computed it, just return what we have. |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 90 | if (Buffer.getPointer() || !ContentsEntry) { |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 91 | if (Invalid) |
| 92 | *Invalid = isBufferInvalid(); |
Chris Lattner | 8fbe98b | 2010-04-20 18:14:03 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 94 | return Buffer.getPointer(); |
| 95 | } |
Benjamin Kramer | 5a3f1cf | 2010-11-18 12:46:39 +0000 | [diff] [blame] | 96 | |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 97 | bool isVolatile = SM.userFilesAreVolatile() && !IsSystemFile; |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 98 | auto BufferOrError = |
| 99 | SM.getFileManager().getBufferForFile(ContentsEntry, isVolatile); |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 100 | |
| 101 | // If we were unable to open the file, then we are in an inconsistent |
| 102 | // situation where the content cache referenced a file which no longer |
| 103 | // exists. Most likely, we were using a stat cache with an invalid entry but |
| 104 | // the file could also have been removed during processing. Since we can't |
| 105 | // really deal with this situation, just create an empty buffer. |
| 106 | // |
| 107 | // FIXME: This is definitely not ideal, but our immediate clients can't |
| 108 | // currently handle returning a null entry here. Ideally we should detect |
| 109 | // that we are in an inconsistent situation and error out as quickly as |
| 110 | // possible. |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 111 | if (!BufferOrError) { |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 112 | StringRef FillStr("<<<MISSING SOURCE FILE>>>\n"); |
Benjamin Kramer | c7dd599 | 2015-04-06 17:45:11 +0000 | [diff] [blame] | 113 | Buffer.setPointer(MemoryBuffer::getNewUninitMemBuffer( |
| 114 | ContentsEntry->getSize(), "<invalid>").release()); |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 115 | char *Ptr = const_cast<char*>(Buffer.getPointer()->getBufferStart()); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 116 | for (unsigned i = 0, e = ContentsEntry->getSize(); i != e; ++i) |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 117 | Ptr[i] = FillStr[i % FillStr.size()]; |
| 118 | |
| 119 | if (Diag.isDiagnosticInFlight()) |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 120 | Diag.SetDelayedDiagnostic(diag::err_cannot_open_file, |
| 121 | ContentsEntry->getName(), |
| 122 | BufferOrError.getError().message()); |
| 123 | else |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 124 | Diag.Report(Loc, diag::err_cannot_open_file) |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 125 | << ContentsEntry->getName() << BufferOrError.getError().message(); |
Chris Lattner | 5631b05 | 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 | } |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 132 | |
| 133 | Buffer.setPointer(BufferOrError->release()); |
| 134 | |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 135 | // Check that the file's size is the same as in the file entry (which may |
| 136 | // have come from a stat cache). |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 137 | if (getRawBuffer()->getBufferSize() != (size_t)ContentsEntry->getSize()) { |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 138 | if (Diag.isDiagnosticInFlight()) |
| 139 | Diag.SetDelayedDiagnostic(diag::err_file_modified, |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 140 | ContentsEntry->getName()); |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 141 | else |
| 142 | Diag.Report(Loc, diag::err_file_modified) |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 143 | << ContentsEntry->getName(); |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 144 | |
| 145 | Buffer.setInt(Buffer.getInt() | InvalidFlag); |
| 146 | if (Invalid) *Invalid = true; |
| 147 | return Buffer.getPointer(); |
| 148 | } |
Eric Christopher | 7f36a79 | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 150 | // If the buffer is valid, check to see if it has a UTF Byte Order Mark |
Eric Christopher | 7f36a79 | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 151 | // (BOM). We only support UTF-8 with and without a BOM right now. See |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 152 | // http://en.wikipedia.org/wiki/Byte_order_mark for more information. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 153 | StringRef BufStr = Buffer.getPointer()->getBuffer(); |
Eric Christopher | 7f36a79 | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 154 | const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr) |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 155 | .StartsWith("\xFE\xFF", "UTF-16 (BE)") |
| 156 | .StartsWith("\xFF\xFE", "UTF-16 (LE)") |
| 157 | .StartsWith("\x00\x00\xFE\xFF", "UTF-32 (BE)") |
| 158 | .StartsWith("\xFF\xFE\x00\x00", "UTF-32 (LE)") |
| 159 | .StartsWith("\x2B\x2F\x76", "UTF-7") |
| 160 | .StartsWith("\xF7\x64\x4C", "UTF-1") |
| 161 | .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC") |
| 162 | .StartsWith("\x0E\xFE\xFF", "SDSU") |
| 163 | .StartsWith("\xFB\xEE\x28", "BOCU-1") |
| 164 | .StartsWith("\x84\x31\x95\x33", "GB-18030") |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 165 | .Default(nullptr); |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 166 | |
Eric Christopher | 7f36a79 | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 167 | if (InvalidBOM) { |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 168 | Diag.Report(Loc, diag::err_unsupported_bom) |
Eric Christopher | 7f36a79 | 2011-04-09 00:01:04 +0000 | [diff] [blame] | 169 | << InvalidBOM << ContentsEntry->getName(); |
Chris Lattner | 5631b05 | 2010-11-23 08:50:03 +0000 | [diff] [blame] | 170 | Buffer.setInt(Buffer.getInt() | InvalidFlag); |
Ted Kremenek | 763ea55 | 2009-01-06 22:43:04 +0000 | [diff] [blame] | 171 | } |
Douglas Gregor | 802b776 | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 172 | |
Douglas Gregor | 82752ec | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 173 | if (Invalid) |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 174 | *Invalid = isBufferInvalid(); |
Douglas Gregor | 82752ec | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 175 | |
| 176 | return Buffer.getPointer(); |
Ted Kremenek | 12c2af4 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 179 | unsigned LineTableInfo::getLineTableFilenameID(StringRef Name) { |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 180 | auto IterBool = |
| 181 | FilenameIDs.insert(std::make_pair(Name, FilenamesByID.size())); |
| 182 | if (IterBool.second) |
| 183 | FilenamesByID.push_back(&*IterBool.first); |
| 184 | return IterBool.first->second; |
Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Lattner | 6e0e1f4 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 187 | /// AddLineNote - Add a line note to the line table that indicates that there |
James Dennett | 88b71e4 | 2012-06-15 21:28:23 +0000 | [diff] [blame] | 188 | /// is a \#line at the specified FID/Offset location which changes the presumed |
Chris Lattner | 6e0e1f4 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 189 | /// location to LineNo/FilenameID. |
Douglas Gregor | 02c2dbf | 2012-06-08 16:40:28 +0000 | [diff] [blame] | 190 | void LineTableInfo::AddLineNote(FileID FID, unsigned Offset, |
Chris Lattner | 6e0e1f4 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 191 | unsigned LineNo, int FilenameID) { |
Chris Lattner | 153a0f1 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 192 | std::vector<LineEntry> &Entries = LineEntries[FID]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 153a0f1 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 194 | assert((Entries.empty() || Entries.back().FileOffset < Offset) && |
| 195 | "Adding line entries out of order!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 197 | SrcMgr::CharacteristicKind Kind = SrcMgr::C_User; |
Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 198 | unsigned IncludeOffset = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 200 | if (!Entries.empty()) { |
| 201 | // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember |
| 202 | // that we are still in "foo.h". |
| 203 | if (FilenameID == -1) |
| 204 | FilenameID = Entries.back().FilenameID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 206 | // If we are after a line marker that switched us to system header mode, or |
| 207 | // that set #include information, preserve it. |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 208 | Kind = Entries.back().FileKind; |
Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 209 | IncludeOffset = Entries.back().IncludeOffset; |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 210 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 212 | Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind, |
| 213 | IncludeOffset)); |
Chris Lattner | 6e0e1f4 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 216 | /// AddLineNote This is the same as the previous version of AddLineNote, but is |
| 217 | /// used for GNU line markers. If EntryExit is 0, then this doesn't change the |
James Dennett | 87a2acf | 2012-06-17 03:22:59 +0000 | [diff] [blame] | 218 | /// presumed \#include stack. If it is 1, this is a file entry, if it is 2 then |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 219 | /// this is a file exit. FileKind specifies whether this is a system header or |
| 220 | /// extern C system header. |
Douglas Gregor | 02c2dbf | 2012-06-08 16:40:28 +0000 | [diff] [blame] | 221 | void LineTableInfo::AddLineNote(FileID FID, unsigned Offset, |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 222 | unsigned LineNo, int FilenameID, |
| 223 | unsigned EntryExit, |
| 224 | SrcMgr::CharacteristicKind FileKind) { |
| 225 | assert(FilenameID != -1 && "Unspecified filename should use other accessor"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 227 | std::vector<LineEntry> &Entries = LineEntries[FID]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 229 | assert((Entries.empty() || Entries.back().FileOffset < Offset) && |
| 230 | "Adding line entries out of order!"); |
| 231 | |
Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 232 | unsigned IncludeOffset = 0; |
| 233 | if (EntryExit == 0) { // No #include stack change. |
| 234 | IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset; |
| 235 | } else if (EntryExit == 1) { |
| 236 | IncludeOffset = Offset-1; |
| 237 | } else if (EntryExit == 2) { |
| 238 | assert(!Entries.empty() && Entries.back().IncludeOffset && |
| 239 | "PPDirectives should have caught case when popping empty include stack"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 241 | // Get the include loc of the last entries' include loc as our include loc. |
| 242 | IncludeOffset = 0; |
| 243 | if (const LineEntry *PrevEntry = |
| 244 | FindNearestLineEntry(FID, Entries.back().IncludeOffset)) |
| 245 | IncludeOffset = PrevEntry->IncludeOffset; |
| 246 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 248 | Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind, |
| 249 | IncludeOffset)); |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
Chris Lattner | d429392 | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 253 | /// FindNearestLineEntry - Find the line entry nearest to FID that is before |
| 254 | /// it. If there is no line entry before Offset in FID, return null. |
Douglas Gregor | 02c2dbf | 2012-06-08 16:40:28 +0000 | [diff] [blame] | 255 | const LineEntry *LineTableInfo::FindNearestLineEntry(FileID FID, |
Chris Lattner | d429392 | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 256 | unsigned Offset) { |
| 257 | const std::vector<LineEntry> &Entries = LineEntries[FID]; |
| 258 | assert(!Entries.empty() && "No #line entries for this FID after all!"); |
| 259 | |
Chris Lattner | 334a2ad | 2009-02-04 04:46:59 +0000 | [diff] [blame] | 260 | // It is very common for the query to be after the last #line, check this |
| 261 | // first. |
| 262 | if (Entries.back().FileOffset <= Offset) |
| 263 | return &Entries.back(); |
Chris Lattner | d429392 | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 334a2ad | 2009-02-04 04:46:59 +0000 | [diff] [blame] | 265 | // Do a binary search to find the maximal element that is still before Offset. |
| 266 | std::vector<LineEntry>::const_iterator I = |
| 267 | std::upper_bound(Entries.begin(), Entries.end(), Offset); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 268 | if (I == Entries.begin()) return nullptr; |
Chris Lattner | 334a2ad | 2009-02-04 04:46:59 +0000 | [diff] [blame] | 269 | return &*--I; |
Chris Lattner | d429392 | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 270 | } |
Chris Lattner | 6e0e1f4 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 272 | /// \brief Add a new line entry that has already been encoded into |
| 273 | /// the internal representation of the line table. |
Douglas Gregor | 02c2dbf | 2012-06-08 16:40:28 +0000 | [diff] [blame] | 274 | void LineTableInfo::AddEntry(FileID FID, |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 275 | const std::vector<LineEntry> &Entries) { |
| 276 | LineEntries[FID] = Entries; |
| 277 | } |
Chris Lattner | 6e0e1f4 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 278 | |
Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 279 | /// getLineTableFilenameID - Return the uniqued ID for the specified filename. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | /// |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 281 | unsigned SourceManager::getLineTableFilenameID(StringRef Name) { |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 282 | if (!LineTable) |
Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 283 | LineTable = new LineTableInfo(); |
Jay Foad | 9a6b098 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 284 | return LineTable->getLineTableFilenameID(Name); |
Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
Chris Lattner | 1eaa70a | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 288 | /// AddLineNote - Add a line note to the line table for the FileID and offset |
| 289 | /// specified by Loc. If FilenameID is -1, it is considered to be |
| 290 | /// unspecified. |
| 291 | void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, |
| 292 | int FilenameID) { |
Chandler Carruth | c7ca521 | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 293 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 295 | bool Invalid = false; |
| 296 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); |
| 297 | if (!Entry.isFile() || Invalid) |
| 298 | return; |
| 299 | |
| 300 | const SrcMgr::FileInfo &FileInfo = Entry.getFile(); |
Chris Lattner | 6e0e1f4 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 301 | |
| 302 | // Remember that this file has #line directives now if it doesn't already. |
| 303 | const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 305 | if (!LineTable) |
Chris Lattner | 6e0e1f4 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 306 | LineTable = new LineTableInfo(); |
Douglas Gregor | 02c2dbf | 2012-06-08 16:40:28 +0000 | [diff] [blame] | 307 | LineTable->AddLineNote(LocInfo.first, LocInfo.second, LineNo, FilenameID); |
Chris Lattner | 1eaa70a | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 310 | /// AddLineNote - Add a GNU line marker to the line table. |
| 311 | void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, |
| 312 | int FilenameID, bool IsFileEntry, |
| 313 | bool IsFileExit, bool IsSystemHeader, |
| 314 | bool IsExternCHeader) { |
| 315 | // If there is no filename and no flags, this is treated just like a #line, |
| 316 | // which does not change the flags of the previous line marker. |
| 317 | if (FilenameID == -1) { |
| 318 | assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader && |
| 319 | "Can't set flags without setting the filename!"); |
| 320 | return AddLineNote(Loc, LineNo, FilenameID); |
| 321 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Chandler Carruth | c7ca521 | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 323 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 324 | |
| 325 | bool Invalid = false; |
| 326 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); |
| 327 | if (!Entry.isFile() || Invalid) |
| 328 | return; |
| 329 | |
| 330 | const SrcMgr::FileInfo &FileInfo = Entry.getFile(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 332 | // Remember that this file has #line directives now if it doesn't already. |
| 333 | const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 335 | if (!LineTable) |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 336 | LineTable = new LineTableInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 338 | SrcMgr::CharacteristicKind FileKind; |
| 339 | if (IsExternCHeader) |
| 340 | FileKind = SrcMgr::C_ExternCSystem; |
| 341 | else if (IsSystemHeader) |
| 342 | FileKind = SrcMgr::C_System; |
| 343 | else |
| 344 | FileKind = SrcMgr::C_User; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 346 | unsigned EntryExit = 0; |
| 347 | if (IsFileEntry) |
| 348 | EntryExit = 1; |
| 349 | else if (IsFileExit) |
| 350 | EntryExit = 2; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
Douglas Gregor | 02c2dbf | 2012-06-08 16:40:28 +0000 | [diff] [blame] | 352 | LineTable->AddLineNote(LocInfo.first, LocInfo.second, LineNo, FilenameID, |
Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 353 | EntryExit, FileKind); |
| 354 | } |
| 355 | |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 356 | LineTableInfo &SourceManager::getLineTable() { |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 357 | if (!LineTable) |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 358 | LineTable = new LineTableInfo(); |
| 359 | return *LineTable; |
| 360 | } |
Chris Lattner | 1eaa70a | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 153a0f1 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 362 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 363 | // Private 'Create' methods. |
Chris Lattner | 153a0f1 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 364 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 12c2af4 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 365 | |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 366 | SourceManager::SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr, |
| 367 | bool UserFilesAreVolatile) |
Argyrios Kyrtzidis | 97d3a38 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 368 | : Diag(Diag), FileMgr(FileMgr), OverridenFilesKeepOriginalName(true), |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 369 | UserFilesAreVolatile(UserFilesAreVolatile), |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 370 | ExternalSLocEntries(nullptr), LineTable(nullptr), NumLinearScans(0), |
Rafael Espindola | e0f6d88 | 2014-08-18 18:33:41 +0000 | [diff] [blame] | 371 | NumBinaryProbes(0) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 372 | clearIDTables(); |
| 373 | Diag.setSourceManager(this); |
| 374 | } |
| 375 | |
Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 376 | SourceManager::~SourceManager() { |
| 377 | delete LineTable; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
Chris Lattner | c8233df | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 379 | // Delete FileEntry objects corresponding to content caches. Since the actual |
| 380 | // content cache objects are bump pointer allocated, we just have to run the |
| 381 | // dtors, but we call the deallocate method for completeness. |
| 382 | for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) { |
Argyrios Kyrtzidis | af41b3f | 2011-12-15 23:37:55 +0000 | [diff] [blame] | 383 | if (MemBufferInfos[i]) { |
| 384 | MemBufferInfos[i]->~ContentCache(); |
| 385 | ContentCacheAlloc.Deallocate(MemBufferInfos[i]); |
| 386 | } |
Chris Lattner | c8233df | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 387 | } |
| 388 | for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator |
| 389 | I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { |
Argyrios Kyrtzidis | af41b3f | 2011-12-15 23:37:55 +0000 | [diff] [blame] | 390 | if (I->second) { |
| 391 | I->second->~ContentCache(); |
| 392 | ContentCacheAlloc.Deallocate(I->second); |
| 393 | } |
Chris Lattner | c8233df | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 394 | } |
Argyrios Kyrtzidis | 4bdd6aa | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 395 | |
Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 396 | llvm::DeleteContainerSeconds(MacroArgsCacheMap); |
Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void SourceManager::clearIDTables() { |
| 400 | MainFileID = FileID(); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 401 | LocalSLocEntryTable.clear(); |
| 402 | LoadedSLocEntryTable.clear(); |
| 403 | SLocEntryLoaded.clear(); |
Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 404 | LastLineNoFileIDQuery = FileID(); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 405 | LastLineNoContentCache = nullptr; |
Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 406 | LastFileIDLookup = FileID(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 408 | if (LineTable) |
| 409 | LineTable->clear(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 411 | // Use up FileID #0 as an invalid expansion. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 412 | NextLocalOffset = 0; |
Argyrios Kyrtzidis | 92a47bd | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 413 | CurrentLoadedOffset = MaxLoadedOffset; |
Chandler Carruth | 115b077 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 414 | createExpansionLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1); |
Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 417 | /// getOrCreateContentCache - Create or return a cached ContentCache for the |
| 418 | /// specified file. |
| 419 | const ContentCache * |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 420 | SourceManager::getOrCreateContentCache(const FileEntry *FileEnt, |
| 421 | bool isSystemFile) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 422 | assert(FileEnt && "Didn't specify a file entry to use?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 424 | // Do we already have information about this file? |
Chris Lattner | c8233df | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 425 | ContentCache *&Entry = FileInfos[FileEnt]; |
| 426 | if (Entry) return Entry; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Chandler Carruth | 47c4808 | 2014-04-15 21:34:12 +0000 | [diff] [blame] | 428 | // Nope, create a new Cache entry. |
| 429 | Entry = ContentCacheAlloc.Allocate<ContentCache>(); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 430 | |
Argyrios Kyrtzidis | 6eec06d | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 431 | if (OverriddenFilesInfo) { |
| 432 | // If the file contents are overridden with contents from another file, |
| 433 | // pass that file to ContentCache. |
| 434 | llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator |
| 435 | overI = OverriddenFilesInfo->OverriddenFiles.find(FileEnt); |
| 436 | if (overI == OverriddenFilesInfo->OverriddenFiles.end()) |
| 437 | new (Entry) ContentCache(FileEnt); |
| 438 | else |
| 439 | new (Entry) ContentCache(OverridenFilesKeepOriginalName ? FileEnt |
| 440 | : overI->second, |
| 441 | overI->second); |
| 442 | } else { |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 443 | new (Entry) ContentCache(FileEnt); |
Argyrios Kyrtzidis | 6eec06d | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 444 | } |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 445 | |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 446 | Entry->IsSystemFile = isSystemFile; |
| 447 | |
Chris Lattner | c8233df | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 448 | return Entry; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | |
Ted Kremenek | 08bed09 | 2007-10-31 17:53:38 +0000 | [diff] [blame] | 452 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 453 | /// memory buffer. This does no caching. |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 454 | const ContentCache *SourceManager::createMemBufferContentCache( |
| 455 | std::unique_ptr<llvm::MemoryBuffer> Buffer) { |
Chandler Carruth | 47c4808 | 2014-04-15 21:34:12 +0000 | [diff] [blame] | 456 | // Add a new ContentCache to the MemBufferInfos list and return it. |
| 457 | ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(); |
Chris Lattner | c8233df | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 458 | new (Entry) ContentCache(); |
| 459 | MemBufferInfos.push_back(Entry); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 460 | Entry->setBuffer(std::move(Buffer)); |
Chris Lattner | c8233df | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 461 | return Entry; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Argyrios Kyrtzidis | 969fdfd | 2012-02-20 23:58:07 +0000 | [diff] [blame] | 464 | const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index, |
| 465 | bool *Invalid) const { |
| 466 | assert(!SLocEntryLoaded[Index]); |
| 467 | if (ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2))) { |
| 468 | if (Invalid) |
| 469 | *Invalid = true; |
| 470 | // If the file of the SLocEntry changed we could still have loaded it. |
| 471 | if (!SLocEntryLoaded[Index]) { |
| 472 | // Try to recover; create a SLocEntry so the rest of clang can handle it. |
| 473 | LoadedSLocEntryTable[Index] = SLocEntry::get(0, |
| 474 | FileInfo::get(SourceLocation(), |
| 475 | getFakeContentCacheForRecovery(), |
| 476 | SrcMgr::C_User)); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return LoadedSLocEntryTable[Index]; |
| 481 | } |
| 482 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 483 | std::pair<int, unsigned> |
| 484 | SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries, |
| 485 | unsigned TotalSize) { |
| 486 | assert(ExternalSLocEntries && "Don't have an external sloc source"); |
Richard Smith | 78d81ec | 2015-08-12 22:25:24 +0000 | [diff] [blame] | 487 | // Make sure we're not about to run out of source locations. |
| 488 | if (CurrentLoadedOffset - TotalSize < NextLocalOffset) |
| 489 | return std::make_pair(0, 0); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 490 | LoadedSLocEntryTable.resize(LoadedSLocEntryTable.size() + NumSLocEntries); |
| 491 | SLocEntryLoaded.resize(LoadedSLocEntryTable.size()); |
| 492 | CurrentLoadedOffset -= TotalSize; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 493 | int ID = LoadedSLocEntryTable.size(); |
| 494 | return std::make_pair(-ID - 1, CurrentLoadedOffset); |
Douglas Gregor | 0bc1293 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 497 | /// \brief As part of recovering from missing or changed content, produce a |
| 498 | /// fake, non-empty buffer. |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 499 | llvm::MemoryBuffer *SourceManager::getFakeBufferForRecovery() const { |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 500 | if (!FakeBufferForRecovery) |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 501 | FakeBufferForRecovery = |
| 502 | llvm::MemoryBuffer::getMemBuffer("<<<INVALID BUFFER>>"); |
Rafael Espindola | e0f6d88 | 2014-08-18 18:33:41 +0000 | [diff] [blame] | 503 | |
| 504 | return FakeBufferForRecovery.get(); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 505 | } |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 506 | |
Argyrios Kyrtzidis | 969fdfd | 2012-02-20 23:58:07 +0000 | [diff] [blame] | 507 | /// \brief As part of recovering from missing or changed content, produce a |
| 508 | /// fake content cache. |
| 509 | const SrcMgr::ContentCache * |
| 510 | SourceManager::getFakeContentCacheForRecovery() const { |
| 511 | if (!FakeContentCacheForRecovery) { |
Rafael Espindola | e0f6d88 | 2014-08-18 18:33:41 +0000 | [diff] [blame] | 512 | FakeContentCacheForRecovery = llvm::make_unique<SrcMgr::ContentCache>(); |
Argyrios Kyrtzidis | 969fdfd | 2012-02-20 23:58:07 +0000 | [diff] [blame] | 513 | FakeContentCacheForRecovery->replaceBuffer(getFakeBufferForRecovery(), |
| 514 | /*DoNotFree=*/true); |
| 515 | } |
Rafael Espindola | e0f6d88 | 2014-08-18 18:33:41 +0000 | [diff] [blame] | 516 | return FakeContentCacheForRecovery.get(); |
Argyrios Kyrtzidis | 969fdfd | 2012-02-20 23:58:07 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Argyrios Kyrtzidis | 065d720 | 2013-05-16 21:37:39 +0000 | [diff] [blame] | 519 | /// \brief Returns the previous in-order FileID or an invalid FileID if there |
| 520 | /// is no previous one. |
| 521 | FileID SourceManager::getPreviousFileID(FileID FID) const { |
| 522 | if (FID.isInvalid()) |
| 523 | return FileID(); |
| 524 | |
| 525 | int ID = FID.ID; |
| 526 | if (ID == -1) |
| 527 | return FileID(); |
| 528 | |
| 529 | if (ID > 0) { |
| 530 | if (ID-1 == 0) |
| 531 | return FileID(); |
| 532 | } else if (unsigned(-(ID-1) - 2) >= LoadedSLocEntryTable.size()) { |
| 533 | return FileID(); |
| 534 | } |
| 535 | |
| 536 | return FileID::get(ID-1); |
| 537 | } |
| 538 | |
| 539 | /// \brief Returns the next in-order FileID or an invalid FileID if there is |
| 540 | /// no next one. |
| 541 | FileID SourceManager::getNextFileID(FileID FID) const { |
| 542 | if (FID.isInvalid()) |
| 543 | return FileID(); |
| 544 | |
| 545 | int ID = FID.ID; |
| 546 | if (ID > 0) { |
| 547 | if (unsigned(ID+1) >= local_sloc_entry_size()) |
| 548 | return FileID(); |
| 549 | } else if (ID+1 >= -1) { |
| 550 | return FileID(); |
| 551 | } |
| 552 | |
| 553 | return FileID::get(ID+1); |
| 554 | } |
| 555 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 556 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 557 | // Methods to create new FileID's and macro expansions. |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 558 | //===----------------------------------------------------------------------===// |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 559 | |
Dan Gohman | ce46f02 | 2010-08-26 21:27:06 +0000 | [diff] [blame] | 560 | /// createFileID - Create a new FileID for the specified ContentCache and |
Ted Kremenek | e26f3c5 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 561 | /// include position. This works regardless of whether the ContentCache |
| 562 | /// corresponds to a file or some other input source. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 563 | FileID SourceManager::createFileID(const ContentCache *File, |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 564 | SourceLocation IncludePos, |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 565 | SrcMgr::CharacteristicKind FileCharacter, |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 566 | int LoadedID, unsigned LoadedOffset) { |
| 567 | if (LoadedID < 0) { |
| 568 | assert(LoadedID != -1 && "Loading sentinel FileID"); |
| 569 | unsigned Index = unsigned(-LoadedID) - 2; |
| 570 | assert(Index < LoadedSLocEntryTable.size() && "FileID out of range"); |
| 571 | assert(!SLocEntryLoaded[Index] && "FileID already loaded"); |
| 572 | LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset, |
| 573 | FileInfo::get(IncludePos, File, FileCharacter)); |
| 574 | SLocEntryLoaded[Index] = true; |
| 575 | return FileID::get(LoadedID); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 576 | } |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 577 | LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, |
| 578 | FileInfo::get(IncludePos, File, |
| 579 | FileCharacter))); |
Ted Kremenek | 12c2af4 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 580 | unsigned FileSize = File->getSize(); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 581 | assert(NextLocalOffset + FileSize + 1 > NextLocalOffset && |
| 582 | NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset && |
| 583 | "Ran out of source locations!"); |
| 584 | // We do a +1 here because we want a SourceLocation that means "the end of the |
| 585 | // file", e.g. for the "no newline at the end of the file" diagnostic. |
| 586 | NextLocalOffset += FileSize + 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 588 | // Set LastFileIDLookup to the newly created file. The next getFileID call is |
| 589 | // almost guaranteed to be from that file. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 590 | FileID FID = FileID::get(LocalSLocEntryTable.size()-1); |
Argyrios Kyrtzidis | 0152c6c | 2009-06-23 00:42:06 +0000 | [diff] [blame] | 591 | return LastFileIDLookup = FID; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Chandler Carruth | 402bb38 | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 594 | SourceLocation |
Chandler Carruth | 115b077 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 595 | SourceManager::createMacroArgExpansionLoc(SourceLocation SpellingLoc, |
| 596 | SourceLocation ExpansionLoc, |
| 597 | unsigned TokLength) { |
Chandler Carruth | 73ee5d7 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 598 | ExpansionInfo Info = ExpansionInfo::createForMacroArg(SpellingLoc, |
| 599 | ExpansionLoc); |
| 600 | return createExpansionLocImpl(Info, TokLength); |
Chandler Carruth | 402bb38 | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | SourceLocation |
Chandler Carruth | 115b077 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 604 | SourceManager::createExpansionLoc(SourceLocation SpellingLoc, |
| 605 | SourceLocation ExpansionLocStart, |
| 606 | SourceLocation ExpansionLocEnd, |
| 607 | unsigned TokLength, |
| 608 | int LoadedID, |
| 609 | unsigned LoadedOffset) { |
Chandler Carruth | 73ee5d7 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 610 | ExpansionInfo Info = ExpansionInfo::create(SpellingLoc, ExpansionLocStart, |
| 611 | ExpansionLocEnd); |
| 612 | return createExpansionLocImpl(Info, TokLength, LoadedID, LoadedOffset); |
Chandler Carruth | 115b077 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | SourceLocation |
Chandler Carruth | 73ee5d7 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 616 | SourceManager::createExpansionLocImpl(const ExpansionInfo &Info, |
Chandler Carruth | 115b077 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 617 | unsigned TokLength, |
| 618 | int LoadedID, |
| 619 | unsigned LoadedOffset) { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 620 | if (LoadedID < 0) { |
| 621 | assert(LoadedID != -1 && "Loading sentinel FileID"); |
| 622 | unsigned Index = unsigned(-LoadedID) - 2; |
| 623 | assert(Index < LoadedSLocEntryTable.size() && "FileID out of range"); |
| 624 | assert(!SLocEntryLoaded[Index] && "FileID already loaded"); |
Chandler Carruth | 73ee5d7 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 625 | LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset, Info); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 626 | SLocEntryLoaded[Index] = true; |
| 627 | return SourceLocation::getMacroLoc(LoadedOffset); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 628 | } |
Chandler Carruth | 73ee5d7 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 629 | LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, Info)); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 630 | assert(NextLocalOffset + TokLength + 1 > NextLocalOffset && |
| 631 | NextLocalOffset + TokLength + 1 <= CurrentLoadedOffset && |
| 632 | "Ran out of source locations!"); |
| 633 | // See createFileID for that +1. |
| 634 | NextLocalOffset += TokLength + 1; |
| 635 | return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1)); |
Chris Lattner | 7d6a4f6 | 2006-06-30 06:10:08 +0000 | [diff] [blame] | 636 | } |
| 637 | |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 638 | llvm::MemoryBuffer *SourceManager::getMemoryBufferForFile(const FileEntry *File, |
| 639 | bool *Invalid) { |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 640 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(File); |
Douglas Gregor | 802b776 | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 641 | assert(IR && "getOrCreateContentCache() cannot return NULL"); |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 642 | return IR->getBuffer(Diag, *this, SourceLocation(), Invalid); |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Dan Gohman | 5d223dc | 2010-10-26 20:47:28 +0000 | [diff] [blame] | 645 | void SourceManager::overrideFileContents(const FileEntry *SourceFile, |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 646 | llvm::MemoryBuffer *Buffer, |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 647 | bool DoNotFree) { |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 648 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); |
Dan Gohman | 5d223dc | 2010-10-26 20:47:28 +0000 | [diff] [blame] | 649 | assert(IR && "getOrCreateContentCache() cannot return NULL"); |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 650 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 651 | const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree); |
Douglas Gregor | 9dc3212 | 2011-11-16 20:05:18 +0000 | [diff] [blame] | 652 | const_cast<SrcMgr::ContentCache *>(IR)->BufferOverridden = true; |
Argyrios Kyrtzidis | 6eec06d | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 653 | |
| 654 | getOverriddenFilesInfo().OverriddenFilesWithBuffer.insert(SourceFile); |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 657 | void SourceManager::overrideFileContents(const FileEntry *SourceFile, |
| 658 | const FileEntry *NewFile) { |
| 659 | assert(SourceFile->getSize() == NewFile->getSize() && |
| 660 | "Different sizes, use the FileManager to create a virtual file with " |
| 661 | "the correct size"); |
| 662 | assert(FileInfos.count(SourceFile) == 0 && |
| 663 | "This function should be called at the initialization stage, before " |
| 664 | "any parsing occurs."); |
Argyrios Kyrtzidis | 6eec06d | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 665 | getOverriddenFilesInfo().OverriddenFiles[SourceFile] = NewFile; |
| 666 | } |
| 667 | |
| 668 | void SourceManager::disableFileContentsOverride(const FileEntry *File) { |
| 669 | if (!isFileOverridden(File)) |
| 670 | return; |
| 671 | |
| 672 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(File); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 673 | const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(nullptr); |
Argyrios Kyrtzidis | 6eec06d | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 674 | const_cast<SrcMgr::ContentCache *>(IR)->ContentsEntry = IR->OrigEntry; |
| 675 | |
| 676 | assert(OverriddenFilesInfo); |
| 677 | OverriddenFilesInfo->OverriddenFiles.erase(File); |
| 678 | OverriddenFilesInfo->OverriddenFilesWithBuffer.erase(File); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Richard Smith | fb1e7f7 | 2015-08-14 05:02:58 +0000 | [diff] [blame] | 681 | void SourceManager::embedFileContentsInModule(const FileEntry *File) { |
| 682 | // We model an embedded file as a file whose buffer has been overridden |
| 683 | // by its contents as they are now. |
| 684 | const SrcMgr::ContentCache *CC = getOrCreateContentCache(File); |
| 685 | const_cast<SrcMgr::ContentCache *>(CC)->BufferOverridden = true; |
| 686 | } |
| 687 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 688 | StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const { |
Douglas Gregor | 4fb7fbe | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 689 | bool MyInvalid = false; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 690 | const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 691 | if (!SLoc.isFile() || MyInvalid) { |
Douglas Gregor | 86af984 | 2011-01-31 22:42:36 +0000 | [diff] [blame] | 692 | if (Invalid) |
| 693 | *Invalid = true; |
| 694 | return "<<<<<INVALID SOURCE LOCATION>>>>>"; |
| 695 | } |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 696 | |
| 697 | llvm::MemoryBuffer *Buf = SLoc.getFile().getContentCache()->getBuffer( |
| 698 | Diag, *this, SourceLocation(), &MyInvalid); |
Douglas Gregor | e0fbb83 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 699 | if (Invalid) |
Douglas Gregor | 4fb7fbe | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 700 | *Invalid = MyInvalid; |
| 701 | |
| 702 | if (MyInvalid) |
Douglas Gregor | 86af984 | 2011-01-31 22:42:36 +0000 | [diff] [blame] | 703 | return "<<<<<INVALID SOURCE LOCATION>>>>>"; |
Douglas Gregor | 4fb7fbe | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 704 | |
Benjamin Kramer | eb92dc0 | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 705 | return Buf->getBuffer(); |
Douglas Gregor | 802b776 | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 706 | } |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 707 | |
Chris Lattner | 153a0f1 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 708 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 709 | // SourceLocation manipulation methods. |
Chris Lattner | 153a0f1 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 710 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 711 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 712 | /// \brief Return the FileID for a SourceLocation. |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 713 | /// |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 714 | /// This is the cache-miss path of getFileID. Not as hot as that function, but |
| 715 | /// still very important. It is responsible for finding the entry in the |
| 716 | /// SLocEntry tables that contains the specified location. |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 717 | FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 718 | if (!SLocOffset) |
| 719 | return FileID::get(0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 721 | // Now it is time to search for the correct file. See where the SLocOffset |
| 722 | // sits in the global view and consult local or loaded buffers for it. |
| 723 | if (SLocOffset < NextLocalOffset) |
| 724 | return getFileIDLocal(SLocOffset); |
| 725 | return getFileIDLoaded(SLocOffset); |
| 726 | } |
| 727 | |
| 728 | /// \brief Return the FileID for a SourceLocation with a low offset. |
| 729 | /// |
| 730 | /// This function knows that the SourceLocation is in a local buffer, not a |
| 731 | /// loaded one. |
| 732 | FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const { |
| 733 | assert(SLocOffset < NextLocalOffset && "Bad function choice"); |
| 734 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 735 | // After the first and second level caches, I see two common sorts of |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 736 | // behavior: 1) a lot of searched FileID's are "near" the cached file |
| 737 | // location or are "near" the cached expansion location. 2) others are just |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 738 | // completely random and may be a very long way away. |
| 739 | // |
| 740 | // To handle this, we do a linear search for up to 8 steps to catch #1 quickly |
| 741 | // then we fall back to a less cache efficient, but more scalable, binary |
| 742 | // search to find the location. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 744 | // See if this is near the file point - worst case we start scanning from the |
| 745 | // most newly created FileID. |
Benjamin Kramer | 2999b77 | 2013-02-22 18:29:39 +0000 | [diff] [blame] | 746 | const SrcMgr::SLocEntry *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 748 | if (LastFileIDLookup.ID < 0 || |
| 749 | LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 750 | // Neither loc prunes our search. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 751 | I = LocalSLocEntryTable.end(); |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 752 | } else { |
| 753 | // Perhaps it is near the file point. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 754 | I = LocalSLocEntryTable.begin()+LastFileIDLookup.ID; |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // Find the FileID that contains this. "I" is an iterator that points to a |
| 758 | // FileID whose offset is known to be larger than SLocOffset. |
| 759 | unsigned NumProbes = 0; |
| 760 | while (1) { |
| 761 | --I; |
| 762 | if (I->getOffset() <= SLocOffset) { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 763 | FileID Res = FileID::get(int(I - LocalSLocEntryTable.begin())); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 764 | |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 765 | // If this isn't an expansion, remember it. We have good locality across |
| 766 | // FileID lookups. |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 767 | if (!I->isExpansion()) |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 768 | LastFileIDLookup = Res; |
| 769 | NumLinearScans += NumProbes+1; |
| 770 | return Res; |
| 771 | } |
| 772 | if (++NumProbes == 8) |
| 773 | break; |
| 774 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 776 | // Convert "I" back into an index. We know that it is an entry whose index is |
| 777 | // larger than the offset we are looking for. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 778 | unsigned GreaterIndex = I - LocalSLocEntryTable.begin(); |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 779 | // LessIndex - This is the lower bound of the range that we're searching. |
| 780 | // We know that the offset corresponding to the FileID is is less than |
| 781 | // SLocOffset. |
| 782 | unsigned LessIndex = 0; |
| 783 | NumProbes = 0; |
| 784 | while (1) { |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 785 | bool Invalid = false; |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 786 | unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 787 | unsigned MidOffset = getLocalSLocEntry(MiddleIndex, &Invalid).getOffset(); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 788 | if (Invalid) |
| 789 | return FileID::get(0); |
| 790 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 791 | ++NumProbes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 793 | // If the offset of the midpoint is too large, chop the high side of the |
| 794 | // range to the midpoint. |
| 795 | if (MidOffset > SLocOffset) { |
| 796 | GreaterIndex = MiddleIndex; |
| 797 | continue; |
| 798 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 800 | // If the middle index contains the value, succeed and return. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 801 | // FIXME: This could be made faster by using a function that's aware of |
| 802 | // being in the local area. |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 803 | if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 804 | FileID Res = FileID::get(MiddleIndex); |
| 805 | |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 806 | // If this isn't a macro expansion, remember it. We have good locality |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 807 | // across FileID lookups. |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 808 | if (!LocalSLocEntryTable[MiddleIndex].isExpansion()) |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 809 | LastFileIDLookup = Res; |
| 810 | NumBinaryProbes += NumProbes; |
| 811 | return Res; |
| 812 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 814 | // Otherwise, move the low-side up to the middle index. |
| 815 | LessIndex = MiddleIndex; |
| 816 | } |
| 817 | } |
| 818 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 819 | /// \brief Return the FileID for a SourceLocation with a high offset. |
| 820 | /// |
| 821 | /// This function knows that the SourceLocation is in a loaded buffer, not a |
| 822 | /// local one. |
| 823 | FileID SourceManager::getFileIDLoaded(unsigned SLocOffset) const { |
Argyrios Kyrtzidis | 25029d4 | 2011-10-03 23:43:01 +0000 | [diff] [blame] | 824 | // Sanity checking, otherwise a bug may lead to hanging in release build. |
Argyrios Kyrtzidis | 8edffaa | 2011-10-25 00:29:44 +0000 | [diff] [blame] | 825 | if (SLocOffset < CurrentLoadedOffset) { |
| 826 | assert(0 && "Invalid SLocOffset or bad function choice"); |
Argyrios Kyrtzidis | 25029d4 | 2011-10-03 23:43:01 +0000 | [diff] [blame] | 827 | return FileID(); |
Argyrios Kyrtzidis | 8edffaa | 2011-10-25 00:29:44 +0000 | [diff] [blame] | 828 | } |
Argyrios Kyrtzidis | 25029d4 | 2011-10-03 23:43:01 +0000 | [diff] [blame] | 829 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 830 | // Essentially the same as the local case, but the loaded array is sorted |
| 831 | // in the other direction. |
| 832 | |
| 833 | // First do a linear scan from the last lookup position, if possible. |
| 834 | unsigned I; |
| 835 | int LastID = LastFileIDLookup.ID; |
| 836 | if (LastID >= 0 || getLoadedSLocEntryByID(LastID).getOffset() < SLocOffset) |
| 837 | I = 0; |
| 838 | else |
| 839 | I = (-LastID - 2) + 1; |
| 840 | |
| 841 | unsigned NumProbes; |
| 842 | for (NumProbes = 0; NumProbes < 8; ++NumProbes, ++I) { |
| 843 | // Make sure the entry is loaded! |
| 844 | const SrcMgr::SLocEntry &E = getLoadedSLocEntry(I); |
| 845 | if (E.getOffset() <= SLocOffset) { |
| 846 | FileID Res = FileID::get(-int(I) - 2); |
| 847 | |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 848 | if (!E.isExpansion()) |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 849 | LastFileIDLookup = Res; |
| 850 | NumLinearScans += NumProbes + 1; |
| 851 | return Res; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | // Linear scan failed. Do the binary search. Note the reverse sorting of the |
| 856 | // table: GreaterIndex is the one where the offset is greater, which is |
| 857 | // actually a lower index! |
| 858 | unsigned GreaterIndex = I; |
| 859 | unsigned LessIndex = LoadedSLocEntryTable.size(); |
| 860 | NumProbes = 0; |
| 861 | while (1) { |
| 862 | ++NumProbes; |
| 863 | unsigned MiddleIndex = (LessIndex - GreaterIndex) / 2 + GreaterIndex; |
| 864 | const SrcMgr::SLocEntry &E = getLoadedSLocEntry(MiddleIndex); |
Argyrios Kyrtzidis | 7dc4f33 | 2013-03-01 03:26:00 +0000 | [diff] [blame] | 865 | if (E.getOffset() == 0) |
| 866 | return FileID(); // invalid entry. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 867 | |
| 868 | ++NumProbes; |
| 869 | |
| 870 | if (E.getOffset() > SLocOffset) { |
Argyrios Kyrtzidis | 7dc4f33 | 2013-03-01 03:26:00 +0000 | [diff] [blame] | 871 | // Sanity checking, otherwise a bug may lead to hanging in release build. |
| 872 | if (GreaterIndex == MiddleIndex) { |
| 873 | assert(0 && "binary search missed the entry"); |
| 874 | return FileID(); |
| 875 | } |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 876 | GreaterIndex = MiddleIndex; |
| 877 | continue; |
| 878 | } |
| 879 | |
| 880 | if (isOffsetInFileID(FileID::get(-int(MiddleIndex) - 2), SLocOffset)) { |
| 881 | FileID Res = FileID::get(-int(MiddleIndex) - 2); |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 882 | if (!E.isExpansion()) |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 883 | LastFileIDLookup = Res; |
| 884 | NumBinaryProbes += NumProbes; |
| 885 | return Res; |
| 886 | } |
| 887 | |
Argyrios Kyrtzidis | 1ca7344 | 2013-03-01 03:43:33 +0000 | [diff] [blame] | 888 | // Sanity checking, otherwise a bug may lead to hanging in release build. |
| 889 | if (LessIndex == MiddleIndex) { |
| 890 | assert(0 && "binary search missed the entry"); |
| 891 | return FileID(); |
| 892 | } |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 893 | LessIndex = MiddleIndex; |
| 894 | } |
| 895 | } |
| 896 | |
Chris Lattner | 659ac5f | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 897 | SourceLocation SourceManager:: |
Chandler Carruth | c84d769 | 2011-07-25 20:52:26 +0000 | [diff] [blame] | 898 | getExpansionLocSlowCase(SourceLocation Loc) const { |
Chris Lattner | 659ac5f | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 899 | do { |
Chris Lattner | 5647d31 | 2010-02-12 19:31:35 +0000 | [diff] [blame] | 900 | // Note: If Loc indicates an offset into a token that came from a macro |
| 901 | // expansion (e.g. the 5th character of the token) we do not want to add |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 902 | // this offset when going to the expansion location. The expansion |
Chris Lattner | 5647d31 | 2010-02-12 19:31:35 +0000 | [diff] [blame] | 903 | // location is the macro invocation, which the offset has nothing to do |
| 904 | // with. This is unlike when we get the spelling loc, because the offset |
| 905 | // directly correspond to the token whose spelling we're inspecting. |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 906 | Loc = getSLocEntry(getFileID(Loc)).getExpansion().getExpansionLocStart(); |
Chris Lattner | 659ac5f | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 907 | } while (!Loc.isFileID()); |
| 908 | |
| 909 | return Loc; |
| 910 | } |
| 911 | |
| 912 | SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { |
| 913 | do { |
| 914 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 915 | Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc(); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 916 | Loc = Loc.getLocWithOffset(LocInfo.second); |
Chris Lattner | 659ac5f | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 917 | } while (!Loc.isFileID()); |
| 918 | return Loc; |
| 919 | } |
| 920 | |
Argyrios Kyrtzidis | 7f6b029 | 2011-10-12 07:07:40 +0000 | [diff] [blame] | 921 | SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const { |
| 922 | do { |
| 923 | if (isMacroArgExpansion(Loc)) |
| 924 | Loc = getImmediateSpellingLoc(Loc); |
| 925 | else |
| 926 | Loc = getImmediateExpansionRange(Loc).first; |
| 927 | } while (!Loc.isFileID()); |
| 928 | return Loc; |
| 929 | } |
| 930 | |
Chris Lattner | 659ac5f | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 932 | std::pair<FileID, unsigned> |
Chandler Carruth | c7ca521 | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 933 | SourceManager::getDecomposedExpansionLocSlowCase( |
Argyrios Kyrtzidis | c8f7e21 | 2011-07-07 03:40:27 +0000 | [diff] [blame] | 934 | const SrcMgr::SLocEntry *E) const { |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 935 | // If this is an expansion record, walk through all the expansion points. |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 936 | FileID FID; |
| 937 | SourceLocation Loc; |
Argyrios Kyrtzidis | c8f7e21 | 2011-07-07 03:40:27 +0000 | [diff] [blame] | 938 | unsigned Offset; |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 939 | do { |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 940 | Loc = E->getExpansion().getExpansionLocStart(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 942 | FID = getFileID(Loc); |
| 943 | E = &getSLocEntry(FID); |
Argyrios Kyrtzidis | c8f7e21 | 2011-07-07 03:40:27 +0000 | [diff] [blame] | 944 | Offset = Loc.getOffset()-E->getOffset(); |
Chris Lattner | 31af4e0 | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 945 | } while (!Loc.isFileID()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 947 | return std::make_pair(FID, Offset); |
| 948 | } |
| 949 | |
| 950 | std::pair<FileID, unsigned> |
| 951 | SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, |
| 952 | unsigned Offset) const { |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 953 | // If this is an expansion record, walk through all the expansion points. |
Chris Lattner | 31af4e0 | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 954 | FileID FID; |
| 955 | SourceLocation Loc; |
| 956 | do { |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 957 | Loc = E->getExpansion().getSpellingLoc(); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 958 | Loc = Loc.getLocWithOffset(Offset); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
Chris Lattner | 31af4e0 | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 960 | FID = getFileID(Loc); |
| 961 | E = &getSLocEntry(FID); |
Argyrios Kyrtzidis | 2797df6 | 2011-08-23 21:02:41 +0000 | [diff] [blame] | 962 | Offset = Loc.getOffset()-E->getOffset(); |
Chris Lattner | 31af4e0 | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 963 | } while (!Loc.isFileID()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 964 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 965 | return std::make_pair(FID, Offset); |
| 966 | } |
| 967 | |
Chris Lattner | 8ad52d5 | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 968 | /// getImmediateSpellingLoc - Given a SourceLocation object, return the |
| 969 | /// spelling location referenced by the ID. This is the first level down |
| 970 | /// towards the place where the characters that make up the lexed token can be |
| 971 | /// found. This should not generally be used by clients. |
| 972 | SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{ |
| 973 | if (Loc.isFileID()) return Loc; |
| 974 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 975 | Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc(); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 976 | return Loc.getLocWithOffset(LocInfo.second); |
Chris Lattner | 8ad52d5 | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 980 | /// getImmediateExpansionRange - Loc is required to be an expansion location. |
| 981 | /// Return the start/end of the expansion information. |
Chris Lattner | 9dc9c20 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 982 | std::pair<SourceLocation,SourceLocation> |
Chandler Carruth | ca75758 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 983 | SourceManager::getImmediateExpansionRange(SourceLocation Loc) const { |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 984 | assert(Loc.isMacroID() && "Not a macro expansion loc!"); |
Chandler Carruth | ee4c1d1 | 2011-07-26 04:56:51 +0000 | [diff] [blame] | 985 | const ExpansionInfo &Expansion = getSLocEntry(getFileID(Loc)).getExpansion(); |
Chandler Carruth | 73ee5d7 | 2011-07-26 04:41:47 +0000 | [diff] [blame] | 986 | return Expansion.getExpansionLocRange(); |
Chris Lattner | 9dc9c20 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Chandler Carruth | 6d28d7f | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 989 | /// getExpansionRange - Given a SourceLocation object, return the range of |
| 990 | /// tokens covered by the expansion in the ultimate file. |
Chris Lattner | f52c0b2 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 991 | std::pair<SourceLocation,SourceLocation> |
Chandler Carruth | 6d28d7f | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 992 | SourceManager::getExpansionRange(SourceLocation Loc) const { |
Chris Lattner | f52c0b2 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 993 | if (Loc.isFileID()) return std::make_pair(Loc, Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
Chris Lattner | f52c0b2 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 995 | std::pair<SourceLocation,SourceLocation> Res = |
Chandler Carruth | ca75758 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 996 | getImmediateExpansionRange(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 998 | // Fully resolve the start and end locations to their ultimate expansion |
Chris Lattner | f52c0b2 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 999 | // points. |
| 1000 | while (!Res.first.isFileID()) |
Chandler Carruth | ca75758 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 1001 | Res.first = getImmediateExpansionRange(Res.first).first; |
Chris Lattner | f52c0b2 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 1002 | while (!Res.second.isFileID()) |
Chandler Carruth | ca75758 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 1003 | Res.second = getImmediateExpansionRange(Res.second).second; |
Chris Lattner | f52c0b2 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 1004 | return Res; |
| 1005 | } |
| 1006 | |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame^] | 1007 | bool SourceManager::isMacroArgExpansion(SourceLocation Loc, |
| 1008 | SourceLocation *StartLoc) const { |
Chandler Carruth | 402bb38 | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 1009 | if (!Loc.isMacroID()) return false; |
| 1010 | |
| 1011 | FileID FID = getFileID(Loc); |
Matt Beaumont-Gay | b1e71a7 | 2013-01-12 00:54:16 +0000 | [diff] [blame] | 1012 | const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion(); |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame^] | 1013 | if (!Expansion.isMacroArgExpansion()) return false; |
| 1014 | |
| 1015 | if (StartLoc) |
| 1016 | *StartLoc = Expansion.getExpansionLocStart(); |
| 1017 | return true; |
Chandler Carruth | 402bb38 | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 1018 | } |
Chris Lattner | 9dc9c20 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 1019 | |
Matt Beaumont-Gay | b1e71a7 | 2013-01-12 00:54:16 +0000 | [diff] [blame] | 1020 | bool SourceManager::isMacroBodyExpansion(SourceLocation Loc) const { |
| 1021 | if (!Loc.isMacroID()) return false; |
| 1022 | |
| 1023 | FileID FID = getFileID(Loc); |
| 1024 | const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion(); |
| 1025 | return Expansion.isMacroBodyExpansion(); |
| 1026 | } |
| 1027 | |
Argyrios Kyrtzidis | 065d720 | 2013-05-16 21:37:39 +0000 | [diff] [blame] | 1028 | bool SourceManager::isAtStartOfImmediateMacroExpansion(SourceLocation Loc, |
| 1029 | SourceLocation *MacroBegin) const { |
| 1030 | assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc"); |
| 1031 | |
| 1032 | std::pair<FileID, unsigned> DecompLoc = getDecomposedLoc(Loc); |
| 1033 | if (DecompLoc.second > 0) |
| 1034 | return false; // Does not point at the start of expansion range. |
| 1035 | |
| 1036 | bool Invalid = false; |
| 1037 | const SrcMgr::ExpansionInfo &ExpInfo = |
| 1038 | getSLocEntry(DecompLoc.first, &Invalid).getExpansion(); |
| 1039 | if (Invalid) |
| 1040 | return false; |
| 1041 | SourceLocation ExpLoc = ExpInfo.getExpansionLocStart(); |
| 1042 | |
| 1043 | if (ExpInfo.isMacroArgExpansion()) { |
| 1044 | // For macro argument expansions, check if the previous FileID is part of |
| 1045 | // the same argument expansion, in which case this Loc is not at the |
| 1046 | // beginning of the expansion. |
| 1047 | FileID PrevFID = getPreviousFileID(DecompLoc.first); |
| 1048 | if (!PrevFID.isInvalid()) { |
| 1049 | const SrcMgr::SLocEntry &PrevEntry = getSLocEntry(PrevFID, &Invalid); |
| 1050 | if (Invalid) |
| 1051 | return false; |
| 1052 | if (PrevEntry.isExpansion() && |
| 1053 | PrevEntry.getExpansion().getExpansionLocStart() == ExpLoc) |
| 1054 | return false; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | if (MacroBegin) |
| 1059 | *MacroBegin = ExpLoc; |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
| 1063 | bool SourceManager::isAtEndOfImmediateMacroExpansion(SourceLocation Loc, |
| 1064 | SourceLocation *MacroEnd) const { |
| 1065 | assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc"); |
| 1066 | |
| 1067 | FileID FID = getFileID(Loc); |
| 1068 | SourceLocation NextLoc = Loc.getLocWithOffset(1); |
| 1069 | if (isInFileID(NextLoc, FID)) |
| 1070 | return false; // Does not point at the end of expansion range. |
| 1071 | |
| 1072 | bool Invalid = false; |
| 1073 | const SrcMgr::ExpansionInfo &ExpInfo = |
| 1074 | getSLocEntry(FID, &Invalid).getExpansion(); |
| 1075 | if (Invalid) |
| 1076 | return false; |
| 1077 | |
| 1078 | if (ExpInfo.isMacroArgExpansion()) { |
| 1079 | // For macro argument expansions, check if the next FileID is part of the |
| 1080 | // same argument expansion, in which case this Loc is not at the end of the |
| 1081 | // expansion. |
| 1082 | FileID NextFID = getNextFileID(FID); |
| 1083 | if (!NextFID.isInvalid()) { |
| 1084 | const SrcMgr::SLocEntry &NextEntry = getSLocEntry(NextFID, &Invalid); |
| 1085 | if (Invalid) |
| 1086 | return false; |
| 1087 | if (NextEntry.isExpansion() && |
| 1088 | NextEntry.getExpansion().getExpansionLocStart() == |
| 1089 | ExpInfo.getExpansionLocStart()) |
| 1090 | return false; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | if (MacroEnd) |
| 1095 | *MacroEnd = ExpInfo.getExpansionLocEnd(); |
| 1096 | return true; |
| 1097 | } |
| 1098 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1099 | |
| 1100 | //===----------------------------------------------------------------------===// |
| 1101 | // Queries about the code at a SourceLocation. |
| 1102 | //===----------------------------------------------------------------------===// |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 1103 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1104 | /// getCharacterData - Return a pointer to the start of the specified location |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 1105 | /// in the appropriate MemoryBuffer. |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1106 | const char *SourceManager::getCharacterData(SourceLocation SL, |
| 1107 | bool *Invalid) const { |
Chris Lattner | d3a15f7 | 2006-07-04 23:01:03 +0000 | [diff] [blame] | 1108 | // Note that this is a hot function in the getSpelling() path, which is |
| 1109 | // heavily used by -E mode. |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1110 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
Ted Kremenek | 12c2af4 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 1112 | // Note that calling 'getBuffer()' may lazily page in a source file. |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1113 | bool CharDataInvalid = false; |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1114 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &CharDataInvalid); |
| 1115 | if (CharDataInvalid || !Entry.isFile()) { |
| 1116 | if (Invalid) |
| 1117 | *Invalid = true; |
| 1118 | |
| 1119 | return "<<<<INVALID BUFFER>>>>"; |
| 1120 | } |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 1121 | llvm::MemoryBuffer *Buffer = Entry.getFile().getContentCache()->getBuffer( |
| 1122 | Diag, *this, SourceLocation(), &CharDataInvalid); |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1123 | if (Invalid) |
| 1124 | *Invalid = CharDataInvalid; |
| 1125 | return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Chris Lattner | 685730f | 2006-06-26 01:36:22 +0000 | [diff] [blame] | 1128 | |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 1129 | /// getColumnNumber - Return the column # for the specified file position. |
Chris Lattner | e4ad417 | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 1130 | /// this is significantly cheaper to compute than the line number. |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1131 | unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos, |
| 1132 | bool *Invalid) const { |
| 1133 | bool MyInvalid = false; |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 1134 | llvm::MemoryBuffer *MemBuf = getBuffer(FID, &MyInvalid); |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1135 | if (Invalid) |
| 1136 | *Invalid = MyInvalid; |
| 1137 | |
| 1138 | if (MyInvalid) |
| 1139 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1140 | |
Jordan Rose | 8d63d5b | 2012-06-19 03:09:38 +0000 | [diff] [blame] | 1141 | // It is okay to request a position just past the end of the buffer. |
| 1142 | if (FilePos > MemBuf->getBufferSize()) { |
Argyrios Kyrtzidis | 6c8d29f | 2011-12-10 00:30:38 +0000 | [diff] [blame] | 1143 | if (Invalid) |
Jordan Rose | 8d63d5b | 2012-06-19 03:09:38 +0000 | [diff] [blame] | 1144 | *Invalid = true; |
Argyrios Kyrtzidis | 6c8d29f | 2011-12-10 00:30:38 +0000 | [diff] [blame] | 1145 | return 1; |
| 1146 | } |
| 1147 | |
Craig Topper | 5e79ee0 | 2012-10-19 04:40:38 +0000 | [diff] [blame] | 1148 | // See if we just calculated the line number for this FilePos and can use |
| 1149 | // that to lookup the start of the line instead of searching for it. |
| 1150 | if (LastLineNoFileIDQuery == FID && |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1151 | LastLineNoContentCache->SourceLineCache != nullptr && |
Craig Topper | f3b839b | 2012-12-16 05:58:32 +0000 | [diff] [blame] | 1152 | LastLineNoResult < LastLineNoContentCache->NumLines) { |
Craig Topper | 5e79ee0 | 2012-10-19 04:40:38 +0000 | [diff] [blame] | 1153 | unsigned *SourceLineCache = LastLineNoContentCache->SourceLineCache; |
| 1154 | unsigned LineStart = SourceLineCache[LastLineNoResult - 1]; |
| 1155 | unsigned LineEnd = SourceLineCache[LastLineNoResult]; |
| 1156 | if (FilePos >= LineStart && FilePos < LineEnd) |
| 1157 | return FilePos - LineStart + 1; |
| 1158 | } |
| 1159 | |
Dylan Noblesmith | 2a8bc15 | 2011-12-19 08:51:05 +0000 | [diff] [blame] | 1160 | const char *Buf = MemBuf->getBufferStart(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1161 | unsigned LineStart = FilePos; |
| 1162 | while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') |
| 1163 | --LineStart; |
| 1164 | return FilePos-LineStart+1; |
| 1165 | } |
| 1166 | |
Zhanyong Wan | ea6d7f3 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1167 | // isInvalid - Return the result of calling loc.isInvalid(), and |
| 1168 | // if Invalid is not null, set its value to same. |
| 1169 | static bool isInvalid(SourceLocation Loc, bool *Invalid) { |
| 1170 | bool MyInvalid = Loc.isInvalid(); |
| 1171 | if (Invalid) |
| 1172 | *Invalid = MyInvalid; |
| 1173 | return MyInvalid; |
| 1174 | } |
| 1175 | |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1176 | unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc, |
| 1177 | bool *Invalid) const { |
Zhanyong Wan | ea6d7f3 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1178 | if (isInvalid(Loc, Invalid)) return 0; |
Chris Lattner | e4ad417 | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 1179 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1180 | return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); |
Chris Lattner | e4ad417 | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Chandler Carruth | 42f35f9 | 2011-07-25 20:57:57 +0000 | [diff] [blame] | 1183 | unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc, |
| 1184 | bool *Invalid) const { |
Zhanyong Wan | ea6d7f3 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1185 | if (isInvalid(Loc, Invalid)) return 0; |
Chandler Carruth | c7ca521 | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 1186 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1187 | return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); |
Chris Lattner | e4ad417 | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Chandler Carruth | 1aef0c5 | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1190 | unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc, |
| 1191 | bool *Invalid) const { |
| 1192 | if (isInvalid(Loc, Invalid)) return 0; |
| 1193 | return getPresumedLoc(Loc).getColumn(); |
| 1194 | } |
| 1195 | |
Benjamin Kramer | 543036a | 2012-04-06 20:49:55 +0000 | [diff] [blame] | 1196 | #ifdef __SSE2__ |
| 1197 | #include <emmintrin.h> |
| 1198 | #endif |
| 1199 | |
Chandler Carruth | c3ce584 | 2010-10-23 08:44:57 +0000 | [diff] [blame] | 1200 | static LLVM_ATTRIBUTE_NOINLINE void |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1201 | ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI, |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1202 | llvm::BumpPtrAllocator &Alloc, |
| 1203 | const SourceManager &SM, bool &Invalid); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1204 | static void ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI, |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1205 | llvm::BumpPtrAllocator &Alloc, |
| 1206 | const SourceManager &SM, bool &Invalid) { |
Ted Kremenek | 12c2af4 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 1207 | // Note that calling 'getBuffer()' may lazily page in the file. |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 1208 | MemoryBuffer *Buffer = FI->getBuffer(Diag, SM, SourceLocation(), &Invalid); |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1209 | if (Invalid) |
| 1210 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1212 | // Find the file offsets of all of the *physical* source lines. This does |
| 1213 | // not look at trigraphs, escaped newlines, or anything else tricky. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1214 | SmallVector<unsigned, 256> LineOffsets; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1216 | // Line #1 starts at char 0. |
| 1217 | LineOffsets.push_back(0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1219 | const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); |
| 1220 | const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); |
| 1221 | unsigned Offs = 0; |
| 1222 | while (1) { |
| 1223 | // Skip over the contents of the line. |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1224 | const unsigned char *NextBuf = (const unsigned char *)Buf; |
Benjamin Kramer | 543036a | 2012-04-06 20:49:55 +0000 | [diff] [blame] | 1225 | |
| 1226 | #ifdef __SSE2__ |
| 1227 | // Try to skip to the next newline using SSE instructions. This is very |
| 1228 | // performance sensitive for programs with lots of diagnostics and in -E |
| 1229 | // mode. |
| 1230 | __m128i CRs = _mm_set1_epi8('\r'); |
| 1231 | __m128i LFs = _mm_set1_epi8('\n'); |
| 1232 | |
| 1233 | // First fix up the alignment to 16 bytes. |
| 1234 | while (((uintptr_t)NextBuf & 0xF) != 0) { |
| 1235 | if (*NextBuf == '\n' || *NextBuf == '\r' || *NextBuf == '\0') |
| 1236 | goto FoundSpecialChar; |
| 1237 | ++NextBuf; |
| 1238 | } |
| 1239 | |
| 1240 | // Scan 16 byte chunks for '\r' and '\n'. Ignore '\0'. |
| 1241 | while (NextBuf+16 <= End) { |
Roman Divacky | e637711 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1242 | const __m128i Chunk = *(const __m128i*)NextBuf; |
Benjamin Kramer | 543036a | 2012-04-06 20:49:55 +0000 | [diff] [blame] | 1243 | __m128i Cmp = _mm_or_si128(_mm_cmpeq_epi8(Chunk, CRs), |
| 1244 | _mm_cmpeq_epi8(Chunk, LFs)); |
| 1245 | unsigned Mask = _mm_movemask_epi8(Cmp); |
| 1246 | |
| 1247 | // If we found a newline, adjust the pointer and jump to the handling code. |
| 1248 | if (Mask != 0) { |
Michael J. Spencer | 8c39840 | 2013-05-24 21:42:04 +0000 | [diff] [blame] | 1249 | NextBuf += llvm::countTrailingZeros(Mask); |
Benjamin Kramer | 543036a | 2012-04-06 20:49:55 +0000 | [diff] [blame] | 1250 | goto FoundSpecialChar; |
| 1251 | } |
| 1252 | NextBuf += 16; |
| 1253 | } |
| 1254 | #endif |
| 1255 | |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1256 | while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') |
| 1257 | ++NextBuf; |
Benjamin Kramer | 543036a | 2012-04-06 20:49:55 +0000 | [diff] [blame] | 1258 | |
| 1259 | #ifdef __SSE2__ |
| 1260 | FoundSpecialChar: |
| 1261 | #endif |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1262 | Offs += NextBuf-Buf; |
| 1263 | Buf = NextBuf; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1265 | if (Buf[0] == '\n' || Buf[0] == '\r') { |
| 1266 | // If this is \n\r or \r\n, skip both characters. |
| 1267 | if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) |
| 1268 | ++Offs, ++Buf; |
| 1269 | ++Offs, ++Buf; |
| 1270 | LineOffsets.push_back(Offs); |
| 1271 | } else { |
| 1272 | // Otherwise, this is a null. If end of file, exit. |
| 1273 | if (Buf == End) break; |
| 1274 | // Otherwise, skip the null. |
| 1275 | ++Offs, ++Buf; |
| 1276 | } |
| 1277 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1279 | // Copy the offsets into the FileInfo structure. |
| 1280 | FI->NumLines = LineOffsets.size(); |
Chris Lattner | c8233df | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 1281 | FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size()); |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1282 | std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); |
| 1283 | } |
Chris Lattner | 9a13bde | 2006-06-21 04:57:09 +0000 | [diff] [blame] | 1284 | |
Chris Lattner | 53e384f | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 1285 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1286 | /// for the position indicated. This requires building and caching a table of |
Chris Lattner | 739e739 | 2007-04-29 07:12:06 +0000 | [diff] [blame] | 1287 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1288 | /// about to emit a diagnostic. |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1289 | unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos, |
| 1290 | bool *Invalid) const { |
Argyrios Kyrtzidis | f15eac1 | 2011-05-17 22:09:53 +0000 | [diff] [blame] | 1291 | if (FID.isInvalid()) { |
| 1292 | if (Invalid) |
| 1293 | *Invalid = true; |
| 1294 | return 1; |
| 1295 | } |
| 1296 | |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1297 | ContentCache *Content; |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1298 | if (LastLineNoFileIDQuery == FID) |
Ted Kremenek | c08bca6 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1299 | Content = LastLineNoContentCache; |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1300 | else { |
| 1301 | bool MyInvalid = false; |
| 1302 | const SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); |
| 1303 | if (MyInvalid || !Entry.isFile()) { |
| 1304 | if (Invalid) |
| 1305 | *Invalid = true; |
| 1306 | return 1; |
| 1307 | } |
| 1308 | |
| 1309 | Content = const_cast<ContentCache*>(Entry.getFile().getContentCache()); |
| 1310 | } |
| 1311 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1312 | // If this is the first use of line information for this buffer, compute the |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1313 | /// SourceLineCache for it on demand. |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1314 | if (!Content->SourceLineCache) { |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1315 | bool MyInvalid = false; |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1316 | ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid); |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1317 | if (Invalid) |
| 1318 | *Invalid = MyInvalid; |
| 1319 | if (MyInvalid) |
| 1320 | return 1; |
| 1321 | } else if (Invalid) |
| 1322 | *Invalid = false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1323 | |
| 1324 | // Okay, we know we have a line number table. Do a binary search to find the |
| 1325 | // line number that this character position lands on. |
Ted Kremenek | c08bca6 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1326 | unsigned *SourceLineCache = Content->SourceLineCache; |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1327 | unsigned *SourceLineCacheStart = SourceLineCache; |
Ted Kremenek | c08bca6 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1328 | unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1330 | unsigned QueriedFilePos = FilePos+1; |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1331 | |
Daniel Dunbar | 70f924df8 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1332 | // FIXME: I would like to be convinced that this code is worth being as |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | // complicated as it is, binary search isn't that slow. |
Daniel Dunbar | 70f924df8 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1334 | // |
| 1335 | // If it is worth being optimized, then in my opinion it could be more |
| 1336 | // performant, simpler, and more obviously correct by just "galloping" outward |
| 1337 | // from the queried file position. In fact, this could be incorporated into a |
| 1338 | // generic algorithm such as lower_bound_with_hint. |
| 1339 | // |
| 1340 | // If someone gives me a test case where this matters, and I will do it! - DWD |
| 1341 | |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1342 | // If the previous query was to the same file, we know both the file pos from |
| 1343 | // that query and the line number returned. This allows us to narrow the |
| 1344 | // search space from the entire file to something near the match. |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1345 | if (LastLineNoFileIDQuery == FID) { |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1346 | if (QueriedFilePos >= LastLineNoFilePos) { |
Daniel Dunbar | 70f924df8 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1347 | // FIXME: Potential overflow? |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1348 | SourceLineCache = SourceLineCache+LastLineNoResult-1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1350 | // The query is likely to be nearby the previous one. Here we check to |
| 1351 | // see if it is within 5, 10 or 20 lines. It can be far away in cases |
| 1352 | // where big comment blocks and vertical whitespace eat up lines but |
| 1353 | // contribute no tokens. |
| 1354 | if (SourceLineCache+5 < SourceLineCacheEnd) { |
| 1355 | if (SourceLineCache[5] > QueriedFilePos) |
| 1356 | SourceLineCacheEnd = SourceLineCache+5; |
| 1357 | else if (SourceLineCache+10 < SourceLineCacheEnd) { |
| 1358 | if (SourceLineCache[10] > QueriedFilePos) |
| 1359 | SourceLineCacheEnd = SourceLineCache+10; |
| 1360 | else if (SourceLineCache+20 < SourceLineCacheEnd) { |
| 1361 | if (SourceLineCache[20] > QueriedFilePos) |
| 1362 | SourceLineCacheEnd = SourceLineCache+20; |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | } else { |
Daniel Dunbar | 70f924df8 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 1367 | if (LastLineNoResult < Content->NumLines) |
| 1368 | SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1369 | } |
| 1370 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | |
Chris Lattner | 830a77f | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 1372 | unsigned *Pos |
| 1373 | = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1374 | unsigned LineNo = Pos-SourceLineCacheStart; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1376 | LastLineNoFileIDQuery = FID; |
Ted Kremenek | c08bca6 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1377 | LastLineNoContentCache = Content; |
Chris Lattner | 8996fff | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 1378 | LastLineNoFilePos = QueriedFilePos; |
| 1379 | LastLineNoResult = LineNo; |
| 1380 | return LineNo; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Chandler Carruth | 1aef0c5 | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1383 | unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc, |
| 1384 | bool *Invalid) const { |
| 1385 | if (isInvalid(Loc, Invalid)) return 0; |
| 1386 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); |
| 1387 | return getLineNumber(LocInfo.first, LocInfo.second); |
| 1388 | } |
Chandler Carruth | d48db21 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 1389 | unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc, |
| 1390 | bool *Invalid) const { |
Zhanyong Wan | ea6d7f3 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1391 | if (isInvalid(Loc, Invalid)) return 0; |
Chandler Carruth | c7ca521 | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 1392 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1393 | return getLineNumber(LocInfo.first, LocInfo.second); |
| 1394 | } |
Chandler Carruth | 1aef0c5 | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1395 | unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc, |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1396 | bool *Invalid) const { |
Zhanyong Wan | ea6d7f3 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1397 | if (isInvalid(Loc, Invalid)) return 0; |
Chandler Carruth | 1aef0c5 | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 1398 | return getPresumedLoc(Loc).getLine(); |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Chris Lattner | 95d9c5e | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1401 | /// getFileCharacteristic - return the file characteristic of the specified |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | /// source location, indicating whether this is a normal file, a system |
Chris Lattner | 95d9c5e | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1403 | /// header, or an "implicit extern C" system header. |
| 1404 | /// |
| 1405 | /// This state can be modified with flags on GNU linemarker directives like: |
| 1406 | /// # 4 "foo.h" 3 |
| 1407 | /// which changes all source locations in the current file after that to be |
| 1408 | /// considered to be from a system header. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | SrcMgr::CharacteristicKind |
Chris Lattner | 95d9c5e | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1410 | SourceManager::getFileCharacteristic(SourceLocation Loc) const { |
| 1411 | assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!"); |
Chandler Carruth | c7ca521 | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 1412 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1413 | bool Invalid = false; |
| 1414 | const SLocEntry &SEntry = getSLocEntry(LocInfo.first, &Invalid); |
| 1415 | if (Invalid || !SEntry.isFile()) |
| 1416 | return C_User; |
| 1417 | |
| 1418 | const SrcMgr::FileInfo &FI = SEntry.getFile(); |
Chris Lattner | 95d9c5e | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1419 | |
| 1420 | // If there are no #line directives in this file, just return the whole-file |
| 1421 | // state. |
| 1422 | if (!FI.hasLineDirectives()) |
| 1423 | return FI.getFileCharacteristic(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Chris Lattner | 95d9c5e | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1425 | assert(LineTable && "Can't have linetable entries without a LineTable!"); |
| 1426 | // See if there is a #line directive before the location. |
| 1427 | const LineEntry *Entry = |
Douglas Gregor | 02c2dbf | 2012-06-08 16:40:28 +0000 | [diff] [blame] | 1428 | LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Chris Lattner | 95d9c5e | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1430 | // If this is before the first line marker, use the file characteristic. |
| 1431 | if (!Entry) |
| 1432 | return FI.getFileCharacteristic(); |
| 1433 | |
| 1434 | return Entry->FileKind; |
| 1435 | } |
| 1436 | |
Chris Lattner | a6f037c | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 1437 | /// Return the filename or buffer identifier of the buffer the location is in. |
James Dennett | 87a2acf | 2012-06-17 03:22:59 +0000 | [diff] [blame] | 1438 | /// Note that this name does not respect \#line directives. Use getPresumedLoc |
Chris Lattner | a6f037c | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 1439 | /// for normal clients. |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1440 | const char *SourceManager::getBufferName(SourceLocation Loc, |
| 1441 | bool *Invalid) const { |
Zhanyong Wan | ea6d7f3 | 2010-10-05 17:56:33 +0000 | [diff] [blame] | 1442 | if (isInvalid(Loc, Invalid)) return "<invalid loc>"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1444 | return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier(); |
Chris Lattner | a6f037c | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1447 | |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1448 | /// getPresumedLoc - This method returns the "presumed" location of a |
James Dennett | 87a2acf | 2012-06-17 03:22:59 +0000 | [diff] [blame] | 1449 | /// SourceLocation specifies. A "presumed location" can be modified by \#line |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1450 | /// or GNU line marker directives. This provides a view on the data that a |
| 1451 | /// user should see in diagnostics, for example. |
| 1452 | /// |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 1453 | /// Note that a presumed location is always given as the expansion point of an |
| 1454 | /// expansion location, not at the spelling location. |
Richard Smith | 0b50cb7 | 2012-11-14 23:55:25 +0000 | [diff] [blame] | 1455 | PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc, |
| 1456 | bool UseLineDirectives) const { |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1457 | if (Loc.isInvalid()) return PresumedLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 1459 | // Presumed locations are always for expansion points. |
Chandler Carruth | c7ca521 | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 1460 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1462 | bool Invalid = false; |
| 1463 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); |
| 1464 | if (Invalid || !Entry.isFile()) |
| 1465 | return PresumedLoc(); |
| 1466 | |
| 1467 | const SrcMgr::FileInfo &FI = Entry.getFile(); |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1468 | const SrcMgr::ContentCache *C = FI.getContentCache(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | |
Chris Lattner | d429392 | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1470 | // To get the source name, first consult the FileEntry (if one exists) |
| 1471 | // before the MemBuffer as this will avoid unnecessarily paging in the |
| 1472 | // MemBuffer. |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1473 | const char *Filename; |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1474 | if (C->OrigEntry) |
| 1475 | Filename = C->OrigEntry->getName(); |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1476 | else |
| 1477 | Filename = C->getBuffer(Diag, *this)->getBufferIdentifier(); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1478 | |
Douglas Gregor | 75f26d6 | 2010-11-02 00:39:22 +0000 | [diff] [blame] | 1479 | unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid); |
| 1480 | if (Invalid) |
| 1481 | return PresumedLoc(); |
| 1482 | unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid); |
| 1483 | if (Invalid) |
| 1484 | return PresumedLoc(); |
| 1485 | |
Chris Lattner | d429392 | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1486 | SourceLocation IncludeLoc = FI.getIncludeLoc(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Chris Lattner | d429392 | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1488 | // If we have #line directives in this file, update and overwrite the physical |
| 1489 | // location info if appropriate. |
Richard Smith | 0b50cb7 | 2012-11-14 23:55:25 +0000 | [diff] [blame] | 1490 | if (UseLineDirectives && FI.hasLineDirectives()) { |
Chris Lattner | d429392 | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1491 | assert(LineTable && "Can't have linetable entries without a LineTable!"); |
| 1492 | // See if there is a #line directive before this. If so, get it. |
| 1493 | if (const LineEntry *Entry = |
Douglas Gregor | 02c2dbf | 2012-06-08 16:40:28 +0000 | [diff] [blame] | 1494 | LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second)) { |
Chris Lattner | c1219ff | 2009-02-04 02:00:59 +0000 | [diff] [blame] | 1495 | // If the LineEntry indicates a filename, use it. |
Chris Lattner | d429392 | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1496 | if (Entry->FilenameID != -1) |
| 1497 | Filename = LineTable->getFilename(Entry->FilenameID); |
Chris Lattner | c1219ff | 2009-02-04 02:00:59 +0000 | [diff] [blame] | 1498 | |
| 1499 | // Use the line number specified by the LineEntry. This line number may |
| 1500 | // be multiple lines down from the line entry. Add the difference in |
| 1501 | // physical line numbers from the query point and the line marker to the |
| 1502 | // total. |
| 1503 | unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset); |
| 1504 | LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | |
Chris Lattner | 20c50ba | 2009-02-04 02:15:40 +0000 | [diff] [blame] | 1506 | // Note that column numbers are not molested by line markers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | |
Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 1508 | // Handle virtual #include manipulation. |
| 1509 | if (Entry->IncludeOffset) { |
| 1510 | IncludeLoc = getLocForStartOfFile(LocInfo.first); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1511 | IncludeLoc = IncludeLoc.getLocWithOffset(Entry->IncludeOffset); |
Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 1512 | } |
Chris Lattner | d429392 | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc); |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Benjamin Kramer | e7800df | 2013-09-27 17:12:50 +0000 | [diff] [blame] | 1519 | /// \brief Returns whether the PresumedLoc for a given SourceLocation is |
| 1520 | /// in the main file. |
| 1521 | /// |
| 1522 | /// This computes the "presumed" location for a SourceLocation, then checks |
| 1523 | /// whether it came from a file other than the main file. This is different |
| 1524 | /// from isWrittenInMainFile() because it takes line marker directives into |
| 1525 | /// account. |
| 1526 | bool SourceManager::isInMainFile(SourceLocation Loc) const { |
| 1527 | if (Loc.isInvalid()) return false; |
| 1528 | |
| 1529 | // Presumed locations are always for expansion points. |
| 1530 | std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); |
| 1531 | |
| 1532 | bool Invalid = false; |
| 1533 | const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); |
| 1534 | if (Invalid || !Entry.isFile()) |
| 1535 | return false; |
| 1536 | |
| 1537 | const SrcMgr::FileInfo &FI = Entry.getFile(); |
| 1538 | |
| 1539 | // Check if there is a line directive for this location. |
| 1540 | if (FI.hasLineDirectives()) |
| 1541 | if (const LineEntry *Entry = |
| 1542 | LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second)) |
| 1543 | if (Entry->IncludeOffset) |
| 1544 | return false; |
| 1545 | |
| 1546 | return FI.getIncludeLoc().isInvalid(); |
| 1547 | } |
| 1548 | |
James Dennett | aa61cbb | 2013-11-24 01:47:49 +0000 | [diff] [blame] | 1549 | /// \brief The size of the SLocEntry that \p FID represents. |
Argyrios Kyrtzidis | 296374b5 | 2011-08-23 21:02:28 +0000 | [diff] [blame] | 1550 | unsigned SourceManager::getFileIDSize(FileID FID) const { |
| 1551 | bool Invalid = false; |
| 1552 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 1553 | if (Invalid) |
| 1554 | return 0; |
| 1555 | |
| 1556 | int ID = FID.ID; |
| 1557 | unsigned NextOffset; |
| 1558 | if ((ID > 0 && unsigned(ID+1) == local_sloc_entry_size())) |
| 1559 | NextOffset = getNextLocalOffset(); |
| 1560 | else if (ID+1 == -1) |
| 1561 | NextOffset = MaxLoadedOffset; |
| 1562 | else |
| 1563 | NextOffset = getSLocEntry(FileID::get(ID+1)).getOffset(); |
| 1564 | |
| 1565 | return NextOffset - Entry.getOffset() - 1; |
| 1566 | } |
| 1567 | |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1568 | //===----------------------------------------------------------------------===// |
| 1569 | // Other miscellaneous methods. |
| 1570 | //===----------------------------------------------------------------------===// |
| 1571 | |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1572 | /// \brief Retrieve the inode for the given file entry, if possible. |
| 1573 | /// |
| 1574 | /// This routine involves a system call, and therefore should only be used |
| 1575 | /// in non-performance-critical code. |
Rafael Espindola | 073ff10 | 2013-07-29 21:26:52 +0000 | [diff] [blame] | 1576 | static Optional<llvm::sys::fs::UniqueID> |
| 1577 | getActualFileUID(const FileEntry *File) { |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1578 | if (!File) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1579 | return None; |
Rafael Espindola | 74ca78e | 2013-07-29 18:43:40 +0000 | [diff] [blame] | 1580 | |
Rafael Espindola | 073ff10 | 2013-07-29 21:26:52 +0000 | [diff] [blame] | 1581 | llvm::sys::fs::UniqueID ID; |
Rafael Espindola | 74ca78e | 2013-07-29 18:43:40 +0000 | [diff] [blame] | 1582 | if (llvm::sys::fs::getUniqueID(File->getName(), ID)) |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1583 | return None; |
Rafael Espindola | 74ca78e | 2013-07-29 18:43:40 +0000 | [diff] [blame] | 1584 | |
| 1585 | return ID; |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Argyrios Kyrtzidis | 88f663c0 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1588 | /// \brief Get the source location for the given file:line:col triplet. |
| 1589 | /// |
| 1590 | /// If the source file is included multiple times, the source location will |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1591 | /// be based upon an arbitrary inclusion. |
Argyrios Kyrtzidis | 92a47bd | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1592 | SourceLocation SourceManager::translateFileLineCol(const FileEntry *SourceFile, |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1593 | unsigned Line, |
| 1594 | unsigned Col) const { |
Argyrios Kyrtzidis | 88f663c0 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1595 | assert(SourceFile && "Null source file!"); |
| 1596 | assert(Line && Col && "Line and column should start from 1!"); |
| 1597 | |
Argyrios Kyrtzidis | 04a6e5f | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1598 | FileID FirstFID = translateFile(SourceFile); |
| 1599 | return translateLineCol(FirstFID, Line, Col); |
| 1600 | } |
| 1601 | |
| 1602 | /// \brief Get the FileID for the given file. |
| 1603 | /// |
| 1604 | /// If the source file is included multiple times, the FileID will be the |
| 1605 | /// first inclusion. |
| 1606 | FileID SourceManager::translateFile(const FileEntry *SourceFile) const { |
| 1607 | assert(SourceFile && "Null source file!"); |
| 1608 | |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1609 | // Find the first file ID that corresponds to the given file. |
| 1610 | FileID FirstFID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1612 | // First, check the main file ID, since it is common to look for a |
| 1613 | // location in the main file. |
Rafael Espindola | 073ff10 | 2013-07-29 21:26:52 +0000 | [diff] [blame] | 1614 | Optional<llvm::sys::fs::UniqueID> SourceFileUID; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1615 | Optional<StringRef> SourceFileName; |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1616 | if (!MainFileID.isInvalid()) { |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1617 | bool Invalid = false; |
| 1618 | const SLocEntry &MainSLoc = getSLocEntry(MainFileID, &Invalid); |
| 1619 | if (Invalid) |
Argyrios Kyrtzidis | 04a6e5f | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1620 | return FileID(); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1621 | |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1622 | if (MainSLoc.isFile()) { |
| 1623 | const ContentCache *MainContentCache |
| 1624 | = MainSLoc.getFile().getContentCache(); |
Douglas Gregor | 6a5be93 | 2011-02-11 18:08:15 +0000 | [diff] [blame] | 1625 | if (!MainContentCache) { |
| 1626 | // Can't do anything |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1627 | } else if (MainContentCache->OrigEntry == SourceFile) { |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1628 | FirstFID = MainFileID; |
Douglas Gregor | 6a5be93 | 2011-02-11 18:08:15 +0000 | [diff] [blame] | 1629 | } else { |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1630 | // Fall back: check whether we have the same base name and inode |
| 1631 | // as the main file. |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1632 | const FileEntry *MainFile = MainContentCache->OrigEntry; |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1633 | SourceFileName = llvm::sys::path::filename(SourceFile->getName()); |
| 1634 | if (*SourceFileName == llvm::sys::path::filename(MainFile->getName())) { |
Rafael Espindola | 74ca78e | 2013-07-29 18:43:40 +0000 | [diff] [blame] | 1635 | SourceFileUID = getActualFileUID(SourceFile); |
| 1636 | if (SourceFileUID) { |
Rafael Espindola | 073ff10 | 2013-07-29 21:26:52 +0000 | [diff] [blame] | 1637 | if (Optional<llvm::sys::fs::UniqueID> MainFileUID = |
| 1638 | getActualFileUID(MainFile)) { |
Rafael Espindola | 74ca78e | 2013-07-29 18:43:40 +0000 | [diff] [blame] | 1639 | if (*SourceFileUID == *MainFileUID) { |
Douglas Gregor | d766be6 | 2011-02-16 19:09:24 +0000 | [diff] [blame] | 1640 | FirstFID = MainFileID; |
| 1641 | SourceFile = MainFile; |
| 1642 | } |
| 1643 | } |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | } |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | if (FirstFID.isInvalid()) { |
| 1651 | // The location we're looking for isn't in the main file; look |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1652 | // through all of the local source locations. |
| 1653 | for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) { |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1654 | bool Invalid = false; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1655 | const SLocEntry &SLoc = getLocalSLocEntry(I, &Invalid); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1656 | if (Invalid) |
Argyrios Kyrtzidis | 04a6e5f | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1657 | return FileID(); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1658 | |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1659 | if (SLoc.isFile() && |
| 1660 | SLoc.getFile().getContentCache() && |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1661 | SLoc.getFile().getContentCache()->OrigEntry == SourceFile) { |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1662 | FirstFID = FileID::get(I); |
| 1663 | break; |
| 1664 | } |
| 1665 | } |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1666 | // If that still didn't help, try the modules. |
| 1667 | if (FirstFID.isInvalid()) { |
| 1668 | for (unsigned I = 0, N = loaded_sloc_entry_size(); I != N; ++I) { |
| 1669 | const SLocEntry &SLoc = getLoadedSLocEntry(I); |
| 1670 | if (SLoc.isFile() && |
| 1671 | SLoc.getFile().getContentCache() && |
| 1672 | SLoc.getFile().getContentCache()->OrigEntry == SourceFile) { |
| 1673 | FirstFID = FileID::get(-int(I) - 2); |
| 1674 | break; |
| 1675 | } |
| 1676 | } |
| 1677 | } |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | // If we haven't found what we want yet, try again, but this time stat() |
| 1681 | // each of the files in case the files have changed since we originally |
Rafael Espindola | 74ca78e | 2013-07-29 18:43:40 +0000 | [diff] [blame] | 1682 | // parsed the file. |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1683 | if (FirstFID.isInvalid() && |
Rafael Espindola | 74ca78e | 2013-07-29 18:43:40 +0000 | [diff] [blame] | 1684 | (SourceFileName || |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1685 | (SourceFileName = llvm::sys::path::filename(SourceFile->getName()))) && |
Rafael Espindola | 74ca78e | 2013-07-29 18:43:40 +0000 | [diff] [blame] | 1686 | (SourceFileUID || (SourceFileUID = getActualFileUID(SourceFile)))) { |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1687 | bool Invalid = false; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1688 | for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) { |
| 1689 | FileID IFileID; |
| 1690 | IFileID.ID = I; |
| 1691 | const SLocEntry &SLoc = getSLocEntry(IFileID, &Invalid); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1692 | if (Invalid) |
Argyrios Kyrtzidis | 04a6e5f | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1693 | return FileID(); |
Douglas Gregor | 49f754f | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1694 | |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1695 | if (SLoc.isFile()) { |
| 1696 | const ContentCache *FileContentCache |
| 1697 | = SLoc.getFile().getContentCache(); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1698 | const FileEntry *Entry = FileContentCache ? FileContentCache->OrigEntry |
| 1699 | : nullptr; |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1700 | if (Entry && |
Douglas Gregor | 6a5be93 | 2011-02-11 18:08:15 +0000 | [diff] [blame] | 1701 | *SourceFileName == llvm::sys::path::filename(Entry->getName())) { |
Rafael Espindola | 073ff10 | 2013-07-29 21:26:52 +0000 | [diff] [blame] | 1702 | if (Optional<llvm::sys::fs::UniqueID> EntryUID = |
| 1703 | getActualFileUID(Entry)) { |
Rafael Espindola | 74ca78e | 2013-07-29 18:43:40 +0000 | [diff] [blame] | 1704 | if (*SourceFileUID == *EntryUID) { |
Douglas Gregor | 6a5be93 | 2011-02-11 18:08:15 +0000 | [diff] [blame] | 1705 | FirstFID = FileID::get(I); |
| 1706 | SourceFile = Entry; |
| 1707 | break; |
| 1708 | } |
| 1709 | } |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | } |
Argyrios Kyrtzidis | 04a6e5f | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1714 | |
Ted Kremenek | bd1d7fa | 2012-10-12 22:56:33 +0000 | [diff] [blame] | 1715 | (void) SourceFile; |
Argyrios Kyrtzidis | 04a6e5f | 2011-09-27 17:22:25 +0000 | [diff] [blame] | 1716 | return FirstFID; |
Argyrios Kyrtzidis | 532c519 | 2011-09-19 20:40:29 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | /// \brief Get the source location in \arg FID for the given line:col. |
| 1720 | /// Returns null location if \arg FID is not a file SLocEntry. |
| 1721 | SourceLocation SourceManager::translateLineCol(FileID FID, |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1722 | unsigned Line, |
| 1723 | unsigned Col) const { |
Aaron Ballman | ef1cf83 | 2013-11-18 18:29:00 +0000 | [diff] [blame] | 1724 | // Lines are used as a one-based index into a zero-based array. This assert |
| 1725 | // checks for possible buffer underruns. |
| 1726 | assert(Line != 0 && "Passed a zero-based line"); |
| 1727 | |
Argyrios Kyrtzidis | 532c519 | 2011-09-19 20:40:29 +0000 | [diff] [blame] | 1728 | if (FID.isInvalid()) |
| 1729 | return SourceLocation(); |
| 1730 | |
| 1731 | bool Invalid = false; |
| 1732 | const SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 1733 | if (Invalid) |
| 1734 | return SourceLocation(); |
Alexander Kornienko | 6d8cf83 | 2013-07-29 22:26:10 +0000 | [diff] [blame] | 1735 | |
Argyrios Kyrtzidis | 532c519 | 2011-09-19 20:40:29 +0000 | [diff] [blame] | 1736 | if (!Entry.isFile()) |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1737 | return SourceLocation(); |
| 1738 | |
Argyrios Kyrtzidis | 7c2b28a | 2011-09-20 22:14:54 +0000 | [diff] [blame] | 1739 | SourceLocation FileLoc = SourceLocation::getFileLoc(Entry.getOffset()); |
| 1740 | |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1741 | if (Line == 1 && Col == 1) |
Argyrios Kyrtzidis | 7c2b28a | 2011-09-20 22:14:54 +0000 | [diff] [blame] | 1742 | return FileLoc; |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1743 | |
| 1744 | ContentCache *Content |
Argyrios Kyrtzidis | 532c519 | 2011-09-19 20:40:29 +0000 | [diff] [blame] | 1745 | = const_cast<ContentCache *>(Entry.getFile().getContentCache()); |
Douglas Gregor | e664276 | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 1746 | if (!Content) |
| 1747 | return SourceLocation(); |
Alexander Kornienko | 6d8cf83 | 2013-07-29 22:26:10 +0000 | [diff] [blame] | 1748 | |
Argyrios Kyrtzidis | 88f663c0 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1749 | // If this is the first use of line information for this buffer, compute the |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1750 | // SourceLineCache for it on demand. |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1751 | if (!Content->SourceLineCache) { |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1752 | bool MyInvalid = false; |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1753 | ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid); |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1754 | if (MyInvalid) |
| 1755 | return SourceLocation(); |
| 1756 | } |
Argyrios Kyrtzidis | 88f663c0 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1757 | |
Douglas Gregor | b8b9f28 | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1758 | if (Line > Content->NumLines) { |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1759 | unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize(); |
Douglas Gregor | b8b9f28 | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1760 | if (Size > 0) |
| 1761 | --Size; |
Argyrios Kyrtzidis | 7c2b28a | 2011-09-20 22:14:54 +0000 | [diff] [blame] | 1762 | return FileLoc.getLocWithOffset(Size); |
Douglas Gregor | b8b9f28 | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
David Blaikie | 66cc07b | 2014-06-27 17:40:03 +0000 | [diff] [blame] | 1765 | llvm::MemoryBuffer *Buffer = Content->getBuffer(Diag, *this); |
Douglas Gregor | b8b9f28 | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1766 | unsigned FilePos = Content->SourceLineCache[Line - 1]; |
Dylan Noblesmith | 2a8bc15 | 2011-12-19 08:51:05 +0000 | [diff] [blame] | 1767 | const char *Buf = Buffer->getBufferStart() + FilePos; |
| 1768 | unsigned BufLength = Buffer->getBufferSize() - FilePos; |
Argyrios Kyrtzidis | 7c2b28a | 2011-09-20 22:14:54 +0000 | [diff] [blame] | 1769 | if (BufLength == 0) |
| 1770 | return FileLoc.getLocWithOffset(FilePos); |
| 1771 | |
Douglas Gregor | b8b9f28 | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1772 | unsigned i = 0; |
| 1773 | |
| 1774 | // Check that the given column is valid. |
| 1775 | while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r') |
| 1776 | ++i; |
Alexander Kornienko | 6d8cf83 | 2013-07-29 22:26:10 +0000 | [diff] [blame] | 1777 | return FileLoc.getLocWithOffset(FilePos + i); |
Argyrios Kyrtzidis | 88f663c0 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1780 | /// \brief Compute a map of macro argument chunks to their expanded source |
| 1781 | /// location. Chunks that are not part of a macro argument will map to an |
| 1782 | /// invalid source location. e.g. if a file contains one macro argument at |
| 1783 | /// offset 100 with length 10, this is how the map will be formed: |
| 1784 | /// 0 -> SourceLocation() |
| 1785 | /// 100 -> Expanded macro arg location |
| 1786 | /// 110 -> SourceLocation() |
Argyrios Kyrtzidis | 4bdd6aa | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1787 | void SourceManager::computeMacroArgsCache(MacroArgsMap *&CachePtr, |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1788 | FileID FID) const { |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1789 | assert(!FID.isInvalid()); |
Argyrios Kyrtzidis | 4bdd6aa | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1790 | assert(!CachePtr); |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1791 | |
Argyrios Kyrtzidis | 4bdd6aa | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1792 | CachePtr = new MacroArgsMap(); |
| 1793 | MacroArgsMap &MacroArgsCache = *CachePtr; |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1794 | // Initially no macro argument chunk is present. |
| 1795 | MacroArgsCache.insert(std::make_pair(0, SourceLocation())); |
| 1796 | |
| 1797 | int ID = FID.ID; |
| 1798 | while (1) { |
| 1799 | ++ID; |
| 1800 | // Stop if there are no more FileIDs to check. |
| 1801 | if (ID > 0) { |
| 1802 | if (unsigned(ID) >= local_sloc_entry_size()) |
| 1803 | return; |
| 1804 | } else if (ID == -1) { |
| 1805 | return; |
| 1806 | } |
| 1807 | |
Argyrios Kyrtzidis | fe68302 | 2013-06-07 17:57:59 +0000 | [diff] [blame] | 1808 | bool Invalid = false; |
| 1809 | const SrcMgr::SLocEntry &Entry = getSLocEntryByID(ID, &Invalid); |
| 1810 | if (Invalid) |
| 1811 | return; |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1812 | if (Entry.isFile()) { |
| 1813 | SourceLocation IncludeLoc = Entry.getFile().getIncludeLoc(); |
| 1814 | if (IncludeLoc.isInvalid()) |
| 1815 | continue; |
| 1816 | if (!isInFileID(IncludeLoc, FID)) |
| 1817 | return; // No more files/macros that may be "contained" in this file. |
| 1818 | |
| 1819 | // Skip the files/macros of the #include'd file, we only care about macros |
| 1820 | // that lexed macro arguments from our file. |
| 1821 | if (Entry.getFile().NumCreatedFIDs) |
| 1822 | ID += Entry.getFile().NumCreatedFIDs - 1/*because of next ++ID*/; |
| 1823 | continue; |
| 1824 | } |
| 1825 | |
Argyrios Kyrtzidis | e841c90 | 2011-12-21 16:56:35 +0000 | [diff] [blame] | 1826 | const ExpansionInfo &ExpInfo = Entry.getExpansion(); |
| 1827 | |
| 1828 | if (ExpInfo.getExpansionLocStart().isFileID()) { |
| 1829 | if (!isInFileID(ExpInfo.getExpansionLocStart(), FID)) |
| 1830 | return; // No more files/macros that may be "contained" in this file. |
| 1831 | } |
| 1832 | |
| 1833 | if (!ExpInfo.isMacroArgExpansion()) |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1834 | continue; |
Argyrios Kyrtzidis | e841c90 | 2011-12-21 16:56:35 +0000 | [diff] [blame] | 1835 | |
Argyrios Kyrtzidis | 73ccdb9 | 2012-10-20 00:51:32 +0000 | [diff] [blame] | 1836 | associateFileChunkWithMacroArgExp(MacroArgsCache, FID, |
| 1837 | ExpInfo.getSpellingLoc(), |
| 1838 | SourceLocation::getMacroLoc(Entry.getOffset()), |
| 1839 | getFileIDSize(FileID::get(ID))); |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | |
Argyrios Kyrtzidis | 73ccdb9 | 2012-10-20 00:51:32 +0000 | [diff] [blame] | 1843 | void SourceManager::associateFileChunkWithMacroArgExp( |
| 1844 | MacroArgsMap &MacroArgsCache, |
| 1845 | FileID FID, |
| 1846 | SourceLocation SpellLoc, |
| 1847 | SourceLocation ExpansionLoc, |
| 1848 | unsigned ExpansionLength) const { |
| 1849 | if (!SpellLoc.isFileID()) { |
| 1850 | unsigned SpellBeginOffs = SpellLoc.getOffset(); |
| 1851 | unsigned SpellEndOffs = SpellBeginOffs + ExpansionLength; |
| 1852 | |
| 1853 | // The spelling range for this macro argument expansion can span multiple |
| 1854 | // consecutive FileID entries. Go through each entry contained in the |
| 1855 | // spelling range and if one is itself a macro argument expansion, recurse |
| 1856 | // and associate the file chunk that it represents. |
| 1857 | |
| 1858 | FileID SpellFID; // Current FileID in the spelling range. |
| 1859 | unsigned SpellRelativeOffs; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1860 | std::tie(SpellFID, SpellRelativeOffs) = getDecomposedLoc(SpellLoc); |
Argyrios Kyrtzidis | 73ccdb9 | 2012-10-20 00:51:32 +0000 | [diff] [blame] | 1861 | while (1) { |
| 1862 | const SLocEntry &Entry = getSLocEntry(SpellFID); |
| 1863 | unsigned SpellFIDBeginOffs = Entry.getOffset(); |
| 1864 | unsigned SpellFIDSize = getFileIDSize(SpellFID); |
| 1865 | unsigned SpellFIDEndOffs = SpellFIDBeginOffs + SpellFIDSize; |
| 1866 | const ExpansionInfo &Info = Entry.getExpansion(); |
| 1867 | if (Info.isMacroArgExpansion()) { |
| 1868 | unsigned CurrSpellLength; |
| 1869 | if (SpellFIDEndOffs < SpellEndOffs) |
| 1870 | CurrSpellLength = SpellFIDSize - SpellRelativeOffs; |
| 1871 | else |
| 1872 | CurrSpellLength = ExpansionLength; |
| 1873 | associateFileChunkWithMacroArgExp(MacroArgsCache, FID, |
| 1874 | Info.getSpellingLoc().getLocWithOffset(SpellRelativeOffs), |
| 1875 | ExpansionLoc, CurrSpellLength); |
| 1876 | } |
| 1877 | |
| 1878 | if (SpellFIDEndOffs >= SpellEndOffs) |
| 1879 | return; // we covered all FileID entries in the spelling range. |
| 1880 | |
| 1881 | // Move to the next FileID entry in the spelling range. |
| 1882 | unsigned advance = SpellFIDSize - SpellRelativeOffs + 1; |
| 1883 | ExpansionLoc = ExpansionLoc.getLocWithOffset(advance); |
| 1884 | ExpansionLength -= advance; |
| 1885 | ++SpellFID.ID; |
| 1886 | SpellRelativeOffs = 0; |
| 1887 | } |
| 1888 | |
| 1889 | } |
| 1890 | |
| 1891 | assert(SpellLoc.isFileID()); |
| 1892 | |
| 1893 | unsigned BeginOffs; |
| 1894 | if (!isInFileID(SpellLoc, FID, &BeginOffs)) |
| 1895 | return; |
| 1896 | |
| 1897 | unsigned EndOffs = BeginOffs + ExpansionLength; |
| 1898 | |
| 1899 | // Add a new chunk for this macro argument. A previous macro argument chunk |
| 1900 | // may have been lexed again, so e.g. if the map is |
| 1901 | // 0 -> SourceLocation() |
| 1902 | // 100 -> Expanded loc #1 |
| 1903 | // 110 -> SourceLocation() |
| 1904 | // and we found a new macro FileID that lexed from offet 105 with length 3, |
| 1905 | // the new map will be: |
| 1906 | // 0 -> SourceLocation() |
| 1907 | // 100 -> Expanded loc #1 |
| 1908 | // 105 -> Expanded loc #2 |
| 1909 | // 108 -> Expanded loc #1 |
| 1910 | // 110 -> SourceLocation() |
| 1911 | // |
| 1912 | // Since re-lexed macro chunks will always be the same size or less of |
| 1913 | // previous chunks, we only need to find where the ending of the new macro |
| 1914 | // chunk is mapped to and update the map with new begin/end mappings. |
| 1915 | |
| 1916 | MacroArgsMap::iterator I = MacroArgsCache.upper_bound(EndOffs); |
| 1917 | --I; |
| 1918 | SourceLocation EndOffsMappedLoc = I->second; |
| 1919 | MacroArgsCache[BeginOffs] = ExpansionLoc; |
| 1920 | MacroArgsCache[EndOffs] = EndOffsMappedLoc; |
| 1921 | } |
| 1922 | |
Argyrios Kyrtzidis | 92a47bd | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1923 | /// \brief If \arg Loc points inside a function macro argument, the returned |
| 1924 | /// location will be the macro location in which the argument was expanded. |
| 1925 | /// If a macro argument is used multiple times, the expanded location will |
| 1926 | /// be at the first expansion of the argument. |
| 1927 | /// e.g. |
| 1928 | /// MY_MACRO(foo); |
| 1929 | /// ^ |
| 1930 | /// Passing a file location pointing at 'foo', will yield a macro location |
| 1931 | /// where 'foo' was expanded into. |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1932 | SourceLocation |
| 1933 | SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) const { |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1934 | if (Loc.isInvalid() || !Loc.isFileID()) |
Argyrios Kyrtzidis | 92a47bd | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1935 | return Loc; |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1936 | |
| 1937 | FileID FID; |
| 1938 | unsigned Offset; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1939 | std::tie(FID, Offset) = getDecomposedLoc(Loc); |
Argyrios Kyrtzidis | 92a47bd | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1940 | if (FID.isInvalid()) |
| 1941 | return Loc; |
| 1942 | |
Argyrios Kyrtzidis | 4bdd6aa | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1943 | MacroArgsMap *&MacroArgsCache = MacroArgsCacheMap[FID]; |
| 1944 | if (!MacroArgsCache) |
| 1945 | computeMacroArgsCache(MacroArgsCache, FID); |
Argyrios Kyrtzidis | 92a47bd | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1946 | |
Argyrios Kyrtzidis | 4bdd6aa | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 1947 | assert(!MacroArgsCache->empty()); |
| 1948 | MacroArgsMap::iterator I = MacroArgsCache->upper_bound(Offset); |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1949 | --I; |
Argyrios Kyrtzidis | 92a47bd | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1950 | |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1951 | unsigned MacroArgBeginOffs = I->first; |
| 1952 | SourceLocation MacroArgExpandedLoc = I->second; |
| 1953 | if (MacroArgExpandedLoc.isValid()) |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 1954 | return MacroArgExpandedLoc.getLocWithOffset(Offset - MacroArgBeginOffs); |
Argyrios Kyrtzidis | 92a47bd | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1955 | |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 1956 | return Loc; |
Argyrios Kyrtzidis | 92a47bd | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Argyrios Kyrtzidis | 37613a9 | 2013-04-13 01:03:57 +0000 | [diff] [blame] | 1959 | std::pair<FileID, unsigned> |
| 1960 | SourceManager::getDecomposedIncludedLoc(FileID FID) const { |
Argyrios Kyrtzidis | 5dca864 | 2013-05-24 22:24:04 +0000 | [diff] [blame] | 1961 | if (FID.isInvalid()) |
| 1962 | return std::make_pair(FileID(), 0); |
| 1963 | |
Argyrios Kyrtzidis | 37613a9 | 2013-04-13 01:03:57 +0000 | [diff] [blame] | 1964 | // Uses IncludedLocMap to retrieve/cache the decomposed loc. |
| 1965 | |
| 1966 | typedef std::pair<FileID, unsigned> DecompTy; |
| 1967 | typedef llvm::DenseMap<FileID, DecompTy> MapTy; |
| 1968 | std::pair<MapTy::iterator, bool> |
| 1969 | InsertOp = IncludedLocMap.insert(std::make_pair(FID, DecompTy())); |
| 1970 | DecompTy &DecompLoc = InsertOp.first->second; |
| 1971 | if (!InsertOp.second) |
| 1972 | return DecompLoc; // already in map. |
| 1973 | |
| 1974 | SourceLocation UpperLoc; |
Argyrios Kyrtzidis | 5dca864 | 2013-05-24 22:24:04 +0000 | [diff] [blame] | 1975 | bool Invalid = false; |
| 1976 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 1977 | if (!Invalid) { |
| 1978 | if (Entry.isExpansion()) |
| 1979 | UpperLoc = Entry.getExpansion().getExpansionLocStart(); |
| 1980 | else |
| 1981 | UpperLoc = Entry.getFile().getIncludeLoc(); |
| 1982 | } |
Argyrios Kyrtzidis | 37613a9 | 2013-04-13 01:03:57 +0000 | [diff] [blame] | 1983 | |
| 1984 | if (UpperLoc.isValid()) |
| 1985 | DecompLoc = getDecomposedLoc(UpperLoc); |
| 1986 | |
| 1987 | return DecompLoc; |
| 1988 | } |
| 1989 | |
Chandler Carruth | 64ee782 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 1990 | /// Given a decomposed source location, move it up the include/expansion stack |
| 1991 | /// to the parent source location. If this is possible, return the decomposed |
| 1992 | /// version of the parent in Loc and return false. If Loc is the top-level |
| 1993 | /// entry, return true and don't modify it. |
Chris Lattner | a99fa1a | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1994 | static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc, |
| 1995 | const SourceManager &SM) { |
Argyrios Kyrtzidis | 37613a9 | 2013-04-13 01:03:57 +0000 | [diff] [blame] | 1996 | std::pair<FileID, unsigned> UpperLoc = SM.getDecomposedIncludedLoc(Loc.first); |
| 1997 | if (UpperLoc.first.isInvalid()) |
Chris Lattner | a99fa1a | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1998 | return true; // We reached the top. |
Argyrios Kyrtzidis | 37613a9 | 2013-04-13 01:03:57 +0000 | [diff] [blame] | 1999 | |
| 2000 | Loc = UpperLoc; |
Chris Lattner | a99fa1a | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 2001 | return false; |
| 2002 | } |
Ted Kremenek | 0803704 | 2013-02-27 00:00:26 +0000 | [diff] [blame] | 2003 | |
| 2004 | /// Return the cache entry for comparing the given file IDs |
| 2005 | /// for isBeforeInTranslationUnit. |
| 2006 | InBeforeInTUCacheEntry &SourceManager::getInBeforeInTUCache(FileID LFID, |
| 2007 | FileID RFID) const { |
| 2008 | // This is a magic number for limiting the cache size. It was experimentally |
| 2009 | // derived from a small Objective-C project (where the cache filled |
| 2010 | // out to ~250 items). We can make it larger if necessary. |
| 2011 | enum { MagicCacheSize = 300 }; |
| 2012 | IsBeforeInTUCacheKey Key(LFID, RFID); |
| 2013 | |
| 2014 | // If the cache size isn't too large, do a lookup and if necessary default |
| 2015 | // construct an entry. We can then return it to the caller for direct |
| 2016 | // use. When they update the value, the cache will get automatically |
| 2017 | // updated as well. |
| 2018 | if (IBTUCache.size() < MagicCacheSize) |
| 2019 | return IBTUCache[Key]; |
| 2020 | |
| 2021 | // Otherwise, do a lookup that will not construct a new value. |
| 2022 | InBeforeInTUCache::iterator I = IBTUCache.find(Key); |
| 2023 | if (I != IBTUCache.end()) |
| 2024 | return I->second; |
| 2025 | |
| 2026 | // Fall back to the overflow value. |
| 2027 | return IBTUCacheOverflow; |
| 2028 | } |
Chris Lattner | a99fa1a | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 2029 | |
Argyrios Kyrtzidis | 33661d9 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 2030 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 2031 | /// |
| 2032 | /// \returns true if LHS source location comes before RHS, false otherwise. |
| 2033 | bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS, |
| 2034 | SourceLocation RHS) const { |
| 2035 | assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!"); |
| 2036 | if (LHS == RHS) |
| 2037 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | |
Argyrios Kyrtzidis | 33661d9 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 2039 | std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS); |
| 2040 | std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2041 | |
Argyrios Kyrtzidis | 5fd822c | 2013-05-24 23:47:43 +0000 | [diff] [blame] | 2042 | // getDecomposedLoc may have failed to return a valid FileID because, e.g. it |
| 2043 | // is a serialized one referring to a file that was removed after we loaded |
| 2044 | // the PCH. |
Argyrios Kyrtzidis | 5dca864 | 2013-05-24 22:24:04 +0000 | [diff] [blame] | 2045 | if (LOffs.first.isInvalid() || ROffs.first.isInvalid()) |
Argyrios Kyrtzidis | d6111d3 | 2013-05-25 01:03:03 +0000 | [diff] [blame] | 2046 | return LOffs.first.isInvalid() && !ROffs.first.isInvalid(); |
Argyrios Kyrtzidis | 5dca864 | 2013-05-24 22:24:04 +0000 | [diff] [blame] | 2047 | |
Argyrios Kyrtzidis | 33661d9 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 2048 | // If the source locations are in the same file, just compare offsets. |
| 2049 | if (LOffs.first == ROffs.first) |
| 2050 | return LOffs.second < ROffs.second; |
| 2051 | |
| 2052 | // If we are comparing a source location with multiple locations in the same |
| 2053 | // file, we get a big win by caching the result. |
Ted Kremenek | 0803704 | 2013-02-27 00:00:26 +0000 | [diff] [blame] | 2054 | InBeforeInTUCacheEntry &IsBeforeInTUCache = |
| 2055 | getInBeforeInTUCache(LOffs.first, ROffs.first); |
| 2056 | |
| 2057 | // If we are comparing a source location with multiple locations in the same |
| 2058 | // file, we get a big win by caching the result. |
Chris Lattner | 46e3b48 | 2010-05-07 05:10:46 +0000 | [diff] [blame] | 2059 | if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first)) |
| 2060 | return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | |
Chris Lattner | 66d2f92 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 2062 | // Okay, we missed in the cache, start updating the cache for this query. |
Argyrios Kyrtzidis | ac199bf | 2011-08-17 00:31:18 +0000 | [diff] [blame] | 2063 | IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first, |
| 2064 | /*isLFIDBeforeRFID=*/LOffs.first.ID < ROffs.first.ID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2066 | // We need to find the common ancestor. The only way of doing this is to |
| 2067 | // build the complete include chain for one and then walking up the chain |
| 2068 | // of the other looking for a match. |
| 2069 | // We use a map from FileID to Offset to store the chain. Easier than writing |
| 2070 | // a custom set hash info that only depends on the first part of a pair. |
Argyrios Kyrtzidis | 37613a9 | 2013-04-13 01:03:57 +0000 | [diff] [blame] | 2071 | typedef llvm::SmallDenseMap<FileID, unsigned, 16> LocSet; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2072 | LocSet LChain; |
Chris Lattner | 06821c9 | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 2073 | do { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2074 | LChain.insert(LOffs); |
| 2075 | // We catch the case where LOffs is in a file included by ROffs and |
| 2076 | // quit early. The other way round unfortunately remains suboptimal. |
| 2077 | } while (LOffs.first != ROffs.first && !MoveUpIncludeHierarchy(LOffs, *this)); |
| 2078 | LocSet::iterator I; |
| 2079 | while((I = LChain.find(ROffs.first)) == LChain.end()) { |
| 2080 | if (MoveUpIncludeHierarchy(ROffs, *this)) |
| 2081 | break; // Met at topmost file. |
| 2082 | } |
| 2083 | if (I != LChain.end()) |
| 2084 | LOffs = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Chris Lattner | 06821c9 | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 2086 | // If we exited because we found a nearest common ancestor, compare the |
| 2087 | // locations within the common file and cache them. |
| 2088 | if (LOffs.first == ROffs.first) { |
| 2089 | IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second); |
| 2090 | return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second); |
Argyrios Kyrtzidis | 33661d9 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 2091 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Joerg Sonnenberger | 1d3b431 | 2015-03-16 17:54:54 +0000 | [diff] [blame] | 2093 | // If we arrived here, the location is either in a built-ins buffer or |
| 2094 | // associated with global inline asm. PR5662 and PR22576 are examples. |
| 2095 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2096 | // Clear the lookup cache, it depends on a common location. |
Argyrios Kyrtzidis | ac199bf | 2011-08-17 00:31:18 +0000 | [diff] [blame] | 2097 | IsBeforeInTUCache.clear(); |
Joerg Sonnenberger | 1d3b431 | 2015-03-16 17:54:54 +0000 | [diff] [blame] | 2098 | llvm::MemoryBuffer *LBuf = getBuffer(LOffs.first); |
| 2099 | llvm::MemoryBuffer *RBuf = getBuffer(ROffs.first); |
| 2100 | bool LIsBuiltins = strcmp("<built-in>", LBuf->getBufferIdentifier()) == 0; |
| 2101 | bool RIsBuiltins = strcmp("<built-in>", RBuf->getBufferIdentifier()) == 0; |
| 2102 | // Sort built-in before non-built-in. |
| 2103 | if (LIsBuiltins || RIsBuiltins) { |
| 2104 | if (LIsBuiltins != RIsBuiltins) |
| 2105 | return LIsBuiltins; |
| 2106 | // Both are in built-in buffers, but from different files. We just claim that |
| 2107 | // lower IDs come first. |
| 2108 | return LOffs.first < ROffs.first; |
| 2109 | } |
| 2110 | bool LIsAsm = strcmp("<inline asm>", LBuf->getBufferIdentifier()) == 0; |
| 2111 | bool RIsAsm = strcmp("<inline asm>", RBuf->getBufferIdentifier()) == 0; |
| 2112 | // Sort assembler after built-ins, but before the rest. |
| 2113 | if (LIsAsm || RIsAsm) { |
| 2114 | if (LIsAsm != RIsAsm) |
| 2115 | return RIsAsm; |
| 2116 | assert(LOffs.first == ROffs.first); |
| 2117 | return false; |
| 2118 | } |
| 2119 | llvm_unreachable("Unsortable locations found"); |
Argyrios Kyrtzidis | 33661d9 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 2120 | } |
Chris Lattner | 4fa2362 | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2122 | void SourceManager::PrintStats() const { |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 2123 | llvm::errs() << "\n*** Source Manager Stats:\n"; |
| 2124 | llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size() |
| 2125 | << " mem buffers mapped.\n"; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2126 | llvm::errs() << LocalSLocEntryTable.size() << " local SLocEntry's allocated (" |
Ted Kremenek | 43e0c4a | 2011-07-27 18:41:16 +0000 | [diff] [blame] | 2127 | << llvm::capacity_in_bytes(LocalSLocEntryTable) |
Argyrios Kyrtzidis | 2cc6209 | 2011-07-07 03:40:24 +0000 | [diff] [blame] | 2128 | << " bytes of capacity), " |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2129 | << NextLocalOffset << "B of Sloc address space used.\n"; |
| 2130 | llvm::errs() << LoadedSLocEntryTable.size() |
| 2131 | << " loaded SLocEntries allocated, " |
Argyrios Kyrtzidis | 92a47bd | 2011-08-17 00:31:20 +0000 | [diff] [blame] | 2132 | << MaxLoadedOffset - CurrentLoadedOffset |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2133 | << "B of Sloc address space used.\n"; |
| 2134 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2135 | unsigned NumLineNumsComputed = 0; |
| 2136 | unsigned NumFileBytesMapped = 0; |
Chris Lattner | c8233df | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 2137 | for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){ |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 2138 | NumLineNumsComputed += I->second->SourceLineCache != nullptr; |
Chris Lattner | c8233df | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 2139 | NumFileBytesMapped += I->second->getSizeBytesMapped(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2140 | } |
Argyrios Kyrtzidis | 4bdd6aa | 2011-09-26 08:01:50 +0000 | [diff] [blame] | 2141 | unsigned NumMacroArgsComputed = MacroArgsCacheMap.size(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 2143 | llvm::errs() << NumFileBytesMapped << " bytes of files mapped, " |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 2144 | << NumLineNumsComputed << " files with line #'s computed, " |
| 2145 | << NumMacroArgsComputed << " files with macro args computed.\n"; |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 2146 | llvm::errs() << "FileID scans: " << NumLinearScans << " linear, " |
| 2147 | << NumBinaryProbes << " binary.\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2148 | } |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 2149 | |
Richard Smith | 03a06dd | 2015-08-13 00:45:11 +0000 | [diff] [blame] | 2150 | LLVM_DUMP_METHOD void SourceManager::dump() const { |
| 2151 | llvm::raw_ostream &out = llvm::errs(); |
| 2152 | |
| 2153 | auto DumpSLocEntry = [&](int ID, const SrcMgr::SLocEntry &Entry, |
| 2154 | llvm::Optional<unsigned> NextStart) { |
| 2155 | out << "SLocEntry <FileID " << ID << "> " << (Entry.isFile() ? "file" : "expansion") |
| 2156 | << " <SourceLocation " << Entry.getOffset() << ":"; |
| 2157 | if (NextStart) |
| 2158 | out << *NextStart << ">\n"; |
| 2159 | else |
| 2160 | out << "???\?>\n"; |
| 2161 | if (Entry.isFile()) { |
| 2162 | auto &FI = Entry.getFile(); |
| 2163 | if (FI.NumCreatedFIDs) |
| 2164 | out << " covers <FileID " << ID << ":" << int(ID + FI.NumCreatedFIDs) |
| 2165 | << ">\n"; |
| 2166 | if (FI.getIncludeLoc().isValid()) |
| 2167 | out << " included from " << FI.getIncludeLoc().getOffset() << "\n"; |
| 2168 | if (auto *CC = FI.getContentCache()) { |
| 2169 | out << " for " << (CC->OrigEntry ? CC->OrigEntry->getName() : "<none>") |
| 2170 | << "\n"; |
| 2171 | if (CC->BufferOverridden) |
| 2172 | out << " contents overridden\n"; |
| 2173 | if (CC->ContentsEntry != CC->OrigEntry) { |
| 2174 | out << " contents from " |
| 2175 | << (CC->ContentsEntry ? CC->ContentsEntry->getName() : "<none>") |
| 2176 | << "\n"; |
| 2177 | } |
| 2178 | } |
| 2179 | } else { |
| 2180 | auto &EI = Entry.getExpansion(); |
| 2181 | out << " spelling from " << EI.getSpellingLoc().getOffset() << "\n"; |
| 2182 | out << " macro " << (EI.isMacroArgExpansion() ? "arg" : "body") |
| 2183 | << " range <" << EI.getExpansionLocStart().getOffset() << ":" |
| 2184 | << EI.getExpansionLocEnd().getOffset() << ">\n"; |
| 2185 | } |
| 2186 | }; |
| 2187 | |
| 2188 | // Dump local SLocEntries. |
| 2189 | for (unsigned ID = 0, NumIDs = LocalSLocEntryTable.size(); ID != NumIDs; ++ID) { |
| 2190 | DumpSLocEntry(ID, LocalSLocEntryTable[ID], |
| 2191 | ID == NumIDs - 1 ? NextLocalOffset |
| 2192 | : LocalSLocEntryTable[ID + 1].getOffset()); |
| 2193 | } |
| 2194 | // Dump loaded SLocEntries. |
| 2195 | llvm::Optional<unsigned> NextStart; |
| 2196 | for (unsigned Index = 0; Index != LoadedSLocEntryTable.size(); ++Index) { |
| 2197 | int ID = -(int)Index - 2; |
| 2198 | if (SLocEntryLoaded[Index]) { |
| 2199 | DumpSLocEntry(ID, LoadedSLocEntryTable[Index], NextStart); |
| 2200 | NextStart = LoadedSLocEntryTable[Index].getOffset(); |
| 2201 | } else { |
| 2202 | NextStart = None; |
| 2203 | } |
| 2204 | } |
| 2205 | } |
| 2206 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 2207 | ExternalSLocEntrySource::~ExternalSLocEntrySource() { } |
Ted Kremenek | 8d58790 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 2208 | |
| 2209 | /// Return the amount of memory used by memory buffers, breaking down |
| 2210 | /// by heap-backed versus mmap'ed memory. |
| 2211 | SourceManager::MemoryBufferSizes SourceManager::getMemoryBufferSizes() const { |
| 2212 | size_t malloc_bytes = 0; |
| 2213 | size_t mmap_bytes = 0; |
| 2214 | |
| 2215 | for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) |
| 2216 | if (size_t sized_mapped = MemBufferInfos[i]->getSizeBytesMapped()) |
| 2217 | switch (MemBufferInfos[i]->getMemoryBufferKind()) { |
| 2218 | case llvm::MemoryBuffer::MemoryBuffer_MMap: |
| 2219 | mmap_bytes += sized_mapped; |
| 2220 | break; |
| 2221 | case llvm::MemoryBuffer::MemoryBuffer_Malloc: |
| 2222 | malloc_bytes += sized_mapped; |
| 2223 | break; |
| 2224 | } |
| 2225 | |
| 2226 | return MemoryBufferSizes(malloc_bytes, mmap_bytes); |
| 2227 | } |
| 2228 | |
Ted Kremenek | 120992a | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 2229 | size_t SourceManager::getDataStructureSizes() const { |
Argyrios Kyrtzidis | 6eec06d | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 2230 | size_t size = llvm::capacity_in_bytes(MemBufferInfos) |
Ted Kremenek | 43e0c4a | 2011-07-27 18:41:16 +0000 | [diff] [blame] | 2231 | + llvm::capacity_in_bytes(LocalSLocEntryTable) |
| 2232 | + llvm::capacity_in_bytes(LoadedSLocEntryTable) |
| 2233 | + llvm::capacity_in_bytes(SLocEntryLoaded) |
Argyrios Kyrtzidis | 6eec06d | 2012-05-03 21:50:39 +0000 | [diff] [blame] | 2234 | + llvm::capacity_in_bytes(FileInfos); |
| 2235 | |
| 2236 | if (OverriddenFilesInfo) |
| 2237 | size += llvm::capacity_in_bytes(OverriddenFilesInfo->OverriddenFiles); |
| 2238 | |
| 2239 | return size; |
Ted Kremenek | 120992a | 2011-07-26 23:46:06 +0000 | [diff] [blame] | 2240 | } |