blob: ce9efb034a9375f7d1e692477c0e927e19285d25 [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
Chris Lattnerde7aeef2009-01-26 00:43:02 +000027//===--------------------------------------------------------------------===//
28// SourceManager Helper Classes
29//===--------------------------------------------------------------------===//
30
Ted Kremenek78d85f52007-10-30 21:08:08 +000031ContentCache::~ContentCache() {
32 delete Buffer;
Reid Spencer5f016e22007-07-11 17:01:13 +000033}
34
Ted Kremenekc16c2082009-01-06 01:55:26 +000035/// getSizeBytesMapped - Returns the number of bytes actually mapped for
36/// this ContentCache. This can be 0 if the MemBuffer was not actually
37/// instantiated.
38unsigned ContentCache::getSizeBytesMapped() const {
39 return Buffer ? Buffer->getBufferSize() : 0;
40}
41
42/// getSize - Returns the size of the content encapsulated by this ContentCache.
43/// This can be the size of the source file or the size of an arbitrary
44/// scratch buffer. If the ContentCache encapsulates a source file, that
45/// file is not lazily brought in from disk to satisfy this query.
46unsigned ContentCache::getSize() const {
47 return Entry ? Entry->getSize() : Buffer->getBufferSize();
48}
49
Chris Lattner987cd3d2009-01-26 07:37:49 +000050const llvm::MemoryBuffer *ContentCache::getBuffer() const {
Ted Kremenek5b034ad2009-01-06 22:43:04 +000051 // Lazily create the Buffer for ContentCaches that wrap files.
52 if (!Buffer && Entry) {
53 // FIXME: Should we support a way to not have to do this check over
54 // and over if we cannot open the file?
Chris Lattner05816592009-01-17 03:54:16 +000055 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
Ted Kremenek5b034ad2009-01-06 22:43:04 +000056 }
Ted Kremenekc16c2082009-01-06 01:55:26 +000057 return Buffer;
58}
59
Chris Lattnerde7aeef2009-01-26 00:43:02 +000060//===--------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +000061// Line Table Implementation
62//===--------------------------------------------------------------------===//
63
64namespace clang {
65/// LineTableInfo - This class is used to hold and unique data used to
66/// represent #line information.
67class LineTableInfo {
68 /// FilenameIDs - This map is used to assign unique IDs to filenames in
69 /// #line directives. This allows us to unique the filenames that
70 /// frequently reoccur and reference them with indices. FilenameIDs holds
71 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
72 /// to string.
73 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
74 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
75public:
76 LineTableInfo() {
77 }
78
79 void clear() {
80 FilenameIDs.clear();
81 FilenamesByID.clear();
82 }
83
84 ~LineTableInfo() {}
85
86 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
87
88};
89} // namespace clang
90
91
92
93
94unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
95 // Look up the filename in the string table, returning the pre-existing value
96 // if it exists.
97 llvm::StringMapEntry<unsigned> &Entry =
98 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
99 if (Entry.getValue() != ~0U)
100 return Entry.getValue();
101
102 // Otherwise, assign this the next available ID.
103 Entry.setValue(FilenamesByID.size());
104 FilenamesByID.push_back(&Entry);
105 return FilenamesByID.size()-1;
106}
107
108/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
109///
110unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
111 if (LineTable == 0)
112 LineTable = new LineTableInfo();
113 return LineTable->getLineTableFilenameID(Ptr, Len);
114}
115
116
Chris Lattner4c4ea172009-02-03 21:52:55 +0000117/// AddLineNote - Add a line note to the line table for the FileID and offset
118/// specified by Loc. If FilenameID is -1, it is considered to be
119/// unspecified.
120void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
121 int FilenameID) {
122
123}
124
125
Chris Lattner5b9a5042009-01-26 07:57:50 +0000126//===--------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000127// Private 'Create' methods.
128//===--------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000129
Chris Lattner5b9a5042009-01-26 07:57:50 +0000130SourceManager::~SourceManager() {
131 delete LineTable;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000132
133 // Delete FileEntry objects corresponding to content caches. Since the actual
134 // content cache objects are bump pointer allocated, we just have to run the
135 // dtors, but we call the deallocate method for completeness.
136 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
137 MemBufferInfos[i]->~ContentCache();
138 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
139 }
140 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
141 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
142 I->second->~ContentCache();
143 ContentCacheAlloc.Deallocate(I->second);
144 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000145}
146
147void SourceManager::clearIDTables() {
148 MainFileID = FileID();
149 SLocEntryTable.clear();
150 LastLineNoFileIDQuery = FileID();
151 LastLineNoContentCache = 0;
152 LastFileIDLookup = FileID();
153
154 if (LineTable)
155 LineTable->clear();
156
157 // Use up FileID #0 as an invalid instantiation.
158 NextOffset = 0;
159 createInstantiationLoc(SourceLocation(), SourceLocation(), 1);
160}
161
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000162/// getOrCreateContentCache - Create or return a cached ContentCache for the
163/// specified file.
164const ContentCache *
165SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000167
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 // Do we already have information about this file?
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000169 ContentCache *&Entry = FileInfos[FileEnt];
170 if (Entry) return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000171
Chris Lattner00282d62009-02-03 07:41:46 +0000172 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
173 // so that FileInfo can use the low 3 bits of the pointer for its own
174 // nefarious purposes.
175 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
176 EntryAlign = std::max(8U, EntryAlign);
177 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000178 new (Entry) ContentCache(FileEnt);
179 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
181
182
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000183/// createMemBufferContentCache - Create a new ContentCache for the specified
184/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000185const ContentCache*
186SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner00282d62009-02-03 07:41:46 +0000187 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
188 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
189 // the pointer for its own nefarious purposes.
190 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
191 EntryAlign = std::max(8U, EntryAlign);
192 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000193 new (Entry) ContentCache();
194 MemBufferInfos.push_back(Entry);
195 Entry->setBuffer(Buffer);
196 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000197}
198
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000199//===----------------------------------------------------------------------===//
200// Methods to create new FileID's and instantiations.
201//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000202
Nico Weber48002c82008-09-29 00:25:48 +0000203/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000204/// include position. This works regardless of whether the ContentCache
205/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000206FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000207 SourceLocation IncludePos,
208 SrcMgr::CharacteristicKind FileCharacter) {
209 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
210 FileInfo::get(IncludePos, File,
211 FileCharacter)));
Ted Kremenekc16c2082009-01-06 01:55:26 +0000212 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000213 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
214 NextOffset += FileSize+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000215
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000216 // Set LastFileIDLookup to the newly created file. The next getFileID call is
217 // almost guaranteed to be from that file.
218 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000219}
220
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000221/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000222/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000223/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000224SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
225 SourceLocation InstantLoc,
226 unsigned TokLength) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000227 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
228 InstantiationInfo::get(InstantLoc,
229 SpellingLoc)));
230 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
231 NextOffset += TokLength+1;
232 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000233}
234
Chris Lattner31530ba2009-01-19 07:32:13 +0000235/// getBufferData - Return a pointer to the start and end of the source buffer
236/// data for the specified FileID.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000237std::pair<const char*, const char*>
238SourceManager::getBufferData(FileID FID) const {
239 const llvm::MemoryBuffer *Buf = getBuffer(FID);
240 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
241}
242
243
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000244//===--------------------------------------------------------------------===//
245// SourceLocation manipulation methods.
246//===--------------------------------------------------------------------===//
247
248/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
249/// method that is used for all SourceManager queries that start with a
250/// SourceLocation object. It is responsible for finding the entry in
251/// SLocEntryTable which contains the specified location.
252///
253FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
254 assert(SLocOffset && "Invalid FileID");
255
256 // After the first and second level caches, I see two common sorts of
257 // behavior: 1) a lot of searched FileID's are "near" the cached file location
258 // or are "near" the cached instantiation location. 2) others are just
259 // completely random and may be a very long way away.
260 //
261 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
262 // then we fall back to a less cache efficient, but more scalable, binary
263 // search to find the location.
264
265 // See if this is near the file point - worst case we start scanning from the
266 // most newly created FileID.
267 std::vector<SrcMgr::SLocEntry>::const_iterator I;
268
269 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
270 // Neither loc prunes our search.
271 I = SLocEntryTable.end();
272 } else {
273 // Perhaps it is near the file point.
274 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
275 }
276
277 // Find the FileID that contains this. "I" is an iterator that points to a
278 // FileID whose offset is known to be larger than SLocOffset.
279 unsigned NumProbes = 0;
280 while (1) {
281 --I;
282 if (I->getOffset() <= SLocOffset) {
283#if 0
284 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
285 I-SLocEntryTable.begin(),
286 I->isInstantiation() ? "inst" : "file",
287 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
288#endif
289 FileID Res = FileID::get(I-SLocEntryTable.begin());
290
291 // If this isn't an instantiation, remember it. We have good locality
292 // across FileID lookups.
293 if (!I->isInstantiation())
294 LastFileIDLookup = Res;
295 NumLinearScans += NumProbes+1;
296 return Res;
297 }
298 if (++NumProbes == 8)
299 break;
300 }
301
302 // Convert "I" back into an index. We know that it is an entry whose index is
303 // larger than the offset we are looking for.
304 unsigned GreaterIndex = I-SLocEntryTable.begin();
305 // LessIndex - This is the lower bound of the range that we're searching.
306 // We know that the offset corresponding to the FileID is is less than
307 // SLocOffset.
308 unsigned LessIndex = 0;
309 NumProbes = 0;
310 while (1) {
311 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
312 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
313
314 ++NumProbes;
315
316 // If the offset of the midpoint is too large, chop the high side of the
317 // range to the midpoint.
318 if (MidOffset > SLocOffset) {
319 GreaterIndex = MiddleIndex;
320 continue;
321 }
322
323 // If the middle index contains the value, succeed and return.
324 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
325#if 0
326 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
327 I-SLocEntryTable.begin(),
328 I->isInstantiation() ? "inst" : "file",
329 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
330#endif
331 FileID Res = FileID::get(MiddleIndex);
332
333 // If this isn't an instantiation, remember it. We have good locality
334 // across FileID lookups.
335 if (!I->isInstantiation())
336 LastFileIDLookup = Res;
337 NumBinaryProbes += NumProbes;
338 return Res;
339 }
340
341 // Otherwise, move the low-side up to the middle index.
342 LessIndex = MiddleIndex;
343 }
344}
345
Chris Lattneraddb7972009-01-26 20:04:19 +0000346SourceLocation SourceManager::
347getInstantiationLocSlowCase(SourceLocation Loc) const {
348 do {
349 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
350 Loc =getSLocEntry(LocInfo.first).getInstantiation().getInstantiationLoc();
351 Loc = Loc.getFileLocWithOffset(LocInfo.second);
352 } while (!Loc.isFileID());
353
354 return Loc;
355}
356
357SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
358 do {
359 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
360 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
361 Loc = Loc.getFileLocWithOffset(LocInfo.second);
362 } while (!Loc.isFileID());
363 return Loc;
364}
365
366
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000367std::pair<FileID, unsigned>
368SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
369 unsigned Offset) const {
370 // If this is an instantiation record, walk through all the instantiation
371 // points.
372 FileID FID;
373 SourceLocation Loc;
374 do {
375 Loc = E->getInstantiation().getInstantiationLoc();
376
377 FID = getFileID(Loc);
378 E = &getSLocEntry(FID);
379 Offset += Loc.getOffset()-E->getOffset();
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000380 } while (!Loc.isFileID());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000381
382 return std::make_pair(FID, Offset);
383}
384
385std::pair<FileID, unsigned>
386SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
387 unsigned Offset) const {
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000388 // If this is an instantiation record, walk through all the instantiation
389 // points.
390 FileID FID;
391 SourceLocation Loc;
392 do {
393 Loc = E->getInstantiation().getSpellingLoc();
394
395 FID = getFileID(Loc);
396 E = &getSLocEntry(FID);
397 Offset += Loc.getOffset()-E->getOffset();
398 } while (!Loc.isFileID());
399
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000400 return std::make_pair(FID, Offset);
401}
402
403
404//===----------------------------------------------------------------------===//
405// Queries about the code at a SourceLocation.
406//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000407
408/// getCharacterData - Return a pointer to the start of the specified location
409/// in the appropriate MemoryBuffer.
410const char *SourceManager::getCharacterData(SourceLocation SL) const {
411 // Note that this is a hot function in the getSpelling() path, which is
412 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000413 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000414
Ted Kremenekc16c2082009-01-06 01:55:26 +0000415 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000416 return getSLocEntry(LocInfo.first).getFile().getContentCache()
417 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000418}
419
Reid Spencer5f016e22007-07-11 17:01:13 +0000420
Chris Lattner9dc1f532007-07-20 16:37:10 +0000421/// getColumnNumber - Return the column # for the specified file position.
Reid Spencer5f016e22007-07-11 17:01:13 +0000422/// this is significantly cheaper to compute than the line number. This returns
423/// zero if the column number isn't known.
424unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000425 if (Loc.isInvalid()) return 0;
426 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
Reid Spencer5f016e22007-07-11 17:01:13 +0000427
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000428 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000429 unsigned FilePos = LocInfo.second;
430
431 const char *Buf = getBuffer(LocInfo.first)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000432
433 unsigned LineStart = FilePos;
434 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
435 --LineStart;
436 return FilePos-LineStart+1;
437}
438
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000439static void ComputeLineNumbers(ContentCache* FI,
440 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
441static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekc16c2082009-01-06 01:55:26 +0000442 // Note that calling 'getBuffer()' may lazily page in the file.
443 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000444
445 // Find the file offsets of all of the *physical* source lines. This does
446 // not look at trigraphs, escaped newlines, or anything else tricky.
447 std::vector<unsigned> LineOffsets;
448
449 // Line #1 starts at char 0.
450 LineOffsets.push_back(0);
451
452 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
453 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
454 unsigned Offs = 0;
455 while (1) {
456 // Skip over the contents of the line.
457 // TODO: Vectorize this? This is very performance sensitive for programs
458 // with lots of diagnostics and in -E mode.
459 const unsigned char *NextBuf = (const unsigned char *)Buf;
460 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
461 ++NextBuf;
462 Offs += NextBuf-Buf;
463 Buf = NextBuf;
464
465 if (Buf[0] == '\n' || Buf[0] == '\r') {
466 // If this is \n\r or \r\n, skip both characters.
467 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
468 ++Offs, ++Buf;
469 ++Offs, ++Buf;
470 LineOffsets.push_back(Offs);
471 } else {
472 // Otherwise, this is a null. If end of file, exit.
473 if (Buf == End) break;
474 // Otherwise, skip the null.
475 ++Offs, ++Buf;
476 }
477 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000478
479 // Copy the offsets into the FileInfo structure.
480 FI->NumLines = LineOffsets.size();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000481 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000482 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
483}
Reid Spencer5f016e22007-07-11 17:01:13 +0000484
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000485/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000486/// for the position indicated. This requires building and caching a table of
487/// line offsets for the MemoryBuffer, so this is not cheap: use only when
488/// about to emit a diagnostic.
Chris Lattnerf812a452008-11-18 06:51:15 +0000489unsigned SourceManager::getLineNumber(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000490 if (Loc.isInvalid()) return 0;
491 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
Ted Kremenek78d85f52007-10-30 21:08:08 +0000492
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000493 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
494
Chris Lattner2b2453a2009-01-17 06:22:33 +0000495 ContentCache *Content;
Chris Lattner2b2453a2009-01-17 06:22:33 +0000496 if (LastLineNoFileIDQuery == LocInfo.first)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000497 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000498 else
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000499 Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first)
500 .getFile().getContentCache());
Reid Spencer5f016e22007-07-11 17:01:13 +0000501
502 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000503 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000504 if (Content->SourceLineCache == 0)
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000505 ComputeLineNumbers(Content, ContentCacheAlloc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000506
507 // Okay, we know we have a line number table. Do a binary search to find the
508 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000509 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000510 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000511 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000512
Chris Lattner2b2453a2009-01-17 06:22:33 +0000513 unsigned QueriedFilePos = LocInfo.second+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000514
515 // If the previous query was to the same file, we know both the file pos from
516 // that query and the line number returned. This allows us to narrow the
517 // search space from the entire file to something near the match.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000518 if (LastLineNoFileIDQuery == LocInfo.first) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000519 if (QueriedFilePos >= LastLineNoFilePos) {
520 SourceLineCache = SourceLineCache+LastLineNoResult-1;
521
522 // The query is likely to be nearby the previous one. Here we check to
523 // see if it is within 5, 10 or 20 lines. It can be far away in cases
524 // where big comment blocks and vertical whitespace eat up lines but
525 // contribute no tokens.
526 if (SourceLineCache+5 < SourceLineCacheEnd) {
527 if (SourceLineCache[5] > QueriedFilePos)
528 SourceLineCacheEnd = SourceLineCache+5;
529 else if (SourceLineCache+10 < SourceLineCacheEnd) {
530 if (SourceLineCache[10] > QueriedFilePos)
531 SourceLineCacheEnd = SourceLineCache+10;
532 else if (SourceLineCache+20 < SourceLineCacheEnd) {
533 if (SourceLineCache[20] > QueriedFilePos)
534 SourceLineCacheEnd = SourceLineCache+20;
535 }
536 }
537 }
538 } else {
539 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
540 }
541 }
542
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000543 // If the spread is large, do a "radix" test as our initial guess, based on
544 // the assumption that lines average to approximately the same length.
545 // NOTE: This is currently disabled, as it does not appear to be profitable in
546 // initial measurements.
547 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000548 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000549
550 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000551 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000552
553 // Check for -10 and +10 lines.
554 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
555 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
556
557 // If the computed lower bound is less than the query location, move it in.
558 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
559 SourceLineCacheStart[LowerBound] < QueriedFilePos)
560 SourceLineCache = SourceLineCacheStart+LowerBound;
561
562 // If the computed upper bound is greater than the query location, move it.
563 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
564 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
565 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
566 }
567
568 unsigned *Pos
569 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000570 unsigned LineNo = Pos-SourceLineCacheStart;
571
Chris Lattner2b2453a2009-01-17 06:22:33 +0000572 LastLineNoFileIDQuery = LocInfo.first;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000573 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000574 LastLineNoFilePos = QueriedFilePos;
575 LastLineNoResult = LineNo;
576 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000577}
578
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000579/// getPresumedLoc - This method returns the "presumed" location of a
580/// SourceLocation specifies. A "presumed location" can be modified by #line
581/// or GNU line marker directives. This provides a view on the data that a
582/// user should see in diagnostics, for example.
583///
584/// Note that a presumed location is always given as the instantiation point
585/// of an instantiation location, not at the spelling location.
586PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
587 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000588
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000589 // Presumed locations are always for instantiation points.
590 Loc = getInstantiationLoc(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000591
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000592 // FIXME: Could just decompose Loc once!
593
594 const SrcMgr::FileInfo &FI = getSLocEntry(getFileID(Loc)).getFile();
595 const SrcMgr::ContentCache *C = FI.getContentCache();
596
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000597 // To get the source name, first consult the FileEntry (if one exists) before
598 // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000599 const char *Filename =
600 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
601
602 return PresumedLoc(Filename, getLineNumber(Loc), getColumnNumber(Loc),
603 FI.getIncludeLoc());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000604}
605
606//===----------------------------------------------------------------------===//
607// Other miscellaneous methods.
608//===----------------------------------------------------------------------===//
609
610
Reid Spencer5f016e22007-07-11 17:01:13 +0000611/// PrintStats - Print statistics to stderr.
612///
613void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000614 llvm::cerr << "\n*** Source Manager Stats:\n";
615 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner08c375c2009-01-27 05:22:43 +0000616 << " mem buffers mapped.\n";
617 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
618 << NextOffset << "B of Sloc address space used.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000619
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 unsigned NumLineNumsComputed = 0;
621 unsigned NumFileBytesMapped = 0;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000622 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
623 NumLineNumsComputed += I->second->SourceLineCache != 0;
624 NumFileBytesMapped += I->second->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000626
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000627 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
628 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000629 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
630 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000631}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000632
633//===----------------------------------------------------------------------===//
634// Serialization.
635//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000636
637void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000638 S.FlushRecord();
639 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000640
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000641 if (Entry) {
642 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
643
644 if (Fname.isAbsolute())
645 S.EmitCStr(Fname.c_str());
646 else {
647 // Create an absolute path.
648 // FIXME: This will potentially contain ".." and "." in the path.
649 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
650 path.appendComponent(Fname.c_str());
651 S.EmitCStr(path.c_str());
652 }
653 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000654 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000655 const char* p = Buffer->getBufferStart();
656 const char* e = Buffer->getBufferEnd();
657
Ted Kremenek099b4742007-12-05 00:14:18 +0000658 S.EmitInt(e-p);
659
Ted Kremeneke21272f2007-12-04 19:39:02 +0000660 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000661 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000662 }
663
Ted Kremenek099b4742007-12-05 00:14:18 +0000664 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000665}
Ted Kremenek099b4742007-12-05 00:14:18 +0000666
667void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
668 SourceManager& SMgr,
669 FileManager* FMgr,
670 std::vector<char>& Buf) {
671 if (FMgr) {
672 llvm::SerializedPtrID PtrID = D.ReadPtrID();
673 D.ReadCStr(Buf,false);
674
675 // Create/fetch the FileEntry.
676 const char* start = &Buf[0];
677 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
678
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000679 // FIXME: Ideally we want a lazy materialization of the ContentCache
680 // anyway, because we don't want to read in source files unless this
681 // is absolutely needed.
682 if (!E)
683 D.RegisterPtr(PtrID,NULL);
Nico Weber48002c82008-09-29 00:25:48 +0000684 else
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000685 // Get the ContextCache object and register it with the deserializer.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000686 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
687 return;
Ted Kremenek099b4742007-12-05 00:14:18 +0000688 }
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000689
690 // Register the ContextCache object with the deserializer.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000691 /* FIXME:
692 ContentCache *Entry
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000693 SMgr.MemBufferInfos.push_back(ContentCache());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000694 = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000695 D.RegisterPtr(&Entry);
696
697 // Create the buffer.
698 unsigned Size = D.ReadInt();
699 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
700
701 // Read the contents of the buffer.
702 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
703 for (unsigned i = 0; i < Size ; ++i)
704 p[i] = D.ReadInt();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000705 */
Ted Kremenek099b4742007-12-05 00:14:18 +0000706}
707
708void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +0000709 S.EnterBlock();
710 S.EmitPtr(this);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000711 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenek1f941002007-12-05 00:19:51 +0000712
Ted Kremenek099b4742007-12-05 00:14:18 +0000713 // Emit: FileInfos. Just emit the file name.
714 S.EnterBlock();
715
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000716 // FIXME: Emit FileInfos.
717 //std::for_each(FileInfos.begin(), FileInfos.end(),
718 // S.MakeEmitter<ContentCache>());
Ted Kremenek099b4742007-12-05 00:14:18 +0000719
720 S.ExitBlock();
721
722 // Emit: MemBufferInfos
723 S.EnterBlock();
724
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000725 /* FIXME: EMIT.
Ted Kremenek099b4742007-12-05 00:14:18 +0000726 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
727 S.MakeEmitter<ContentCache>());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000728 */
Ted Kremenek099b4742007-12-05 00:14:18 +0000729
730 S.ExitBlock();
731
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000732 // FIXME: Emit SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000733
734 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +0000735}
736
Ted Kremenek1f941002007-12-05 00:19:51 +0000737SourceManager*
738SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
739 SourceManager *M = new SourceManager();
740 D.RegisterPtr(M);
741
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000742 // Read: the FileID of the main source file of the translation unit.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000743 M->MainFileID = FileID::get(D.ReadInt());
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000744
Ted Kremenek099b4742007-12-05 00:14:18 +0000745 std::vector<char> Buf;
746
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000747 /*{ // FIXME Read: FileInfos.
Ted Kremenek099b4742007-12-05 00:14:18 +0000748 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
749 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000750 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000751 }*/
Ted Kremenek099b4742007-12-05 00:14:18 +0000752
753 { // Read: MemBufferInfos.
754 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
755 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +0000756 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +0000757 }
758
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000759 // FIXME: Read SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +0000760
761 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +0000762}