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