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