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