blob: d7d2c84a4c1a710640150dd9e64f15e1610a8a21 [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
Ted Kremenek0d892d82007-10-30 22:57:35 +000074/// createFileID - Create a new fileID for the specified ContentCache and
75/// 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,
Reid Spencer5f016e22007-07-11 17:01:13 +000078 SourceLocation IncludePos) {
79 // If FileEnt is really large (e.g. it's a large .i file), we may not be able
80 // to fit an arbitrary position in the file in the FilePos field. To handle
81 // this, we create one FileID for each chunk of the file that fits in a
82 // FilePos field.
Ted Kremenek78d85f52007-10-30 21:08:08 +000083 unsigned FileSize = File->Buffer->getBufferSize();
Reid Spencer5f016e22007-07-11 17:01:13 +000084 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000085 FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File));
Reid Spencer5f016e22007-07-11 17:01:13 +000086 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
87 "Ran out of file ID's!");
88 return FileIDs.size();
89 }
90
91 // Create one FileID for each chunk of the file.
92 unsigned Result = FileIDs.size()+1;
93
94 unsigned ChunkNo = 0;
95 while (1) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000096 FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File));
Reid Spencer5f016e22007-07-11 17:01:13 +000097
98 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break;
99 FileSize -= (1 << SourceLocation::FilePosBits);
100 }
101
102 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
103 "Ran out of file ID's!");
104 return Result;
105}
106
107/// getInstantiationLoc - Return a new SourceLocation that encodes the fact
108/// that a token from physloc PhysLoc should actually be referenced from
109/// InstantiationLoc.
Chris Lattner31bb8be2007-07-20 18:00:12 +0000110SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 SourceLocation InstantLoc) {
Chris Lattnerabca2bb2007-07-15 06:35:27 +0000112 // The specified source location may be a mapped location, due to a macro
113 // instantiation or #line directive. Strip off this information to find out
114 // where the characters are actually located.
Chris Lattner31bb8be2007-07-20 18:00:12 +0000115 PhysLoc = getPhysicalLoc(PhysLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000116
117 // Resolve InstantLoc down to a real logical location.
118 InstantLoc = getLogicalLoc(InstantLoc);
119
Chris Lattner31bb8be2007-07-20 18:00:12 +0000120
121 // If the last macro id is close to the currently requested location, try to
Chris Lattner991ae512007-08-02 03:55:37 +0000122 // reuse it. This implements a small cache.
123 for (int i = MacroIDs.size()-1, e = MacroIDs.size()-6; i >= 0 && i != e; --i){
124 MacroIDInfo &LastOne = MacroIDs[i];
Chris Lattnerd1623a82007-07-21 06:41:57 +0000125
Chris Lattner991ae512007-08-02 03:55:37 +0000126 // The instanitation point and source physloc have to exactly match to reuse
127 // (for now). We could allow "nearby" instantiations in the future.
Chris Lattner18807d22007-11-09 23:59:17 +0000128 if (LastOne.getVirtualLoc() != InstantLoc ||
Chris Lattner991ae512007-08-02 03:55:37 +0000129 LastOne.getPhysicalLoc().getFileID() != PhysLoc.getFileID())
130 continue;
131
132 // Check to see if the physloc of the token came from near enough to reuse.
133 int PhysDelta = PhysLoc.getRawFilePos() -
134 LastOne.getPhysicalLoc().getRawFilePos();
135 if (SourceLocation::isValidMacroPhysOffs(PhysDelta))
Chris Lattnerf8484542008-02-03 08:24:13 +0000136 return SourceLocation::getMacroLoc(i, PhysDelta);
Chris Lattner31bb8be2007-07-20 18:00:12 +0000137 }
138
Chris Lattner45011cf2007-07-20 18:26:45 +0000139
Chris Lattner9dc1f532007-07-20 16:37:10 +0000140 MacroIDs.push_back(MacroIDInfo::get(InstantLoc, PhysLoc));
Chris Lattnerf8484542008-02-03 08:24:13 +0000141 return SourceLocation::getMacroLoc(MacroIDs.size()-1, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000142}
143
Chris Lattner8a12c272007-10-11 18:38:32 +0000144/// getBufferData - Return a pointer to the start and end of the character
145/// data for the specified FileID.
146std::pair<const char*, const char*>
147SourceManager::getBufferData(unsigned FileID) const {
148 const llvm::MemoryBuffer *Buf = getBuffer(FileID);
149 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
150}
Reid Spencer5f016e22007-07-11 17:01:13 +0000151
152
153/// getCharacterData - Return a pointer to the start of the specified location
154/// in the appropriate MemoryBuffer.
155const char *SourceManager::getCharacterData(SourceLocation SL) const {
156 // Note that this is a hot function in the getSpelling() path, which is
157 // heavily used by -E mode.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000158 SL = getPhysicalLoc(SL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159
Ted Kremenek78d85f52007-10-30 21:08:08 +0000160 return getContentCache(SL.getFileID())->Buffer->getBufferStart() +
Chris Lattner9dc1f532007-07-20 16:37:10 +0000161 getFullFilePos(SL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000162}
163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164
Chris Lattner9dc1f532007-07-20 16:37:10 +0000165/// getColumnNumber - Return the column # for the specified file position.
Reid Spencer5f016e22007-07-11 17:01:13 +0000166/// this is significantly cheaper to compute than the line number. This returns
167/// zero if the column number isn't known.
168unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 unsigned FileID = Loc.getFileID();
170 if (FileID == 0) return 0;
171
Chris Lattner9dc1f532007-07-20 16:37:10 +0000172 unsigned FilePos = getFullFilePos(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 const MemoryBuffer *Buffer = getBuffer(FileID);
174 const char *Buf = Buffer->getBufferStart();
175
176 unsigned LineStart = FilePos;
177 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
178 --LineStart;
179 return FilePos-LineStart+1;
180}
181
182/// getSourceName - This method returns the name of the file or buffer that
183/// the SourceLocation specifies. This can be modified with #line directives,
184/// etc.
Chris Lattner8b6ca882007-08-30 05:59:30 +0000185const char *SourceManager::getSourceName(SourceLocation Loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 unsigned FileID = Loc.getFileID();
187 if (FileID == 0) return "";
Ted Kremenek78d85f52007-10-30 21:08:08 +0000188 return getContentCache(FileID)->Buffer->getBufferIdentifier();
Reid Spencer5f016e22007-07-11 17:01:13 +0000189}
190
Ted Kremenek78d85f52007-10-30 21:08:08 +0000191static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
192static void ComputeLineNumbers(ContentCache* FI) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000193 const MemoryBuffer *Buffer = FI->Buffer;
194
195 // Find the file offsets of all of the *physical* source lines. This does
196 // not look at trigraphs, escaped newlines, or anything else tricky.
197 std::vector<unsigned> LineOffsets;
198
199 // Line #1 starts at char 0.
200 LineOffsets.push_back(0);
201
202 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
203 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
204 unsigned Offs = 0;
205 while (1) {
206 // Skip over the contents of the line.
207 // TODO: Vectorize this? This is very performance sensitive for programs
208 // with lots of diagnostics and in -E mode.
209 const unsigned char *NextBuf = (const unsigned char *)Buf;
210 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
211 ++NextBuf;
212 Offs += NextBuf-Buf;
213 Buf = NextBuf;
214
215 if (Buf[0] == '\n' || Buf[0] == '\r') {
216 // If this is \n\r or \r\n, skip both characters.
217 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
218 ++Offs, ++Buf;
219 ++Offs, ++Buf;
220 LineOffsets.push_back(Offs);
221 } else {
222 // Otherwise, this is a null. If end of file, exit.
223 if (Buf == End) break;
224 // Otherwise, skip the null.
225 ++Offs, ++Buf;
226 }
227 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000228
229 // Copy the offsets into the FileInfo structure.
230 FI->NumLines = LineOffsets.size();
231 FI->SourceLineCache = new unsigned[LineOffsets.size()];
232 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
233}
Reid Spencer5f016e22007-07-11 17:01:13 +0000234
235/// getLineNumber - Given a SourceLocation, return the physical line number
236/// for the position indicated. This requires building and caching a table of
237/// line offsets for the MemoryBuffer, so this is not cheap: use only when
238/// about to emit a diagnostic.
239unsigned SourceManager::getLineNumber(SourceLocation Loc) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 unsigned FileID = Loc.getFileID();
241 if (FileID == 0) return 0;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000242
243 ContentCache* Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000244
245 if (LastLineNoFileIDQuery == FileID)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000246 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000247 else
Ted Kremenek78d85f52007-10-30 21:08:08 +0000248 Content = const_cast<ContentCache*>(getContentCache(FileID));
Reid Spencer5f016e22007-07-11 17:01:13 +0000249
250 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000251 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000252 if (Content->SourceLineCache == 0)
253 ComputeLineNumbers(Content);
Reid Spencer5f016e22007-07-11 17:01:13 +0000254
255 // Okay, we know we have a line number table. Do a binary search to find the
256 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000257 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000258 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000259 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000260
261 unsigned QueriedFilePos = getFullFilePos(Loc)+1;
262
263 // If the previous query was to the same file, we know both the file pos from
264 // that query and the line number returned. This allows us to narrow the
265 // search space from the entire file to something near the match.
266 if (LastLineNoFileIDQuery == FileID) {
267 if (QueriedFilePos >= LastLineNoFilePos) {
268 SourceLineCache = SourceLineCache+LastLineNoResult-1;
269
270 // The query is likely to be nearby the previous one. Here we check to
271 // see if it is within 5, 10 or 20 lines. It can be far away in cases
272 // where big comment blocks and vertical whitespace eat up lines but
273 // contribute no tokens.
274 if (SourceLineCache+5 < SourceLineCacheEnd) {
275 if (SourceLineCache[5] > QueriedFilePos)
276 SourceLineCacheEnd = SourceLineCache+5;
277 else if (SourceLineCache+10 < SourceLineCacheEnd) {
278 if (SourceLineCache[10] > QueriedFilePos)
279 SourceLineCacheEnd = SourceLineCache+10;
280 else if (SourceLineCache+20 < SourceLineCacheEnd) {
281 if (SourceLineCache[20] > QueriedFilePos)
282 SourceLineCacheEnd = SourceLineCache+20;
283 }
284 }
285 }
286 } else {
287 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
288 }
289 }
290
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000291 // If the spread is large, do a "radix" test as our initial guess, based on
292 // the assumption that lines average to approximately the same length.
293 // NOTE: This is currently disabled, as it does not appear to be profitable in
294 // initial measurements.
295 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000296 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000297
298 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000299 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000300
301 // Check for -10 and +10 lines.
302 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
303 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
304
305 // If the computed lower bound is less than the query location, move it in.
306 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
307 SourceLineCacheStart[LowerBound] < QueriedFilePos)
308 SourceLineCache = SourceLineCacheStart+LowerBound;
309
310 // If the computed upper bound is greater than the query location, move it.
311 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
312 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
313 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
314 }
315
316 unsigned *Pos
317 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000318 unsigned LineNo = Pos-SourceLineCacheStart;
319
320 LastLineNoFileIDQuery = FileID;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000321 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000322 LastLineNoFilePos = QueriedFilePos;
323 LastLineNoResult = LineNo;
324 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000325}
326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327/// PrintStats - Print statistics to stderr.
328///
329void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000330 llvm::cerr << "\n*** Source Manager Stats:\n";
331 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
332 << " mem buffers mapped, " << FileIDs.size()
333 << " file ID's allocated.\n";
334 llvm::cerr << " " << FileIDs.size() << " normal buffer FileID's, "
335 << MacroIDs.size() << " macro expansion FileID's.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 unsigned NumLineNumsComputed = 0;
338 unsigned NumFileBytesMapped = 0;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000339 for (std::set<ContentCache>::const_iterator I =
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000341 NumLineNumsComputed += I->SourceLineCache != 0;
342 NumFileBytesMapped += I->Buffer->getBufferSize();
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000344
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000345 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
346 << NumLineNumsComputed << " files with line #'s computed.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000347}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000348
349//===----------------------------------------------------------------------===//
350// Serialization.
351//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000352
353void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000354 S.FlushRecord();
355 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000356
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000357 if (Entry) {
358 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
359
360 if (Fname.isAbsolute())
361 S.EmitCStr(Fname.c_str());
362 else {
363 // Create an absolute path.
364 // FIXME: This will potentially contain ".." and "." in the path.
365 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
366 path.appendComponent(Fname.c_str());
367 S.EmitCStr(path.c_str());
368 }
369 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000370 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000371 const char* p = Buffer->getBufferStart();
372 const char* e = Buffer->getBufferEnd();
373
Ted Kremenek099b4742007-12-05 00:14:18 +0000374 S.EmitInt(e-p);
375
Ted Kremeneke21272f2007-12-04 19:39:02 +0000376 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000377 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000378 }
379
Ted Kremenek099b4742007-12-05 00:14:18 +0000380 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000381}
Ted Kremenek099b4742007-12-05 00:14:18 +0000382
383void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
384 SourceManager& SMgr,
385 FileManager* FMgr,
386 std::vector<char>& Buf) {
387 if (FMgr) {
388 llvm::SerializedPtrID PtrID = D.ReadPtrID();
389 D.ReadCStr(Buf,false);
390
391 // Create/fetch the FileEntry.
392 const char* start = &Buf[0];
393 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
394
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000395 // FIXME: Ideally we want a lazy materialization of the ContentCache
396 // anyway, because we don't want to read in source files unless this
397 // is absolutely needed.
398 if (!E)
399 D.RegisterPtr(PtrID,NULL);
400 else
401 // Get the ContextCache object and register it with the deserializer.
402 D.RegisterPtr(PtrID,SMgr.getContentCache(E));
Ted Kremenek099b4742007-12-05 00:14:18 +0000403 }
404 else {
405 // Register the ContextCache object with the deserializer.
406 SMgr.MemBufferInfos.push_back(ContentCache());
407 ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
408 D.RegisterPtr(&Entry);
409
410 // Create the buffer.
411 unsigned Size = D.ReadInt();
412 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
413
414 // Read the contents of the buffer.
415 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
416 for (unsigned i = 0; i < Size ; ++i)
417 p[i] = D.ReadInt();
418 }
419}
420
421void FileIDInfo::Emit(llvm::Serializer& S) const {
422 S.Emit(IncludeLoc);
423 S.EmitInt(ChunkNo);
424 S.EmitPtr(Content);
425}
426
427FileIDInfo FileIDInfo::ReadVal(llvm::Deserializer& D) {
428 FileIDInfo I;
429 I.IncludeLoc = SourceLocation::ReadVal(D);
430 I.ChunkNo = D.ReadInt();
431 D.ReadPtr(I.Content,false);
432 return I;
433}
434
435void MacroIDInfo::Emit(llvm::Serializer& S) const {
436 S.Emit(VirtualLoc);
437 S.Emit(PhysicalLoc);
438}
439
440MacroIDInfo MacroIDInfo::ReadVal(llvm::Deserializer& D) {
441 MacroIDInfo I;
442 I.VirtualLoc = SourceLocation::ReadVal(D);
443 I.PhysicalLoc = SourceLocation::ReadVal(D);
444 return I;
445}
446
447void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000448 S.EnterBlock();
449 S.EmitPtr(this);
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000450 S.EmitInt(MainFileID);
Ted Kremenek1f941002007-12-05 00:19:51 +0000451
Ted Kremenek099b4742007-12-05 00:14:18 +0000452 // Emit: FileInfos. Just emit the file name.
453 S.EnterBlock();
454
455 std::for_each(FileInfos.begin(),FileInfos.end(),
456 S.MakeEmitter<ContentCache>());
457
458 S.ExitBlock();
459
460 // Emit: MemBufferInfos
461 S.EnterBlock();
462
463 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
464 S.MakeEmitter<ContentCache>());
465
466 S.ExitBlock();
467
468 // Emit: FileIDs
469 S.EmitInt(FileIDs.size());
470 std::for_each(FileIDs.begin(), FileIDs.end(), S.MakeEmitter<FileIDInfo>());
471
472 // Emit: MacroIDs
473 S.EmitInt(MacroIDs.size());
474 std::for_each(MacroIDs.begin(), MacroIDs.end(), S.MakeEmitter<MacroIDInfo>());
Ted Kremenek1f941002007-12-05 00:19:51 +0000475
476 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000477}
478
Ted Kremenek1f941002007-12-05 00:19:51 +0000479SourceManager*
480SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
481 SourceManager *M = new SourceManager();
482 D.RegisterPtr(M);
483
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000484 // Read: the FileID of the main source file of the translation unit.
485 M->MainFileID = D.ReadInt();
486
Ted Kremenek099b4742007-12-05 00:14:18 +0000487 std::vector<char> Buf;
488
489 { // Read: FileInfos.
490 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
491 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000492 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000493 }
494
495 { // Read: MemBufferInfos.
496 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
497 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000498 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000499 }
500
501 // Read: FileIDs.
502 unsigned Size = D.ReadInt();
Ted Kremenek1f941002007-12-05 00:19:51 +0000503 M->FileIDs.reserve(Size);
Ted Kremenek099b4742007-12-05 00:14:18 +0000504 for (; Size > 0 ; --Size)
Ted Kremenek1f941002007-12-05 00:19:51 +0000505 M->FileIDs.push_back(FileIDInfo::ReadVal(D));
Ted Kremenek099b4742007-12-05 00:14:18 +0000506
507 // Read: MacroIDs.
508 Size = D.ReadInt();
Ted Kremenek1f941002007-12-05 00:19:51 +0000509 M->MacroIDs.reserve(Size);
Ted Kremenek099b4742007-12-05 00:14:18 +0000510 for (; Size > 0 ; --Size)
Ted Kremenek1f941002007-12-05 00:19:51 +0000511 M->MacroIDs.push_back(MacroIDInfo::ReadVal(D));
512
513 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +0000514}