blob: 1eed0bc14320a82da9b585890e65975f4d016441 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SourceManager.cpp - Track and cache source files -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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 Lattner5e36a7a2007-07-24 05:57:19 +000016#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/System/Path.h"
Ted Kremenek78d85f52007-10-30 21:08:08 +000019#include "llvm/Bitcode/Serialize.h"
20#include "llvm/Bitcode/Deserialize.h"
Ted Kremenek665dd4a2007-12-05 22:21:13 +000021#include "llvm/Support/Streams.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24using namespace SrcMgr;
25using llvm::MemoryBuffer;
26
Ted Kremenek78d85f52007-10-30 21:08:08 +000027ContentCache::~ContentCache() {
28 delete Buffer;
29 delete [] SourceLineCache;
Reid Spencer5f016e22007-07-11 17:01:13 +000030}
31
Reid Spencer5f016e22007-07-11 17:01:13 +000032/// getFileInfo - Create or return a cached FileInfo for the specified file.
33///
Ted Kremenek78d85f52007-10-30 21:08:08 +000034const ContentCache* SourceManager::getContentCache(const FileEntry *FileEnt) {
35
Reid Spencer5f016e22007-07-11 17:01:13 +000036 assert(FileEnt && "Didn't specify a file entry to use?");
37 // Do we already have information about this file?
Ted Kremenek78d85f52007-10-30 21:08:08 +000038 std::set<ContentCache>::iterator I =
39 FileInfos.lower_bound(ContentCache(FileEnt));
40
41 if (I != FileInfos.end() && I->Entry == FileEnt)
Reid Spencer5f016e22007-07-11 17:01:13 +000042 return &*I;
43
44 // Nope, get information.
Chris Lattner3c1f7b62008-04-01 06:06:37 +000045 const MemoryBuffer *File =
Chris Lattner35de5122008-04-01 18:04:30 +000046 MemoryBuffer::getFile(FileEnt->getName(), 0, FileEnt->getSize());
Reid Spencer5f016e22007-07-11 17:01:13 +000047 if (File == 0)
48 return 0;
49
Ted Kremenek78d85f52007-10-30 21:08:08 +000050 ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt));
Reid Spencer5f016e22007-07-11 17:01:13 +000051
Ted Kremenek78d85f52007-10-30 21:08:08 +000052 Entry.Buffer = File;
53 Entry.SourceLineCache = 0;
54 Entry.NumLines = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000055 return &Entry;
56}
57
58
Ted Kremenekd1c0eee2007-10-31 17:53:38 +000059/// createMemBufferContentCache - Create a new ContentCache for the specified
60/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +000061const ContentCache*
62SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Ted Kremenek0d892d82007-10-30 22:57:35 +000063 // Add a new ContentCache to the MemBufferInfos list and return it. We
64 // must default construct the object first that the instance actually
65 // stored within MemBufferInfos actually owns the Buffer, and not any
66 // temporary we would use in the call to "push_back".
Ted Kremenek78d85f52007-10-30 21:08:08 +000067 MemBufferInfos.push_back(ContentCache());
68 ContentCache& Entry = const_cast<ContentCache&>(MemBufferInfos.back());
69 Entry.Buffer = Buffer;
70 return &Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +000071}
72
73
Nico Weber48002c82008-09-29 00:25:48 +000074/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +000075/// include position. This works regardless of whether the ContentCache
76/// corresponds to a file or some other input source.
Ted Kremenek78d85f52007-10-30 21:08:08 +000077unsigned SourceManager::createFileID(const ContentCache *File,
Nico Weber7bfaaae2008-08-10 19:59:06 +000078 SourceLocation IncludePos,
Chris Lattner0b9e7362008-09-26 21:18:42 +000079 SrcMgr::Characteristic_t FileCharacter) {
Reid Spencer5f016e22007-07-11 17:01:13 +000080 // If FileEnt is really large (e.g. it's a large .i file), we may not be able
81 // to fit an arbitrary position in the file in the FilePos field. To handle
82 // this, we create one FileID for each chunk of the file that fits in a
83 // FilePos field.
Ted Kremenek78d85f52007-10-30 21:08:08 +000084 unsigned FileSize = File->Buffer->getBufferSize();
Reid Spencer5f016e22007-07-11 17:01:13 +000085 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) {
Chris Lattner0b9e7362008-09-26 21:18:42 +000086 FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File, FileCharacter));
Reid Spencer5f016e22007-07-11 17:01:13 +000087 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
88 "Ran out of file ID's!");
89 return FileIDs.size();
90 }
91
92 // Create one FileID for each chunk of the file.
93 unsigned Result = FileIDs.size()+1;
94
95 unsigned ChunkNo = 0;
96 while (1) {
Nico Weber7bfaaae2008-08-10 19:59:06 +000097 FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File,
Chris Lattner0b9e7362008-09-26 21:18:42 +000098 FileCharacter));
Reid Spencer5f016e22007-07-11 17:01:13 +000099
100 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break;
101 FileSize -= (1 << SourceLocation::FilePosBits);
102 }
103
104 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
105 "Ran out of file ID's!");
106 return Result;
107}
108
109/// getInstantiationLoc - Return a new SourceLocation that encodes the fact
110/// that a token from physloc PhysLoc should actually be referenced from
111/// InstantiationLoc.
Chris Lattner31bb8be2007-07-20 18:00:12 +0000112SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 SourceLocation InstantLoc) {
Chris Lattnerabca2bb2007-07-15 06:35:27 +0000114 // The specified source location may be a mapped location, due to a macro
115 // instantiation or #line directive. Strip off this information to find out
116 // where the characters are actually located.
Chris Lattner31bb8be2007-07-20 18:00:12 +0000117 PhysLoc = getPhysicalLoc(PhysLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000118
119 // Resolve InstantLoc down to a real logical location.
120 InstantLoc = getLogicalLoc(InstantLoc);
121
Chris Lattner31bb8be2007-07-20 18:00:12 +0000122
123 // If the last macro id is close to the currently requested location, try to
Chris Lattner991ae512007-08-02 03:55:37 +0000124 // reuse it. This implements a small cache.
125 for (int i = MacroIDs.size()-1, e = MacroIDs.size()-6; i >= 0 && i != e; --i){
126 MacroIDInfo &LastOne = MacroIDs[i];
Chris Lattnerd1623a82007-07-21 06:41:57 +0000127
Chris Lattner991ae512007-08-02 03:55:37 +0000128 // The instanitation point and source physloc have to exactly match to reuse
129 // (for now). We could allow "nearby" instantiations in the future.
Chris Lattner18807d22007-11-09 23:59:17 +0000130 if (LastOne.getVirtualLoc() != InstantLoc ||
Chris Lattner991ae512007-08-02 03:55:37 +0000131 LastOne.getPhysicalLoc().getFileID() != PhysLoc.getFileID())
132 continue;
133
134 // Check to see if the physloc of the token came from near enough to reuse.
135 int PhysDelta = PhysLoc.getRawFilePos() -
136 LastOne.getPhysicalLoc().getRawFilePos();
137 if (SourceLocation::isValidMacroPhysOffs(PhysDelta))
Chris Lattnerf8484542008-02-03 08:24:13 +0000138 return SourceLocation::getMacroLoc(i, PhysDelta);
Chris Lattner31bb8be2007-07-20 18:00:12 +0000139 }
140
Chris Lattner45011cf2007-07-20 18:26:45 +0000141
Chris Lattner9dc1f532007-07-20 16:37:10 +0000142 MacroIDs.push_back(MacroIDInfo::get(InstantLoc, PhysLoc));
Chris Lattnerf8484542008-02-03 08:24:13 +0000143 return SourceLocation::getMacroLoc(MacroIDs.size()-1, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
Chris Lattner8a12c272007-10-11 18:38:32 +0000146/// getBufferData - Return a pointer to the start and end of the character
147/// data for the specified FileID.
148std::pair<const char*, const char*>
149SourceManager::getBufferData(unsigned FileID) const {
150 const llvm::MemoryBuffer *Buf = getBuffer(FileID);
151 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
152}
Reid Spencer5f016e22007-07-11 17:01:13 +0000153
154
155/// getCharacterData - Return a pointer to the start of the specified location
156/// in the appropriate MemoryBuffer.
157const char *SourceManager::getCharacterData(SourceLocation SL) const {
158 // Note that this is a hot function in the getSpelling() path, which is
159 // heavily used by -E mode.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000160 SL = getPhysicalLoc(SL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161
Ted Kremenek78d85f52007-10-30 21:08:08 +0000162 return getContentCache(SL.getFileID())->Buffer->getBufferStart() +
Chris Lattner9dc1f532007-07-20 16:37:10 +0000163 getFullFilePos(SL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164}
165
Reid Spencer5f016e22007-07-11 17:01:13 +0000166
Chris Lattner9dc1f532007-07-20 16:37:10 +0000167/// getColumnNumber - Return the column # for the specified file position.
Reid Spencer5f016e22007-07-11 17:01:13 +0000168/// this is significantly cheaper to compute than the line number. This returns
169/// zero if the column number isn't known.
170unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 unsigned FileID = Loc.getFileID();
172 if (FileID == 0) return 0;
173
Chris Lattner9dc1f532007-07-20 16:37:10 +0000174 unsigned FilePos = getFullFilePos(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 const MemoryBuffer *Buffer = getBuffer(FileID);
176 const char *Buf = Buffer->getBufferStart();
177
178 unsigned LineStart = FilePos;
179 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
180 --LineStart;
181 return FilePos-LineStart+1;
182}
183
184/// getSourceName - This method returns the name of the file or buffer that
185/// the SourceLocation specifies. This can be modified with #line directives,
186/// etc.
Chris Lattner8b6ca882007-08-30 05:59:30 +0000187const char *SourceManager::getSourceName(SourceLocation Loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 unsigned FileID = Loc.getFileID();
189 if (FileID == 0) return "";
Ted Kremenek78d85f52007-10-30 21:08:08 +0000190 return getContentCache(FileID)->Buffer->getBufferIdentifier();
Reid Spencer5f016e22007-07-11 17:01:13 +0000191}
192
Ted Kremenek78d85f52007-10-30 21:08:08 +0000193static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
194static void ComputeLineNumbers(ContentCache* FI) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000195 const MemoryBuffer *Buffer = FI->Buffer;
196
197 // Find the file offsets of all of the *physical* source lines. This does
198 // not look at trigraphs, escaped newlines, or anything else tricky.
199 std::vector<unsigned> LineOffsets;
200
201 // Line #1 starts at char 0.
202 LineOffsets.push_back(0);
203
204 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
205 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
206 unsigned Offs = 0;
207 while (1) {
208 // Skip over the contents of the line.
209 // TODO: Vectorize this? This is very performance sensitive for programs
210 // with lots of diagnostics and in -E mode.
211 const unsigned char *NextBuf = (const unsigned char *)Buf;
212 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
213 ++NextBuf;
214 Offs += NextBuf-Buf;
215 Buf = NextBuf;
216
217 if (Buf[0] == '\n' || Buf[0] == '\r') {
218 // If this is \n\r or \r\n, skip both characters.
219 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
220 ++Offs, ++Buf;
221 ++Offs, ++Buf;
222 LineOffsets.push_back(Offs);
223 } else {
224 // Otherwise, this is a null. If end of file, exit.
225 if (Buf == End) break;
226 // Otherwise, skip the null.
227 ++Offs, ++Buf;
228 }
229 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000230
231 // Copy the offsets into the FileInfo structure.
232 FI->NumLines = LineOffsets.size();
233 FI->SourceLineCache = new unsigned[LineOffsets.size()];
234 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
235}
Reid Spencer5f016e22007-07-11 17:01:13 +0000236
237/// getLineNumber - Given a SourceLocation, return the physical line number
238/// for the position indicated. This requires building and caching a table of
239/// line offsets for the MemoryBuffer, so this is not cheap: use only when
240/// about to emit a diagnostic.
241unsigned SourceManager::getLineNumber(SourceLocation Loc) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 unsigned FileID = Loc.getFileID();
243 if (FileID == 0) return 0;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000244
245 ContentCache* Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000246
247 if (LastLineNoFileIDQuery == FileID)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000248 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000249 else
Ted Kremenek78d85f52007-10-30 21:08:08 +0000250 Content = const_cast<ContentCache*>(getContentCache(FileID));
Reid Spencer5f016e22007-07-11 17:01:13 +0000251
252 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000253 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000254 if (Content->SourceLineCache == 0)
255 ComputeLineNumbers(Content);
Reid Spencer5f016e22007-07-11 17:01:13 +0000256
257 // Okay, we know we have a line number table. Do a binary search to find the
258 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000259 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000260 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000261 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000262
263 unsigned QueriedFilePos = getFullFilePos(Loc)+1;
264
265 // If the previous query was to the same file, we know both the file pos from
266 // that query and the line number returned. This allows us to narrow the
267 // search space from the entire file to something near the match.
268 if (LastLineNoFileIDQuery == FileID) {
269 if (QueriedFilePos >= LastLineNoFilePos) {
270 SourceLineCache = SourceLineCache+LastLineNoResult-1;
271
272 // The query is likely to be nearby the previous one. Here we check to
273 // see if it is within 5, 10 or 20 lines. It can be far away in cases
274 // where big comment blocks and vertical whitespace eat up lines but
275 // contribute no tokens.
276 if (SourceLineCache+5 < SourceLineCacheEnd) {
277 if (SourceLineCache[5] > QueriedFilePos)
278 SourceLineCacheEnd = SourceLineCache+5;
279 else if (SourceLineCache+10 < SourceLineCacheEnd) {
280 if (SourceLineCache[10] > QueriedFilePos)
281 SourceLineCacheEnd = SourceLineCache+10;
282 else if (SourceLineCache+20 < SourceLineCacheEnd) {
283 if (SourceLineCache[20] > QueriedFilePos)
284 SourceLineCacheEnd = SourceLineCache+20;
285 }
286 }
287 }
288 } else {
289 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
290 }
291 }
292
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000293 // If the spread is large, do a "radix" test as our initial guess, based on
294 // the assumption that lines average to approximately the same length.
295 // NOTE: This is currently disabled, as it does not appear to be profitable in
296 // initial measurements.
297 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000298 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000299
300 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000301 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000302
303 // Check for -10 and +10 lines.
304 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
305 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
306
307 // If the computed lower bound is less than the query location, move it in.
308 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
309 SourceLineCacheStart[LowerBound] < QueriedFilePos)
310 SourceLineCache = SourceLineCacheStart+LowerBound;
311
312 // If the computed upper bound is greater than the query location, move it.
313 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
314 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
315 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
316 }
317
318 unsigned *Pos
319 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000320 unsigned LineNo = Pos-SourceLineCacheStart;
321
322 LastLineNoFileIDQuery = FileID;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000323 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000324 LastLineNoFilePos = QueriedFilePos;
325 LastLineNoResult = LineNo;
326 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000327}
328
Reid Spencer5f016e22007-07-11 17:01:13 +0000329/// PrintStats - Print statistics to stderr.
330///
331void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000332 llvm::cerr << "\n*** Source Manager Stats:\n";
333 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
334 << " mem buffers mapped, " << FileIDs.size()
335 << " file ID's allocated.\n";
336 llvm::cerr << " " << FileIDs.size() << " normal buffer FileID's, "
337 << MacroIDs.size() << " macro expansion FileID's.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000338
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 unsigned NumLineNumsComputed = 0;
340 unsigned NumFileBytesMapped = 0;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000341 for (std::set<ContentCache>::const_iterator I =
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000343 NumLineNumsComputed += I->SourceLineCache != 0;
344 NumFileBytesMapped += I->Buffer->getBufferSize();
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000346
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000347 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
348 << NumLineNumsComputed << " files with line #'s computed.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000349}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000350
351//===----------------------------------------------------------------------===//
352// Serialization.
353//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000354
355void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000356 S.FlushRecord();
357 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000358
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000359 if (Entry) {
360 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
361
362 if (Fname.isAbsolute())
363 S.EmitCStr(Fname.c_str());
364 else {
365 // Create an absolute path.
366 // FIXME: This will potentially contain ".." and "." in the path.
367 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
368 path.appendComponent(Fname.c_str());
369 S.EmitCStr(path.c_str());
370 }
371 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000372 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000373 const char* p = Buffer->getBufferStart();
374 const char* e = Buffer->getBufferEnd();
375
Ted Kremenek099b4742007-12-05 00:14:18 +0000376 S.EmitInt(e-p);
377
Ted Kremeneke21272f2007-12-04 19:39:02 +0000378 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000379 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000380 }
381
Ted Kremenek099b4742007-12-05 00:14:18 +0000382 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000383}
Ted Kremenek099b4742007-12-05 00:14:18 +0000384
385void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
386 SourceManager& SMgr,
387 FileManager* FMgr,
388 std::vector<char>& Buf) {
389 if (FMgr) {
390 llvm::SerializedPtrID PtrID = D.ReadPtrID();
391 D.ReadCStr(Buf,false);
392
393 // Create/fetch the FileEntry.
394 const char* start = &Buf[0];
395 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
396
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000397 // FIXME: Ideally we want a lazy materialization of the ContentCache
398 // anyway, because we don't want to read in source files unless this
399 // is absolutely needed.
400 if (!E)
401 D.RegisterPtr(PtrID,NULL);
Nico Weber48002c82008-09-29 00:25:48 +0000402 else
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000403 // Get the ContextCache object and register it with the deserializer.
404 D.RegisterPtr(PtrID,SMgr.getContentCache(E));
Ted Kremenek099b4742007-12-05 00:14:18 +0000405 }
406 else {
407 // Register the ContextCache object with the deserializer.
408 SMgr.MemBufferInfos.push_back(ContentCache());
Nico Weber48002c82008-09-29 00:25:48 +0000409 ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
Ted Kremenek099b4742007-12-05 00:14:18 +0000410 D.RegisterPtr(&Entry);
411
412 // Create the buffer.
413 unsigned Size = D.ReadInt();
414 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
415
416 // Read the contents of the buffer.
417 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
418 for (unsigned i = 0; i < Size ; ++i)
419 p[i] = D.ReadInt();
420 }
421}
422
423void FileIDInfo::Emit(llvm::Serializer& S) const {
424 S.Emit(IncludeLoc);
425 S.EmitInt(ChunkNo);
426 S.EmitPtr(Content);
427}
428
429FileIDInfo FileIDInfo::ReadVal(llvm::Deserializer& D) {
430 FileIDInfo I;
431 I.IncludeLoc = SourceLocation::ReadVal(D);
432 I.ChunkNo = D.ReadInt();
433 D.ReadPtr(I.Content,false);
434 return I;
435}
436
437void MacroIDInfo::Emit(llvm::Serializer& S) const {
438 S.Emit(VirtualLoc);
439 S.Emit(PhysicalLoc);
440}
441
442MacroIDInfo MacroIDInfo::ReadVal(llvm::Deserializer& D) {
443 MacroIDInfo I;
444 I.VirtualLoc = SourceLocation::ReadVal(D);
445 I.PhysicalLoc = SourceLocation::ReadVal(D);
446 return I;
447}
448
449void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000450 S.EnterBlock();
451 S.EmitPtr(this);
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000452 S.EmitInt(MainFileID);
Ted Kremenek1f941002007-12-05 00:19:51 +0000453
Ted Kremenek099b4742007-12-05 00:14:18 +0000454 // Emit: FileInfos. Just emit the file name.
455 S.EnterBlock();
456
457 std::for_each(FileInfos.begin(),FileInfos.end(),
458 S.MakeEmitter<ContentCache>());
459
460 S.ExitBlock();
461
462 // Emit: MemBufferInfos
463 S.EnterBlock();
464
465 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
466 S.MakeEmitter<ContentCache>());
467
468 S.ExitBlock();
469
Nico Weber48002c82008-09-29 00:25:48 +0000470 // Emit: FileIDs
Ted Kremenek099b4742007-12-05 00:14:18 +0000471 S.EmitInt(FileIDs.size());
472 std::for_each(FileIDs.begin(), FileIDs.end(), S.MakeEmitter<FileIDInfo>());
473
474 // Emit: MacroIDs
475 S.EmitInt(MacroIDs.size());
476 std::for_each(MacroIDs.begin(), MacroIDs.end(), S.MakeEmitter<MacroIDInfo>());
Ted Kremenek1f941002007-12-05 00:19:51 +0000477
478 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000479}
480
Ted Kremenek1f941002007-12-05 00:19:51 +0000481SourceManager*
482SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
483 SourceManager *M = new SourceManager();
484 D.RegisterPtr(M);
485
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000486 // Read: the FileID of the main source file of the translation unit.
487 M->MainFileID = D.ReadInt();
488
Ted Kremenek099b4742007-12-05 00:14:18 +0000489 std::vector<char> Buf;
490
491 { // Read: FileInfos.
492 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
493 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000494 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000495 }
496
497 { // Read: MemBufferInfos.
498 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
499 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000500 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000501 }
502
503 // Read: FileIDs.
504 unsigned Size = D.ReadInt();
Ted Kremenek1f941002007-12-05 00:19:51 +0000505 M->FileIDs.reserve(Size);
Ted Kremenek099b4742007-12-05 00:14:18 +0000506 for (; Size > 0 ; --Size)
Ted Kremenek1f941002007-12-05 00:19:51 +0000507 M->FileIDs.push_back(FileIDInfo::ReadVal(D));
Ted Kremenek099b4742007-12-05 00:14:18 +0000508
509 // Read: MacroIDs.
510 Size = D.ReadInt();
Ted Kremenek1f941002007-12-05 00:19:51 +0000511 M->MacroIDs.reserve(Size);
Ted Kremenek099b4742007-12-05 00:14:18 +0000512 for (; Size > 0 ; --Size)
Ted Kremenek1f941002007-12-05 00:19:51 +0000513 M->MacroIDs.push_back(MacroIDInfo::ReadVal(D));
514
515 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +0000516}