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