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" |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/System/Path.h" |
| 22 | #include <algorithm> |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 23 | #include <string> |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 24 | #include <cstring> |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 25 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace SrcMgr; |
| 28 | using llvm::MemoryBuffer; |
| 29 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 31 | // SourceManager Helper Classes |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 34 | ContentCache::~ContentCache() { |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 35 | if (shouldFreeBuffer()) |
| 36 | delete Buffer.getPointer(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 39 | /// getSizeBytesMapped - Returns the number of bytes actually mapped for |
| 40 | /// this ContentCache. This can be 0 if the MemBuffer was not actually |
| 41 | /// instantiated. |
| 42 | unsigned ContentCache::getSizeBytesMapped() const { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 43 | return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0; |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /// getSize - Returns the size of the content encapsulated by this ContentCache. |
| 47 | /// This can be the size of the source file or the size of an arbitrary |
| 48 | /// scratch buffer. If the ContentCache encapsulates a source file, that |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 49 | /// file is not lazily brought in from disk to satisfy this query. |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 50 | unsigned ContentCache::getSize() const { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 51 | return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize() |
| 52 | : (unsigned) Entry->getSize(); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 55 | void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B, |
| 56 | bool DoNotFree) { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 57 | assert(B != Buffer.getPointer()); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 58 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 59 | if (shouldFreeBuffer()) |
| 60 | delete Buffer.getPointer(); |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 61 | Buffer.setPointer(B); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 62 | Buffer.setInt(DoNotFree? DoNotFreeFlag : 0); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 65 | const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag, |
Chris Lattner | 5c5db4e | 2010-04-20 20:49:23 +0000 | [diff] [blame] | 66 | const SourceManager &SM, |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 67 | SourceLocation Loc, |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 68 | bool *Invalid) const { |
| 69 | if (Invalid) |
| 70 | *Invalid = false; |
| 71 | |
Ted Kremenek | 5b034ad | 2009-01-06 22:43:04 +0000 | [diff] [blame] | 72 | // Lazily create the Buffer for ContentCaches that wrap files. |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 73 | if (!Buffer.getPointer() && Entry) { |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 74 | std::string ErrorStr; |
| 75 | struct stat FileInfo; |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 76 | Buffer.setPointer(MemoryBuffer::getFile(Entry->getName(), &ErrorStr, |
| 77 | Entry->getSize(), &FileInfo)); |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 78 | |
Daniel Dunbar | 21a8bed | 2009-12-06 05:43:36 +0000 | [diff] [blame] | 79 | // If we were unable to open the file, then we are in an inconsistent |
| 80 | // situation where the content cache referenced a file which no longer |
| 81 | // exists. Most likely, we were using a stat cache with an invalid entry but |
| 82 | // the file could also have been removed during processing. Since we can't |
| 83 | // really deal with this situation, just create an empty buffer. |
| 84 | // |
| 85 | // FIXME: This is definitely not ideal, but our immediate clients can't |
| 86 | // currently handle returning a null entry here. Ideally we should detect |
| 87 | // that we are in an inconsistent situation and error out as quickly as |
| 88 | // possible. |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 89 | if (!Buffer.getPointer()) { |
Daniel Dunbar | 21a8bed | 2009-12-06 05:43:36 +0000 | [diff] [blame] | 90 | const llvm::StringRef FillStr("<<<MISSING SOURCE FILE>>>\n"); |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 91 | Buffer.setPointer(MemoryBuffer::getNewMemBuffer(Entry->getSize(), |
| 92 | "<invalid>")); |
| 93 | char *Ptr = const_cast<char*>(Buffer.getPointer()->getBufferStart()); |
Daniel Dunbar | 21a8bed | 2009-12-06 05:43:36 +0000 | [diff] [blame] | 94 | for (unsigned i = 0, e = Entry->getSize(); i != e; ++i) |
| 95 | Ptr[i] = FillStr[i % FillStr.size()]; |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 96 | |
| 97 | if (Diag.isDiagnosticInFlight()) |
| 98 | Diag.SetDelayedDiagnostic(diag::err_cannot_open_file, |
| 99 | Entry->getName(), ErrorStr); |
| 100 | else |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 101 | Diag.Report(FullSourceLoc(Loc, SM), diag::err_cannot_open_file) |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 102 | << Entry->getName() << ErrorStr; |
| 103 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 104 | Buffer.setInt(Buffer.getInt() | InvalidFlag); |
Daniel Dunbar | 0b3c773 | 2010-04-10 01:17:16 +0000 | [diff] [blame] | 105 | |
| 106 | // FIXME: This conditionalization is horrible, but we see spurious failures |
| 107 | // in the test suite due to this warning and no one has had time to hunt it |
| 108 | // down. So for now, we just don't emit this diagnostic on Win32, and hope |
| 109 | // nothing bad happens. |
| 110 | // |
| 111 | // PR6812. |
Douglas Gregor | 9f692a0 | 2010-04-09 15:54:22 +0000 | [diff] [blame] | 112 | #if !defined(LLVM_ON_WIN32) |
Daniel Dunbar | 0b3c773 | 2010-04-10 01:17:16 +0000 | [diff] [blame] | 113 | } else if (FileInfo.st_size != Entry->getSize() || |
| 114 | FileInfo.st_mtime != Entry->getModificationTime()) { |
Douglas Gregor | 9f692a0 | 2010-04-09 15:54:22 +0000 | [diff] [blame] | 115 | // Check that the file's size and modification time are the same |
| 116 | // as in the file entry (which may have come from a stat cache). |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 117 | if (Diag.isDiagnosticInFlight()) |
Daniel Dunbar | 0b3c773 | 2010-04-10 01:17:16 +0000 | [diff] [blame] | 118 | Diag.SetDelayedDiagnostic(diag::err_file_modified, |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 119 | Entry->getName()); |
Daniel Dunbar | 0b3c773 | 2010-04-10 01:17:16 +0000 | [diff] [blame] | 120 | else |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 121 | Diag.Report(FullSourceLoc(Loc, SM), diag::err_file_modified) |
| 122 | << Entry->getName(); |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 123 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 124 | Buffer.setInt(Buffer.getInt() | InvalidFlag); |
Daniel Dunbar | 0b3c773 | 2010-04-10 01:17:16 +0000 | [diff] [blame] | 125 | #endif |
Daniel Dunbar | 21a8bed | 2009-12-06 05:43:36 +0000 | [diff] [blame] | 126 | } |
Chris Lattner | 38caec4 | 2010-04-20 18:14:03 +0000 | [diff] [blame] | 127 | |
| 128 | // If the buffer is valid, check to see if it has a UTF Byte Order Mark |
| 129 | // (BOM). We only support UTF-8 without a BOM right now. See |
| 130 | // http://en.wikipedia.org/wiki/Byte_order_mark for more information. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 131 | if (!isBufferInvalid()) { |
Chris Lattner | 38caec4 | 2010-04-20 18:14:03 +0000 | [diff] [blame] | 132 | llvm::StringRef BufStr = Buffer.getPointer()->getBuffer(); |
| 133 | const char *BOM = 0; |
| 134 | if (BufStr.startswith("\xFE\xBB\xBF")) |
| 135 | BOM = "UTF-8"; |
| 136 | else if (BufStr.startswith("\xFE\xFF")) |
| 137 | BOM = "UTF-16 (BE)"; |
| 138 | else if (BufStr.startswith("\xFF\xFE")) |
| 139 | BOM = "UTF-16 (LE)"; |
| 140 | else if (BufStr.startswith(llvm::StringRef("\x00\x00\xFE\xFF", 4))) |
| 141 | BOM = "UTF-32 (BE)"; |
| 142 | else if (BufStr.startswith(llvm::StringRef("\xFF\xFE\x00\x00", 4))) |
| 143 | BOM = "UTF-32 (LE)"; |
| 144 | else if (BufStr.startswith("\x2B\x2F\x76")) |
| 145 | BOM = "UTF-7"; |
| 146 | else if (BufStr.startswith("\xF7\x64\x4C")) |
| 147 | BOM = "UTF-1"; |
| 148 | else if (BufStr.startswith("\xDD\x73\x66\x73")) |
| 149 | BOM = "UTF-EBCDIC"; |
| 150 | else if (BufStr.startswith("\x0E\xFE\xFF")) |
| 151 | BOM = "SDSU"; |
| 152 | else if (BufStr.startswith("\xFB\xEE\x28")) |
| 153 | BOM = "BOCU-1"; |
| 154 | else if (BufStr.startswith("\x84\x31\x95\x33")) |
| 155 | BOM = "BOCU-1"; |
| 156 | |
| 157 | if (BOM) { |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 158 | Diag.Report(FullSourceLoc(Loc, SM), diag::err_unsupported_bom) |
| 159 | << BOM << Entry->getName(); |
Chris Lattner | 38caec4 | 2010-04-20 18:14:03 +0000 | [diff] [blame] | 160 | Buffer.setInt(1); |
| 161 | } |
| 162 | } |
Ted Kremenek | 5b034ad | 2009-01-06 22:43:04 +0000 | [diff] [blame] | 163 | } |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 164 | |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 165 | if (Invalid) |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 166 | *Invalid = isBufferInvalid(); |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 167 | |
| 168 | return Buffer.getPointer(); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 171 | unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) { |
| 172 | // Look up the filename in the string table, returning the pre-existing value |
| 173 | // if it exists. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | llvm::StringMapEntry<unsigned> &Entry = |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 175 | FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U); |
| 176 | if (Entry.getValue() != ~0U) |
| 177 | return Entry.getValue(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 179 | // Otherwise, assign this the next available ID. |
| 180 | Entry.setValue(FilenamesByID.size()); |
| 181 | FilenamesByID.push_back(&Entry); |
| 182 | return FilenamesByID.size()-1; |
| 183 | } |
| 184 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 185 | /// AddLineNote - Add a line note to the line table that indicates that there |
| 186 | /// is a #line at the specified FID/Offset location which changes the presumed |
| 187 | /// location to LineNo/FilenameID. |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 188 | void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset, |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 189 | unsigned LineNo, int FilenameID) { |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 190 | std::vector<LineEntry> &Entries = LineEntries[FID]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 192 | assert((Entries.empty() || Entries.back().FileOffset < Offset) && |
| 193 | "Adding line entries out of order!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 195 | SrcMgr::CharacteristicKind Kind = SrcMgr::C_User; |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 196 | unsigned IncludeOffset = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 198 | if (!Entries.empty()) { |
| 199 | // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember |
| 200 | // that we are still in "foo.h". |
| 201 | if (FilenameID == -1) |
| 202 | FilenameID = Entries.back().FilenameID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 204 | // If we are after a line marker that switched us to system header mode, or |
| 205 | // that set #include information, preserve it. |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 206 | Kind = Entries.back().FileKind; |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 207 | IncludeOffset = Entries.back().IncludeOffset; |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 208 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 210 | Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind, |
| 211 | IncludeOffset)); |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 214 | /// AddLineNote This is the same as the previous version of AddLineNote, but is |
| 215 | /// used for GNU line markers. If EntryExit is 0, then this doesn't change the |
| 216 | /// presumed #include stack. If it is 1, this is a file entry, if it is 2 then |
| 217 | /// this is a file exit. FileKind specifies whether this is a system header or |
| 218 | /// extern C system header. |
| 219 | void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset, |
| 220 | unsigned LineNo, int FilenameID, |
| 221 | unsigned EntryExit, |
| 222 | SrcMgr::CharacteristicKind FileKind) { |
| 223 | assert(FilenameID != -1 && "Unspecified filename should use other accessor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 225 | std::vector<LineEntry> &Entries = LineEntries[FID]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 227 | assert((Entries.empty() || Entries.back().FileOffset < Offset) && |
| 228 | "Adding line entries out of order!"); |
| 229 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 230 | unsigned IncludeOffset = 0; |
| 231 | if (EntryExit == 0) { // No #include stack change. |
| 232 | IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset; |
| 233 | } else if (EntryExit == 1) { |
| 234 | IncludeOffset = Offset-1; |
| 235 | } else if (EntryExit == 2) { |
| 236 | assert(!Entries.empty() && Entries.back().IncludeOffset && |
| 237 | "PPDirectives should have caught case when popping empty include stack"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 239 | // Get the include loc of the last entries' include loc as our include loc. |
| 240 | IncludeOffset = 0; |
| 241 | if (const LineEntry *PrevEntry = |
| 242 | FindNearestLineEntry(FID, Entries.back().IncludeOffset)) |
| 243 | IncludeOffset = PrevEntry->IncludeOffset; |
| 244 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 246 | Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind, |
| 247 | IncludeOffset)); |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 251 | /// FindNearestLineEntry - Find the line entry nearest to FID that is before |
| 252 | /// it. If there is no line entry before Offset in FID, return null. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID, |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 254 | unsigned Offset) { |
| 255 | const std::vector<LineEntry> &Entries = LineEntries[FID]; |
| 256 | assert(!Entries.empty() && "No #line entries for this FID after all!"); |
| 257 | |
Chris Lattner | 6c1fbe0 | 2009-02-04 04:46:59 +0000 | [diff] [blame] | 258 | // It is very common for the query to be after the last #line, check this |
| 259 | // first. |
| 260 | if (Entries.back().FileOffset <= Offset) |
| 261 | return &Entries.back(); |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 6c1fbe0 | 2009-02-04 04:46:59 +0000 | [diff] [blame] | 263 | // Do a binary search to find the maximal element that is still before Offset. |
| 264 | std::vector<LineEntry>::const_iterator I = |
| 265 | std::upper_bound(Entries.begin(), Entries.end(), Offset); |
| 266 | if (I == Entries.begin()) return 0; |
| 267 | return &*--I; |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 269 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 270 | /// \brief Add a new line entry that has already been encoded into |
| 271 | /// the internal representation of the line table. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | void LineTableInfo::AddEntry(unsigned FID, |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 273 | const std::vector<LineEntry> &Entries) { |
| 274 | LineEntries[FID] = Entries; |
| 275 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 277 | /// getLineTableFilenameID - Return the uniqued ID for the specified filename. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | /// |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 279 | unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) { |
| 280 | if (LineTable == 0) |
| 281 | LineTable = new LineTableInfo(); |
| 282 | return LineTable->getLineTableFilenameID(Ptr, Len); |
| 283 | } |
| 284 | |
| 285 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 286 | /// AddLineNote - Add a line note to the line table for the FileID and offset |
| 287 | /// specified by Loc. If FilenameID is -1, it is considered to be |
| 288 | /// unspecified. |
| 289 | void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, |
| 290 | int FilenameID) { |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 291 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 293 | const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile(); |
| 294 | |
| 295 | // Remember that this file has #line directives now if it doesn't already. |
| 296 | const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 298 | if (LineTable == 0) |
| 299 | LineTable = new LineTableInfo(); |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 300 | LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID); |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 303 | /// AddLineNote - Add a GNU line marker to the line table. |
| 304 | void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, |
| 305 | int FilenameID, bool IsFileEntry, |
| 306 | bool IsFileExit, bool IsSystemHeader, |
| 307 | bool IsExternCHeader) { |
| 308 | // If there is no filename and no flags, this is treated just like a #line, |
| 309 | // which does not change the flags of the previous line marker. |
| 310 | if (FilenameID == -1) { |
| 311 | assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader && |
| 312 | "Can't set flags without setting the filename!"); |
| 313 | return AddLineNote(Loc, LineNo, FilenameID); |
| 314 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 316 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
| 317 | const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 319 | // Remember that this file has #line directives now if it doesn't already. |
| 320 | const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 322 | if (LineTable == 0) |
| 323 | LineTable = new LineTableInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 325 | SrcMgr::CharacteristicKind FileKind; |
| 326 | if (IsExternCHeader) |
| 327 | FileKind = SrcMgr::C_ExternCSystem; |
| 328 | else if (IsSystemHeader) |
| 329 | FileKind = SrcMgr::C_System; |
| 330 | else |
| 331 | FileKind = SrcMgr::C_User; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 333 | unsigned EntryExit = 0; |
| 334 | if (IsFileEntry) |
| 335 | EntryExit = 1; |
| 336 | else if (IsFileExit) |
| 337 | EntryExit = 2; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 339 | LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID, |
| 340 | EntryExit, FileKind); |
| 341 | } |
| 342 | |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 343 | LineTableInfo &SourceManager::getLineTable() { |
| 344 | if (LineTable == 0) |
| 345 | LineTable = new LineTableInfo(); |
| 346 | return *LineTable; |
| 347 | } |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 349 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 350 | // Private 'Create' methods. |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 351 | //===----------------------------------------------------------------------===// |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 353 | SourceManager::~SourceManager() { |
| 354 | delete LineTable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 356 | // Delete FileEntry objects corresponding to content caches. Since the actual |
| 357 | // content cache objects are bump pointer allocated, we just have to run the |
| 358 | // dtors, but we call the deallocate method for completeness. |
| 359 | for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) { |
| 360 | MemBufferInfos[i]->~ContentCache(); |
| 361 | ContentCacheAlloc.Deallocate(MemBufferInfos[i]); |
| 362 | } |
| 363 | for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator |
| 364 | I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { |
| 365 | I->second->~ContentCache(); |
| 366 | ContentCacheAlloc.Deallocate(I->second); |
| 367 | } |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void SourceManager::clearIDTables() { |
| 371 | MainFileID = FileID(); |
| 372 | SLocEntryTable.clear(); |
| 373 | LastLineNoFileIDQuery = FileID(); |
| 374 | LastLineNoContentCache = 0; |
| 375 | LastFileIDLookup = FileID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 377 | if (LineTable) |
| 378 | LineTable->clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 380 | // Use up FileID #0 as an invalid instantiation. |
| 381 | NextOffset = 0; |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 382 | createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 385 | /// getOrCreateContentCache - Create or return a cached ContentCache for the |
| 386 | /// specified file. |
| 387 | const ContentCache * |
| 388 | SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | assert(FileEnt && "Didn't specify a file entry to use?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 391 | // Do we already have information about this file? |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 392 | ContentCache *&Entry = FileInfos[FileEnt]; |
| 393 | if (Entry) return Entry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 395 | // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned |
| 396 | // so that FileInfo can use the low 3 bits of the pointer for its own |
| 397 | // nefarious purposes. |
| 398 | unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; |
| 399 | EntryAlign = std::max(8U, EntryAlign); |
| 400 | Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 401 | new (Entry) ContentCache(FileEnt); |
| 402 | return Entry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | |
Ted Kremenek | d1c0eee | 2007-10-31 17:53:38 +0000 | [diff] [blame] | 406 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 407 | /// memory buffer. This does no caching. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 408 | const ContentCache* |
| 409 | SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) { |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 410 | // Add a new ContentCache to the MemBufferInfos list and return it. Make sure |
| 411 | // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of |
| 412 | // the pointer for its own nefarious purposes. |
| 413 | unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; |
| 414 | EntryAlign = std::max(8U, EntryAlign); |
| 415 | ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 416 | new (Entry) ContentCache(); |
| 417 | MemBufferInfos.push_back(Entry); |
| 418 | Entry->setBuffer(Buffer); |
| 419 | return Entry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 422 | void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source, |
| 423 | unsigned NumSLocEntries, |
| 424 | unsigned NextOffset) { |
| 425 | ExternalSLocEntries = Source; |
| 426 | this->NextOffset = NextOffset; |
Sebastian Redl | b86238d | 2010-07-28 21:07:02 +0000 | [diff] [blame] | 427 | unsigned CurPrealloc = SLocEntryLoaded.size(); |
| 428 | // If we've ever preallocated, we must not count the dummy entry. |
| 429 | if (CurPrealloc) --CurPrealloc; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 430 | SLocEntryLoaded.resize(NumSLocEntries + 1); |
| 431 | SLocEntryLoaded[0] = true; |
Sebastian Redl | b86238d | 2010-07-28 21:07:02 +0000 | [diff] [blame] | 432 | SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries - CurPrealloc); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 435 | void SourceManager::ClearPreallocatedSLocEntries() { |
| 436 | unsigned I = 0; |
| 437 | for (unsigned N = SLocEntryLoaded.size(); I != N; ++I) |
| 438 | if (!SLocEntryLoaded[I]) |
| 439 | break; |
| 440 | |
| 441 | // We've already loaded all preallocated source location entries. |
| 442 | if (I == SLocEntryLoaded.size()) |
| 443 | return; |
| 444 | |
| 445 | // Remove everything from location I onward. |
| 446 | SLocEntryTable.resize(I); |
| 447 | SLocEntryLoaded.clear(); |
| 448 | ExternalSLocEntries = 0; |
| 449 | } |
| 450 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 451 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 452 | //===----------------------------------------------------------------------===// |
| 453 | // Methods to create new FileID's and instantiations. |
| 454 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 455 | |
Dan Gohman | 3f86b78 | 2010-08-26 21:27:06 +0000 | [diff] [blame] | 456 | /// createFileID - Create a new FileID for the specified ContentCache and |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 457 | /// include position. This works regardless of whether the ContentCache |
| 458 | /// corresponds to a file or some other input source. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 459 | FileID SourceManager::createFileID(const ContentCache *File, |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 460 | SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 461 | SrcMgr::CharacteristicKind FileCharacter, |
| 462 | unsigned PreallocatedID, |
| 463 | unsigned Offset) { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 464 | if (PreallocatedID) { |
| 465 | // If we're filling in a preallocated ID, just load in the file |
| 466 | // entry and return. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | assert(PreallocatedID < SLocEntryLoaded.size() && |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 468 | "Preallocate ID out-of-range"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | assert(!SLocEntryLoaded[PreallocatedID] && |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 470 | "Source location entry already loaded"); |
| 471 | assert(Offset && "Preallocate source location cannot have zero offset"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | SLocEntryTable[PreallocatedID] |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 473 | = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter)); |
| 474 | SLocEntryLoaded[PreallocatedID] = true; |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 475 | FileID FID = FileID::get(PreallocatedID); |
Douglas Gregor | 5de6572 | 2010-03-19 06:12:06 +0000 | [diff] [blame] | 476 | return FID; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | SLocEntryTable.push_back(SLocEntry::get(NextOffset, |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 480 | FileInfo::get(IncludePos, File, |
| 481 | FileCharacter))); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 482 | unsigned FileSize = File->getSize(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 483 | assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!"); |
| 484 | NextOffset += FileSize+1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 486 | // Set LastFileIDLookup to the newly created file. The next getFileID call is |
| 487 | // almost guaranteed to be from that file. |
Argyrios Kyrtzidis | ea703f1 | 2009-06-23 00:42:06 +0000 | [diff] [blame] | 488 | FileID FID = FileID::get(SLocEntryTable.size()-1); |
Argyrios Kyrtzidis | ea703f1 | 2009-06-23 00:42:06 +0000 | [diff] [blame] | 489 | return LastFileIDLookup = FID; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 492 | /// createInstantiationLoc - Return a new SourceLocation that encodes the fact |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 493 | /// that a token from SpellingLoc should actually be referenced from |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 494 | /// InstantiationLoc. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 495 | SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 496 | SourceLocation ILocStart, |
| 497 | SourceLocation ILocEnd, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 498 | unsigned TokLength, |
| 499 | unsigned PreallocatedID, |
| 500 | unsigned Offset) { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 501 | InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 502 | if (PreallocatedID) { |
| 503 | // If we're filling in a preallocated ID, just load in the |
| 504 | // instantiation entry and return. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | assert(PreallocatedID < SLocEntryLoaded.size() && |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 506 | "Preallocate ID out-of-range"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | assert(!SLocEntryLoaded[PreallocatedID] && |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 508 | "Source location entry already loaded"); |
| 509 | assert(Offset && "Preallocate source location cannot have zero offset"); |
| 510 | SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II); |
| 511 | SLocEntryLoaded[PreallocatedID] = true; |
| 512 | return SourceLocation::getMacroLoc(Offset); |
| 513 | } |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 514 | SLocEntryTable.push_back(SLocEntry::get(NextOffset, II)); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 515 | assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!"); |
| 516 | NextOffset += TokLength+1; |
| 517 | return SourceLocation::getMacroLoc(NextOffset-(TokLength+1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 520 | const llvm::MemoryBuffer * |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 521 | SourceManager::getMemoryBufferForFile(const FileEntry *File, |
| 522 | bool *Invalid) { |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 523 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(File); |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 524 | assert(IR && "getOrCreateContentCache() cannot return NULL"); |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 525 | return IR->getBuffer(Diag, *this, SourceLocation(), Invalid); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | bool SourceManager::overrideFileContents(const FileEntry *SourceFile, |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 529 | const llvm::MemoryBuffer *Buffer, |
| 530 | bool DoNotFree) { |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 531 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); |
| 532 | if (IR == 0) |
| 533 | return true; |
| 534 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 535 | const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 536 | return false; |
| 537 | } |
| 538 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 539 | llvm::StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const { |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 540 | bool MyInvalid = false; |
| 541 | const llvm::MemoryBuffer *Buf = getBuffer(FID, &MyInvalid); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 542 | if (Invalid) |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 543 | *Invalid = MyInvalid; |
| 544 | |
| 545 | if (MyInvalid) |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 546 | return ""; |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 547 | |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 548 | return Buf->getBuffer(); |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 549 | } |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 551 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 552 | // SourceLocation manipulation methods. |
Chris Lattner | 23b5dc6 | 2009-02-04 00:40:31 +0000 | [diff] [blame] | 553 | //===----------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 554 | |
| 555 | /// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot |
| 556 | /// method that is used for all SourceManager queries that start with a |
| 557 | /// SourceLocation object. It is responsible for finding the entry in |
| 558 | /// SLocEntryTable which contains the specified location. |
| 559 | /// |
| 560 | FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { |
| 561 | assert(SLocOffset && "Invalid FileID"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 563 | // After the first and second level caches, I see two common sorts of |
| 564 | // behavior: 1) a lot of searched FileID's are "near" the cached file location |
| 565 | // or are "near" the cached instantiation location. 2) others are just |
| 566 | // completely random and may be a very long way away. |
| 567 | // |
| 568 | // To handle this, we do a linear search for up to 8 steps to catch #1 quickly |
| 569 | // then we fall back to a less cache efficient, but more scalable, binary |
| 570 | // search to find the location. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 572 | // See if this is near the file point - worst case we start scanning from the |
| 573 | // most newly created FileID. |
| 574 | std::vector<SrcMgr::SLocEntry>::const_iterator I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 576 | if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { |
| 577 | // Neither loc prunes our search. |
| 578 | I = SLocEntryTable.end(); |
| 579 | } else { |
| 580 | // Perhaps it is near the file point. |
| 581 | I = SLocEntryTable.begin()+LastFileIDLookup.ID; |
| 582 | } |
| 583 | |
| 584 | // Find the FileID that contains this. "I" is an iterator that points to a |
| 585 | // FileID whose offset is known to be larger than SLocOffset. |
| 586 | unsigned NumProbes = 0; |
| 587 | while (1) { |
| 588 | --I; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 589 | if (ExternalSLocEntries) |
| 590 | getSLocEntry(FileID::get(I - SLocEntryTable.begin())); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 591 | if (I->getOffset() <= SLocOffset) { |
| 592 | #if 0 |
| 593 | printf("lin %d -> %d [%s] %d %d\n", SLocOffset, |
| 594 | I-SLocEntryTable.begin(), |
| 595 | I->isInstantiation() ? "inst" : "file", |
| 596 | LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); |
| 597 | #endif |
| 598 | FileID Res = FileID::get(I-SLocEntryTable.begin()); |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 599 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 600 | // If this isn't an instantiation, remember it. We have good locality |
| 601 | // across FileID lookups. |
| 602 | if (!I->isInstantiation()) |
| 603 | LastFileIDLookup = Res; |
| 604 | NumLinearScans += NumProbes+1; |
| 605 | return Res; |
| 606 | } |
| 607 | if (++NumProbes == 8) |
| 608 | break; |
| 609 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 611 | // Convert "I" back into an index. We know that it is an entry whose index is |
| 612 | // larger than the offset we are looking for. |
| 613 | unsigned GreaterIndex = I-SLocEntryTable.begin(); |
| 614 | // LessIndex - This is the lower bound of the range that we're searching. |
| 615 | // We know that the offset corresponding to the FileID is is less than |
| 616 | // SLocOffset. |
| 617 | unsigned LessIndex = 0; |
| 618 | NumProbes = 0; |
| 619 | while (1) { |
| 620 | unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 621 | unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 623 | ++NumProbes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 625 | // If the offset of the midpoint is too large, chop the high side of the |
| 626 | // range to the midpoint. |
| 627 | if (MidOffset > SLocOffset) { |
| 628 | GreaterIndex = MiddleIndex; |
| 629 | continue; |
| 630 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 632 | // If the middle index contains the value, succeed and return. |
| 633 | if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { |
| 634 | #if 0 |
| 635 | printf("bin %d -> %d [%s] %d %d\n", SLocOffset, |
| 636 | I-SLocEntryTable.begin(), |
| 637 | I->isInstantiation() ? "inst" : "file", |
| 638 | LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); |
| 639 | #endif |
| 640 | FileID Res = FileID::get(MiddleIndex); |
| 641 | |
| 642 | // If this isn't an instantiation, remember it. We have good locality |
| 643 | // across FileID lookups. |
| 644 | if (!I->isInstantiation()) |
| 645 | LastFileIDLookup = Res; |
| 646 | NumBinaryProbes += NumProbes; |
| 647 | return Res; |
| 648 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 650 | // Otherwise, move the low-side up to the middle index. |
| 651 | LessIndex = MiddleIndex; |
| 652 | } |
| 653 | } |
| 654 | |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 655 | SourceLocation SourceManager:: |
| 656 | getInstantiationLocSlowCase(SourceLocation Loc) const { |
| 657 | do { |
Chris Lattner | a5c6c58 | 2010-02-12 19:31:35 +0000 | [diff] [blame] | 658 | // Note: If Loc indicates an offset into a token that came from a macro |
| 659 | // expansion (e.g. the 5th character of the token) we do not want to add |
| 660 | // this offset when going to the instantiation location. The instatiation |
| 661 | // location is the macro invocation, which the offset has nothing to do |
| 662 | // with. This is unlike when we get the spelling loc, because the offset |
| 663 | // directly correspond to the token whose spelling we're inspecting. |
| 664 | Loc = getSLocEntry(getFileID(Loc)).getInstantiation() |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 665 | .getInstantiationLocStart(); |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 666 | } while (!Loc.isFileID()); |
| 667 | |
| 668 | return Loc; |
| 669 | } |
| 670 | |
| 671 | SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { |
| 672 | do { |
| 673 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
| 674 | Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); |
| 675 | Loc = Loc.getFileLocWithOffset(LocInfo.second); |
| 676 | } while (!Loc.isFileID()); |
| 677 | return Loc; |
| 678 | } |
| 679 | |
| 680 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 681 | std::pair<FileID, unsigned> |
| 682 | SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, |
| 683 | unsigned Offset) const { |
| 684 | // If this is an instantiation record, walk through all the instantiation |
| 685 | // points. |
| 686 | FileID FID; |
| 687 | SourceLocation Loc; |
| 688 | do { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 689 | Loc = E->getInstantiation().getInstantiationLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 691 | FID = getFileID(Loc); |
| 692 | E = &getSLocEntry(FID); |
| 693 | Offset += Loc.getOffset()-E->getOffset(); |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 694 | } while (!Loc.isFileID()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 696 | return std::make_pair(FID, Offset); |
| 697 | } |
| 698 | |
| 699 | std::pair<FileID, unsigned> |
| 700 | SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, |
| 701 | unsigned Offset) const { |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 702 | // If this is an instantiation record, walk through all the instantiation |
| 703 | // points. |
| 704 | FileID FID; |
| 705 | SourceLocation Loc; |
| 706 | do { |
| 707 | Loc = E->getInstantiation().getSpellingLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | |
Chris Lattner | bcd1a1b | 2009-01-26 19:41:58 +0000 | [diff] [blame] | 709 | FID = getFileID(Loc); |
| 710 | E = &getSLocEntry(FID); |
| 711 | Offset += Loc.getOffset()-E->getOffset(); |
| 712 | } while (!Loc.isFileID()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 714 | return std::make_pair(FID, Offset); |
| 715 | } |
| 716 | |
Chris Lattner | 387616e | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 717 | /// getImmediateSpellingLoc - Given a SourceLocation object, return the |
| 718 | /// spelling location referenced by the ID. This is the first level down |
| 719 | /// towards the place where the characters that make up the lexed token can be |
| 720 | /// found. This should not generally be used by clients. |
| 721 | SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{ |
| 722 | if (Loc.isFileID()) return Loc; |
| 723 | std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); |
| 724 | Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); |
| 725 | return Loc.getFileLocWithOffset(LocInfo.second); |
| 726 | } |
| 727 | |
| 728 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 729 | /// getImmediateInstantiationRange - Loc is required to be an instantiation |
| 730 | /// location. Return the start/end of the instantiation information. |
| 731 | std::pair<SourceLocation,SourceLocation> |
| 732 | SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const { |
| 733 | assert(Loc.isMacroID() && "Not an instantiation loc!"); |
| 734 | const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation(); |
| 735 | return II.getInstantiationLocRange(); |
| 736 | } |
| 737 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 738 | /// getInstantiationRange - Given a SourceLocation object, return the |
| 739 | /// range of tokens covered by the instantiation in the ultimate file. |
| 740 | std::pair<SourceLocation,SourceLocation> |
| 741 | SourceManager::getInstantiationRange(SourceLocation Loc) const { |
| 742 | if (Loc.isFileID()) return std::make_pair(Loc, Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 744 | std::pair<SourceLocation,SourceLocation> Res = |
| 745 | getImmediateInstantiationRange(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 747 | // Fully resolve the start and end locations to their ultimate instantiation |
| 748 | // points. |
| 749 | while (!Res.first.isFileID()) |
| 750 | Res.first = getImmediateInstantiationRange(Res.first).first; |
| 751 | while (!Res.second.isFileID()) |
| 752 | Res.second = getImmediateInstantiationRange(Res.second).second; |
| 753 | return Res; |
| 754 | } |
| 755 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 756 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 757 | |
| 758 | //===----------------------------------------------------------------------===// |
| 759 | // Queries about the code at a SourceLocation. |
| 760 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 761 | |
| 762 | /// getCharacterData - Return a pointer to the start of the specified location |
| 763 | /// in the appropriate MemoryBuffer. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 764 | const char *SourceManager::getCharacterData(SourceLocation SL, |
| 765 | bool *Invalid) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 766 | // Note that this is a hot function in the getSpelling() path, which is |
| 767 | // heavily used by -E mode. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 768 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 770 | // Note that calling 'getBuffer()' may lazily page in a source file. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 771 | bool CharDataInvalid = false; |
| 772 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 773 | = getSLocEntry(LocInfo.first).getFile().getContentCache() |
| 774 | ->getBuffer(Diag, *this, SourceLocation(), &CharDataInvalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 775 | if (Invalid) |
| 776 | *Invalid = CharDataInvalid; |
| 777 | return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 780 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 781 | /// getColumnNumber - Return the column # for the specified file position. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 782 | /// this is significantly cheaper to compute than the line number. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 783 | unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos, |
| 784 | bool *Invalid) const { |
| 785 | bool MyInvalid = false; |
| 786 | const char *Buf = getBuffer(FID, &MyInvalid)->getBufferStart(); |
| 787 | if (Invalid) |
| 788 | *Invalid = MyInvalid; |
| 789 | |
| 790 | if (MyInvalid) |
| 791 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 793 | unsigned LineStart = FilePos; |
| 794 | while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') |
| 795 | --LineStart; |
| 796 | return FilePos-LineStart+1; |
| 797 | } |
| 798 | |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame^] | 799 | // isInvalid - Return the result of calling loc.isInvalid(), and |
| 800 | // if Invalid is not null, set its value to same. |
| 801 | static bool isInvalid(SourceLocation Loc, bool *Invalid) { |
| 802 | bool MyInvalid = Loc.isInvalid(); |
| 803 | if (Invalid) |
| 804 | *Invalid = MyInvalid; |
| 805 | return MyInvalid; |
| 806 | } |
| 807 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 808 | unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc, |
| 809 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame^] | 810 | if (isInvalid(Loc, Invalid)) return 0; |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 811 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 812 | return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 815 | unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc, |
| 816 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame^] | 817 | if (isInvalid(Loc, Invalid)) return 0; |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 818 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 819 | return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 822 | static DISABLE_INLINE void |
| 823 | ComputeLineNumbers(Diagnostic &Diag, ContentCache *FI, |
| 824 | llvm::BumpPtrAllocator &Alloc, |
| 825 | const SourceManager &SM, bool &Invalid); |
| 826 | static void ComputeLineNumbers(Diagnostic &Diag, ContentCache *FI, |
| 827 | llvm::BumpPtrAllocator &Alloc, |
| 828 | const SourceManager &SM, bool &Invalid) { |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 829 | // Note that calling 'getBuffer()' may lazily page in the file. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 830 | const MemoryBuffer *Buffer = FI->getBuffer(Diag, SM, SourceLocation(), |
| 831 | &Invalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 832 | if (Invalid) |
| 833 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 835 | // Find the file offsets of all of the *physical* source lines. This does |
| 836 | // not look at trigraphs, escaped newlines, or anything else tricky. |
| 837 | std::vector<unsigned> LineOffsets; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 839 | // Line #1 starts at char 0. |
| 840 | LineOffsets.push_back(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 842 | const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); |
| 843 | const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); |
| 844 | unsigned Offs = 0; |
| 845 | while (1) { |
| 846 | // Skip over the contents of the line. |
| 847 | // TODO: Vectorize this? This is very performance sensitive for programs |
| 848 | // with lots of diagnostics and in -E mode. |
| 849 | const unsigned char *NextBuf = (const unsigned char *)Buf; |
| 850 | while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') |
| 851 | ++NextBuf; |
| 852 | Offs += NextBuf-Buf; |
| 853 | Buf = NextBuf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 855 | if (Buf[0] == '\n' || Buf[0] == '\r') { |
| 856 | // If this is \n\r or \r\n, skip both characters. |
| 857 | if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) |
| 858 | ++Offs, ++Buf; |
| 859 | ++Offs, ++Buf; |
| 860 | LineOffsets.push_back(Offs); |
| 861 | } else { |
| 862 | // Otherwise, this is a null. If end of file, exit. |
| 863 | if (Buf == End) break; |
| 864 | // Otherwise, skip the null. |
| 865 | ++Offs, ++Buf; |
| 866 | } |
| 867 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 869 | // Copy the offsets into the FileInfo structure. |
| 870 | FI->NumLines = LineOffsets.size(); |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 871 | FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size()); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 872 | std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); |
| 873 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 874 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 875 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 876 | /// for the position indicated. This requires building and caching a table of |
| 877 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 878 | /// about to emit a diagnostic. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 879 | unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos, |
| 880 | bool *Invalid) const { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 881 | ContentCache *Content; |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 882 | if (LastLineNoFileIDQuery == FID) |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 883 | Content = LastLineNoContentCache; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 884 | else |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 885 | Content = const_cast<ContentCache*>(getSLocEntry(FID) |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 886 | .getFile().getContentCache()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 888 | // 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] | 889 | /// SourceLineCache for it on demand. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 890 | if (Content->SourceLineCache == 0) { |
| 891 | bool MyInvalid = false; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 892 | ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 893 | if (Invalid) |
| 894 | *Invalid = MyInvalid; |
| 895 | if (MyInvalid) |
| 896 | return 1; |
| 897 | } else if (Invalid) |
| 898 | *Invalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 899 | |
| 900 | // Okay, we know we have a line number table. Do a binary search to find the |
| 901 | // line number that this character position lands on. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 902 | unsigned *SourceLineCache = Content->SourceLineCache; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 903 | unsigned *SourceLineCacheStart = SourceLineCache; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 904 | unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 906 | unsigned QueriedFilePos = FilePos+1; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 907 | |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 908 | // 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] | 909 | // complicated as it is, binary search isn't that slow. |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 910 | // |
| 911 | // If it is worth being optimized, then in my opinion it could be more |
| 912 | // performant, simpler, and more obviously correct by just "galloping" outward |
| 913 | // from the queried file position. In fact, this could be incorporated into a |
| 914 | // generic algorithm such as lower_bound_with_hint. |
| 915 | // |
| 916 | // If someone gives me a test case where this matters, and I will do it! - DWD |
| 917 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 918 | // If the previous query was to the same file, we know both the file pos from |
| 919 | // that query and the line number returned. This allows us to narrow the |
| 920 | // search space from the entire file to something near the match. |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 921 | if (LastLineNoFileIDQuery == FID) { |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 922 | if (QueriedFilePos >= LastLineNoFilePos) { |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 923 | // FIXME: Potential overflow? |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 924 | SourceLineCache = SourceLineCache+LastLineNoResult-1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 926 | // The query is likely to be nearby the previous one. Here we check to |
| 927 | // see if it is within 5, 10 or 20 lines. It can be far away in cases |
| 928 | // where big comment blocks and vertical whitespace eat up lines but |
| 929 | // contribute no tokens. |
| 930 | if (SourceLineCache+5 < SourceLineCacheEnd) { |
| 931 | if (SourceLineCache[5] > QueriedFilePos) |
| 932 | SourceLineCacheEnd = SourceLineCache+5; |
| 933 | else if (SourceLineCache+10 < SourceLineCacheEnd) { |
| 934 | if (SourceLineCache[10] > QueriedFilePos) |
| 935 | SourceLineCacheEnd = SourceLineCache+10; |
| 936 | else if (SourceLineCache+20 < SourceLineCacheEnd) { |
| 937 | if (SourceLineCache[20] > QueriedFilePos) |
| 938 | SourceLineCacheEnd = SourceLineCache+20; |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | } else { |
Daniel Dunbar | 4106d69 | 2009-05-18 17:30:52 +0000 | [diff] [blame] | 943 | if (LastLineNoResult < Content->NumLines) |
| 944 | SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 948 | // If the spread is large, do a "radix" test as our initial guess, based on |
| 949 | // the assumption that lines average to approximately the same length. |
| 950 | // NOTE: This is currently disabled, as it does not appear to be profitable in |
| 951 | // initial measurements. |
| 952 | if (0 && SourceLineCacheEnd-SourceLineCache > 20) { |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 953 | unsigned FileLen = Content->SourceLineCache[Content->NumLines-1]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 955 | // Take a stab at guessing where it is. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 956 | unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 958 | // Check for -10 and +10 lines. |
| 959 | unsigned LowerBound = std::max(int(ApproxPos-10), 0); |
| 960 | unsigned UpperBound = std::min(ApproxPos+10, FileLen); |
| 961 | |
| 962 | // If the computed lower bound is less than the query location, move it in. |
| 963 | if (SourceLineCache < SourceLineCacheStart+LowerBound && |
| 964 | SourceLineCacheStart[LowerBound] < QueriedFilePos) |
| 965 | SourceLineCache = SourceLineCacheStart+LowerBound; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 967 | // If the computed upper bound is greater than the query location, move it. |
| 968 | if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound && |
| 969 | SourceLineCacheStart[UpperBound] >= QueriedFilePos) |
| 970 | SourceLineCacheEnd = SourceLineCacheStart+UpperBound; |
| 971 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Chris Lattner | 1cf12bf | 2007-07-24 06:43:46 +0000 | [diff] [blame] | 973 | unsigned *Pos |
| 974 | = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 975 | unsigned LineNo = Pos-SourceLineCacheStart; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 977 | LastLineNoFileIDQuery = FID; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 978 | LastLineNoContentCache = Content; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 979 | LastLineNoFilePos = QueriedFilePos; |
| 980 | LastLineNoResult = LineNo; |
| 981 | return LineNo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 984 | unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc, |
| 985 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame^] | 986 | if (isInvalid(Loc, Invalid)) return 0; |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 987 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
| 988 | return getLineNumber(LocInfo.first, LocInfo.second); |
| 989 | } |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 990 | unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc, |
| 991 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame^] | 992 | if (isInvalid(Loc, Invalid)) return 0; |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 993 | std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); |
| 994 | return getLineNumber(LocInfo.first, LocInfo.second); |
| 995 | } |
| 996 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 997 | /// getFileCharacteristic - return the file characteristic of the specified |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 | /// source location, indicating whether this is a normal file, a system |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 999 | /// header, or an "implicit extern C" system header. |
| 1000 | /// |
| 1001 | /// This state can be modified with flags on GNU linemarker directives like: |
| 1002 | /// # 4 "foo.h" 3 |
| 1003 | /// which changes all source locations in the current file after that to be |
| 1004 | /// considered to be from a system header. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | SrcMgr::CharacteristicKind |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1006 | SourceManager::getFileCharacteristic(SourceLocation Loc) const { |
| 1007 | assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!"); |
| 1008 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
| 1009 | const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile(); |
| 1010 | |
| 1011 | // If there are no #line directives in this file, just return the whole-file |
| 1012 | // state. |
| 1013 | if (!FI.hasLineDirectives()) |
| 1014 | return FI.getFileCharacteristic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1016 | assert(LineTable && "Can't have linetable entries without a LineTable!"); |
| 1017 | // See if there is a #line directive before the location. |
| 1018 | const LineEntry *Entry = |
| 1019 | LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 1021 | // If this is before the first line marker, use the file characteristic. |
| 1022 | if (!Entry) |
| 1023 | return FI.getFileCharacteristic(); |
| 1024 | |
| 1025 | return Entry->FileKind; |
| 1026 | } |
| 1027 | |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 1028 | /// Return the filename or buffer identifier of the buffer the location is in. |
| 1029 | /// Note that this name does not respect #line directives. Use getPresumedLoc |
| 1030 | /// for normal clients. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1031 | const char *SourceManager::getBufferName(SourceLocation Loc, |
| 1032 | bool *Invalid) const { |
Zhanyong Wan | 1f24e11 | 2010-10-05 17:56:33 +0000 | [diff] [blame^] | 1033 | if (isInvalid(Loc, Invalid)) return "<invalid loc>"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1035 | return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier(); |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1038 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1039 | /// getPresumedLoc - This method returns the "presumed" location of a |
| 1040 | /// SourceLocation specifies. A "presumed location" can be modified by #line |
| 1041 | /// or GNU line marker directives. This provides a view on the data that a |
| 1042 | /// user should see in diagnostics, for example. |
| 1043 | /// |
| 1044 | /// Note that a presumed location is always given as the instantiation point |
| 1045 | /// of an instantiation location, not at the spelling location. |
| 1046 | PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { |
| 1047 | if (Loc.isInvalid()) return PresumedLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1049 | // Presumed locations are always for instantiation points. |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 1050 | std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 1052 | const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile(); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 1053 | const SrcMgr::ContentCache *C = FI.getContentCache(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1055 | // To get the source name, first consult the FileEntry (if one exists) |
| 1056 | // before the MemBuffer as this will avoid unnecessarily paging in the |
| 1057 | // MemBuffer. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1058 | const char *Filename; |
| 1059 | if (C->Entry) |
| 1060 | Filename = C->Entry->getName(); |
| 1061 | else |
| 1062 | Filename = C->getBuffer(Diag, *this)->getBufferIdentifier(); |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1063 | unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second); |
| 1064 | unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second); |
| 1065 | SourceLocation IncludeLoc = FI.getIncludeLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1067 | // If we have #line directives in this file, update and overwrite the physical |
| 1068 | // location info if appropriate. |
| 1069 | if (FI.hasLineDirectives()) { |
| 1070 | assert(LineTable && "Can't have linetable entries without a LineTable!"); |
| 1071 | // See if there is a #line directive before this. If so, get it. |
| 1072 | if (const LineEntry *Entry = |
| 1073 | LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) { |
Chris Lattner | fc39133 | 2009-02-04 02:00:59 +0000 | [diff] [blame] | 1074 | // If the LineEntry indicates a filename, use it. |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1075 | if (Entry->FilenameID != -1) |
| 1076 | Filename = LineTable->getFilename(Entry->FilenameID); |
Chris Lattner | fc39133 | 2009-02-04 02:00:59 +0000 | [diff] [blame] | 1077 | |
| 1078 | // Use the line number specified by the LineEntry. This line number may |
| 1079 | // be multiple lines down from the line entry. Add the difference in |
| 1080 | // physical line numbers from the query point and the line marker to the |
| 1081 | // total. |
| 1082 | unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset); |
| 1083 | LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | |
Chris Lattner | 0e0e5da | 2009-02-04 02:15:40 +0000 | [diff] [blame] | 1085 | // Note that column numbers are not molested by line markers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 1087 | // Handle virtual #include manipulation. |
| 1088 | if (Entry->IncludeOffset) { |
| 1089 | IncludeLoc = getLocForStartOfFile(LocInfo.first); |
| 1090 | IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset); |
| 1091 | } |
Chris Lattner | 3cd949c | 2009-02-04 01:55:42 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | //===----------------------------------------------------------------------===// |
| 1099 | // Other miscellaneous methods. |
| 1100 | //===----------------------------------------------------------------------===// |
| 1101 | |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1102 | /// \brief Get the source location for the given file:line:col triplet. |
| 1103 | /// |
| 1104 | /// If the source file is included multiple times, the source location will |
| 1105 | /// be based upon the first inclusion. |
| 1106 | SourceLocation SourceManager::getLocation(const FileEntry *SourceFile, |
| 1107 | unsigned Line, unsigned Col) const { |
| 1108 | assert(SourceFile && "Null source file!"); |
| 1109 | assert(Line && Col && "Line and column should start from 1!"); |
| 1110 | |
| 1111 | fileinfo_iterator FI = FileInfos.find(SourceFile); |
| 1112 | if (FI == FileInfos.end()) |
| 1113 | return SourceLocation(); |
| 1114 | ContentCache *Content = FI->second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1116 | // If this is the first use of line information for this buffer, compute the |
| 1117 | /// SourceLineCache for it on demand. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1118 | if (Content->SourceLineCache == 0) { |
| 1119 | bool MyInvalid = false; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1120 | ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 1121 | if (MyInvalid) |
| 1122 | return SourceLocation(); |
| 1123 | } |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1124 | |
Douglas Gregor | 4a160e1 | 2009-12-02 05:34:39 +0000 | [diff] [blame] | 1125 | // Find the first file ID that corresponds to the given file. |
| 1126 | FileID FirstFID; |
| 1127 | |
| 1128 | // First, check the main file ID, since it is common to look for a |
| 1129 | // location in the main file. |
| 1130 | if (!MainFileID.isInvalid()) { |
| 1131 | const SLocEntry &MainSLoc = getSLocEntry(MainFileID); |
| 1132 | if (MainSLoc.isFile() && MainSLoc.getFile().getContentCache() == Content) |
| 1133 | FirstFID = MainFileID; |
| 1134 | } |
| 1135 | |
| 1136 | if (FirstFID.isInvalid()) { |
| 1137 | // The location we're looking for isn't in the main file; look |
| 1138 | // through all of the source locations. |
| 1139 | for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) { |
| 1140 | const SLocEntry &SLoc = getSLocEntry(I); |
| 1141 | if (SLoc.isFile() && SLoc.getFile().getContentCache() == Content) { |
| 1142 | FirstFID = FileID::get(I); |
| 1143 | break; |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | if (FirstFID.isInvalid()) |
| 1149 | return SourceLocation(); |
| 1150 | |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1151 | if (Line > Content->NumLines) { |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1152 | unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize(); |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1153 | if (Size > 0) |
| 1154 | --Size; |
| 1155 | return getLocForStartOfFile(FirstFID).getFileLocWithOffset(Size); |
| 1156 | } |
| 1157 | |
| 1158 | unsigned FilePos = Content->SourceLineCache[Line - 1]; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1159 | const char *Buf = Content->getBuffer(Diag, *this)->getBufferStart() + FilePos; |
| 1160 | unsigned BufLength = Content->getBuffer(Diag, *this)->getBufferEnd() - Buf; |
Douglas Gregor | d1eabfb | 2010-02-27 02:42:25 +0000 | [diff] [blame] | 1161 | unsigned i = 0; |
| 1162 | |
| 1163 | // Check that the given column is valid. |
| 1164 | while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r') |
| 1165 | ++i; |
| 1166 | if (i < Col-1) |
| 1167 | return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + i); |
| 1168 | |
Douglas Gregor | 4a160e1 | 2009-12-02 05:34:39 +0000 | [diff] [blame] | 1169 | return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + Col - 1); |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1172 | /// Given a decomposed source location, move it up the include/instantiation |
| 1173 | /// stack to the parent source location. If this is possible, return the |
| 1174 | /// decomposed version of the parent in Loc and return false. If Loc is the |
| 1175 | /// top-level entry, return true and don't modify it. |
| 1176 | static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc, |
| 1177 | const SourceManager &SM) { |
| 1178 | SourceLocation UpperLoc; |
| 1179 | const SrcMgr::SLocEntry &Entry = SM.getSLocEntry(Loc.first); |
| 1180 | if (Entry.isInstantiation()) |
| 1181 | UpperLoc = Entry.getInstantiation().getInstantiationLocStart(); |
| 1182 | else |
| 1183 | UpperLoc = Entry.getFile().getIncludeLoc(); |
| 1184 | |
| 1185 | if (UpperLoc.isInvalid()) |
| 1186 | return true; // We reached the top. |
| 1187 | |
| 1188 | Loc = SM.getDecomposedLoc(UpperLoc); |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
| 1192 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1193 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 1194 | /// |
| 1195 | /// \returns true if LHS source location comes before RHS, false otherwise. |
| 1196 | bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS, |
| 1197 | SourceLocation RHS) const { |
| 1198 | assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!"); |
| 1199 | if (LHS == RHS) |
| 1200 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1202 | std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS); |
| 1203 | std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1205 | // If the source locations are in the same file, just compare offsets. |
| 1206 | if (LOffs.first == ROffs.first) |
| 1207 | return LOffs.second < ROffs.second; |
| 1208 | |
| 1209 | // If we are comparing a source location with multiple locations in the same |
| 1210 | // file, we get a big win by caching the result. |
Chris Lattner | 66a915f | 2010-05-07 05:10:46 +0000 | [diff] [blame] | 1211 | if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first)) |
| 1212 | return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 1214 | // Okay, we missed in the cache, start updating the cache for this query. |
| 1215 | IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1217 | // "Traverse" the include/instantiation stacks of both locations and try to |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1218 | // find a common "ancestor". FileIDs build a tree-like structure that |
| 1219 | // reflects the #include hierarchy, and this algorithm needs to find the |
| 1220 | // nearest common ancestor between the two locations. For example, if you |
| 1221 | // have a.c that includes b.h and c.h, and are comparing a location in b.h to |
| 1222 | // a location in c.h, we need to find that their nearest common ancestor is |
| 1223 | // a.c, and compare the locations of the two #includes to find their relative |
| 1224 | // ordering. |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1225 | // |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1226 | // SourceManager assigns FileIDs in order of parsing. This means that an |
| 1227 | // includee always has a larger FileID than an includer. While you might |
| 1228 | // think that we could just compare the FileID's here, that doesn't work to |
| 1229 | // compare a point at the end of a.c with a point within c.h. Though c.h has |
| 1230 | // a larger FileID, we have to compare the include point of c.h to the |
| 1231 | // location in a.c. |
| 1232 | // |
| 1233 | // Despite not being able to directly compare FileID's, we can tell that a |
| 1234 | // larger FileID is necessarily more deeply nested than a lower one and use |
| 1235 | // this information to walk up the tree to the nearest common ancestor. |
| 1236 | do { |
| 1237 | // If LOffs is larger than ROffs, then LOffs must be more deeply nested than |
| 1238 | // ROffs, walk up the #include chain. |
| 1239 | if (LOffs.first.ID > ROffs.first.ID) { |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1240 | if (MoveUpIncludeHierarchy(LOffs, *this)) |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1241 | break; // We reached the top. |
| 1242 | |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1243 | } else { |
| 1244 | // Otherwise, ROffs is larger than LOffs, so ROffs must be more deeply |
| 1245 | // nested than LOffs, walk up the #include chain. |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1246 | if (MoveUpIncludeHierarchy(ROffs, *this)) |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1247 | break; // We reached the top. |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 1248 | } |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1249 | } while (LOffs.first != ROffs.first); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1250 | |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1251 | // If we exited because we found a nearest common ancestor, compare the |
| 1252 | // locations within the common file and cache them. |
| 1253 | if (LOffs.first == ROffs.first) { |
| 1254 | IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second); |
| 1255 | return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second); |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1256 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
Daniel Dunbar | fbcc7be | 2009-12-01 23:07:57 +0000 | [diff] [blame] | 1258 | // There is no common ancestor, most probably because one location is in the |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 1259 | // predefines buffer or an AST file. |
Daniel Dunbar | fbcc7be | 2009-12-01 23:07:57 +0000 | [diff] [blame] | 1260 | // FIXME: We should rearrange the external interface so this simply never |
| 1261 | // happens; it can't conceptually happen. Also see PR5662. |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1262 | IsBeforeInTUCache.setQueryFIDs(FileID(), FileID()); // Don't try caching. |
| 1263 | |
| 1264 | // Zip both entries up to the top level record. |
| 1265 | while (!MoveUpIncludeHierarchy(LOffs, *this)) /*empty*/; |
| 1266 | while (!MoveUpIncludeHierarchy(ROffs, *this)) /*empty*/; |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1267 | |
Daniel Dunbar | fbcc7be | 2009-12-01 23:07:57 +0000 | [diff] [blame] | 1268 | // If exactly one location is a memory buffer, assume it preceeds the other. |
Chris Lattner | d3b8cc2 | 2010-05-07 20:35:24 +0000 | [diff] [blame] | 1269 | |
| 1270 | // Strip off macro instantation locations, going up to the top-level File |
| 1271 | // SLocEntry. |
| 1272 | bool LIsMB = getFileEntryForID(LOffs.first) == 0; |
| 1273 | bool RIsMB = getFileEntryForID(ROffs.first) == 0; |
Chris Lattner | 48296ba | 2010-05-07 05:51:13 +0000 | [diff] [blame] | 1274 | if (LIsMB != RIsMB) |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 1275 | return LIsMB; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | |
Daniel Dunbar | fbcc7be | 2009-12-01 23:07:57 +0000 | [diff] [blame] | 1277 | // Otherwise, just assume FileIDs were created in order. |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 1278 | return LOffs.first < ROffs.first; |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 1279 | } |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1280 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1281 | /// PrintStats - Print statistics to stderr. |
| 1282 | /// |
| 1283 | void SourceManager::PrintStats() const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 1284 | llvm::errs() << "\n*** Source Manager Stats:\n"; |
| 1285 | llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size() |
| 1286 | << " mem buffers mapped.\n"; |
| 1287 | llvm::errs() << SLocEntryTable.size() << " SLocEntry's allocated, " |
| 1288 | << NextOffset << "B of Sloc address space used.\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1290 | unsigned NumLineNumsComputed = 0; |
| 1291 | unsigned NumFileBytesMapped = 0; |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 1292 | for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){ |
| 1293 | NumLineNumsComputed += I->second->SourceLineCache != 0; |
| 1294 | NumFileBytesMapped += I->second->getSizeBytesMapped(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1295 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 1297 | llvm::errs() << NumFileBytesMapped << " bytes of files mapped, " |
| 1298 | << NumLineNumsComputed << " files with line #'s computed.\n"; |
| 1299 | llvm::errs() << "FileID scans: " << NumLinearScans << " linear, " |
| 1300 | << NumBinaryProbes << " binary.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1301 | } |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1302 | |
| 1303 | ExternalSLocEntrySource::~ExternalSLocEntrySource() { } |