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