blob: 9f6624c0b7ebc591cd0364dd1932e743bee51eea [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 =
46 MemoryBuffer::getFile(FileEnt->getName(), strlen(FileEnt->getName()), 0,
47 FileEnt->getSize());
Reid Spencer5f016e22007-07-11 17:01:13 +000048 if (File == 0)
49 return 0;
50
Ted Kremenek78d85f52007-10-30 21:08:08 +000051 ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt));
Reid Spencer5f016e22007-07-11 17:01:13 +000052
Ted Kremenek78d85f52007-10-30 21:08:08 +000053 Entry.Buffer = File;
54 Entry.SourceLineCache = 0;
55 Entry.NumLines = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000056 return &Entry;
57}
58
59
Ted Kremenekd1c0eee2007-10-31 17:53:38 +000060/// createMemBufferContentCache - Create a new ContentCache for the specified
61/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +000062const ContentCache*
63SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Ted Kremenek0d892d82007-10-30 22:57:35 +000064 // Add a new ContentCache to the MemBufferInfos list and return it. We
65 // must default construct the object first that the instance actually
66 // stored within MemBufferInfos actually owns the Buffer, and not any
67 // temporary we would use in the call to "push_back".
Ted Kremenek78d85f52007-10-30 21:08:08 +000068 MemBufferInfos.push_back(ContentCache());
69 ContentCache& Entry = const_cast<ContentCache&>(MemBufferInfos.back());
70 Entry.Buffer = Buffer;
71 return &Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +000072}
73
74
Ted Kremenek0d892d82007-10-30 22:57:35 +000075/// createFileID - Create a new fileID for the specified ContentCache and
76/// include position. This works regardless of whether the ContentCache
77/// corresponds to a file or some other input source.
Ted Kremenek78d85f52007-10-30 21:08:08 +000078unsigned SourceManager::createFileID(const ContentCache *File,
Reid Spencer5f016e22007-07-11 17:01:13 +000079 SourceLocation IncludePos) {
80 // 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 Lattner9dc1f532007-07-20 16:37:10 +000086 FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File));
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) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000097 FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File));
Reid Spencer5f016e22007-07-11 17:01:13 +000098
99 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break;
100 FileSize -= (1 << SourceLocation::FilePosBits);
101 }
102
103 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
104 "Ran out of file ID's!");
105 return Result;
106}
107
108/// getInstantiationLoc - Return a new SourceLocation that encodes the fact
109/// that a token from physloc PhysLoc should actually be referenced from
110/// InstantiationLoc.
Chris Lattner31bb8be2007-07-20 18:00:12 +0000111SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 SourceLocation InstantLoc) {
Chris Lattnerabca2bb2007-07-15 06:35:27 +0000113 // The specified source location may be a mapped location, due to a macro
114 // instantiation or #line directive. Strip off this information to find out
115 // where the characters are actually located.
Chris Lattner31bb8be2007-07-20 18:00:12 +0000116 PhysLoc = getPhysicalLoc(PhysLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000117
118 // Resolve InstantLoc down to a real logical location.
119 InstantLoc = getLogicalLoc(InstantLoc);
120
Chris Lattner31bb8be2007-07-20 18:00:12 +0000121
122 // If the last macro id is close to the currently requested location, try to
Chris Lattner991ae512007-08-02 03:55:37 +0000123 // reuse it. This implements a small cache.
124 for (int i = MacroIDs.size()-1, e = MacroIDs.size()-6; i >= 0 && i != e; --i){
125 MacroIDInfo &LastOne = MacroIDs[i];
Chris Lattnerd1623a82007-07-21 06:41:57 +0000126
Chris Lattner991ae512007-08-02 03:55:37 +0000127 // The instanitation point and source physloc have to exactly match to reuse
128 // (for now). We could allow "nearby" instantiations in the future.
Chris Lattner18807d22007-11-09 23:59:17 +0000129 if (LastOne.getVirtualLoc() != InstantLoc ||
Chris Lattner991ae512007-08-02 03:55:37 +0000130 LastOne.getPhysicalLoc().getFileID() != PhysLoc.getFileID())
131 continue;
132
133 // Check to see if the physloc of the token came from near enough to reuse.
134 int PhysDelta = PhysLoc.getRawFilePos() -
135 LastOne.getPhysicalLoc().getRawFilePos();
136 if (SourceLocation::isValidMacroPhysOffs(PhysDelta))
Chris Lattnerf8484542008-02-03 08:24:13 +0000137 return SourceLocation::getMacroLoc(i, PhysDelta);
Chris Lattner31bb8be2007-07-20 18:00:12 +0000138 }
139
Chris Lattner45011cf2007-07-20 18:26:45 +0000140
Chris Lattner9dc1f532007-07-20 16:37:10 +0000141 MacroIDs.push_back(MacroIDInfo::get(InstantLoc, PhysLoc));
Chris Lattnerf8484542008-02-03 08:24:13 +0000142 return SourceLocation::getMacroLoc(MacroIDs.size()-1, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000143}
144
Chris Lattner8a12c272007-10-11 18:38:32 +0000145/// getBufferData - Return a pointer to the start and end of the character
146/// data for the specified FileID.
147std::pair<const char*, const char*>
148SourceManager::getBufferData(unsigned FileID) const {
149 const llvm::MemoryBuffer *Buf = getBuffer(FileID);
150 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
151}
Reid Spencer5f016e22007-07-11 17:01:13 +0000152
153
154/// getCharacterData - Return a pointer to the start of the specified location
155/// in the appropriate MemoryBuffer.
156const char *SourceManager::getCharacterData(SourceLocation SL) const {
157 // Note that this is a hot function in the getSpelling() path, which is
158 // heavily used by -E mode.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000159 SL = getPhysicalLoc(SL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160
Ted Kremenek78d85f52007-10-30 21:08:08 +0000161 return getContentCache(SL.getFileID())->Buffer->getBufferStart() +
Chris Lattner9dc1f532007-07-20 16:37:10 +0000162 getFullFilePos(SL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163}
164
Reid Spencer5f016e22007-07-11 17:01:13 +0000165
Chris Lattner9dc1f532007-07-20 16:37:10 +0000166/// getColumnNumber - Return the column # for the specified file position.
Reid Spencer5f016e22007-07-11 17:01:13 +0000167/// this is significantly cheaper to compute than the line number. This returns
168/// zero if the column number isn't known.
169unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 unsigned FileID = Loc.getFileID();
171 if (FileID == 0) return 0;
172
Chris Lattner9dc1f532007-07-20 16:37:10 +0000173 unsigned FilePos = getFullFilePos(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 const MemoryBuffer *Buffer = getBuffer(FileID);
175 const char *Buf = Buffer->getBufferStart();
176
177 unsigned LineStart = FilePos;
178 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
179 --LineStart;
180 return FilePos-LineStart+1;
181}
182
183/// getSourceName - This method returns the name of the file or buffer that
184/// the SourceLocation specifies. This can be modified with #line directives,
185/// etc.
Chris Lattner8b6ca882007-08-30 05:59:30 +0000186const char *SourceManager::getSourceName(SourceLocation Loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 unsigned FileID = Loc.getFileID();
188 if (FileID == 0) return "";
Ted Kremenek78d85f52007-10-30 21:08:08 +0000189 return getContentCache(FileID)->Buffer->getBufferIdentifier();
Reid Spencer5f016e22007-07-11 17:01:13 +0000190}
191
Ted Kremenek78d85f52007-10-30 21:08:08 +0000192static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
193static void ComputeLineNumbers(ContentCache* FI) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000194 const MemoryBuffer *Buffer = FI->Buffer;
195
196 // Find the file offsets of all of the *physical* source lines. This does
197 // not look at trigraphs, escaped newlines, or anything else tricky.
198 std::vector<unsigned> LineOffsets;
199
200 // Line #1 starts at char 0.
201 LineOffsets.push_back(0);
202
203 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
204 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
205 unsigned Offs = 0;
206 while (1) {
207 // Skip over the contents of the line.
208 // TODO: Vectorize this? This is very performance sensitive for programs
209 // with lots of diagnostics and in -E mode.
210 const unsigned char *NextBuf = (const unsigned char *)Buf;
211 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
212 ++NextBuf;
213 Offs += NextBuf-Buf;
214 Buf = NextBuf;
215
216 if (Buf[0] == '\n' || Buf[0] == '\r') {
217 // If this is \n\r or \r\n, skip both characters.
218 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
219 ++Offs, ++Buf;
220 ++Offs, ++Buf;
221 LineOffsets.push_back(Offs);
222 } else {
223 // Otherwise, this is a null. If end of file, exit.
224 if (Buf == End) break;
225 // Otherwise, skip the null.
226 ++Offs, ++Buf;
227 }
228 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000229
230 // Copy the offsets into the FileInfo structure.
231 FI->NumLines = LineOffsets.size();
232 FI->SourceLineCache = new unsigned[LineOffsets.size()];
233 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
234}
Reid Spencer5f016e22007-07-11 17:01:13 +0000235
236/// getLineNumber - Given a SourceLocation, return the physical line number
237/// for the position indicated. This requires building and caching a table of
238/// line offsets for the MemoryBuffer, so this is not cheap: use only when
239/// about to emit a diagnostic.
240unsigned SourceManager::getLineNumber(SourceLocation Loc) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 unsigned FileID = Loc.getFileID();
242 if (FileID == 0) return 0;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000243
244 ContentCache* Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000245
246 if (LastLineNoFileIDQuery == FileID)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000247 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000248 else
Ted Kremenek78d85f52007-10-30 21:08:08 +0000249 Content = const_cast<ContentCache*>(getContentCache(FileID));
Reid Spencer5f016e22007-07-11 17:01:13 +0000250
251 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000252 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000253 if (Content->SourceLineCache == 0)
254 ComputeLineNumbers(Content);
Reid Spencer5f016e22007-07-11 17:01:13 +0000255
256 // Okay, we know we have a line number table. Do a binary search to find the
257 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000258 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000259 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000260 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000261
262 unsigned QueriedFilePos = getFullFilePos(Loc)+1;
263
264 // If the previous query was to the same file, we know both the file pos from
265 // that query and the line number returned. This allows us to narrow the
266 // search space from the entire file to something near the match.
267 if (LastLineNoFileIDQuery == FileID) {
268 if (QueriedFilePos >= LastLineNoFilePos) {
269 SourceLineCache = SourceLineCache+LastLineNoResult-1;
270
271 // The query is likely to be nearby the previous one. Here we check to
272 // see if it is within 5, 10 or 20 lines. It can be far away in cases
273 // where big comment blocks and vertical whitespace eat up lines but
274 // contribute no tokens.
275 if (SourceLineCache+5 < SourceLineCacheEnd) {
276 if (SourceLineCache[5] > QueriedFilePos)
277 SourceLineCacheEnd = SourceLineCache+5;
278 else if (SourceLineCache+10 < SourceLineCacheEnd) {
279 if (SourceLineCache[10] > QueriedFilePos)
280 SourceLineCacheEnd = SourceLineCache+10;
281 else if (SourceLineCache+20 < SourceLineCacheEnd) {
282 if (SourceLineCache[20] > QueriedFilePos)
283 SourceLineCacheEnd = SourceLineCache+20;
284 }
285 }
286 }
287 } else {
288 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
289 }
290 }
291
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000292 // If the spread is large, do a "radix" test as our initial guess, based on
293 // the assumption that lines average to approximately the same length.
294 // NOTE: This is currently disabled, as it does not appear to be profitable in
295 // initial measurements.
296 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000297 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000298
299 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000300 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000301
302 // Check for -10 and +10 lines.
303 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
304 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
305
306 // If the computed lower bound is less than the query location, move it in.
307 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
308 SourceLineCacheStart[LowerBound] < QueriedFilePos)
309 SourceLineCache = SourceLineCacheStart+LowerBound;
310
311 // If the computed upper bound is greater than the query location, move it.
312 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
313 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
314 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
315 }
316
317 unsigned *Pos
318 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000319 unsigned LineNo = Pos-SourceLineCacheStart;
320
321 LastLineNoFileIDQuery = FileID;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000322 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000323 LastLineNoFilePos = QueriedFilePos;
324 LastLineNoResult = LineNo;
325 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000326}
327
Reid Spencer5f016e22007-07-11 17:01:13 +0000328/// PrintStats - Print statistics to stderr.
329///
330void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000331 llvm::cerr << "\n*** Source Manager Stats:\n";
332 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
333 << " mem buffers mapped, " << FileIDs.size()
334 << " file ID's allocated.\n";
335 llvm::cerr << " " << FileIDs.size() << " normal buffer FileID's, "
336 << MacroIDs.size() << " macro expansion FileID's.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000337
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 unsigned NumLineNumsComputed = 0;
339 unsigned NumFileBytesMapped = 0;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000340 for (std::set<ContentCache>::const_iterator I =
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000342 NumLineNumsComputed += I->SourceLineCache != 0;
343 NumFileBytesMapped += I->Buffer->getBufferSize();
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000345
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000346 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
347 << NumLineNumsComputed << " files with line #'s computed.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000348}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000349
350//===----------------------------------------------------------------------===//
351// Serialization.
352//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000353
354void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000355 S.FlushRecord();
356 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000357
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000358 if (Entry) {
359 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
360
361 if (Fname.isAbsolute())
362 S.EmitCStr(Fname.c_str());
363 else {
364 // Create an absolute path.
365 // FIXME: This will potentially contain ".." and "." in the path.
366 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
367 path.appendComponent(Fname.c_str());
368 S.EmitCStr(path.c_str());
369 }
370 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000371 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000372 const char* p = Buffer->getBufferStart();
373 const char* e = Buffer->getBufferEnd();
374
Ted Kremenek099b4742007-12-05 00:14:18 +0000375 S.EmitInt(e-p);
376
Ted Kremeneke21272f2007-12-04 19:39:02 +0000377 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000378 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000379 }
380
Ted Kremenek099b4742007-12-05 00:14:18 +0000381 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000382}
Ted Kremenek099b4742007-12-05 00:14:18 +0000383
384void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
385 SourceManager& SMgr,
386 FileManager* FMgr,
387 std::vector<char>& Buf) {
388 if (FMgr) {
389 llvm::SerializedPtrID PtrID = D.ReadPtrID();
390 D.ReadCStr(Buf,false);
391
392 // Create/fetch the FileEntry.
393 const char* start = &Buf[0];
394 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
395
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000396 // FIXME: Ideally we want a lazy materialization of the ContentCache
397 // anyway, because we don't want to read in source files unless this
398 // is absolutely needed.
399 if (!E)
400 D.RegisterPtr(PtrID,NULL);
401 else
402 // Get the ContextCache object and register it with the deserializer.
403 D.RegisterPtr(PtrID,SMgr.getContentCache(E));
Ted Kremenek099b4742007-12-05 00:14:18 +0000404 }
405 else {
406 // Register the ContextCache object with the deserializer.
407 SMgr.MemBufferInfos.push_back(ContentCache());
408 ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
409 D.RegisterPtr(&Entry);
410
411 // Create the buffer.
412 unsigned Size = D.ReadInt();
413 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
414
415 // Read the contents of the buffer.
416 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
417 for (unsigned i = 0; i < Size ; ++i)
418 p[i] = D.ReadInt();
419 }
420}
421
422void FileIDInfo::Emit(llvm::Serializer& S) const {
423 S.Emit(IncludeLoc);
424 S.EmitInt(ChunkNo);
425 S.EmitPtr(Content);
426}
427
428FileIDInfo FileIDInfo::ReadVal(llvm::Deserializer& D) {
429 FileIDInfo I;
430 I.IncludeLoc = SourceLocation::ReadVal(D);
431 I.ChunkNo = D.ReadInt();
432 D.ReadPtr(I.Content,false);
433 return I;
434}
435
436void MacroIDInfo::Emit(llvm::Serializer& S) const {
437 S.Emit(VirtualLoc);
438 S.Emit(PhysicalLoc);
439}
440
441MacroIDInfo MacroIDInfo::ReadVal(llvm::Deserializer& D) {
442 MacroIDInfo I;
443 I.VirtualLoc = SourceLocation::ReadVal(D);
444 I.PhysicalLoc = SourceLocation::ReadVal(D);
445 return I;
446}
447
448void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000449 S.EnterBlock();
450 S.EmitPtr(this);
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000451 S.EmitInt(MainFileID);
Ted Kremenek1f941002007-12-05 00:19:51 +0000452
Ted Kremenek099b4742007-12-05 00:14:18 +0000453 // Emit: FileInfos. Just emit the file name.
454 S.EnterBlock();
455
456 std::for_each(FileInfos.begin(),FileInfos.end(),
457 S.MakeEmitter<ContentCache>());
458
459 S.ExitBlock();
460
461 // Emit: MemBufferInfos
462 S.EnterBlock();
463
464 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
465 S.MakeEmitter<ContentCache>());
466
467 S.ExitBlock();
468
469 // Emit: FileIDs
470 S.EmitInt(FileIDs.size());
471 std::for_each(FileIDs.begin(), FileIDs.end(), S.MakeEmitter<FileIDInfo>());
472
473 // Emit: MacroIDs
474 S.EmitInt(MacroIDs.size());
475 std::for_each(MacroIDs.begin(), MacroIDs.end(), S.MakeEmitter<MacroIDInfo>());
Ted Kremenek1f941002007-12-05 00:19:51 +0000476
477 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000478}
479
Ted Kremenek1f941002007-12-05 00:19:51 +0000480SourceManager*
481SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
482 SourceManager *M = new SourceManager();
483 D.RegisterPtr(M);
484
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000485 // Read: the FileID of the main source file of the translation unit.
486 M->MainFileID = D.ReadInt();
487
Ted Kremenek099b4742007-12-05 00:14:18 +0000488 std::vector<char> Buf;
489
490 { // Read: FileInfos.
491 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
492 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000493 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000494 }
495
496 { // Read: MemBufferInfos.
497 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
498 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000499 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000500 }
501
502 // Read: FileIDs.
503 unsigned Size = D.ReadInt();
Ted Kremenek1f941002007-12-05 00:19:51 +0000504 M->FileIDs.reserve(Size);
Ted Kremenek099b4742007-12-05 00:14:18 +0000505 for (; Size > 0 ; --Size)
Ted Kremenek1f941002007-12-05 00:19:51 +0000506 M->FileIDs.push_back(FileIDInfo::ReadVal(D));
Ted Kremenek099b4742007-12-05 00:14:18 +0000507
508 // Read: MacroIDs.
509 Size = D.ReadInt();
Ted Kremenek1f941002007-12-05 00:19:51 +0000510 M->MacroIDs.reserve(Size);
Ted Kremenek099b4742007-12-05 00:14:18 +0000511 for (; Size > 0 ; --Size)
Ted Kremenek1f941002007-12-05 00:19:51 +0000512 M->MacroIDs.push_back(MacroIDInfo::ReadVal(D));
513
514 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +0000515}