blob: 2b581bb1ed32f8aa8334be1b506136a241f5e42b [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 Lattner23b5dc62009-02-04 00:40:31 +000027//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000028// SourceManager Helper Classes
Chris Lattner23b5dc62009-02-04 00:40:31 +000029//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000030
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 Lattner23b5dc62009-02-04 00:40:31 +000060//===----------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +000061// Line Table Implementation
Chris Lattner23b5dc62009-02-04 00:40:31 +000062//===----------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +000063
64namespace clang {
Chris Lattner23b5dc62009-02-04 00:40:31 +000065struct LineEntry {
66 /// FileOffset - The offset in this file that the line entry occurs at.
67 unsigned FileOffset;
Chris Lattner9d79eba2009-02-04 05:21:58 +000068
Chris Lattner23b5dc62009-02-04 00:40:31 +000069 /// LineNo - The presumed line number of this line entry: #line 4.
70 unsigned LineNo;
Chris Lattner9d79eba2009-02-04 05:21:58 +000071
Chris Lattner23b5dc62009-02-04 00:40:31 +000072 /// FilenameID - The ID of the filename identified by this line entry:
73 /// #line 4 "foo.c". This is -1 if not specified.
74 int FilenameID;
75
Chris Lattner9d79eba2009-02-04 05:21:58 +000076 /// Flags - Set the 0 if no flags, 1 if a system header,
77 SrcMgr::CharacteristicKind FileKind;
78
Chris Lattner137b6a62009-02-04 06:25:26 +000079 /// IncludeOffset - This is the offset of the virtual include stack location,
80 /// which is manipulated by GNU linemarker directives. If this is 0 then
81 /// there is no virtual #includer.
82 unsigned IncludeOffset;
83
Chris Lattner9d79eba2009-02-04 05:21:58 +000084 static LineEntry get(unsigned Offs, unsigned Line, int Filename,
Chris Lattner137b6a62009-02-04 06:25:26 +000085 SrcMgr::CharacteristicKind FileKind,
86 unsigned IncludeOffset) {
Chris Lattner23b5dc62009-02-04 00:40:31 +000087 LineEntry E;
88 E.FileOffset = Offs;
89 E.LineNo = Line;
90 E.FilenameID = Filename;
Chris Lattner6b306672009-02-04 05:33:01 +000091 E.FileKind = FileKind;
Chris Lattner137b6a62009-02-04 06:25:26 +000092 E.IncludeOffset = IncludeOffset;
Chris Lattner23b5dc62009-02-04 00:40:31 +000093 return E;
94 }
95};
Chris Lattner6c1fbe02009-02-04 04:46:59 +000096
Cedric Venetea684e62009-02-14 16:15:20 +000097// needed for FindNearestLineEntry (upper_bound of LineEntry)
98inline bool operator<(const LineEntry &lhs, const LineEntry &rhs) {
99 // FIXME: should check the other field?
100 return lhs.FileOffset < rhs.FileOffset;
101}
102
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000103inline bool operator<(const LineEntry &E, unsigned Offset) {
104 return E.FileOffset < Offset;
105}
106
107inline bool operator<(unsigned Offset, const LineEntry &E) {
108 return Offset < E.FileOffset;
109}
Chris Lattner23b5dc62009-02-04 00:40:31 +0000110
Chris Lattner5b9a5042009-01-26 07:57:50 +0000111/// LineTableInfo - This class is used to hold and unique data used to
112/// represent #line information.
113class LineTableInfo {
114 /// FilenameIDs - This map is used to assign unique IDs to filenames in
115 /// #line directives. This allows us to unique the filenames that
116 /// frequently reoccur and reference them with indices. FilenameIDs holds
117 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
118 /// to string.
119 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
120 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
Chris Lattner23b5dc62009-02-04 00:40:31 +0000121
122 /// LineEntries - This is a map from FileIDs to a list of line entries (sorted
123 /// by the offset they occur in the file.
124 std::map<unsigned, std::vector<LineEntry> > LineEntries;
Chris Lattner5b9a5042009-01-26 07:57:50 +0000125public:
126 LineTableInfo() {
127 }
128
129 void clear() {
130 FilenameIDs.clear();
131 FilenamesByID.clear();
132 }
133
134 ~LineTableInfo() {}
135
136 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
Chris Lattner3cd949c2009-02-04 01:55:42 +0000137 const char *getFilename(unsigned ID) const {
138 assert(ID < FilenamesByID.size() && "Invalid FilenameID");
139 return FilenamesByID[ID]->getKeyData();
140 }
141
Chris Lattner23b5dc62009-02-04 00:40:31 +0000142 void AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000143 unsigned LineNo, int FilenameID);
Chris Lattner9d79eba2009-02-04 05:21:58 +0000144 void AddLineNote(unsigned FID, unsigned Offset,
145 unsigned LineNo, int FilenameID,
146 unsigned EntryExit, SrcMgr::CharacteristicKind FileKind);
147
Chris Lattner3cd949c2009-02-04 01:55:42 +0000148
149 /// FindNearestLineEntry - Find the line entry nearest to FID that is before
150 /// it. If there is no line entry before Offset in FID, return null.
151 const LineEntry *FindNearestLineEntry(unsigned FID, unsigned Offset);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000152};
153} // namespace clang
154
Chris Lattner5b9a5042009-01-26 07:57:50 +0000155unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
156 // Look up the filename in the string table, returning the pre-existing value
157 // if it exists.
158 llvm::StringMapEntry<unsigned> &Entry =
159 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
160 if (Entry.getValue() != ~0U)
161 return Entry.getValue();
162
163 // Otherwise, assign this the next available ID.
164 Entry.setValue(FilenamesByID.size());
165 FilenamesByID.push_back(&Entry);
166 return FilenamesByID.size()-1;
167}
168
Chris Lattnerac50e342009-02-03 22:13:05 +0000169/// AddLineNote - Add a line note to the line table that indicates that there
170/// is a #line at the specified FID/Offset location which changes the presumed
171/// location to LineNo/FilenameID.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000172void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000173 unsigned LineNo, int FilenameID) {
Chris Lattner23b5dc62009-02-04 00:40:31 +0000174 std::vector<LineEntry> &Entries = LineEntries[FID];
Chris Lattnerac50e342009-02-03 22:13:05 +0000175
Chris Lattner23b5dc62009-02-04 00:40:31 +0000176 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
177 "Adding line entries out of order!");
Chris Lattner3cd949c2009-02-04 01:55:42 +0000178
Chris Lattner9d79eba2009-02-04 05:21:58 +0000179 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
Chris Lattner137b6a62009-02-04 06:25:26 +0000180 unsigned IncludeOffset = 0;
Chris Lattner3cd949c2009-02-04 01:55:42 +0000181
Chris Lattner9d79eba2009-02-04 05:21:58 +0000182 if (!Entries.empty()) {
183 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
184 // that we are still in "foo.h".
185 if (FilenameID == -1)
186 FilenameID = Entries.back().FilenameID;
187
Chris Lattner137b6a62009-02-04 06:25:26 +0000188 // If we are after a line marker that switched us to system header mode, or
189 // that set #include information, preserve it.
Chris Lattner9d79eba2009-02-04 05:21:58 +0000190 Kind = Entries.back().FileKind;
Chris Lattner137b6a62009-02-04 06:25:26 +0000191 IncludeOffset = Entries.back().IncludeOffset;
Chris Lattner9d79eba2009-02-04 05:21:58 +0000192 }
193
Chris Lattner137b6a62009-02-04 06:25:26 +0000194 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
195 IncludeOffset));
Chris Lattnerac50e342009-02-03 22:13:05 +0000196}
197
Chris Lattner9d79eba2009-02-04 05:21:58 +0000198/// AddLineNote This is the same as the previous version of AddLineNote, but is
199/// used for GNU line markers. If EntryExit is 0, then this doesn't change the
200/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then
201/// this is a file exit. FileKind specifies whether this is a system header or
202/// extern C system header.
203void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
204 unsigned LineNo, int FilenameID,
205 unsigned EntryExit,
206 SrcMgr::CharacteristicKind FileKind) {
207 assert(FilenameID != -1 && "Unspecified filename should use other accessor");
208
209 std::vector<LineEntry> &Entries = LineEntries[FID];
210
211 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
212 "Adding line entries out of order!");
213
Chris Lattner137b6a62009-02-04 06:25:26 +0000214 unsigned IncludeOffset = 0;
215 if (EntryExit == 0) { // No #include stack change.
216 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
217 } else if (EntryExit == 1) {
218 IncludeOffset = Offset-1;
219 } else if (EntryExit == 2) {
220 assert(!Entries.empty() && Entries.back().IncludeOffset &&
221 "PPDirectives should have caught case when popping empty include stack");
222
223 // Get the include loc of the last entries' include loc as our include loc.
224 IncludeOffset = 0;
225 if (const LineEntry *PrevEntry =
226 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
227 IncludeOffset = PrevEntry->IncludeOffset;
228 }
Chris Lattner9d79eba2009-02-04 05:21:58 +0000229
Chris Lattner137b6a62009-02-04 06:25:26 +0000230 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
231 IncludeOffset));
Chris Lattner9d79eba2009-02-04 05:21:58 +0000232}
233
234
Chris Lattner3cd949c2009-02-04 01:55:42 +0000235/// FindNearestLineEntry - Find the line entry nearest to FID that is before
236/// it. If there is no line entry before Offset in FID, return null.
237const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
238 unsigned Offset) {
239 const std::vector<LineEntry> &Entries = LineEntries[FID];
240 assert(!Entries.empty() && "No #line entries for this FID after all!");
241
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000242 // It is very common for the query to be after the last #line, check this
243 // first.
244 if (Entries.back().FileOffset <= Offset)
245 return &Entries.back();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000246
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000247 // Do a binary search to find the maximal element that is still before Offset.
248 std::vector<LineEntry>::const_iterator I =
249 std::upper_bound(Entries.begin(), Entries.end(), Offset);
250 if (I == Entries.begin()) return 0;
251 return &*--I;
Chris Lattner3cd949c2009-02-04 01:55:42 +0000252}
Chris Lattnerac50e342009-02-03 22:13:05 +0000253
254
Chris Lattner5b9a5042009-01-26 07:57:50 +0000255/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
256///
257unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
258 if (LineTable == 0)
259 LineTable = new LineTableInfo();
260 return LineTable->getLineTableFilenameID(Ptr, Len);
261}
262
263
Chris Lattner4c4ea172009-02-03 21:52:55 +0000264/// AddLineNote - Add a line note to the line table for the FileID and offset
265/// specified by Loc. If FilenameID is -1, it is considered to be
266/// unspecified.
267void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
268 int FilenameID) {
Chris Lattnerac50e342009-02-03 22:13:05 +0000269 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000270
Chris Lattnerac50e342009-02-03 22:13:05 +0000271 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
272
273 // Remember that this file has #line directives now if it doesn't already.
274 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
275
276 if (LineTable == 0)
277 LineTable = new LineTableInfo();
Chris Lattner23b5dc62009-02-04 00:40:31 +0000278 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000279}
280
Chris Lattner9d79eba2009-02-04 05:21:58 +0000281/// AddLineNote - Add a GNU line marker to the line table.
282void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
283 int FilenameID, bool IsFileEntry,
284 bool IsFileExit, bool IsSystemHeader,
285 bool IsExternCHeader) {
286 // If there is no filename and no flags, this is treated just like a #line,
287 // which does not change the flags of the previous line marker.
288 if (FilenameID == -1) {
289 assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
290 "Can't set flags without setting the filename!");
291 return AddLineNote(Loc, LineNo, FilenameID);
292 }
293
294 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
295 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
296
297 // Remember that this file has #line directives now if it doesn't already.
298 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
299
300 if (LineTable == 0)
301 LineTable = new LineTableInfo();
302
303 SrcMgr::CharacteristicKind FileKind;
304 if (IsExternCHeader)
305 FileKind = SrcMgr::C_ExternCSystem;
306 else if (IsSystemHeader)
307 FileKind = SrcMgr::C_System;
308 else
309 FileKind = SrcMgr::C_User;
310
311 unsigned EntryExit = 0;
312 if (IsFileEntry)
313 EntryExit = 1;
314 else if (IsFileExit)
315 EntryExit = 2;
316
317 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
318 EntryExit, FileKind);
319}
320
Chris Lattner4c4ea172009-02-03 21:52:55 +0000321
Chris Lattner23b5dc62009-02-04 00:40:31 +0000322//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000323// Private 'Create' methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000324//===----------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000325
Chris Lattner5b9a5042009-01-26 07:57:50 +0000326SourceManager::~SourceManager() {
327 delete LineTable;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000328
329 // Delete FileEntry objects corresponding to content caches. Since the actual
330 // content cache objects are bump pointer allocated, we just have to run the
331 // dtors, but we call the deallocate method for completeness.
332 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
333 MemBufferInfos[i]->~ContentCache();
334 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
335 }
336 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
337 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
338 I->second->~ContentCache();
339 ContentCacheAlloc.Deallocate(I->second);
340 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000341}
342
343void SourceManager::clearIDTables() {
344 MainFileID = FileID();
345 SLocEntryTable.clear();
346 LastLineNoFileIDQuery = FileID();
347 LastLineNoContentCache = 0;
348 LastFileIDLookup = FileID();
349
350 if (LineTable)
351 LineTable->clear();
352
353 // Use up FileID #0 as an invalid instantiation.
354 NextOffset = 0;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000355 createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000356}
357
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000358/// getOrCreateContentCache - Create or return a cached ContentCache for the
359/// specified file.
360const ContentCache *
361SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 assert(FileEnt && "Didn't specify a file entry to use?");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000363
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 // Do we already have information about this file?
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000365 ContentCache *&Entry = FileInfos[FileEnt];
366 if (Entry) return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000367
Chris Lattner00282d62009-02-03 07:41:46 +0000368 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
369 // so that FileInfo can use the low 3 bits of the pointer for its own
370 // nefarious purposes.
371 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
372 EntryAlign = std::max(8U, EntryAlign);
373 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000374 new (Entry) ContentCache(FileEnt);
375 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376}
377
378
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000379/// createMemBufferContentCache - Create a new ContentCache for the specified
380/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000381const ContentCache*
382SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner00282d62009-02-03 07:41:46 +0000383 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
384 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
385 // the pointer for its own nefarious purposes.
386 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
387 EntryAlign = std::max(8U, EntryAlign);
388 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000389 new (Entry) ContentCache();
390 MemBufferInfos.push_back(Entry);
391 Entry->setBuffer(Buffer);
392 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000393}
394
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000395//===----------------------------------------------------------------------===//
396// Methods to create new FileID's and instantiations.
397//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000398
Nico Weber48002c82008-09-29 00:25:48 +0000399/// createFileID - Create a new fileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000400/// include position. This works regardless of whether the ContentCache
401/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000402FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000403 SourceLocation IncludePos,
404 SrcMgr::CharacteristicKind FileCharacter) {
405 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
406 FileInfo::get(IncludePos, File,
407 FileCharacter)));
Ted Kremenekc16c2082009-01-06 01:55:26 +0000408 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000409 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
410 NextOffset += FileSize+1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000411
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000412 // Set LastFileIDLookup to the newly created file. The next getFileID call is
413 // almost guaranteed to be from that file.
414 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000415}
416
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000417/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000418/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000419/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000420SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000421 SourceLocation ILocStart,
422 SourceLocation ILocEnd,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000423 unsigned TokLength) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000424 InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
425 SLocEntryTable.push_back(SLocEntry::get(NextOffset, II));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000426 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
427 NextOffset += TokLength+1;
428 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000429}
430
Chris Lattner31530ba2009-01-19 07:32:13 +0000431/// getBufferData - Return a pointer to the start and end of the source buffer
432/// data for the specified FileID.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000433std::pair<const char*, const char*>
434SourceManager::getBufferData(FileID FID) const {
435 const llvm::MemoryBuffer *Buf = getBuffer(FID);
436 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
437}
438
439
Chris Lattner23b5dc62009-02-04 00:40:31 +0000440//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000441// SourceLocation manipulation methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000442//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000443
444/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
445/// method that is used for all SourceManager queries that start with a
446/// SourceLocation object. It is responsible for finding the entry in
447/// SLocEntryTable which contains the specified location.
448///
449FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
450 assert(SLocOffset && "Invalid FileID");
451
452 // After the first and second level caches, I see two common sorts of
453 // behavior: 1) a lot of searched FileID's are "near" the cached file location
454 // or are "near" the cached instantiation location. 2) others are just
455 // completely random and may be a very long way away.
456 //
457 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
458 // then we fall back to a less cache efficient, but more scalable, binary
459 // search to find the location.
460
461 // See if this is near the file point - worst case we start scanning from the
462 // most newly created FileID.
463 std::vector<SrcMgr::SLocEntry>::const_iterator I;
464
465 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
466 // Neither loc prunes our search.
467 I = SLocEntryTable.end();
468 } else {
469 // Perhaps it is near the file point.
470 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
471 }
472
473 // Find the FileID that contains this. "I" is an iterator that points to a
474 // FileID whose offset is known to be larger than SLocOffset.
475 unsigned NumProbes = 0;
476 while (1) {
477 --I;
478 if (I->getOffset() <= SLocOffset) {
479#if 0
480 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
481 I-SLocEntryTable.begin(),
482 I->isInstantiation() ? "inst" : "file",
483 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
484#endif
485 FileID Res = FileID::get(I-SLocEntryTable.begin());
486
487 // If this isn't an instantiation, remember it. We have good locality
488 // across FileID lookups.
489 if (!I->isInstantiation())
490 LastFileIDLookup = Res;
491 NumLinearScans += NumProbes+1;
492 return Res;
493 }
494 if (++NumProbes == 8)
495 break;
496 }
497
498 // Convert "I" back into an index. We know that it is an entry whose index is
499 // larger than the offset we are looking for.
500 unsigned GreaterIndex = I-SLocEntryTable.begin();
501 // LessIndex - This is the lower bound of the range that we're searching.
502 // We know that the offset corresponding to the FileID is is less than
503 // SLocOffset.
504 unsigned LessIndex = 0;
505 NumProbes = 0;
506 while (1) {
507 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
508 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
509
510 ++NumProbes;
511
512 // If the offset of the midpoint is too large, chop the high side of the
513 // range to the midpoint.
514 if (MidOffset > SLocOffset) {
515 GreaterIndex = MiddleIndex;
516 continue;
517 }
518
519 // If the middle index contains the value, succeed and return.
520 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
521#if 0
522 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
523 I-SLocEntryTable.begin(),
524 I->isInstantiation() ? "inst" : "file",
525 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
526#endif
527 FileID Res = FileID::get(MiddleIndex);
528
529 // If this isn't an instantiation, remember it. We have good locality
530 // across FileID lookups.
531 if (!I->isInstantiation())
532 LastFileIDLookup = Res;
533 NumBinaryProbes += NumProbes;
534 return Res;
535 }
536
537 // Otherwise, move the low-side up to the middle index.
538 LessIndex = MiddleIndex;
539 }
540}
541
Chris Lattneraddb7972009-01-26 20:04:19 +0000542SourceLocation SourceManager::
543getInstantiationLocSlowCase(SourceLocation Loc) const {
544 do {
545 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000546 Loc = getSLocEntry(LocInfo.first).getInstantiation()
547 .getInstantiationLocStart();
Chris Lattneraddb7972009-01-26 20:04:19 +0000548 Loc = Loc.getFileLocWithOffset(LocInfo.second);
549 } while (!Loc.isFileID());
550
551 return Loc;
552}
553
554SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
555 do {
556 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
557 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
558 Loc = Loc.getFileLocWithOffset(LocInfo.second);
559 } while (!Loc.isFileID());
560 return Loc;
561}
562
563
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000564std::pair<FileID, unsigned>
565SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
566 unsigned Offset) const {
567 // If this is an instantiation record, walk through all the instantiation
568 // points.
569 FileID FID;
570 SourceLocation Loc;
571 do {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000572 Loc = E->getInstantiation().getInstantiationLocStart();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000573
574 FID = getFileID(Loc);
575 E = &getSLocEntry(FID);
576 Offset += Loc.getOffset()-E->getOffset();
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000577 } while (!Loc.isFileID());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000578
579 return std::make_pair(FID, Offset);
580}
581
582std::pair<FileID, unsigned>
583SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
584 unsigned Offset) const {
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000585 // If this is an instantiation record, walk through all the instantiation
586 // points.
587 FileID FID;
588 SourceLocation Loc;
589 do {
590 Loc = E->getInstantiation().getSpellingLoc();
591
592 FID = getFileID(Loc);
593 E = &getSLocEntry(FID);
594 Offset += Loc.getOffset()-E->getOffset();
595 } while (!Loc.isFileID());
596
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000597 return std::make_pair(FID, Offset);
598}
599
Chris Lattner387616e2009-02-17 08:04:48 +0000600/// getImmediateSpellingLoc - Given a SourceLocation object, return the
601/// spelling location referenced by the ID. This is the first level down
602/// towards the place where the characters that make up the lexed token can be
603/// found. This should not generally be used by clients.
604SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
605 if (Loc.isFileID()) return Loc;
606 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
607 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
608 return Loc.getFileLocWithOffset(LocInfo.second);
609}
610
611
Chris Lattnere7fb4842009-02-15 20:52:18 +0000612/// getImmediateInstantiationRange - Loc is required to be an instantiation
613/// location. Return the start/end of the instantiation information.
614std::pair<SourceLocation,SourceLocation>
615SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const {
616 assert(Loc.isMacroID() && "Not an instantiation loc!");
617 const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation();
618 return II.getInstantiationLocRange();
619}
620
Chris Lattner66781332009-02-15 21:26:50 +0000621/// getInstantiationRange - Given a SourceLocation object, return the
622/// range of tokens covered by the instantiation in the ultimate file.
623std::pair<SourceLocation,SourceLocation>
624SourceManager::getInstantiationRange(SourceLocation Loc) const {
625 if (Loc.isFileID()) return std::make_pair(Loc, Loc);
626
627 std::pair<SourceLocation,SourceLocation> Res =
628 getImmediateInstantiationRange(Loc);
629
630 // Fully resolve the start and end locations to their ultimate instantiation
631 // points.
632 while (!Res.first.isFileID())
633 Res.first = getImmediateInstantiationRange(Res.first).first;
634 while (!Res.second.isFileID())
635 Res.second = getImmediateInstantiationRange(Res.second).second;
636 return Res;
637}
638
Chris Lattnere7fb4842009-02-15 20:52:18 +0000639
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000640
641//===----------------------------------------------------------------------===//
642// Queries about the code at a SourceLocation.
643//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000644
645/// getCharacterData - Return a pointer to the start of the specified location
646/// in the appropriate MemoryBuffer.
647const char *SourceManager::getCharacterData(SourceLocation SL) const {
648 // Note that this is a hot function in the getSpelling() path, which is
649 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000650 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000651
Ted Kremenekc16c2082009-01-06 01:55:26 +0000652 // Note that calling 'getBuffer()' may lazily page in a source file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000653 return getSLocEntry(LocInfo.first).getFile().getContentCache()
654 ->getBuffer()->getBufferStart() + LocInfo.second;
Reid Spencer5f016e22007-07-11 17:01:13 +0000655}
656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657
Chris Lattner9dc1f532007-07-20 16:37:10 +0000658/// getColumnNumber - Return the column # for the specified file position.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000659/// this is significantly cheaper to compute than the line number.
660unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const {
661 const char *Buf = getBuffer(FID)->getBufferStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000662
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 unsigned LineStart = FilePos;
664 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
665 --LineStart;
666 return FilePos-LineStart+1;
667}
668
Chris Lattner7da5aea2009-02-04 00:55:58 +0000669unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000670 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000671 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
672 return getColumnNumber(LocInfo.first, LocInfo.second);
673}
674
675unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const {
Chris Lattner30fc9332009-02-04 01:06:56 +0000676 if (Loc.isInvalid()) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000677 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
678 return getColumnNumber(LocInfo.first, LocInfo.second);
679}
680
681
682
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000683static void ComputeLineNumbers(ContentCache* FI,
684 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE;
685static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){
Ted Kremenekc16c2082009-01-06 01:55:26 +0000686 // Note that calling 'getBuffer()' may lazily page in the file.
687 const MemoryBuffer *Buffer = FI->getBuffer();
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000688
689 // Find the file offsets of all of the *physical* source lines. This does
690 // not look at trigraphs, escaped newlines, or anything else tricky.
691 std::vector<unsigned> LineOffsets;
692
693 // Line #1 starts at char 0.
694 LineOffsets.push_back(0);
695
696 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
697 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
698 unsigned Offs = 0;
699 while (1) {
700 // Skip over the contents of the line.
701 // TODO: Vectorize this? This is very performance sensitive for programs
702 // with lots of diagnostics and in -E mode.
703 const unsigned char *NextBuf = (const unsigned char *)Buf;
704 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
705 ++NextBuf;
706 Offs += NextBuf-Buf;
707 Buf = NextBuf;
708
709 if (Buf[0] == '\n' || Buf[0] == '\r') {
710 // If this is \n\r or \r\n, skip both characters.
711 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
712 ++Offs, ++Buf;
713 ++Offs, ++Buf;
714 LineOffsets.push_back(Offs);
715 } else {
716 // Otherwise, this is a null. If end of file, exit.
717 if (Buf == End) break;
718 // Otherwise, skip the null.
719 ++Offs, ++Buf;
720 }
721 }
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000722
723 // Copy the offsets into the FileInfo structure.
724 FI->NumLines = LineOffsets.size();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000725 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000726 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
727}
Reid Spencer5f016e22007-07-11 17:01:13 +0000728
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000729/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000730/// for the position indicated. This requires building and caching a table of
731/// line offsets for the MemoryBuffer, so this is not cheap: use only when
732/// about to emit a diagnostic.
Chris Lattner30fc9332009-02-04 01:06:56 +0000733unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000734 ContentCache *Content;
Chris Lattner30fc9332009-02-04 01:06:56 +0000735 if (LastLineNoFileIDQuery == FID)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000736 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000737 else
Chris Lattner30fc9332009-02-04 01:06:56 +0000738 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000739 .getFile().getContentCache());
Reid Spencer5f016e22007-07-11 17:01:13 +0000740
741 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000742 /// SourceLineCache for it on demand.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000743 if (Content->SourceLineCache == 0)
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000744 ComputeLineNumbers(Content, ContentCacheAlloc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000745
746 // Okay, we know we have a line number table. Do a binary search to find the
747 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000748 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000749 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000750 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000751
Chris Lattner30fc9332009-02-04 01:06:56 +0000752 unsigned QueriedFilePos = FilePos+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000753
754 // If the previous query was to the same file, we know both the file pos from
755 // that query and the line number returned. This allows us to narrow the
756 // search space from the entire file to something near the match.
Chris Lattner30fc9332009-02-04 01:06:56 +0000757 if (LastLineNoFileIDQuery == FID) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000758 if (QueriedFilePos >= LastLineNoFilePos) {
759 SourceLineCache = SourceLineCache+LastLineNoResult-1;
760
761 // The query is likely to be nearby the previous one. Here we check to
762 // see if it is within 5, 10 or 20 lines. It can be far away in cases
763 // where big comment blocks and vertical whitespace eat up lines but
764 // contribute no tokens.
765 if (SourceLineCache+5 < SourceLineCacheEnd) {
766 if (SourceLineCache[5] > QueriedFilePos)
767 SourceLineCacheEnd = SourceLineCache+5;
768 else if (SourceLineCache+10 < SourceLineCacheEnd) {
769 if (SourceLineCache[10] > QueriedFilePos)
770 SourceLineCacheEnd = SourceLineCache+10;
771 else if (SourceLineCache+20 < SourceLineCacheEnd) {
772 if (SourceLineCache[20] > QueriedFilePos)
773 SourceLineCacheEnd = SourceLineCache+20;
774 }
775 }
776 }
777 } else {
778 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
779 }
780 }
781
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000782 // If the spread is large, do a "radix" test as our initial guess, based on
783 // the assumption that lines average to approximately the same length.
784 // NOTE: This is currently disabled, as it does not appear to be profitable in
785 // initial measurements.
786 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000787 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000788
789 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000790 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000791
792 // Check for -10 and +10 lines.
793 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
794 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
795
796 // If the computed lower bound is less than the query location, move it in.
797 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
798 SourceLineCacheStart[LowerBound] < QueriedFilePos)
799 SourceLineCache = SourceLineCacheStart+LowerBound;
800
801 // If the computed upper bound is greater than the query location, move it.
802 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
803 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
804 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
805 }
806
807 unsigned *Pos
808 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000809 unsigned LineNo = Pos-SourceLineCacheStart;
810
Chris Lattner30fc9332009-02-04 01:06:56 +0000811 LastLineNoFileIDQuery = FID;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000812 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000813 LastLineNoFilePos = QueriedFilePos;
814 LastLineNoResult = LineNo;
815 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000816}
817
Chris Lattner30fc9332009-02-04 01:06:56 +0000818unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const {
819 if (Loc.isInvalid()) return 0;
820 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
821 return getLineNumber(LocInfo.first, LocInfo.second);
822}
823unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const {
824 if (Loc.isInvalid()) return 0;
825 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
826 return getLineNumber(LocInfo.first, LocInfo.second);
827}
828
Chris Lattner6b306672009-02-04 05:33:01 +0000829/// getFileCharacteristic - return the file characteristic of the specified
830/// source location, indicating whether this is a normal file, a system
831/// header, or an "implicit extern C" system header.
832///
833/// This state can be modified with flags on GNU linemarker directives like:
834/// # 4 "foo.h" 3
835/// which changes all source locations in the current file after that to be
836/// considered to be from a system header.
837SrcMgr::CharacteristicKind
838SourceManager::getFileCharacteristic(SourceLocation Loc) const {
839 assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
840 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
841 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
842
843 // If there are no #line directives in this file, just return the whole-file
844 // state.
845 if (!FI.hasLineDirectives())
846 return FI.getFileCharacteristic();
847
848 assert(LineTable && "Can't have linetable entries without a LineTable!");
849 // See if there is a #line directive before the location.
850 const LineEntry *Entry =
851 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
852
853 // If this is before the first line marker, use the file characteristic.
854 if (!Entry)
855 return FI.getFileCharacteristic();
856
857 return Entry->FileKind;
858}
859
Chris Lattnerbff5c512009-02-17 08:39:06 +0000860/// Return the filename or buffer identifier of the buffer the location is in.
861/// Note that this name does not respect #line directives. Use getPresumedLoc
862/// for normal clients.
863const char *SourceManager::getBufferName(SourceLocation Loc) const {
864 if (Loc.isInvalid()) return "<invalid loc>";
865
866 return getBuffer(getFileID(Loc))->getBufferIdentifier();
867}
868
Chris Lattner30fc9332009-02-04 01:06:56 +0000869
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000870/// getPresumedLoc - This method returns the "presumed" location of a
871/// SourceLocation specifies. A "presumed location" can be modified by #line
872/// or GNU line marker directives. This provides a view on the data that a
873/// user should see in diagnostics, for example.
874///
875/// Note that a presumed location is always given as the instantiation point
876/// of an instantiation location, not at the spelling location.
877PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
878 if (Loc.isInvalid()) return PresumedLoc();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000879
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000880 // Presumed locations are always for instantiation points.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000881 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000882
Chris Lattner30fc9332009-02-04 01:06:56 +0000883 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000884 const SrcMgr::ContentCache *C = FI.getContentCache();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000885
886 // To get the source name, first consult the FileEntry (if one exists)
887 // before the MemBuffer as this will avoid unnecessarily paging in the
888 // MemBuffer.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000889 const char *Filename =
890 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000891 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second);
892 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second);
893 SourceLocation IncludeLoc = FI.getIncludeLoc();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000894
Chris Lattner3cd949c2009-02-04 01:55:42 +0000895 // If we have #line directives in this file, update and overwrite the physical
896 // location info if appropriate.
897 if (FI.hasLineDirectives()) {
898 assert(LineTable && "Can't have linetable entries without a LineTable!");
899 // See if there is a #line directive before this. If so, get it.
900 if (const LineEntry *Entry =
901 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattnerfc391332009-02-04 02:00:59 +0000902 // If the LineEntry indicates a filename, use it.
Chris Lattner3cd949c2009-02-04 01:55:42 +0000903 if (Entry->FilenameID != -1)
904 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattnerfc391332009-02-04 02:00:59 +0000905
906 // Use the line number specified by the LineEntry. This line number may
907 // be multiple lines down from the line entry. Add the difference in
908 // physical line numbers from the query point and the line marker to the
909 // total.
910 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
911 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
912
Chris Lattner0e0e5da2009-02-04 02:15:40 +0000913 // Note that column numbers are not molested by line markers.
Chris Lattner137b6a62009-02-04 06:25:26 +0000914
915 // Handle virtual #include manipulation.
916 if (Entry->IncludeOffset) {
917 IncludeLoc = getLocForStartOfFile(LocInfo.first);
918 IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
919 }
Chris Lattner3cd949c2009-02-04 01:55:42 +0000920 }
921 }
922
923 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000924}
925
926//===----------------------------------------------------------------------===//
927// Other miscellaneous methods.
928//===----------------------------------------------------------------------===//
929
930
Reid Spencer5f016e22007-07-11 17:01:13 +0000931/// PrintStats - Print statistics to stderr.
932///
933void SourceManager::PrintStats() const {
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000934 llvm::cerr << "\n*** Source Manager Stats:\n";
935 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
Chris Lattner08c375c2009-01-27 05:22:43 +0000936 << " mem buffers mapped.\n";
937 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, "
938 << NextOffset << "B of Sloc address space used.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000939
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 unsigned NumLineNumsComputed = 0;
941 unsigned NumFileBytesMapped = 0;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000942 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
943 NumLineNumsComputed += I->second->SourceLineCache != 0;
944 NumFileBytesMapped += I->second->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 }
Ted Kremenek78d85f52007-10-30 21:08:08 +0000946
Ted Kremenek665dd4a2007-12-05 22:21:13 +0000947 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
948 << NumLineNumsComputed << " files with line #'s computed.\n";
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000949 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
950 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000951}
Ted Kremeneke21272f2007-12-04 19:39:02 +0000952
953//===----------------------------------------------------------------------===//
954// Serialization.
955//===----------------------------------------------------------------------===//
Ted Kremenek099b4742007-12-05 00:14:18 +0000956
957void ContentCache::Emit(llvm::Serializer& S) const {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000958 S.FlushRecord();
959 S.EmitPtr(this);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000960
Ted Kremenek82dfaf72007-12-18 22:12:19 +0000961 if (Entry) {
962 llvm::sys::Path Fname(Buffer->getBufferIdentifier());
963
964 if (Fname.isAbsolute())
965 S.EmitCStr(Fname.c_str());
966 else {
967 // Create an absolute path.
968 // FIXME: This will potentially contain ".." and "." in the path.
969 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
970 path.appendComponent(Fname.c_str());
971 S.EmitCStr(path.c_str());
972 }
973 }
Ted Kremenek099b4742007-12-05 00:14:18 +0000974 else {
Ted Kremeneke21272f2007-12-04 19:39:02 +0000975 const char* p = Buffer->getBufferStart();
976 const char* e = Buffer->getBufferEnd();
977
Ted Kremenek099b4742007-12-05 00:14:18 +0000978 S.EmitInt(e-p);
979
Ted Kremeneke21272f2007-12-04 19:39:02 +0000980 for ( ; p != e; ++p)
Ted Kremenek099b4742007-12-05 00:14:18 +0000981 S.EmitInt(*p);
Ted Kremeneke21272f2007-12-04 19:39:02 +0000982 }
983
Ted Kremenek099b4742007-12-05 00:14:18 +0000984 S.FlushRecord();
Ted Kremeneke21272f2007-12-04 19:39:02 +0000985}
Ted Kremenek099b4742007-12-05 00:14:18 +0000986
987void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
988 SourceManager& SMgr,
989 FileManager* FMgr,
990 std::vector<char>& Buf) {
991 if (FMgr) {
992 llvm::SerializedPtrID PtrID = D.ReadPtrID();
993 D.ReadCStr(Buf,false);
994
995 // Create/fetch the FileEntry.
996 const char* start = &Buf[0];
997 const FileEntry* E = FMgr->getFile(start,start+Buf.size());
998
Ted Kremenekdb9c2292007-12-13 18:12:10 +0000999 // FIXME: Ideally we want a lazy materialization of the ContentCache
1000 // anyway, because we don't want to read in source files unless this
1001 // is absolutely needed.
1002 if (!E)
1003 D.RegisterPtr(PtrID,NULL);
Nico Weber48002c82008-09-29 00:25:48 +00001004 else
Ted Kremenekdb9c2292007-12-13 18:12:10 +00001005 // Get the ContextCache object and register it with the deserializer.
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001006 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
1007 return;
Ted Kremenek099b4742007-12-05 00:14:18 +00001008 }
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001009
1010 // Register the ContextCache object with the deserializer.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001011 /* FIXME:
1012 ContentCache *Entry
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001013 SMgr.MemBufferInfos.push_back(ContentCache());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001014 = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001015 D.RegisterPtr(&Entry);
1016
1017 // Create the buffer.
1018 unsigned Size = D.ReadInt();
1019 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
1020
1021 // Read the contents of the buffer.
1022 char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
1023 for (unsigned i = 0; i < Size ; ++i)
1024 p[i] = D.ReadInt();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001025 */
Ted Kremenek099b4742007-12-05 00:14:18 +00001026}
1027
1028void SourceManager::Emit(llvm::Serializer& S) const {
Ted Kremenek1f941002007-12-05 00:19:51 +00001029 S.EnterBlock();
1030 S.EmitPtr(this);
Chris Lattner2b2453a2009-01-17 06:22:33 +00001031 S.EmitInt(MainFileID.getOpaqueValue());
Ted Kremenek1f941002007-12-05 00:19:51 +00001032
Ted Kremenek099b4742007-12-05 00:14:18 +00001033 // Emit: FileInfos. Just emit the file name.
1034 S.EnterBlock();
1035
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001036 // FIXME: Emit FileInfos.
1037 //std::for_each(FileInfos.begin(), FileInfos.end(),
1038 // S.MakeEmitter<ContentCache>());
Ted Kremenek099b4742007-12-05 00:14:18 +00001039
1040 S.ExitBlock();
1041
1042 // Emit: MemBufferInfos
1043 S.EnterBlock();
1044
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001045 /* FIXME: EMIT.
Ted Kremenek099b4742007-12-05 00:14:18 +00001046 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
1047 S.MakeEmitter<ContentCache>());
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001048 */
Ted Kremenek099b4742007-12-05 00:14:18 +00001049
1050 S.ExitBlock();
1051
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001052 // FIXME: Emit SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +00001053
1054 S.ExitBlock();
Ted Kremenek099b4742007-12-05 00:14:18 +00001055}
1056
Ted Kremenek1f941002007-12-05 00:19:51 +00001057SourceManager*
Chris Lattner23b5dc62009-02-04 00:40:31 +00001058SourceManager::CreateAndRegister(llvm::Deserializer &D, FileManager &FMgr) {
Ted Kremenek1f941002007-12-05 00:19:51 +00001059 SourceManager *M = new SourceManager();
1060 D.RegisterPtr(M);
1061
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001062 // Read: the FileID of the main source file of the translation unit.
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001063 M->MainFileID = FileID::get(D.ReadInt());
Ted Kremenek76edd0e2007-12-19 22:29:55 +00001064
Ted Kremenek099b4742007-12-05 00:14:18 +00001065 std::vector<char> Buf;
1066
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001067 /*{ // FIXME Read: FileInfos.
Ted Kremenek099b4742007-12-05 00:14:18 +00001068 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
1069 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +00001070 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001071 }*/
Ted Kremenek099b4742007-12-05 00:14:18 +00001072
1073 { // Read: MemBufferInfos.
1074 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
1075 while (!D.FinishedBlock(BLoc))
Ted Kremenek1f941002007-12-05 00:19:51 +00001076 ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
Ted Kremenek099b4742007-12-05 00:14:18 +00001077 }
1078
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001079 // FIXME: Read SLocEntryTable.
Ted Kremenek1f941002007-12-05 00:19:51 +00001080
1081 return M;
Ted Kremenek1f2c7d12007-12-10 18:01:25 +00001082}