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