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