blob: 1240262007e3bb3e9e1c53c31a993664b7364a66 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SourceManager.cpp - Track and cache source files -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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"
16#include "llvm/Support/Compiler.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/System/Path.h"
Ted Kremenekdd364ea2007-10-30 21:08:08 +000019#include "llvm/Bitcode/Serialize.h"
20#include "llvm/Bitcode/Deserialize.h"
Ted Kremenekda29d8c2007-12-05 22:21:13 +000021#include "llvm/Support/Streams.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24using namespace SrcMgr;
25using llvm::MemoryBuffer;
26
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000027// This (temporary) directive toggles between lazy and eager creation of
28// MemBuffers. This directive is not permanent, and is here to test a few
29// potential optimizations in PTH. Once it is clear whether eager or lazy
30// creation of MemBuffers is better this directive will get removed.
31#define LAZY
32
Ted Kremenekdd364ea2007-10-30 21:08:08 +000033ContentCache::~ContentCache() {
34 delete Buffer;
35 delete [] SourceLineCache;
Chris Lattner4b009652007-07-25 00:24:17 +000036}
37
Ted Kremenekaa7dac12009-01-06 01:55:26 +000038/// getSizeBytesMapped - Returns the number of bytes actually mapped for
39/// this ContentCache. This can be 0 if the MemBuffer was not actually
40/// instantiated.
41unsigned ContentCache::getSizeBytesMapped() const {
42 return Buffer ? Buffer->getBufferSize() : 0;
43}
44
45/// getSize - Returns the size of the content encapsulated by this ContentCache.
46/// This can be the size of the source file or the size of an arbitrary
47/// scratch buffer. If the ContentCache encapsulates a source file, that
48/// file is not lazily brought in from disk to satisfy this query.
49unsigned ContentCache::getSize() const {
50 return Entry ? Entry->getSize() : Buffer->getBufferSize();
51}
52
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000053const llvm::MemoryBuffer* ContentCache::getBuffer() const {
54#ifdef LAZY
55 // Lazily create the Buffer for ContentCaches that wrap files.
56 if (!Buffer && Entry) {
57 // FIXME: Should we support a way to not have to do this check over
58 // and over if we cannot open the file?
Chris Lattnerac49bb42009-01-17 03:54:16 +000059 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000060 }
61#endif
Ted Kremenekaa7dac12009-01-06 01:55:26 +000062 return Buffer;
63}
64
65
Chris Lattner4b009652007-07-25 00:24:17 +000066/// getFileInfo - Create or return a cached FileInfo for the specified file.
67///
Ted Kremenekdd364ea2007-10-30 21:08:08 +000068const ContentCache* SourceManager::getContentCache(const FileEntry *FileEnt) {
69
Chris Lattner4b009652007-07-25 00:24:17 +000070 assert(FileEnt && "Didn't specify a file entry to use?");
71 // Do we already have information about this file?
Ted Kremenekdd364ea2007-10-30 21:08:08 +000072 std::set<ContentCache>::iterator I =
73 FileInfos.lower_bound(ContentCache(FileEnt));
74
75 if (I != FileInfos.end() && I->Entry == FileEnt)
Chris Lattner4b009652007-07-25 00:24:17 +000076 return &*I;
77
78 // Nope, get information.
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000079#ifndef LAZY
Chris Lattner56f330d2008-04-01 06:06:37 +000080 const MemoryBuffer *File =
Chris Lattnerd9606102008-04-01 18:04:30 +000081 MemoryBuffer::getFile(FileEnt->getName(), 0, FileEnt->getSize());
Chris Lattner4b009652007-07-25 00:24:17 +000082 if (File == 0)
83 return 0;
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000084#endif
85
Ted Kremenekdd364ea2007-10-30 21:08:08 +000086 ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt));
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000087#ifndef LAZY
Ted Kremenekaa7dac12009-01-06 01:55:26 +000088 Entry.setBuffer(File);
Ted Kremenek2bb9e6c2009-01-06 22:43:04 +000089#endif
Ted Kremenekdd364ea2007-10-30 21:08:08 +000090 Entry.SourceLineCache = 0;
91 Entry.NumLines = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000092 return &Entry;
93}
94
95
Ted Kremenek27f9c9b2007-10-31 17:53:38 +000096/// createMemBufferContentCache - Create a new ContentCache for the specified
97/// memory buffer. This does no caching.
Ted Kremenekdd364ea2007-10-30 21:08:08 +000098const ContentCache*
99SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Ted Kremenek7670cca2007-10-30 22:57:35 +0000100 // Add a new ContentCache to the MemBufferInfos list and return it. We
101 // must default construct the object first that the instance actually
102 // stored within MemBufferInfos actually owns the Buffer, and not any
103 // temporary we would use in the call to "push_back".
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000104 MemBufferInfos.push_back(ContentCache());
105 ContentCache& Entry = const_cast<ContentCache&>(MemBufferInfos.back());
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000106 Entry.setBuffer(Buffer);
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000107 return &Entry;
Chris Lattner4b009652007-07-25 00:24:17 +0000108}
109
110
Nico Weber630347d2008-09-29 00:25:48 +0000111/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek7670cca2007-10-30 22:57:35 +0000112/// include position. This works regardless of whether the ContentCache
113/// corresponds to a file or some other input source.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000114unsigned SourceManager::createFileID(const ContentCache *File,
Nico Weberd2a6ac92008-08-10 19:59:06 +0000115 SourceLocation IncludePos,
Chris Lattner7a4864e2008-10-27 01:19:25 +0000116 SrcMgr::CharacteristicKind FileCharacter) {
Chris Lattner4b009652007-07-25 00:24:17 +0000117 // If FileEnt is really large (e.g. it's a large .i file), we may not be able
118 // to fit an arbitrary position in the file in the FilePos field. To handle
119 // this, we create one FileID for each chunk of the file that fits in a
120 // FilePos field.
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000121 unsigned FileSize = File->getSize();
Chris Lattner4b009652007-07-25 00:24:17 +0000122 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) {
Chris Lattner6f044062008-09-26 21:18:42 +0000123 FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File, FileCharacter));
Chris Lattner4b009652007-07-25 00:24:17 +0000124 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
125 "Ran out of file ID's!");
126 return FileIDs.size();
127 }
128
129 // Create one FileID for each chunk of the file.
130 unsigned Result = FileIDs.size()+1;
131
132 unsigned ChunkNo = 0;
133 while (1) {
Nico Weberd2a6ac92008-08-10 19:59:06 +0000134 FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File,
Chris Lattner6f044062008-09-26 21:18:42 +0000135 FileCharacter));
Chris Lattner4b009652007-07-25 00:24:17 +0000136
137 if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break;
138 FileSize -= (1 << SourceLocation::FilePosBits);
139 }
140
141 assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
142 "Ran out of file ID's!");
143 return Result;
144}
145
146/// getInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnercdf600e2009-01-16 07:00:02 +0000147/// that a token from SpellingLoc should actually be referenced from
Chris Lattner4b009652007-07-25 00:24:17 +0000148/// InstantiationLoc.
Chris Lattnercdf600e2009-01-16 07:00:02 +0000149SourceLocation SourceManager::getInstantiationLoc(SourceLocation SpellingLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000150 SourceLocation InstantLoc) {
151 // The specified source location may be a mapped location, due to a macro
152 // instantiation or #line directive. Strip off this information to find out
153 // where the characters are actually located.
Chris Lattnercdf600e2009-01-16 07:00:02 +0000154 SpellingLoc = getSpellingLoc(SpellingLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000155
Chris Lattner18c8dc02009-01-16 07:36:28 +0000156 // Resolve InstantLoc down to a real instantiation location.
157 InstantLoc = getInstantiationLoc(InstantLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000158
159
160 // If the last macro id is close to the currently requested location, try to
Chris Lattnerc5c08972007-08-02 03:55:37 +0000161 // reuse it. This implements a small cache.
162 for (int i = MacroIDs.size()-1, e = MacroIDs.size()-6; i >= 0 && i != e; --i){
163 MacroIDInfo &LastOne = MacroIDs[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000164
Chris Lattnercdf600e2009-01-16 07:00:02 +0000165 // The instanitation point and source SpellingLoc have to exactly match to
166 // reuse (for now). We could allow "nearby" instantiations in the future.
Chris Lattner74f67012009-01-16 07:15:35 +0000167 if (LastOne.getInstantiationLoc() != InstantLoc ||
Chris Lattnercdf600e2009-01-16 07:00:02 +0000168 LastOne.getSpellingLoc().getFileID() != SpellingLoc.getFileID())
Chris Lattnerc5c08972007-08-02 03:55:37 +0000169 continue;
170
Chris Lattnercdf600e2009-01-16 07:00:02 +0000171 // Check to see if the spellloc of the token came from near enough to reuse.
172 int SpellDelta = SpellingLoc.getRawFilePos() -
173 LastOne.getSpellingLoc().getRawFilePos();
174 if (SourceLocation::isValidMacroSpellingOffs(SpellDelta))
175 return SourceLocation::getMacroLoc(i, SpellDelta);
Chris Lattner4b009652007-07-25 00:24:17 +0000176 }
177
178
Chris Lattnercdf600e2009-01-16 07:00:02 +0000179 MacroIDs.push_back(MacroIDInfo::get(InstantLoc, SpellingLoc));
Chris Lattnerdfcad782008-02-03 08:24:13 +0000180 return SourceLocation::getMacroLoc(MacroIDs.size()-1, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000181}
182
Chris Lattner569faa62007-10-11 18:38:32 +0000183/// getBufferData - Return a pointer to the start and end of the character
184/// data for the specified FileID.
185std::pair<const char*, const char*>
186SourceManager::getBufferData(unsigned FileID) const {
187 const llvm::MemoryBuffer *Buf = getBuffer(FileID);
188 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
189}
Chris Lattner4b009652007-07-25 00:24:17 +0000190
191
192/// getCharacterData - Return a pointer to the start of the specified location
193/// in the appropriate MemoryBuffer.
194const char *SourceManager::getCharacterData(SourceLocation SL) const {
195 // Note that this is a hot function in the getSpelling() path, which is
196 // heavily used by -E mode.
Chris Lattnercdf600e2009-01-16 07:00:02 +0000197 SL = getSpellingLoc(SL);
Chris Lattner4b009652007-07-25 00:24:17 +0000198
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000199 // Note that calling 'getBuffer()' may lazily page in a source file.
200 return getContentCache(SL.getFileID())->getBuffer()->getBufferStart() +
Chris Lattner4b009652007-07-25 00:24:17 +0000201 getFullFilePos(SL);
202}
203
204
205/// getColumnNumber - Return the column # for the specified file position.
206/// this is significantly cheaper to compute than the line number. This returns
207/// zero if the column number isn't known.
208unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
209 unsigned FileID = Loc.getFileID();
210 if (FileID == 0) return 0;
211
212 unsigned FilePos = getFullFilePos(Loc);
213 const MemoryBuffer *Buffer = getBuffer(FileID);
214 const char *Buf = Buffer->getBufferStart();
215
216 unsigned LineStart = FilePos;
217 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
218 --LineStart;
219 return FilePos-LineStart+1;
220}
221
222/// getSourceName - This method returns the name of the file or buffer that
223/// the SourceLocation specifies. This can be modified with #line directives,
224/// etc.
Chris Lattner37f041172007-08-30 05:59:30 +0000225const char *SourceManager::getSourceName(SourceLocation Loc) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000226 unsigned FileID = Loc.getFileID();
227 if (FileID == 0) return "";
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000228
229 // To get the source name, first consult the FileEntry (if one exists) before
230 // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer.
231 const SrcMgr::ContentCache* C = getContentCache(FileID);
232 return C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattner4b009652007-07-25 00:24:17 +0000233}
234
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000235static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000236static void ComputeLineNumbers(ContentCache* FI) {
237 // Note that calling 'getBuffer()' may lazily page in the file.
238 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000239
240 // Find the file offsets of all of the *physical* source lines. This does
241 // not look at trigraphs, escaped newlines, or anything else tricky.
242 std::vector<unsigned> LineOffsets;
243
244 // Line #1 starts at char 0.
245 LineOffsets.push_back(0);
246
247 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
248 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
249 unsigned Offs = 0;
250 while (1) {
251 // Skip over the contents of the line.
252 // TODO: Vectorize this? This is very performance sensitive for programs
253 // with lots of diagnostics and in -E mode.
254 const unsigned char *NextBuf = (const unsigned char *)Buf;
255 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
256 ++NextBuf;
257 Offs += NextBuf-Buf;
258 Buf = NextBuf;
259
260 if (Buf[0] == '\n' || Buf[0] == '\r') {
261 // If this is \n\r or \r\n, skip both characters.
262 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
263 ++Offs, ++Buf;
264 ++Offs, ++Buf;
265 LineOffsets.push_back(Offs);
266 } else {
267 // Otherwise, this is a null. If end of file, exit.
268 if (Buf == End) break;
269 // Otherwise, skip the null.
270 ++Offs, ++Buf;
271 }
272 }
Chris Lattner4b009652007-07-25 00:24:17 +0000273
274 // Copy the offsets into the FileInfo structure.
275 FI->NumLines = LineOffsets.size();
276 FI->SourceLineCache = new unsigned[LineOffsets.size()];
277 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
278}
279
Chris Lattnercdf600e2009-01-16 07:00:02 +0000280/// getLineNumber - Given a SourceLocation, return the spelling line number
Chris Lattner4b009652007-07-25 00:24:17 +0000281/// for the position indicated. This requires building and caching a table of
282/// line offsets for the MemoryBuffer, so this is not cheap: use only when
283/// about to emit a diagnostic.
Chris Lattnere9bf3e32008-11-18 06:51:15 +0000284unsigned SourceManager::getLineNumber(SourceLocation Loc) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000285 unsigned FileID = Loc.getFileID();
286 if (FileID == 0) return 0;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000287
288 ContentCache* Content;
Chris Lattner4b009652007-07-25 00:24:17 +0000289
290 if (LastLineNoFileIDQuery == FileID)
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000291 Content = LastLineNoContentCache;
Chris Lattner4b009652007-07-25 00:24:17 +0000292 else
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000293 Content = const_cast<ContentCache*>(getContentCache(FileID));
Chris Lattner4b009652007-07-25 00:24:17 +0000294
295 // If this is the first use of line information for this buffer, compute the
296 /// SourceLineCache for it on demand.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000297 if (Content->SourceLineCache == 0)
298 ComputeLineNumbers(Content);
Chris Lattner4b009652007-07-25 00:24:17 +0000299
300 // Okay, we know we have a line number table. Do a binary search to find the
301 // line number that this character position lands on.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000302 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner4b009652007-07-25 00:24:17 +0000303 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000304 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner4b009652007-07-25 00:24:17 +0000305
306 unsigned QueriedFilePos = getFullFilePos(Loc)+1;
307
308 // If the previous query was to the same file, we know both the file pos from
309 // that query and the line number returned. This allows us to narrow the
310 // search space from the entire file to something near the match.
311 if (LastLineNoFileIDQuery == FileID) {
312 if (QueriedFilePos >= LastLineNoFilePos) {
313 SourceLineCache = SourceLineCache+LastLineNoResult-1;
314
315 // The query is likely to be nearby the previous one. Here we check to
316 // see if it is within 5, 10 or 20 lines. It can be far away in cases
317 // where big comment blocks and vertical whitespace eat up lines but
318 // contribute no tokens.
319 if (SourceLineCache+5 < SourceLineCacheEnd) {
320 if (SourceLineCache[5] > QueriedFilePos)
321 SourceLineCacheEnd = SourceLineCache+5;
322 else if (SourceLineCache+10 < SourceLineCacheEnd) {
323 if (SourceLineCache[10] > QueriedFilePos)
324 SourceLineCacheEnd = SourceLineCache+10;
325 else if (SourceLineCache+20 < SourceLineCacheEnd) {
326 if (SourceLineCache[20] > QueriedFilePos)
327 SourceLineCacheEnd = SourceLineCache+20;
328 }
329 }
330 }
331 } else {
332 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
333 }
334 }
335
336 // If the spread is large, do a "radix" test as our initial guess, based on
337 // the assumption that lines average to approximately the same length.
338 // NOTE: This is currently disabled, as it does not appear to be profitable in
339 // initial measurements.
340 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000341 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner4b009652007-07-25 00:24:17 +0000342
343 // Take a stab at guessing where it is.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000344 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner4b009652007-07-25 00:24:17 +0000345
346 // Check for -10 and +10 lines.
347 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
348 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
349
350 // If the computed lower bound is less than the query location, move it in.
351 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
352 SourceLineCacheStart[LowerBound] < QueriedFilePos)
353 SourceLineCache = SourceLineCacheStart+LowerBound;
354
355 // If the computed upper bound is greater than the query location, move it.
356 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
357 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
358 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
359 }
360
361 unsigned *Pos
362 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
363 unsigned LineNo = Pos-SourceLineCacheStart;
364
365 LastLineNoFileIDQuery = FileID;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000366 LastLineNoContentCache = Content;
Chris Lattner4b009652007-07-25 00:24:17 +0000367 LastLineNoFilePos = QueriedFilePos;
368 LastLineNoResult = LineNo;
369 return LineNo;
370}
371
372/// PrintStats - Print statistics to stderr.
373///
374void SourceManager::PrintStats() const {
Ted Kremenekda29d8c2007-12-05 22:21:13 +0000375 llvm::cerr << "\n*** Source Manager Stats:\n";
376 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
377 << " mem buffers mapped, " << FileIDs.size()
378 << " file ID's allocated.\n";
379 llvm::cerr << " " << FileIDs.size() << " normal buffer FileID's, "
380 << MacroIDs.size() << " macro expansion FileID's.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000381
Chris Lattner4b009652007-07-25 00:24:17 +0000382 unsigned NumLineNumsComputed = 0;
383 unsigned NumFileBytesMapped = 0;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000384 for (std::set<ContentCache>::const_iterator I =
Chris Lattner4b009652007-07-25 00:24:17 +0000385 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000386 NumLineNumsComputed += I->SourceLineCache != 0;
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000387 NumFileBytesMapped += I->getSizeBytesMapped();
Chris Lattner4b009652007-07-25 00:24:17 +0000388 }
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000389
Ted Kremenekda29d8c2007-12-05 22:21:13 +0000390 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
391 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000392}
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000393
394//===----------------------------------------------------------------------===//
395// Serialization.
396//===----------------------------------------------------------------------===//
Ted Kremenek9c856e92007-12-05 00:14:18 +0000397
398void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000399 S.FlushRecord();
400 S.EmitPtr(this);
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000401
Ted Kremenek1b6dd6f2007-12-18 22:12:19 +0000402 if (Entry) {
403 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
404
405 if (Fname.isAbsolute())
406 S.EmitCStr(Fname.c_str());
407 else {
408 // Create an absolute path.
409 // FIXME: This will potentially contain ".." and "." in the path.
410 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
411 path.appendComponent(Fname.c_str());
412 S.EmitCStr(path.c_str());
413 }
414 }
Ted Kremenek9c856e92007-12-05 00:14:18 +0000415 else {
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000416 const char* p = Buffer->getBufferStart();
417 const char* e = Buffer->getBufferEnd();
418
Ted Kremenek9c856e92007-12-05 00:14:18 +0000419 S.EmitInt(e-p);
420
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000421 for ( ; p != e; ++p)
Ted Kremenek9c856e92007-12-05 00:14:18 +0000422 S.EmitInt(*p);
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000423 }
424
Ted Kremenek9c856e92007-12-05 00:14:18 +0000425 S.FlushRecord();
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000426}
Ted Kremenek9c856e92007-12-05 00:14:18 +0000427
428void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
429 SourceManager& SMgr,
430 FileManager* FMgr,
431 std::vector<char>& Buf) {
432 if (FMgr) {
433 llvm::SerializedPtrID PtrID = D.ReadPtrID();
434 D.ReadCStr(Buf,false);
435
436 // Create/fetch the FileEntry.
437 const char* start = &Buf[0];
438 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
439
Ted Kremenekb92cd872007-12-13 18:12:10 +0000440 // FIXME: Ideally we want a lazy materialization of the ContentCache
441 // anyway, because we don't want to read in source files unless this
442 // is absolutely needed.
443 if (!E)
444 D.RegisterPtr(PtrID,NULL);
Nico Weber630347d2008-09-29 00:25:48 +0000445 else
Ted Kremenekb92cd872007-12-13 18:12:10 +0000446 // Get the ContextCache object and register it with the deserializer.
447 D.RegisterPtr(PtrID,SMgr.getContentCache(E));
Ted Kremenek9c856e92007-12-05 00:14:18 +0000448 }
449 else {
450 // Register the ContextCache object with the deserializer.
451 SMgr.MemBufferInfos.push_back(ContentCache());
Nico Weber630347d2008-09-29 00:25:48 +0000452 ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
Ted Kremenek9c856e92007-12-05 00:14:18 +0000453 D.RegisterPtr(&Entry);
454
455 // Create the buffer.
456 unsigned Size = D.ReadInt();
457 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
458
459 // Read the contents of the buffer.
460 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
461 for (unsigned i = 0; i < Size ; ++i)
462 p[i] = D.ReadInt();
463 }
464}
465
466void FileIDInfo::Emit(llvm::Serializer& S) const {
467 S.Emit(IncludeLoc);
468 S.EmitInt(ChunkNo);
469 S.EmitPtr(Content);
470}
471
472FileIDInfo FileIDInfo::ReadVal(llvm::Deserializer& D) {
473 FileIDInfo I;
474 I.IncludeLoc = SourceLocation::ReadVal(D);
475 I.ChunkNo = D.ReadInt();
476 D.ReadPtr(I.Content,false);
477 return I;
478}
479
480void MacroIDInfo::Emit(llvm::Serializer& S) const {
Chris Lattner74f67012009-01-16 07:15:35 +0000481 S.Emit(InstantiationLoc);
Chris Lattnercdf600e2009-01-16 07:00:02 +0000482 S.Emit(SpellingLoc);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000483}
484
485MacroIDInfo MacroIDInfo::ReadVal(llvm::Deserializer& D) {
486 MacroIDInfo I;
Chris Lattner74f67012009-01-16 07:15:35 +0000487 I.InstantiationLoc = SourceLocation::ReadVal(D);
Chris Lattnercdf600e2009-01-16 07:00:02 +0000488 I.SpellingLoc = SourceLocation::ReadVal(D);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000489 return I;
490}
491
492void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000493 S.EnterBlock();
494 S.EmitPtr(this);
Ted Kremenek2578dd02007-12-19 22:29:55 +0000495 S.EmitInt(MainFileID);
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000496
Ted Kremenek9c856e92007-12-05 00:14:18 +0000497 // Emit: FileInfos. Just emit the file name.
498 S.EnterBlock();
499
500 std::for_each(FileInfos.begin(),FileInfos.end(),
501 S.MakeEmitter<ContentCache>());
502
503 S.ExitBlock();
504
505 // Emit: MemBufferInfos
506 S.EnterBlock();
507
508 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
509 S.MakeEmitter<ContentCache>());
510
511 S.ExitBlock();
512
Nico Weber630347d2008-09-29 00:25:48 +0000513 // Emit: FileIDs
Ted Kremenek9c856e92007-12-05 00:14:18 +0000514 S.EmitInt(FileIDs.size());
515 std::for_each(FileIDs.begin(), FileIDs.end(), S.MakeEmitter<FileIDInfo>());
516
517 // Emit: MacroIDs
518 S.EmitInt(MacroIDs.size());
519 std::for_each(MacroIDs.begin(), MacroIDs.end(), S.MakeEmitter<MacroIDInfo>());
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000520
521 S.ExitBlock();
Ted Kremenek9c856e92007-12-05 00:14:18 +0000522}
523
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000524SourceManager*
525SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
526 SourceManager *M = new SourceManager();
527 D.RegisterPtr(M);
528
Ted Kremenek2578dd02007-12-19 22:29:55 +0000529 // Read: the FileID of the main source file of the translation unit.
530 M->MainFileID = D.ReadInt();
531
Ted Kremenek9c856e92007-12-05 00:14:18 +0000532 std::vector<char> Buf;
533
534 { // Read: FileInfos.
535 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
536 while (!D.FinishedBlock(BLoc))
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000537 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000538 }
539
540 { // Read: MemBufferInfos.
541 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
542 while (!D.FinishedBlock(BLoc))
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000543 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000544 }
545
546 // Read: FileIDs.
547 unsigned Size = D.ReadInt();
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000548 M->FileIDs.reserve(Size);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000549 for (; Size > 0 ; --Size)
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000550 M->FileIDs.push_back(FileIDInfo::ReadVal(D));
Ted Kremenek9c856e92007-12-05 00:14:18 +0000551
552 // Read: MacroIDs.
553 Size = D.ReadInt();
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000554 M->MacroIDs.reserve(Size);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000555 for (; Size > 0 ; --Size)
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000556 M->MacroIDs.push_back(MacroIDInfo::ReadVal(D));
557
558 return M;
Ted Kremenek12206af2007-12-10 18:01:25 +0000559}