blob: d73a0af419e0d05d53921990131b235ace615dca [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"
Douglas Gregord4f77aa2009-04-13 15:31:25 +000015#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregoraea67db2010-03-15 22:54:52 +000016#include "clang/Basic/Diagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/FileManager.h"
Benjamin Kramer5807d9c2010-11-18 12:46:39 +000018#include "llvm/ADT/StringSwitch.h"
Douglas Gregor86a4d0d2011-02-03 17:17:35 +000019#include "llvm/ADT/Optional.h"
Chris Lattner5e36a7a2007-07-24 05:57:19 +000020#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000022#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000023#include "llvm/Support/Path.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include <algorithm>
Douglas Gregoraea67db2010-03-15 22:54:52 +000025#include <string>
Douglas Gregorf715ca12010-03-16 00:06:06 +000026#include <cstring>
Douglas Gregor86a4d0d2011-02-03 17:17:35 +000027#include <sys/stat.h>
Douglas Gregoraea67db2010-03-15 22:54:52 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30using namespace SrcMgr;
31using llvm::MemoryBuffer;
32
Chris Lattner23b5dc62009-02-04 00:40:31 +000033//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000034// SourceManager Helper Classes
Chris Lattner23b5dc62009-02-04 00:40:31 +000035//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +000036
Ted Kremenek78d85f52007-10-30 21:08:08 +000037ContentCache::~ContentCache() {
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000038 if (shouldFreeBuffer())
39 delete Buffer.getPointer();
Reid Spencer5f016e22007-07-11 17:01:13 +000040}
41
Ted Kremenekc16c2082009-01-06 01:55:26 +000042/// getSizeBytesMapped - Returns the number of bytes actually mapped for
43/// this ContentCache. This can be 0 if the MemBuffer was not actually
44/// instantiated.
45unsigned ContentCache::getSizeBytesMapped() const {
Douglas Gregorc8151082010-03-16 22:53:51 +000046 return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0;
Ted Kremenekc16c2082009-01-06 01:55:26 +000047}
48
49/// getSize - Returns the size of the content encapsulated by this ContentCache.
50/// This can be the size of the source file or the size of an arbitrary
51/// scratch buffer. If the ContentCache encapsulates a source file, that
Douglas Gregor29684422009-12-02 06:49:09 +000052/// file is not lazily brought in from disk to satisfy this query.
Ted Kremenekc16c2082009-01-06 01:55:26 +000053unsigned ContentCache::getSize() const {
Douglas Gregorc8151082010-03-16 22:53:51 +000054 return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize()
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000055 : (unsigned) ContentsEntry->getSize();
Ted Kremenekc16c2082009-01-06 01:55:26 +000056}
57
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000058void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B,
59 bool DoNotFree) {
Douglas Gregorc8151082010-03-16 22:53:51 +000060 assert(B != Buffer.getPointer());
Douglas Gregor29684422009-12-02 06:49:09 +000061
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000062 if (shouldFreeBuffer())
63 delete Buffer.getPointer();
Douglas Gregorc8151082010-03-16 22:53:51 +000064 Buffer.setPointer(B);
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000065 Buffer.setInt(DoNotFree? DoNotFreeFlag : 0);
Douglas Gregor29684422009-12-02 06:49:09 +000066}
67
Douglas Gregor36c35ba2010-03-16 00:35:39 +000068const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag,
Chris Lattner5c5db4e2010-04-20 20:49:23 +000069 const SourceManager &SM,
Chris Lattnere127a0d2010-04-20 20:35:58 +000070 SourceLocation Loc,
Douglas Gregor36c35ba2010-03-16 00:35:39 +000071 bool *Invalid) const {
Chris Lattnerb088cd32010-11-23 08:50:03 +000072 // Lazily create the Buffer for ContentCaches that wrap files. If we already
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000073 // computed it, just return what we have.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000074 if (Buffer.getPointer() || ContentsEntry == 0) {
Chris Lattnerb088cd32010-11-23 08:50:03 +000075 if (Invalid)
76 *Invalid = isBufferInvalid();
Chris Lattner38caec42010-04-20 18:14:03 +000077
Chris Lattnerb088cd32010-11-23 08:50:03 +000078 return Buffer.getPointer();
79 }
Benjamin Kramer5807d9c2010-11-18 12:46:39 +000080
Chris Lattnerb088cd32010-11-23 08:50:03 +000081 std::string ErrorStr;
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000082 Buffer.setPointer(SM.getFileManager().getBufferForFile(ContentsEntry, &ErrorStr));
Chris Lattnerb088cd32010-11-23 08:50:03 +000083
84 // If we were unable to open the file, then we are in an inconsistent
85 // situation where the content cache referenced a file which no longer
86 // exists. Most likely, we were using a stat cache with an invalid entry but
87 // the file could also have been removed during processing. Since we can't
88 // really deal with this situation, just create an empty buffer.
89 //
90 // FIXME: This is definitely not ideal, but our immediate clients can't
91 // currently handle returning a null entry here. Ideally we should detect
92 // that we are in an inconsistent situation and error out as quickly as
93 // possible.
94 if (!Buffer.getPointer()) {
95 const llvm::StringRef FillStr("<<<MISSING SOURCE FILE>>>\n");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000096 Buffer.setPointer(MemoryBuffer::getNewMemBuffer(ContentsEntry->getSize(),
Chris Lattnerb088cd32010-11-23 08:50:03 +000097 "<invalid>"));
98 char *Ptr = const_cast<char*>(Buffer.getPointer()->getBufferStart());
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000099 for (unsigned i = 0, e = ContentsEntry->getSize(); i != e; ++i)
Chris Lattnerb088cd32010-11-23 08:50:03 +0000100 Ptr[i] = FillStr[i % FillStr.size()];
101
102 if (Diag.isDiagnosticInFlight())
103 Diag.SetDelayedDiagnostic(diag::err_cannot_open_file,
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000104 ContentsEntry->getName(), ErrorStr);
Chris Lattnerb088cd32010-11-23 08:50:03 +0000105 else
106 Diag.Report(Loc, diag::err_cannot_open_file)
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000107 << ContentsEntry->getName() << ErrorStr;
Chris Lattnerb088cd32010-11-23 08:50:03 +0000108
109 Buffer.setInt(Buffer.getInt() | InvalidFlag);
110
111 if (Invalid) *Invalid = true;
112 return Buffer.getPointer();
113 }
114
115 // Check that the file's size is the same as in the file entry (which may
116 // have come from a stat cache).
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000117 if (getRawBuffer()->getBufferSize() != (size_t)ContentsEntry->getSize()) {
Chris Lattnerb088cd32010-11-23 08:50:03 +0000118 if (Diag.isDiagnosticInFlight())
119 Diag.SetDelayedDiagnostic(diag::err_file_modified,
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000120 ContentsEntry->getName());
Chris Lattnerb088cd32010-11-23 08:50:03 +0000121 else
122 Diag.Report(Loc, diag::err_file_modified)
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000123 << ContentsEntry->getName();
Chris Lattnerb088cd32010-11-23 08:50:03 +0000124
125 Buffer.setInt(Buffer.getInt() | InvalidFlag);
126 if (Invalid) *Invalid = true;
127 return Buffer.getPointer();
128 }
Eric Christopher156119d2011-04-09 00:01:04 +0000129
Chris Lattnerb088cd32010-11-23 08:50:03 +0000130 // If the buffer is valid, check to see if it has a UTF Byte Order Mark
Eric Christopher156119d2011-04-09 00:01:04 +0000131 // (BOM). We only support UTF-8 with and without a BOM right now. See
Chris Lattnerb088cd32010-11-23 08:50:03 +0000132 // http://en.wikipedia.org/wiki/Byte_order_mark for more information.
133 llvm::StringRef BufStr = Buffer.getPointer()->getBuffer();
Eric Christopher156119d2011-04-09 00:01:04 +0000134 const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr)
Chris Lattnerb088cd32010-11-23 08:50:03 +0000135 .StartsWith("\xFE\xFF", "UTF-16 (BE)")
136 .StartsWith("\xFF\xFE", "UTF-16 (LE)")
137 .StartsWith("\x00\x00\xFE\xFF", "UTF-32 (BE)")
138 .StartsWith("\xFF\xFE\x00\x00", "UTF-32 (LE)")
139 .StartsWith("\x2B\x2F\x76", "UTF-7")
140 .StartsWith("\xF7\x64\x4C", "UTF-1")
141 .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC")
142 .StartsWith("\x0E\xFE\xFF", "SDSU")
143 .StartsWith("\xFB\xEE\x28", "BOCU-1")
144 .StartsWith("\x84\x31\x95\x33", "GB-18030")
145 .Default(0);
146
Eric Christopher156119d2011-04-09 00:01:04 +0000147 if (InvalidBOM) {
Chris Lattnerb088cd32010-11-23 08:50:03 +0000148 Diag.Report(Loc, diag::err_unsupported_bom)
Eric Christopher156119d2011-04-09 00:01:04 +0000149 << InvalidBOM << ContentsEntry->getName();
Chris Lattnerb088cd32010-11-23 08:50:03 +0000150 Buffer.setInt(Buffer.getInt() | InvalidFlag);
Ted Kremenek5b034ad2009-01-06 22:43:04 +0000151 }
Douglas Gregoraea67db2010-03-15 22:54:52 +0000152
Douglas Gregorc8151082010-03-16 22:53:51 +0000153 if (Invalid)
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000154 *Invalid = isBufferInvalid();
Douglas Gregorc8151082010-03-16 22:53:51 +0000155
156 return Buffer.getPointer();
Ted Kremenekc16c2082009-01-06 01:55:26 +0000157}
158
Chris Lattner5b9a5042009-01-26 07:57:50 +0000159unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
160 // Look up the filename in the string table, returning the pre-existing value
161 // if it exists.
Mike Stump1eb44332009-09-09 15:08:12 +0000162 llvm::StringMapEntry<unsigned> &Entry =
Chris Lattner5b9a5042009-01-26 07:57:50 +0000163 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
164 if (Entry.getValue() != ~0U)
165 return Entry.getValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Chris Lattner5b9a5042009-01-26 07:57:50 +0000167 // Otherwise, assign this the next available ID.
168 Entry.setValue(FilenamesByID.size());
169 FilenamesByID.push_back(&Entry);
170 return FilenamesByID.size()-1;
171}
172
Chris Lattnerac50e342009-02-03 22:13:05 +0000173/// AddLineNote - Add a line note to the line table that indicates that there
174/// is a #line at the specified FID/Offset location which changes the presumed
175/// location to LineNo/FilenameID.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000176void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
Chris Lattnerac50e342009-02-03 22:13:05 +0000177 unsigned LineNo, int FilenameID) {
Chris Lattner23b5dc62009-02-04 00:40:31 +0000178 std::vector<LineEntry> &Entries = LineEntries[FID];
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner23b5dc62009-02-04 00:40:31 +0000180 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
181 "Adding line entries out of order!");
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattner9d79eba2009-02-04 05:21:58 +0000183 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
Chris Lattner137b6a62009-02-04 06:25:26 +0000184 unsigned IncludeOffset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Chris Lattner9d79eba2009-02-04 05:21:58 +0000186 if (!Entries.empty()) {
187 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
188 // that we are still in "foo.h".
189 if (FilenameID == -1)
190 FilenameID = Entries.back().FilenameID;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Chris Lattner137b6a62009-02-04 06:25:26 +0000192 // If we are after a line marker that switched us to system header mode, or
193 // that set #include information, preserve it.
Chris Lattner9d79eba2009-02-04 05:21:58 +0000194 Kind = Entries.back().FileKind;
Chris Lattner137b6a62009-02-04 06:25:26 +0000195 IncludeOffset = Entries.back().IncludeOffset;
Chris Lattner9d79eba2009-02-04 05:21:58 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Chris Lattner137b6a62009-02-04 06:25:26 +0000198 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
199 IncludeOffset));
Chris Lattnerac50e342009-02-03 22:13:05 +0000200}
201
Chris Lattner9d79eba2009-02-04 05:21:58 +0000202/// AddLineNote This is the same as the previous version of AddLineNote, but is
203/// used for GNU line markers. If EntryExit is 0, then this doesn't change the
204/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then
205/// this is a file exit. FileKind specifies whether this is a system header or
206/// extern C system header.
207void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
208 unsigned LineNo, int FilenameID,
209 unsigned EntryExit,
210 SrcMgr::CharacteristicKind FileKind) {
211 assert(FilenameID != -1 && "Unspecified filename should use other accessor");
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Chris Lattner9d79eba2009-02-04 05:21:58 +0000213 std::vector<LineEntry> &Entries = LineEntries[FID];
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Chris Lattner9d79eba2009-02-04 05:21:58 +0000215 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
216 "Adding line entries out of order!");
217
Chris Lattner137b6a62009-02-04 06:25:26 +0000218 unsigned IncludeOffset = 0;
219 if (EntryExit == 0) { // No #include stack change.
220 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
221 } else if (EntryExit == 1) {
222 IncludeOffset = Offset-1;
223 } else if (EntryExit == 2) {
224 assert(!Entries.empty() && Entries.back().IncludeOffset &&
225 "PPDirectives should have caught case when popping empty include stack");
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattner137b6a62009-02-04 06:25:26 +0000227 // Get the include loc of the last entries' include loc as our include loc.
228 IncludeOffset = 0;
229 if (const LineEntry *PrevEntry =
230 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
231 IncludeOffset = PrevEntry->IncludeOffset;
232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattner137b6a62009-02-04 06:25:26 +0000234 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
235 IncludeOffset));
Chris Lattner9d79eba2009-02-04 05:21:58 +0000236}
237
238
Chris Lattner3cd949c2009-02-04 01:55:42 +0000239/// FindNearestLineEntry - Find the line entry nearest to FID that is before
240/// it. If there is no line entry before Offset in FID, return null.
Mike Stump1eb44332009-09-09 15:08:12 +0000241const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
Chris Lattner3cd949c2009-02-04 01:55:42 +0000242 unsigned Offset) {
243 const std::vector<LineEntry> &Entries = LineEntries[FID];
244 assert(!Entries.empty() && "No #line entries for this FID after all!");
245
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000246 // It is very common for the query to be after the last #line, check this
247 // first.
248 if (Entries.back().FileOffset <= Offset)
249 return &Entries.back();
Chris Lattner3cd949c2009-02-04 01:55:42 +0000250
Chris Lattner6c1fbe02009-02-04 04:46:59 +0000251 // Do a binary search to find the maximal element that is still before Offset.
252 std::vector<LineEntry>::const_iterator I =
253 std::upper_bound(Entries.begin(), Entries.end(), Offset);
254 if (I == Entries.begin()) return 0;
255 return &*--I;
Chris Lattner3cd949c2009-02-04 01:55:42 +0000256}
Chris Lattnerac50e342009-02-03 22:13:05 +0000257
Douglas Gregorbd945002009-04-13 16:31:14 +0000258/// \brief Add a new line entry that has already been encoded into
259/// the internal representation of the line table.
Mike Stump1eb44332009-09-09 15:08:12 +0000260void LineTableInfo::AddEntry(unsigned FID,
Douglas Gregorbd945002009-04-13 16:31:14 +0000261 const std::vector<LineEntry> &Entries) {
262 LineEntries[FID] = Entries;
263}
Chris Lattnerac50e342009-02-03 22:13:05 +0000264
Chris Lattner5b9a5042009-01-26 07:57:50 +0000265/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
Mike Stump1eb44332009-09-09 15:08:12 +0000266///
Chris Lattner5b9a5042009-01-26 07:57:50 +0000267unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
268 if (LineTable == 0)
269 LineTable = new LineTableInfo();
270 return LineTable->getLineTableFilenameID(Ptr, Len);
271}
272
273
Chris Lattner4c4ea172009-02-03 21:52:55 +0000274/// AddLineNote - Add a line note to the line table for the FileID and offset
275/// specified by Loc. If FilenameID is -1, it is considered to be
276/// unspecified.
277void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
278 int FilenameID) {
Chris Lattnerac50e342009-02-03 22:13:05 +0000279 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattnerac50e342009-02-03 22:13:05 +0000281 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
282
283 // Remember that this file has #line directives now if it doesn't already.
284 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattnerac50e342009-02-03 22:13:05 +0000286 if (LineTable == 0)
287 LineTable = new LineTableInfo();
Chris Lattner23b5dc62009-02-04 00:40:31 +0000288 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
Chris Lattner4c4ea172009-02-03 21:52:55 +0000289}
290
Chris Lattner9d79eba2009-02-04 05:21:58 +0000291/// AddLineNote - Add a GNU line marker to the line table.
292void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
293 int FilenameID, bool IsFileEntry,
294 bool IsFileExit, bool IsSystemHeader,
295 bool IsExternCHeader) {
296 // If there is no filename and no flags, this is treated just like a #line,
297 // which does not change the flags of the previous line marker.
298 if (FilenameID == -1) {
299 assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
300 "Can't set flags without setting the filename!");
301 return AddLineNote(Loc, LineNo, FilenameID);
302 }
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattner9d79eba2009-02-04 05:21:58 +0000304 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
305 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Chris Lattner9d79eba2009-02-04 05:21:58 +0000307 // Remember that this file has #line directives now if it doesn't already.
308 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattner9d79eba2009-02-04 05:21:58 +0000310 if (LineTable == 0)
311 LineTable = new LineTableInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Chris Lattner9d79eba2009-02-04 05:21:58 +0000313 SrcMgr::CharacteristicKind FileKind;
314 if (IsExternCHeader)
315 FileKind = SrcMgr::C_ExternCSystem;
316 else if (IsSystemHeader)
317 FileKind = SrcMgr::C_System;
318 else
319 FileKind = SrcMgr::C_User;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Chris Lattner9d79eba2009-02-04 05:21:58 +0000321 unsigned EntryExit = 0;
322 if (IsFileEntry)
323 EntryExit = 1;
324 else if (IsFileExit)
325 EntryExit = 2;
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Chris Lattner9d79eba2009-02-04 05:21:58 +0000327 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
328 EntryExit, FileKind);
329}
330
Douglas Gregorbd945002009-04-13 16:31:14 +0000331LineTableInfo &SourceManager::getLineTable() {
332 if (LineTable == 0)
333 LineTable = new LineTableInfo();
334 return *LineTable;
335}
Chris Lattner4c4ea172009-02-03 21:52:55 +0000336
Chris Lattner23b5dc62009-02-04 00:40:31 +0000337//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000338// Private 'Create' methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000339//===----------------------------------------------------------------------===//
Ted Kremenekc16c2082009-01-06 01:55:26 +0000340
Chris Lattner39b49bc2010-11-23 08:35:12 +0000341SourceManager::SourceManager(Diagnostic &Diag, FileManager &FileMgr)
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000342 : Diag(Diag), FileMgr(FileMgr), OverridenFilesKeepOriginalName(true),
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000343 ExternalSLocEntries(0), LineTable(0), NumLinearScans(0),
344 NumBinaryProbes(0) {
345 clearIDTables();
346 Diag.setSourceManager(this);
347}
348
Chris Lattner5b9a5042009-01-26 07:57:50 +0000349SourceManager::~SourceManager() {
350 delete LineTable;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000352 // Delete FileEntry objects corresponding to content caches. Since the actual
353 // content cache objects are bump pointer allocated, we just have to run the
354 // dtors, but we call the deallocate method for completeness.
355 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
356 MemBufferInfos[i]->~ContentCache();
357 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
358 }
359 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
360 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
361 I->second->~ContentCache();
362 ContentCacheAlloc.Deallocate(I->second);
363 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000364}
365
366void SourceManager::clearIDTables() {
367 MainFileID = FileID();
368 SLocEntryTable.clear();
369 LastLineNoFileIDQuery = FileID();
370 LastLineNoContentCache = 0;
371 LastFileIDLookup = FileID();
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Chris Lattner5b9a5042009-01-26 07:57:50 +0000373 if (LineTable)
374 LineTable->clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattner5b9a5042009-01-26 07:57:50 +0000376 // Use up FileID #0 as an invalid instantiation.
377 NextOffset = 0;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000378 createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000379}
380
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000381/// getOrCreateContentCache - Create or return a cached ContentCache for the
382/// specified file.
383const ContentCache *
384SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 assert(FileEnt && "Didn't specify a file entry to use?");
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 // Do we already have information about this file?
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000388 ContentCache *&Entry = FileInfos[FileEnt];
389 if (Entry) return Entry;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattner00282d62009-02-03 07:41:46 +0000391 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned
392 // so that FileInfo can use the low 3 bits of the pointer for its own
393 // nefarious purposes.
394 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
395 EntryAlign = std::max(8U, EntryAlign);
396 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000397
398 // If the file contents are overridden with contents from another file,
399 // pass that file to ContentCache.
400 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
401 overI = OverriddenFiles.find(FileEnt);
402 if (overI == OverriddenFiles.end())
403 new (Entry) ContentCache(FileEnt);
404 else
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000405 new (Entry) ContentCache(OverridenFilesKeepOriginalName ? FileEnt
406 : overI->second,
407 overI->second);
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000408
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000409 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000410}
411
412
Ted Kremenekd1c0eee2007-10-31 17:53:38 +0000413/// createMemBufferContentCache - Create a new ContentCache for the specified
414/// memory buffer. This does no caching.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000415const ContentCache*
416SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
Chris Lattner00282d62009-02-03 07:41:46 +0000417 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure
418 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
419 // the pointer for its own nefarious purposes.
420 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
421 EntryAlign = std::max(8U, EntryAlign);
422 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000423 new (Entry) ContentCache();
424 MemBufferInfos.push_back(Entry);
425 Entry->setBuffer(Buffer);
426 return Entry;
Reid Spencer5f016e22007-07-11 17:01:13 +0000427}
428
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000429void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source,
430 unsigned NumSLocEntries,
431 unsigned NextOffset) {
432 ExternalSLocEntries = Source;
433 this->NextOffset = NextOffset;
Sebastian Redlb86238d2010-07-28 21:07:02 +0000434 unsigned CurPrealloc = SLocEntryLoaded.size();
435 // If we've ever preallocated, we must not count the dummy entry.
436 if (CurPrealloc) --CurPrealloc;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000437 SLocEntryLoaded.resize(NumSLocEntries + 1);
438 SLocEntryLoaded[0] = true;
Sebastian Redlb86238d2010-07-28 21:07:02 +0000439 SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries - CurPrealloc);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000440}
441
Douglas Gregor2bf1eb02009-04-27 21:28:04 +0000442void SourceManager::ClearPreallocatedSLocEntries() {
443 unsigned I = 0;
444 for (unsigned N = SLocEntryLoaded.size(); I != N; ++I)
445 if (!SLocEntryLoaded[I])
446 break;
447
448 // We've already loaded all preallocated source location entries.
449 if (I == SLocEntryLoaded.size())
450 return;
451
452 // Remove everything from location I onward.
453 SLocEntryTable.resize(I);
454 SLocEntryLoaded.clear();
455 ExternalSLocEntries = 0;
456}
457
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000458
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000459//===----------------------------------------------------------------------===//
460// Methods to create new FileID's and instantiations.
461//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000462
Dan Gohman3f86b782010-08-26 21:27:06 +0000463/// createFileID - Create a new FileID for the specified ContentCache and
Ted Kremenek0d892d82007-10-30 22:57:35 +0000464/// include position. This works regardless of whether the ContentCache
465/// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000466FileID SourceManager::createFileID(const ContentCache *File,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000467 SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000468 SrcMgr::CharacteristicKind FileCharacter,
469 unsigned PreallocatedID,
470 unsigned Offset) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000471 if (PreallocatedID) {
472 // If we're filling in a preallocated ID, just load in the file
473 // entry and return.
Mike Stump1eb44332009-09-09 15:08:12 +0000474 assert(PreallocatedID < SLocEntryLoaded.size() &&
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000475 "Preallocate ID out-of-range");
Mike Stump1eb44332009-09-09 15:08:12 +0000476 assert(!SLocEntryLoaded[PreallocatedID] &&
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000477 "Source location entry already loaded");
478 assert(Offset && "Preallocate source location cannot have zero offset");
Mike Stump1eb44332009-09-09 15:08:12 +0000479 SLocEntryTable[PreallocatedID]
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000480 = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter));
481 SLocEntryLoaded[PreallocatedID] = true;
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000482 FileID FID = FileID::get(PreallocatedID);
Douglas Gregor5de65722010-03-19 06:12:06 +0000483 return FID;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000484 }
485
Mike Stump1eb44332009-09-09 15:08:12 +0000486 SLocEntryTable.push_back(SLocEntry::get(NextOffset,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000487 FileInfo::get(IncludePos, File,
488 FileCharacter)));
Ted Kremenekc16c2082009-01-06 01:55:26 +0000489 unsigned FileSize = File->getSize();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000490 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
491 NextOffset += FileSize+1;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000493 // Set LastFileIDLookup to the newly created file. The next getFileID call is
494 // almost guaranteed to be from that file.
Argyrios Kyrtzidisea703f12009-06-23 00:42:06 +0000495 FileID FID = FileID::get(SLocEntryTable.size()-1);
Argyrios Kyrtzidisea703f12009-06-23 00:42:06 +0000496 return LastFileIDLookup = FID;
Reid Spencer5f016e22007-07-11 17:01:13 +0000497}
498
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000499/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000500/// that a token from SpellingLoc should actually be referenced from
Reid Spencer5f016e22007-07-11 17:01:13 +0000501/// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000502SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000503 SourceLocation ILocStart,
504 SourceLocation ILocEnd,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000505 unsigned TokLength,
506 unsigned PreallocatedID,
507 unsigned Offset) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000508 InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000509 if (PreallocatedID) {
510 // If we're filling in a preallocated ID, just load in the
511 // instantiation entry and return.
Mike Stump1eb44332009-09-09 15:08:12 +0000512 assert(PreallocatedID < SLocEntryLoaded.size() &&
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000513 "Preallocate ID out-of-range");
Mike Stump1eb44332009-09-09 15:08:12 +0000514 assert(!SLocEntryLoaded[PreallocatedID] &&
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000515 "Source location entry already loaded");
516 assert(Offset && "Preallocate source location cannot have zero offset");
517 SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II);
518 SLocEntryLoaded[PreallocatedID] = true;
519 return SourceLocation::getMacroLoc(Offset);
520 }
Chris Lattnere7fb4842009-02-15 20:52:18 +0000521 SLocEntryTable.push_back(SLocEntry::get(NextOffset, II));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000522 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
523 NextOffset += TokLength+1;
524 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000525}
526
Douglas Gregor36c35ba2010-03-16 00:35:39 +0000527const llvm::MemoryBuffer *
Douglas Gregor50f6af72010-03-16 05:20:39 +0000528SourceManager::getMemoryBufferForFile(const FileEntry *File,
529 bool *Invalid) {
Douglas Gregor29684422009-12-02 06:49:09 +0000530 const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
Douglas Gregoraea67db2010-03-15 22:54:52 +0000531 assert(IR && "getOrCreateContentCache() cannot return NULL");
Chris Lattnere127a0d2010-04-20 20:35:58 +0000532 return IR->getBuffer(Diag, *this, SourceLocation(), Invalid);
Douglas Gregor29684422009-12-02 06:49:09 +0000533}
534
Dan Gohman0d06e992010-10-26 20:47:28 +0000535void SourceManager::overrideFileContents(const FileEntry *SourceFile,
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000536 const llvm::MemoryBuffer *Buffer,
537 bool DoNotFree) {
Douglas Gregor29684422009-12-02 06:49:09 +0000538 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
Dan Gohman0d06e992010-10-26 20:47:28 +0000539 assert(IR && "getOrCreateContentCache() cannot return NULL");
Douglas Gregor29684422009-12-02 06:49:09 +0000540
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000541 const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree);
Douglas Gregor29684422009-12-02 06:49:09 +0000542}
543
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000544void SourceManager::overrideFileContents(const FileEntry *SourceFile,
545 const FileEntry *NewFile) {
546 assert(SourceFile->getSize() == NewFile->getSize() &&
547 "Different sizes, use the FileManager to create a virtual file with "
548 "the correct size");
549 assert(FileInfos.count(SourceFile) == 0 &&
550 "This function should be called at the initialization stage, before "
551 "any parsing occurs.");
552 OverriddenFiles[SourceFile] = NewFile;
553}
554
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000555llvm::StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
Douglas Gregoraae58b02010-03-16 20:01:30 +0000556 bool MyInvalid = false;
Douglas Gregor3de84242011-01-31 22:42:36 +0000557 const SLocEntry &SLoc = getSLocEntry(FID.ID);
558 if (!SLoc.isFile()) {
559 if (Invalid)
560 *Invalid = true;
561 return "<<<<<INVALID SOURCE LOCATION>>>>>";
562 }
563
564 const llvm::MemoryBuffer *Buf
565 = SLoc.getFile().getContentCache()->getBuffer(Diag, *this, SourceLocation(),
566 &MyInvalid);
Douglas Gregorf715ca12010-03-16 00:06:06 +0000567 if (Invalid)
Douglas Gregoraae58b02010-03-16 20:01:30 +0000568 *Invalid = MyInvalid;
569
570 if (MyInvalid)
Douglas Gregor3de84242011-01-31 22:42:36 +0000571 return "<<<<<INVALID SOURCE LOCATION>>>>>";
Douglas Gregoraae58b02010-03-16 20:01:30 +0000572
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000573 return Buf->getBuffer();
Douglas Gregoraea67db2010-03-15 22:54:52 +0000574}
Chris Lattner2b2453a2009-01-17 06:22:33 +0000575
Chris Lattner23b5dc62009-02-04 00:40:31 +0000576//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000577// SourceLocation manipulation methods.
Chris Lattner23b5dc62009-02-04 00:40:31 +0000578//===----------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000579
580/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
581/// method that is used for all SourceManager queries that start with a
582/// SourceLocation object. It is responsible for finding the entry in
583/// SLocEntryTable which contains the specified location.
584///
585FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
586 assert(SLocOffset && "Invalid FileID");
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000588 // After the first and second level caches, I see two common sorts of
589 // behavior: 1) a lot of searched FileID's are "near" the cached file location
590 // or are "near" the cached instantiation location. 2) others are just
591 // completely random and may be a very long way away.
592 //
593 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
594 // then we fall back to a less cache efficient, but more scalable, binary
595 // search to find the location.
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000597 // See if this is near the file point - worst case we start scanning from the
598 // most newly created FileID.
599 std::vector<SrcMgr::SLocEntry>::const_iterator I;
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000601 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
602 // Neither loc prunes our search.
603 I = SLocEntryTable.end();
604 } else {
605 // Perhaps it is near the file point.
606 I = SLocEntryTable.begin()+LastFileIDLookup.ID;
607 }
608
609 // Find the FileID that contains this. "I" is an iterator that points to a
610 // FileID whose offset is known to be larger than SLocOffset.
611 unsigned NumProbes = 0;
612 while (1) {
613 --I;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000614 if (ExternalSLocEntries)
615 getSLocEntry(FileID::get(I - SLocEntryTable.begin()));
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000616 if (I->getOffset() <= SLocOffset) {
617#if 0
618 printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
619 I-SLocEntryTable.begin(),
620 I->isInstantiation() ? "inst" : "file",
621 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
622#endif
623 FileID Res = FileID::get(I-SLocEntryTable.begin());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000624
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000625 // If this isn't an instantiation, remember it. We have good locality
626 // across FileID lookups.
627 if (!I->isInstantiation())
628 LastFileIDLookup = Res;
629 NumLinearScans += NumProbes+1;
630 return Res;
631 }
632 if (++NumProbes == 8)
633 break;
634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000636 // Convert "I" back into an index. We know that it is an entry whose index is
637 // larger than the offset we are looking for.
638 unsigned GreaterIndex = I-SLocEntryTable.begin();
639 // LessIndex - This is the lower bound of the range that we're searching.
640 // We know that the offset corresponding to the FileID is is less than
641 // SLocOffset.
642 unsigned LessIndex = 0;
643 NumProbes = 0;
644 while (1) {
645 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000646 unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset();
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000648 ++NumProbes;
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000650 // If the offset of the midpoint is too large, chop the high side of the
651 // range to the midpoint.
652 if (MidOffset > SLocOffset) {
653 GreaterIndex = MiddleIndex;
654 continue;
655 }
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000657 // If the middle index contains the value, succeed and return.
658 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
659#if 0
660 printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
661 I-SLocEntryTable.begin(),
662 I->isInstantiation() ? "inst" : "file",
663 LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
664#endif
665 FileID Res = FileID::get(MiddleIndex);
666
667 // If this isn't an instantiation, remember it. We have good locality
668 // across FileID lookups.
669 if (!I->isInstantiation())
670 LastFileIDLookup = Res;
671 NumBinaryProbes += NumProbes;
672 return Res;
673 }
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000675 // Otherwise, move the low-side up to the middle index.
676 LessIndex = MiddleIndex;
677 }
678}
679
Chris Lattneraddb7972009-01-26 20:04:19 +0000680SourceLocation SourceManager::
681getInstantiationLocSlowCase(SourceLocation Loc) const {
682 do {
Chris Lattnera5c6c582010-02-12 19:31:35 +0000683 // Note: If Loc indicates an offset into a token that came from a macro
684 // expansion (e.g. the 5th character of the token) we do not want to add
685 // this offset when going to the instantiation location. The instatiation
686 // location is the macro invocation, which the offset has nothing to do
687 // with. This is unlike when we get the spelling loc, because the offset
688 // directly correspond to the token whose spelling we're inspecting.
689 Loc = getSLocEntry(getFileID(Loc)).getInstantiation()
Chris Lattnere7fb4842009-02-15 20:52:18 +0000690 .getInstantiationLocStart();
Chris Lattneraddb7972009-01-26 20:04:19 +0000691 } while (!Loc.isFileID());
692
693 return Loc;
694}
695
696SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
697 do {
698 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
699 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
700 Loc = Loc.getFileLocWithOffset(LocInfo.second);
701 } while (!Loc.isFileID());
702 return Loc;
703}
704
705
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000706std::pair<FileID, unsigned>
707SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
708 unsigned Offset) const {
709 // If this is an instantiation record, walk through all the instantiation
710 // points.
711 FileID FID;
712 SourceLocation Loc;
713 do {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000714 Loc = E->getInstantiation().getInstantiationLocStart();
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000716 FID = getFileID(Loc);
717 E = &getSLocEntry(FID);
718 Offset += Loc.getOffset()-E->getOffset();
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000719 } while (!Loc.isFileID());
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000721 return std::make_pair(FID, Offset);
722}
723
724std::pair<FileID, unsigned>
725SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
726 unsigned Offset) const {
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000727 // If this is an instantiation record, walk through all the instantiation
728 // points.
729 FileID FID;
730 SourceLocation Loc;
731 do {
732 Loc = E->getInstantiation().getSpellingLoc();
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Chris Lattnerbcd1a1b2009-01-26 19:41:58 +0000734 FID = getFileID(Loc);
735 E = &getSLocEntry(FID);
736 Offset += Loc.getOffset()-E->getOffset();
737 } while (!Loc.isFileID());
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000739 return std::make_pair(FID, Offset);
740}
741
Chris Lattner387616e2009-02-17 08:04:48 +0000742/// getImmediateSpellingLoc - Given a SourceLocation object, return the
743/// spelling location referenced by the ID. This is the first level down
744/// towards the place where the characters that make up the lexed token can be
745/// found. This should not generally be used by clients.
746SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
747 if (Loc.isFileID()) return Loc;
748 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
749 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
750 return Loc.getFileLocWithOffset(LocInfo.second);
751}
752
753
Chris Lattnere7fb4842009-02-15 20:52:18 +0000754/// getImmediateInstantiationRange - Loc is required to be an instantiation
755/// location. Return the start/end of the instantiation information.
756std::pair<SourceLocation,SourceLocation>
757SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const {
758 assert(Loc.isMacroID() && "Not an instantiation loc!");
759 const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation();
760 return II.getInstantiationLocRange();
761}
762
Chris Lattner66781332009-02-15 21:26:50 +0000763/// getInstantiationRange - Given a SourceLocation object, return the
764/// range of tokens covered by the instantiation in the ultimate file.
765std::pair<SourceLocation,SourceLocation>
766SourceManager::getInstantiationRange(SourceLocation Loc) const {
767 if (Loc.isFileID()) return std::make_pair(Loc, Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattner66781332009-02-15 21:26:50 +0000769 std::pair<SourceLocation,SourceLocation> Res =
770 getImmediateInstantiationRange(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Chris Lattner66781332009-02-15 21:26:50 +0000772 // Fully resolve the start and end locations to their ultimate instantiation
773 // points.
774 while (!Res.first.isFileID())
775 Res.first = getImmediateInstantiationRange(Res.first).first;
776 while (!Res.second.isFileID())
777 Res.second = getImmediateInstantiationRange(Res.second).second;
778 return Res;
779}
780
Chris Lattnere7fb4842009-02-15 20:52:18 +0000781
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000782
783//===----------------------------------------------------------------------===//
784// Queries about the code at a SourceLocation.
785//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +0000786
787/// getCharacterData - Return a pointer to the start of the specified location
788/// in the appropriate MemoryBuffer.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000789const char *SourceManager::getCharacterData(SourceLocation SL,
790 bool *Invalid) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 // Note that this is a hot function in the getSpelling() path, which is
792 // heavily used by -E mode.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000793 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenekc16c2082009-01-06 01:55:26 +0000795 // Note that calling 'getBuffer()' may lazily page in a source file.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000796 bool CharDataInvalid = false;
797 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +0000798 = getSLocEntry(LocInfo.first).getFile().getContentCache()
799 ->getBuffer(Diag, *this, SourceLocation(), &CharDataInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000800 if (Invalid)
801 *Invalid = CharDataInvalid;
802 return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second);
Reid Spencer5f016e22007-07-11 17:01:13 +0000803}
804
Reid Spencer5f016e22007-07-11 17:01:13 +0000805
Chris Lattner9dc1f532007-07-20 16:37:10 +0000806/// getColumnNumber - Return the column # for the specified file position.
Chris Lattner7da5aea2009-02-04 00:55:58 +0000807/// this is significantly cheaper to compute than the line number.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000808unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
809 bool *Invalid) const {
810 bool MyInvalid = false;
811 const char *Buf = getBuffer(FID, &MyInvalid)->getBufferStart();
812 if (Invalid)
813 *Invalid = MyInvalid;
814
815 if (MyInvalid)
816 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 unsigned LineStart = FilePos;
819 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
820 --LineStart;
821 return FilePos-LineStart+1;
822}
823
Zhanyong Wan1f24e112010-10-05 17:56:33 +0000824// isInvalid - Return the result of calling loc.isInvalid(), and
825// if Invalid is not null, set its value to same.
826static bool isInvalid(SourceLocation Loc, bool *Invalid) {
827 bool MyInvalid = Loc.isInvalid();
828 if (Invalid)
829 *Invalid = MyInvalid;
830 return MyInvalid;
831}
832
Douglas Gregor50f6af72010-03-16 05:20:39 +0000833unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
834 bool *Invalid) const {
Zhanyong Wan1f24e112010-10-05 17:56:33 +0000835 if (isInvalid(Loc, Invalid)) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000836 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000837 return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
Chris Lattner7da5aea2009-02-04 00:55:58 +0000838}
839
Douglas Gregor50f6af72010-03-16 05:20:39 +0000840unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc,
841 bool *Invalid) const {
Zhanyong Wan1f24e112010-10-05 17:56:33 +0000842 if (isInvalid(Loc, Invalid)) return 0;
Chris Lattner7da5aea2009-02-04 00:55:58 +0000843 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000844 return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
Chris Lattner7da5aea2009-02-04 00:55:58 +0000845}
846
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000847unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc,
848 bool *Invalid) const {
849 if (isInvalid(Loc, Invalid)) return 0;
850 return getPresumedLoc(Loc).getColumn();
851}
852
Chandler Carruth14bd9652010-10-23 08:44:57 +0000853static LLVM_ATTRIBUTE_NOINLINE void
Chris Lattnere127a0d2010-04-20 20:35:58 +0000854ComputeLineNumbers(Diagnostic &Diag, ContentCache *FI,
855 llvm::BumpPtrAllocator &Alloc,
856 const SourceManager &SM, bool &Invalid);
857static void ComputeLineNumbers(Diagnostic &Diag, ContentCache *FI,
858 llvm::BumpPtrAllocator &Alloc,
859 const SourceManager &SM, bool &Invalid) {
Ted Kremenekc16c2082009-01-06 01:55:26 +0000860 // Note that calling 'getBuffer()' may lazily page in the file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000861 const MemoryBuffer *Buffer = FI->getBuffer(Diag, SM, SourceLocation(),
862 &Invalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000863 if (Invalid)
864 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000866 // Find the file offsets of all of the *physical* source lines. This does
867 // not look at trigraphs, escaped newlines, or anything else tricky.
868 std::vector<unsigned> LineOffsets;
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000870 // Line #1 starts at char 0.
871 LineOffsets.push_back(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000873 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
874 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
875 unsigned Offs = 0;
876 while (1) {
877 // Skip over the contents of the line.
878 // TODO: Vectorize this? This is very performance sensitive for programs
879 // with lots of diagnostics and in -E mode.
880 const unsigned char *NextBuf = (const unsigned char *)Buf;
881 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
882 ++NextBuf;
883 Offs += NextBuf-Buf;
884 Buf = NextBuf;
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000886 if (Buf[0] == '\n' || Buf[0] == '\r') {
887 // If this is \n\r or \r\n, skip both characters.
888 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
889 ++Offs, ++Buf;
890 ++Offs, ++Buf;
891 LineOffsets.push_back(Offs);
892 } else {
893 // Otherwise, this is a null. If end of file, exit.
894 if (Buf == End) break;
895 // Otherwise, skip the null.
896 ++Offs, ++Buf;
897 }
898 }
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000900 // Copy the offsets into the FileInfo structure.
901 FI->NumLines = LineOffsets.size();
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000902 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000903 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
904}
Reid Spencer5f016e22007-07-11 17:01:13 +0000905
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000906/// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000907/// for the position indicated. This requires building and caching a table of
908/// line offsets for the MemoryBuffer, so this is not cheap: use only when
909/// about to emit a diagnostic.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000910unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
911 bool *Invalid) const {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000912 ContentCache *Content;
Chris Lattner30fc9332009-02-04 01:06:56 +0000913 if (LastLineNoFileIDQuery == FID)
Ted Kremenek78d85f52007-10-30 21:08:08 +0000914 Content = LastLineNoContentCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000915 else
Chris Lattner30fc9332009-02-04 01:06:56 +0000916 Content = const_cast<ContentCache*>(getSLocEntry(FID)
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000917 .getFile().getContentCache());
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 // If this is the first use of line information for this buffer, compute the
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000920 /// SourceLineCache for it on demand.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000921 if (Content->SourceLineCache == 0) {
922 bool MyInvalid = false;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000923 ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +0000924 if (Invalid)
925 *Invalid = MyInvalid;
926 if (MyInvalid)
927 return 1;
928 } else if (Invalid)
929 *Invalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000930
931 // Okay, we know we have a line number table. Do a binary search to find the
932 // line number that this character position lands on.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000933 unsigned *SourceLineCache = Content->SourceLineCache;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000934 unsigned *SourceLineCacheStart = SourceLineCache;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000935 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Chris Lattner30fc9332009-02-04 01:06:56 +0000937 unsigned QueriedFilePos = FilePos+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000938
Daniel Dunbar4106d692009-05-18 17:30:52 +0000939 // FIXME: I would like to be convinced that this code is worth being as
Mike Stump1eb44332009-09-09 15:08:12 +0000940 // complicated as it is, binary search isn't that slow.
Daniel Dunbar4106d692009-05-18 17:30:52 +0000941 //
942 // If it is worth being optimized, then in my opinion it could be more
943 // performant, simpler, and more obviously correct by just "galloping" outward
944 // from the queried file position. In fact, this could be incorporated into a
945 // generic algorithm such as lower_bound_with_hint.
946 //
947 // If someone gives me a test case where this matters, and I will do it! - DWD
948
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000949 // If the previous query was to the same file, we know both the file pos from
950 // that query and the line number returned. This allows us to narrow the
951 // search space from the entire file to something near the match.
Chris Lattner30fc9332009-02-04 01:06:56 +0000952 if (LastLineNoFileIDQuery == FID) {
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000953 if (QueriedFilePos >= LastLineNoFilePos) {
Daniel Dunbar4106d692009-05-18 17:30:52 +0000954 // FIXME: Potential overflow?
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000955 SourceLineCache = SourceLineCache+LastLineNoResult-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000957 // The query is likely to be nearby the previous one. Here we check to
958 // see if it is within 5, 10 or 20 lines. It can be far away in cases
959 // where big comment blocks and vertical whitespace eat up lines but
960 // contribute no tokens.
961 if (SourceLineCache+5 < SourceLineCacheEnd) {
962 if (SourceLineCache[5] > QueriedFilePos)
963 SourceLineCacheEnd = SourceLineCache+5;
964 else if (SourceLineCache+10 < SourceLineCacheEnd) {
965 if (SourceLineCache[10] > QueriedFilePos)
966 SourceLineCacheEnd = SourceLineCache+10;
967 else if (SourceLineCache+20 < SourceLineCacheEnd) {
968 if (SourceLineCache[20] > QueriedFilePos)
969 SourceLineCacheEnd = SourceLineCache+20;
970 }
971 }
972 }
973 } else {
Daniel Dunbar4106d692009-05-18 17:30:52 +0000974 if (LastLineNoResult < Content->NumLines)
975 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000976 }
977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000979 // If the spread is large, do a "radix" test as our initial guess, based on
980 // the assumption that lines average to approximately the same length.
981 // NOTE: This is currently disabled, as it does not appear to be profitable in
982 // initial measurements.
983 if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
Ted Kremenek78d85f52007-10-30 21:08:08 +0000984 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000986 // Take a stab at guessing where it is.
Ted Kremenek78d85f52007-10-30 21:08:08 +0000987 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000989 // Check for -10 and +10 lines.
990 unsigned LowerBound = std::max(int(ApproxPos-10), 0);
991 unsigned UpperBound = std::min(ApproxPos+10, FileLen);
992
993 // If the computed lower bound is less than the query location, move it in.
994 if (SourceLineCache < SourceLineCacheStart+LowerBound &&
995 SourceLineCacheStart[LowerBound] < QueriedFilePos)
996 SourceLineCache = SourceLineCacheStart+LowerBound;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattner1cf12bf2007-07-24 06:43:46 +0000998 // If the computed upper bound is greater than the query location, move it.
999 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
1000 SourceLineCacheStart[UpperBound] >= QueriedFilePos)
1001 SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
1002 }
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Chris Lattner1cf12bf2007-07-24 06:43:46 +00001004 unsigned *Pos
1005 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
Chris Lattner5e36a7a2007-07-24 05:57:19 +00001006 unsigned LineNo = Pos-SourceLineCacheStart;
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Chris Lattner30fc9332009-02-04 01:06:56 +00001008 LastLineNoFileIDQuery = FID;
Ted Kremenek78d85f52007-10-30 21:08:08 +00001009 LastLineNoContentCache = Content;
Chris Lattner5e36a7a2007-07-24 05:57:19 +00001010 LastLineNoFilePos = QueriedFilePos;
1011 LastLineNoResult = LineNo;
1012 return LineNo;
Reid Spencer5f016e22007-07-11 17:01:13 +00001013}
1014
Chandler Carruth5ef04ee2011-02-23 00:47:48 +00001015unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
1016 bool *Invalid) const {
1017 if (isInvalid(Loc, Invalid)) return 0;
1018 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
1019 return getLineNumber(LocInfo.first, LocInfo.second);
1020}
Douglas Gregor50f6af72010-03-16 05:20:39 +00001021unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc,
1022 bool *Invalid) const {
Zhanyong Wan1f24e112010-10-05 17:56:33 +00001023 if (isInvalid(Loc, Invalid)) return 0;
Chris Lattner30fc9332009-02-04 01:06:56 +00001024 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
1025 return getLineNumber(LocInfo.first, LocInfo.second);
1026}
Chandler Carruth5ef04ee2011-02-23 00:47:48 +00001027unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
Douglas Gregor50f6af72010-03-16 05:20:39 +00001028 bool *Invalid) const {
Zhanyong Wan1f24e112010-10-05 17:56:33 +00001029 if (isInvalid(Loc, Invalid)) return 0;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +00001030 return getPresumedLoc(Loc).getLine();
Chris Lattner30fc9332009-02-04 01:06:56 +00001031}
1032
Chris Lattner6b306672009-02-04 05:33:01 +00001033/// getFileCharacteristic - return the file characteristic of the specified
Mike Stump1eb44332009-09-09 15:08:12 +00001034/// source location, indicating whether this is a normal file, a system
Chris Lattner6b306672009-02-04 05:33:01 +00001035/// header, or an "implicit extern C" system header.
1036///
1037/// This state can be modified with flags on GNU linemarker directives like:
1038/// # 4 "foo.h" 3
1039/// which changes all source locations in the current file after that to be
1040/// considered to be from a system header.
Mike Stump1eb44332009-09-09 15:08:12 +00001041SrcMgr::CharacteristicKind
Chris Lattner6b306672009-02-04 05:33:01 +00001042SourceManager::getFileCharacteristic(SourceLocation Loc) const {
1043 assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
1044 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
1045 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
1046
1047 // If there are no #line directives in this file, just return the whole-file
1048 // state.
1049 if (!FI.hasLineDirectives())
1050 return FI.getFileCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Chris Lattner6b306672009-02-04 05:33:01 +00001052 assert(LineTable && "Can't have linetable entries without a LineTable!");
1053 // See if there is a #line directive before the location.
1054 const LineEntry *Entry =
1055 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chris Lattner6b306672009-02-04 05:33:01 +00001057 // If this is before the first line marker, use the file characteristic.
1058 if (!Entry)
1059 return FI.getFileCharacteristic();
1060
1061 return Entry->FileKind;
1062}
1063
Chris Lattnerbff5c512009-02-17 08:39:06 +00001064/// Return the filename or buffer identifier of the buffer the location is in.
1065/// Note that this name does not respect #line directives. Use getPresumedLoc
1066/// for normal clients.
Douglas Gregor50f6af72010-03-16 05:20:39 +00001067const char *SourceManager::getBufferName(SourceLocation Loc,
1068 bool *Invalid) const {
Zhanyong Wan1f24e112010-10-05 17:56:33 +00001069 if (isInvalid(Loc, Invalid)) return "<invalid loc>";
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Douglas Gregor50f6af72010-03-16 05:20:39 +00001071 return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier();
Chris Lattnerbff5c512009-02-17 08:39:06 +00001072}
1073
Chris Lattner30fc9332009-02-04 01:06:56 +00001074
Chris Lattnerb9c3f962009-01-27 07:57:44 +00001075/// getPresumedLoc - This method returns the "presumed" location of a
1076/// SourceLocation specifies. A "presumed location" can be modified by #line
1077/// or GNU line marker directives. This provides a view on the data that a
1078/// user should see in diagnostics, for example.
1079///
1080/// Note that a presumed location is always given as the instantiation point
1081/// of an instantiation location, not at the spelling location.
1082PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
1083 if (Loc.isInvalid()) return PresumedLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Chris Lattnerb9c3f962009-01-27 07:57:44 +00001085 // Presumed locations are always for instantiation points.
Chris Lattner7da5aea2009-02-04 00:55:58 +00001086 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Chris Lattner30fc9332009-02-04 01:06:56 +00001088 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
Chris Lattnerb9c3f962009-01-27 07:57:44 +00001089 const SrcMgr::ContentCache *C = FI.getContentCache();
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Chris Lattner3cd949c2009-02-04 01:55:42 +00001091 // To get the source name, first consult the FileEntry (if one exists)
1092 // before the MemBuffer as this will avoid unnecessarily paging in the
1093 // MemBuffer.
Chris Lattnere127a0d2010-04-20 20:35:58 +00001094 const char *Filename;
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001095 if (C->OrigEntry)
1096 Filename = C->OrigEntry->getName();
Chris Lattnere127a0d2010-04-20 20:35:58 +00001097 else
1098 Filename = C->getBuffer(Diag, *this)->getBufferIdentifier();
Douglas Gregorc417fa02010-11-02 00:39:22 +00001099 bool Invalid = false;
1100 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid);
1101 if (Invalid)
1102 return PresumedLoc();
1103 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid);
1104 if (Invalid)
1105 return PresumedLoc();
1106
Chris Lattner3cd949c2009-02-04 01:55:42 +00001107 SourceLocation IncludeLoc = FI.getIncludeLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Chris Lattner3cd949c2009-02-04 01:55:42 +00001109 // If we have #line directives in this file, update and overwrite the physical
1110 // location info if appropriate.
1111 if (FI.hasLineDirectives()) {
1112 assert(LineTable && "Can't have linetable entries without a LineTable!");
1113 // See if there is a #line directive before this. If so, get it.
1114 if (const LineEntry *Entry =
1115 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
Chris Lattnerfc391332009-02-04 02:00:59 +00001116 // If the LineEntry indicates a filename, use it.
Chris Lattner3cd949c2009-02-04 01:55:42 +00001117 if (Entry->FilenameID != -1)
1118 Filename = LineTable->getFilename(Entry->FilenameID);
Chris Lattnerfc391332009-02-04 02:00:59 +00001119
1120 // Use the line number specified by the LineEntry. This line number may
1121 // be multiple lines down from the line entry. Add the difference in
1122 // physical line numbers from the query point and the line marker to the
1123 // total.
1124 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
1125 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Chris Lattner0e0e5da2009-02-04 02:15:40 +00001127 // Note that column numbers are not molested by line markers.
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Chris Lattner137b6a62009-02-04 06:25:26 +00001129 // Handle virtual #include manipulation.
1130 if (Entry->IncludeOffset) {
1131 IncludeLoc = getLocForStartOfFile(LocInfo.first);
1132 IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
1133 }
Chris Lattner3cd949c2009-02-04 01:55:42 +00001134 }
1135 }
1136
1137 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001138}
1139
1140//===----------------------------------------------------------------------===//
1141// Other miscellaneous methods.
1142//===----------------------------------------------------------------------===//
1143
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001144/// \brief Retrieve the inode for the given file entry, if possible.
1145///
1146/// This routine involves a system call, and therefore should only be used
1147/// in non-performance-critical code.
1148static llvm::Optional<ino_t> getActualFileInode(const FileEntry *File) {
1149 if (!File)
1150 return llvm::Optional<ino_t>();
1151
1152 struct stat StatBuf;
1153 if (::stat(File->getName(), &StatBuf))
1154 return llvm::Optional<ino_t>();
1155
1156 return StatBuf.st_ino;
1157}
1158
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +00001159/// \brief Get the source location for the given file:line:col triplet.
1160///
1161/// If the source file is included multiple times, the source location will
1162/// be based upon the first inclusion.
1163SourceLocation SourceManager::getLocation(const FileEntry *SourceFile,
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001164 unsigned Line, unsigned Col) {
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +00001165 assert(SourceFile && "Null source file!");
1166 assert(Line && Col && "Line and column should start from 1!");
1167
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001168 // Find the first file ID that corresponds to the given file.
1169 FileID FirstFID;
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001171 // First, check the main file ID, since it is common to look for a
1172 // location in the main file.
1173 llvm::Optional<ino_t> SourceFileInode;
1174 llvm::Optional<llvm::StringRef> SourceFileName;
1175 if (!MainFileID.isInvalid()) {
1176 const SLocEntry &MainSLoc = getSLocEntry(MainFileID);
1177 if (MainSLoc.isFile()) {
1178 const ContentCache *MainContentCache
1179 = MainSLoc.getFile().getContentCache();
Douglas Gregorb7a18412011-02-11 18:08:15 +00001180 if (!MainContentCache) {
1181 // Can't do anything
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001182 } else if (MainContentCache->OrigEntry == SourceFile) {
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001183 FirstFID = MainFileID;
Douglas Gregorb7a18412011-02-11 18:08:15 +00001184 } else {
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001185 // Fall back: check whether we have the same base name and inode
1186 // as the main file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001187 const FileEntry *MainFile = MainContentCache->OrigEntry;
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001188 SourceFileName = llvm::sys::path::filename(SourceFile->getName());
1189 if (*SourceFileName == llvm::sys::path::filename(MainFile->getName())) {
1190 SourceFileInode = getActualFileInode(SourceFile);
Douglas Gregor37c02bf2011-02-16 19:09:24 +00001191 if (SourceFileInode) {
1192 if (llvm::Optional<ino_t> MainFileInode
1193 = getActualFileInode(MainFile)) {
1194 if (*SourceFileInode == *MainFileInode) {
1195 FirstFID = MainFileID;
1196 SourceFile = MainFile;
1197 }
1198 }
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001199 }
1200 }
1201 }
1202 }
1203 }
1204
1205 if (FirstFID.isInvalid()) {
1206 // The location we're looking for isn't in the main file; look
1207 // through all of the source locations.
1208 for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) {
1209 const SLocEntry &SLoc = getSLocEntry(I);
1210 if (SLoc.isFile() &&
1211 SLoc.getFile().getContentCache() &&
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001212 SLoc.getFile().getContentCache()->OrigEntry == SourceFile) {
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001213 FirstFID = FileID::get(I);
1214 break;
1215 }
1216 }
1217 }
1218
1219 // If we haven't found what we want yet, try again, but this time stat()
1220 // each of the files in case the files have changed since we originally
1221 // parsed the file.
1222 if (FirstFID.isInvalid() &&
1223 (SourceFileName ||
1224 (SourceFileName = llvm::sys::path::filename(SourceFile->getName()))) &&
1225 (SourceFileInode ||
1226 (SourceFileInode = getActualFileInode(SourceFile)))) {
1227 for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) {
1228 const SLocEntry &SLoc = getSLocEntry(I);
1229 if (SLoc.isFile()) {
1230 const ContentCache *FileContentCache
1231 = SLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001232 const FileEntry *Entry =FileContentCache? FileContentCache->OrigEntry : 0;
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001233 if (Entry &&
Douglas Gregorb7a18412011-02-11 18:08:15 +00001234 *SourceFileName == llvm::sys::path::filename(Entry->getName())) {
1235 if (llvm::Optional<ino_t> EntryInode = getActualFileInode(Entry)) {
1236 if (*SourceFileInode == *EntryInode) {
1237 FirstFID = FileID::get(I);
1238 SourceFile = Entry;
1239 break;
1240 }
1241 }
Douglas Gregor86a4d0d2011-02-03 17:17:35 +00001242 }
1243 }
1244 }
1245 }
1246
1247 if (FirstFID.isInvalid())
1248 return SourceLocation();
1249
1250 if (Line == 1 && Col == 1)
1251 return getLocForStartOfFile(FirstFID);
1252
1253 ContentCache *Content
1254 = const_cast<ContentCache *>(getOrCreateContentCache(SourceFile));
1255 if (!Content)
1256 return SourceLocation();
1257
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +00001258 // If this is the first use of line information for this buffer, compute the
1259 /// SourceLineCache for it on demand.
Douglas Gregor50f6af72010-03-16 05:20:39 +00001260 if (Content->SourceLineCache == 0) {
1261 bool MyInvalid = false;
Chris Lattnere127a0d2010-04-20 20:35:58 +00001262 ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
Douglas Gregor50f6af72010-03-16 05:20:39 +00001263 if (MyInvalid)
1264 return SourceLocation();
1265 }
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +00001266
Douglas Gregord1eabfb2010-02-27 02:42:25 +00001267 if (Line > Content->NumLines) {
Chris Lattnere127a0d2010-04-20 20:35:58 +00001268 unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize();
Douglas Gregord1eabfb2010-02-27 02:42:25 +00001269 if (Size > 0)
1270 --Size;
1271 return getLocForStartOfFile(FirstFID).getFileLocWithOffset(Size);
1272 }
1273
1274 unsigned FilePos = Content->SourceLineCache[Line - 1];
Chris Lattnere127a0d2010-04-20 20:35:58 +00001275 const char *Buf = Content->getBuffer(Diag, *this)->getBufferStart() + FilePos;
1276 unsigned BufLength = Content->getBuffer(Diag, *this)->getBufferEnd() - Buf;
Douglas Gregord1eabfb2010-02-27 02:42:25 +00001277 unsigned i = 0;
1278
1279 // Check that the given column is valid.
1280 while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
1281 ++i;
1282 if (i < Col-1)
1283 return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + i);
1284
Douglas Gregor4a160e12009-12-02 05:34:39 +00001285 return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + Col - 1);
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +00001286}
1287
Chris Lattnerd3b8cc22010-05-07 20:35:24 +00001288/// Given a decomposed source location, move it up the include/instantiation
1289/// stack to the parent source location. If this is possible, return the
1290/// decomposed version of the parent in Loc and return false. If Loc is the
1291/// top-level entry, return true and don't modify it.
1292static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc,
1293 const SourceManager &SM) {
1294 SourceLocation UpperLoc;
1295 const SrcMgr::SLocEntry &Entry = SM.getSLocEntry(Loc.first);
1296 if (Entry.isInstantiation())
1297 UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
1298 else
1299 UpperLoc = Entry.getFile().getIncludeLoc();
1300
1301 if (UpperLoc.isInvalid())
1302 return true; // We reached the top.
1303
1304 Loc = SM.getDecomposedLoc(UpperLoc);
1305 return false;
1306}
1307
1308
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001309/// \brief Determines the order of 2 source locations in the translation unit.
1310///
1311/// \returns true if LHS source location comes before RHS, false otherwise.
1312bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
1313 SourceLocation RHS) const {
1314 assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
1315 if (LHS == RHS)
1316 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Argyrios Kyrtzidisee933e12010-12-24 02:53:53 +00001318 // If both locations are macro instantiations, the order of their offsets
1319 // reflect the order that the tokens, pointed to by these locations, were
1320 // instantiated (during parsing each token that is instantiated by a macro,
1321 // expands the SLocEntries).
1322 if (LHS.isMacroID() && RHS.isMacroID())
1323 return LHS.getOffset() < RHS.getOffset();
1324
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001325 std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
1326 std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001328 // If the source locations are in the same file, just compare offsets.
1329 if (LOffs.first == ROffs.first)
1330 return LOffs.second < ROffs.second;
1331
1332 // If we are comparing a source location with multiple locations in the same
1333 // file, we get a big win by caching the result.
Chris Lattner66a915f2010-05-07 05:10:46 +00001334 if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first))
1335 return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second);
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Chris Lattnerdcb1d682010-05-07 01:17:07 +00001337 // Okay, we missed in the cache, start updating the cache for this query.
1338 IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001340 // "Traverse" the include/instantiation stacks of both locations and try to
Chris Lattner48296ba2010-05-07 05:51:13 +00001341 // find a common "ancestor". FileIDs build a tree-like structure that
1342 // reflects the #include hierarchy, and this algorithm needs to find the
1343 // nearest common ancestor between the two locations. For example, if you
1344 // have a.c that includes b.h and c.h, and are comparing a location in b.h to
1345 // a location in c.h, we need to find that their nearest common ancestor is
1346 // a.c, and compare the locations of the two #includes to find their relative
1347 // ordering.
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001348 //
Chris Lattner48296ba2010-05-07 05:51:13 +00001349 // SourceManager assigns FileIDs in order of parsing. This means that an
1350 // includee always has a larger FileID than an includer. While you might
1351 // think that we could just compare the FileID's here, that doesn't work to
1352 // compare a point at the end of a.c with a point within c.h. Though c.h has
1353 // a larger FileID, we have to compare the include point of c.h to the
1354 // location in a.c.
1355 //
1356 // Despite not being able to directly compare FileID's, we can tell that a
1357 // larger FileID is necessarily more deeply nested than a lower one and use
1358 // this information to walk up the tree to the nearest common ancestor.
1359 do {
1360 // If LOffs is larger than ROffs, then LOffs must be more deeply nested than
1361 // ROffs, walk up the #include chain.
1362 if (LOffs.first.ID > ROffs.first.ID) {
Chris Lattnerd3b8cc22010-05-07 20:35:24 +00001363 if (MoveUpIncludeHierarchy(LOffs, *this))
Chris Lattner48296ba2010-05-07 05:51:13 +00001364 break; // We reached the top.
1365
Chris Lattner48296ba2010-05-07 05:51:13 +00001366 } else {
1367 // Otherwise, ROffs is larger than LOffs, so ROffs must be more deeply
1368 // nested than LOffs, walk up the #include chain.
Chris Lattnerd3b8cc22010-05-07 20:35:24 +00001369 if (MoveUpIncludeHierarchy(ROffs, *this))
Chris Lattner48296ba2010-05-07 05:51:13 +00001370 break; // We reached the top.
Chris Lattnerdcb1d682010-05-07 01:17:07 +00001371 }
Chris Lattner48296ba2010-05-07 05:51:13 +00001372 } while (LOffs.first != ROffs.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Chris Lattner48296ba2010-05-07 05:51:13 +00001374 // If we exited because we found a nearest common ancestor, compare the
1375 // locations within the common file and cache them.
1376 if (LOffs.first == ROffs.first) {
1377 IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second);
1378 return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second);
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001379 }
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Daniel Dunbarfbcc7be2009-12-01 23:07:57 +00001381 // There is no common ancestor, most probably because one location is in the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001382 // predefines buffer or an AST file.
Daniel Dunbarfbcc7be2009-12-01 23:07:57 +00001383 // FIXME: We should rearrange the external interface so this simply never
1384 // happens; it can't conceptually happen. Also see PR5662.
Chris Lattnerd3b8cc22010-05-07 20:35:24 +00001385 IsBeforeInTUCache.setQueryFIDs(FileID(), FileID()); // Don't try caching.
1386
1387 // Zip both entries up to the top level record.
1388 while (!MoveUpIncludeHierarchy(LOffs, *this)) /*empty*/;
1389 while (!MoveUpIncludeHierarchy(ROffs, *this)) /*empty*/;
Chris Lattner48296ba2010-05-07 05:51:13 +00001390
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001391 // If exactly one location is a memory buffer, assume it precedes the other.
Chris Lattnerd3b8cc22010-05-07 20:35:24 +00001392
1393 // Strip off macro instantation locations, going up to the top-level File
1394 // SLocEntry.
1395 bool LIsMB = getFileEntryForID(LOffs.first) == 0;
1396 bool RIsMB = getFileEntryForID(ROffs.first) == 0;
Chris Lattner48296ba2010-05-07 05:51:13 +00001397 if (LIsMB != RIsMB)
Chris Lattnerdcb1d682010-05-07 01:17:07 +00001398 return LIsMB;
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Daniel Dunbarfbcc7be2009-12-01 23:07:57 +00001400 // Otherwise, just assume FileIDs were created in order.
Chris Lattnerdcb1d682010-05-07 01:17:07 +00001401 return LOffs.first < ROffs.first;
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001402}
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001403
Reid Spencer5f016e22007-07-11 17:01:13 +00001404/// PrintStats - Print statistics to stderr.
1405///
1406void SourceManager::PrintStats() const {
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +00001407 llvm::errs() << "\n*** Source Manager Stats:\n";
1408 llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
1409 << " mem buffers mapped.\n";
1410 llvm::errs() << SLocEntryTable.size() << " SLocEntry's allocated, "
1411 << NextOffset << "B of Sloc address space used.\n";
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 unsigned NumLineNumsComputed = 0;
1414 unsigned NumFileBytesMapped = 0;
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001415 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
1416 NumLineNumsComputed += I->second->SourceLineCache != 0;
1417 NumFileBytesMapped += I->second->getSizeBytesMapped();
Reid Spencer5f016e22007-07-11 17:01:13 +00001418 }
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +00001420 llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
1421 << NumLineNumsComputed << " files with line #'s computed.\n";
1422 llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
1423 << NumBinaryProbes << " binary.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +00001424}
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001425
1426ExternalSLocEntrySource::~ExternalSLocEntrySource() { }