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