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