1 //===- SourceManager.cpp - Track and cache source files -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the SourceManager interface.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/LLVM.h"
17 #include "clang/Basic/SourceLocation.h"
18 #include "clang/Basic/SourceManagerInternals.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/None.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Support/Allocator.h"
27 #include "llvm/Support/Capacity.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/Path.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <algorithm>
36 #include <cassert>
37 #include <cstddef>
38 #include <cstdint>
39 #include <memory>
40 #include <tuple>
41 #include <utility>
42 #include <vector>
43
44 using namespace clang;
45 using namespace SrcMgr;
46 using llvm::MemoryBuffer;
47
48 //===----------------------------------------------------------------------===//
49 // SourceManager Helper Classes
50 //===----------------------------------------------------------------------===//
51
~ContentCache()52 ContentCache::~ContentCache() {
53 if (shouldFreeBuffer())
54 delete Buffer.getPointer();
55 }
56
57 /// getSizeBytesMapped - Returns the number of bytes actually mapped for this
58 /// ContentCache. This can be 0 if the MemBuffer was not actually expanded.
getSizeBytesMapped() const59 unsigned ContentCache::getSizeBytesMapped() const {
60 return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0;
61 }
62
63 /// Returns the kind of memory used to back the memory buffer for
64 /// this content cache. This is used for performance analysis.
getMemoryBufferKind() const65 llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind() const {
66 assert(Buffer.getPointer());
67
68 // Should be unreachable, but keep for sanity.
69 if (!Buffer.getPointer())
70 return llvm::MemoryBuffer::MemoryBuffer_Malloc;
71
72 const llvm::MemoryBuffer *buf = Buffer.getPointer();
73 return buf->getBufferKind();
74 }
75
76 /// getSize - Returns the size of the content encapsulated by this ContentCache.
77 /// This can be the size of the source file or the size of an arbitrary
78 /// scratch buffer. If the ContentCache encapsulates a source file, that
79 /// file is not lazily brought in from disk to satisfy this query.
getSize() const80 unsigned ContentCache::getSize() const {
81 return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize()
82 : (unsigned) ContentsEntry->getSize();
83 }
84
replaceBuffer(const llvm::MemoryBuffer * B,bool DoNotFree)85 void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree) {
86 if (B && B == Buffer.getPointer()) {
87 assert(0 && "Replacing with the same buffer");
88 Buffer.setInt(DoNotFree? DoNotFreeFlag : 0);
89 return;
90 }
91
92 if (shouldFreeBuffer())
93 delete Buffer.getPointer();
94 Buffer.setPointer(B);
95 Buffer.setInt((B && DoNotFree) ? DoNotFreeFlag : 0);
96 }
97
getBuffer(DiagnosticsEngine & Diag,const SourceManager & SM,SourceLocation Loc,bool * Invalid) const98 const llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag,
99 const SourceManager &SM,
100 SourceLocation Loc,
101 bool *Invalid) const {
102 // Lazily create the Buffer for ContentCaches that wrap files. If we already
103 // computed it, just return what we have.
104 if (Buffer.getPointer() || !ContentsEntry) {
105 if (Invalid)
106 *Invalid = isBufferInvalid();
107
108 return Buffer.getPointer();
109 }
110
111 // Check that the file's size fits in an 'unsigned' (with room for a
112 // past-the-end value). This is deeply regrettable, but various parts of
113 // Clang (including elsewhere in this file!) use 'unsigned' to represent file
114 // offsets, line numbers, string literal lengths, and so on, and fail
115 // miserably on large source files.
116 if ((uint64_t)ContentsEntry->getSize() >=
117 std::numeric_limits<unsigned>::max()) {
118 // We can't make a memory buffer of the required size, so just make a small
119 // one. We should never hit a situation where we've already parsed to a
120 // later offset of the file, so it shouldn't matter that the buffer is
121 // smaller than the file.
122 Buffer.setPointer(
123 llvm::MemoryBuffer::getMemBuffer("", ContentsEntry->getName())
124 .release());
125 if (Diag.isDiagnosticInFlight())
126 Diag.SetDelayedDiagnostic(diag::err_file_too_large,
127 ContentsEntry->getName());
128 else
129 Diag.Report(Loc, diag::err_file_too_large)
130 << ContentsEntry->getName();
131
132 Buffer.setInt(Buffer.getInt() | InvalidFlag);
133 if (Invalid) *Invalid = true;
134 return Buffer.getPointer();
135 }
136
137 bool isVolatile = SM.userFilesAreVolatile() && !IsSystemFile;
138 auto BufferOrError =
139 SM.getFileManager().getBufferForFile(ContentsEntry, isVolatile);
140
141 // If we were unable to open the file, then we are in an inconsistent
142 // situation where the content cache referenced a file which no longer
143 // exists. Most likely, we were using a stat cache with an invalid entry but
144 // the file could also have been removed during processing. Since we can't
145 // really deal with this situation, just create an empty buffer.
146 //
147 // FIXME: This is definitely not ideal, but our immediate clients can't
148 // currently handle returning a null entry here. Ideally we should detect
149 // that we are in an inconsistent situation and error out as quickly as
150 // possible.
151 if (!BufferOrError) {
152 StringRef FillStr("<<<MISSING SOURCE FILE>>>\n");
153 auto BackupBuffer = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
154 ContentsEntry->getSize(), "<invalid>");
155 char *Ptr = BackupBuffer->getBufferStart();
156 for (unsigned i = 0, e = ContentsEntry->getSize(); i != e; ++i)
157 Ptr[i] = FillStr[i % FillStr.size()];
158 Buffer.setPointer(BackupBuffer.release());
159
160 if (Diag.isDiagnosticInFlight())
161 Diag.SetDelayedDiagnostic(diag::err_cannot_open_file,
162 ContentsEntry->getName(),
163 BufferOrError.getError().message());
164 else
165 Diag.Report(Loc, diag::err_cannot_open_file)
166 << ContentsEntry->getName() << BufferOrError.getError().message();
167
168 Buffer.setInt(Buffer.getInt() | InvalidFlag);
169
170 if (Invalid) *Invalid = true;
171 return Buffer.getPointer();
172 }
173
174 Buffer.setPointer(BufferOrError->release());
175
176 // Check that the file's size is the same as in the file entry (which may
177 // have come from a stat cache).
178 if (getRawBuffer()->getBufferSize() != (size_t)ContentsEntry->getSize()) {
179 if (Diag.isDiagnosticInFlight())
180 Diag.SetDelayedDiagnostic(diag::err_file_modified,
181 ContentsEntry->getName());
182 else
183 Diag.Report(Loc, diag::err_file_modified)
184 << ContentsEntry->getName();
185
186 Buffer.setInt(Buffer.getInt() | InvalidFlag);
187 if (Invalid) *Invalid = true;
188 return Buffer.getPointer();
189 }
190
191 // If the buffer is valid, check to see if it has a UTF Byte Order Mark
192 // (BOM). We only support UTF-8 with and without a BOM right now. See
193 // http://en.wikipedia.org/wiki/Byte_order_mark for more information.
194 StringRef BufStr = Buffer.getPointer()->getBuffer();
195 const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr)
196 .StartsWith(llvm::StringLiteral::withInnerNUL("\x00\x00\xFE\xFF"),
197 "UTF-32 (BE)")
198 .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"),
199 "UTF-32 (LE)")
200 .StartsWith("\xFE\xFF", "UTF-16 (BE)")
201 .StartsWith("\xFF\xFE", "UTF-16 (LE)")
202 .StartsWith("\x2B\x2F\x76", "UTF-7")
203 .StartsWith("\xF7\x64\x4C", "UTF-1")
204 .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC")
205 .StartsWith("\x0E\xFE\xFF", "SCSU")
206 .StartsWith("\xFB\xEE\x28", "BOCU-1")
207 .StartsWith("\x84\x31\x95\x33", "GB-18030")
208 .Default(nullptr);
209
210 if (InvalidBOM) {
211 Diag.Report(Loc, diag::err_unsupported_bom)
212 << InvalidBOM << ContentsEntry->getName();
213 Buffer.setInt(Buffer.getInt() | InvalidFlag);
214 }
215
216 if (Invalid)
217 *Invalid = isBufferInvalid();
218
219 return Buffer.getPointer();
220 }
221
getLineTableFilenameID(StringRef Name)222 unsigned LineTableInfo::getLineTableFilenameID(StringRef Name) {
223 auto IterBool = FilenameIDs.try_emplace(Name, FilenamesByID.size());
224 if (IterBool.second)
225 FilenamesByID.push_back(&*IterBool.first);
226 return IterBool.first->second;
227 }
228
229 /// Add a line note to the line table that indicates that there is a \#line or
230 /// GNU line marker at the specified FID/Offset location which changes the
231 /// presumed location to LineNo/FilenameID. If EntryExit is 0, then this doesn't
232 /// change the presumed \#include stack. If it is 1, this is a file entry, if
233 /// it is 2 then this is a file exit. FileKind specifies whether this is a
234 /// system header or extern C system header.
AddLineNote(FileID FID,unsigned Offset,unsigned LineNo,int FilenameID,unsigned EntryExit,SrcMgr::CharacteristicKind FileKind)235 void LineTableInfo::AddLineNote(FileID FID, unsigned Offset, unsigned LineNo,
236 int FilenameID, unsigned EntryExit,
237 SrcMgr::CharacteristicKind FileKind) {
238 std::vector<LineEntry> &Entries = LineEntries[FID];
239
240 // An unspecified FilenameID means use the last filename if available, or the
241 // main source file otherwise.
242 if (FilenameID == -1 && !Entries.empty())
243 FilenameID = Entries.back().FilenameID;
244
245 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
246 "Adding line entries out of order!");
247
248 unsigned IncludeOffset = 0;
249 if (EntryExit == 0) { // No #include stack change.
250 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
251 } else if (EntryExit == 1) {
252 IncludeOffset = Offset-1;
253 } else if (EntryExit == 2) {
254 assert(!Entries.empty() && Entries.back().IncludeOffset &&
255 "PPDirectives should have caught case when popping empty include stack");
256
257 // Get the include loc of the last entries' include loc as our include loc.
258 IncludeOffset = 0;
259 if (const LineEntry *PrevEntry =
260 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
261 IncludeOffset = PrevEntry->IncludeOffset;
262 }
263
264 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
265 IncludeOffset));
266 }
267
268 /// FindNearestLineEntry - Find the line entry nearest to FID that is before
269 /// it. If there is no line entry before Offset in FID, return null.
FindNearestLineEntry(FileID FID,unsigned Offset)270 const LineEntry *LineTableInfo::FindNearestLineEntry(FileID FID,
271 unsigned Offset) {
272 const std::vector<LineEntry> &Entries = LineEntries[FID];
273 assert(!Entries.empty() && "No #line entries for this FID after all!");
274
275 // It is very common for the query to be after the last #line, check this
276 // first.
277 if (Entries.back().FileOffset <= Offset)
278 return &Entries.back();
279
280 // Do a binary search to find the maximal element that is still before Offset.
281 std::vector<LineEntry>::const_iterator I = llvm::upper_bound(Entries, Offset);
282 if (I == Entries.begin())
283 return nullptr;
284 return &*--I;
285 }
286
287 /// Add a new line entry that has already been encoded into
288 /// the internal representation of the line table.
AddEntry(FileID FID,const std::vector<LineEntry> & Entries)289 void LineTableInfo::AddEntry(FileID FID,
290 const std::vector<LineEntry> &Entries) {
291 LineEntries[FID] = Entries;
292 }
293
294 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
getLineTableFilenameID(StringRef Name)295 unsigned SourceManager::getLineTableFilenameID(StringRef Name) {
296 return getLineTable().getLineTableFilenameID(Name);
297 }
298
299 /// AddLineNote - Add a line note to the line table for the FileID and offset
300 /// specified by Loc. If FilenameID is -1, it is considered to be
301 /// unspecified.
AddLineNote(SourceLocation Loc,unsigned LineNo,int FilenameID,bool IsFileEntry,bool IsFileExit,SrcMgr::CharacteristicKind FileKind)302 void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
303 int FilenameID, bool IsFileEntry,
304 bool IsFileExit,
305 SrcMgr::CharacteristicKind FileKind) {
306 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
307
308 bool Invalid = false;
309 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
310 if (!Entry.isFile() || Invalid)
311 return;
312
313 const SrcMgr::FileInfo &FileInfo = Entry.getFile();
314
315 // Remember that this file has #line directives now if it doesn't already.
316 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
317
318 (void) getLineTable();
319
320 unsigned EntryExit = 0;
321 if (IsFileEntry)
322 EntryExit = 1;
323 else if (IsFileExit)
324 EntryExit = 2;
325
326 LineTable->AddLineNote(LocInfo.first, LocInfo.second, LineNo, FilenameID,
327 EntryExit, FileKind);
328 }
329
getLineTable()330 LineTableInfo &SourceManager::getLineTable() {
331 if (!LineTable)
332 LineTable.reset(new LineTableInfo());
333 return *LineTable;
334 }
335
336 //===----------------------------------------------------------------------===//
337 // Private 'Create' methods.
338 //===----------------------------------------------------------------------===//
339
SourceManager(DiagnosticsEngine & Diag,FileManager & FileMgr,bool UserFilesAreVolatile)340 SourceManager::SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
341 bool UserFilesAreVolatile)
342 : Diag(Diag), FileMgr(FileMgr), UserFilesAreVolatile(UserFilesAreVolatile) {
343 clearIDTables();
344 Diag.setSourceManager(this);
345 }
346
~SourceManager()347 SourceManager::~SourceManager() {
348 // Delete FileEntry objects corresponding to content caches. Since the actual
349 // content cache objects are bump pointer allocated, we just have to run the
350 // dtors, but we call the deallocate method for completeness.
351 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
352 if (MemBufferInfos[i]) {
353 MemBufferInfos[i]->~ContentCache();
354 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
355 }
356 }
357 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
358 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
359 if (I->second) {
360 I->second->~ContentCache();
361 ContentCacheAlloc.Deallocate(I->second);
362 }
363 }
364 }
365
clearIDTables()366 void SourceManager::clearIDTables() {
367 MainFileID = FileID();
368 LocalSLocEntryTable.clear();
369 LoadedSLocEntryTable.clear();
370 SLocEntryLoaded.clear();
371 LastLineNoFileIDQuery = FileID();
372 LastLineNoContentCache = nullptr;
373 LastFileIDLookup = FileID();
374
375 if (LineTable)
376 LineTable->clear();
377
378 // Use up FileID #0 as an invalid expansion.
379 NextLocalOffset = 0;
380 CurrentLoadedOffset = MaxLoadedOffset;
381 createExpansionLoc(SourceLocation(), SourceLocation(), SourceLocation(), 1);
382 }
383
initializeForReplay(const SourceManager & Old)384 void SourceManager::initializeForReplay(const SourceManager &Old) {
385 assert(MainFileID.isInvalid() && "expected uninitialized SourceManager");
386
387 auto CloneContentCache = [&](const ContentCache *Cache) -> ContentCache * {
388 auto *Clone = new (ContentCacheAlloc.Allocate<ContentCache>()) ContentCache;
389 Clone->OrigEntry = Cache->OrigEntry;
390 Clone->ContentsEntry = Cache->ContentsEntry;
391 Clone->BufferOverridden = Cache->BufferOverridden;
392 Clone->IsSystemFile = Cache->IsSystemFile;
393 Clone->IsTransient = Cache->IsTransient;
394 Clone->replaceBuffer(Cache->getRawBuffer(), /*DoNotFree*/true);
395 return Clone;
396 };
397
398 // Ensure all SLocEntries are loaded from the external source.
399 for (unsigned I = 0, N = Old.LoadedSLocEntryTable.size(); I != N; ++I)
400 if (!Old.SLocEntryLoaded[I])
401 Old.loadSLocEntry(I, nullptr);
402
403 // Inherit any content cache data from the old source manager.
404 for (auto &FileInfo : Old.FileInfos) {
405 SrcMgr::ContentCache *&Slot = FileInfos[FileInfo.first];
406 if (Slot)
407 continue;
408 Slot = CloneContentCache(FileInfo.second);
409 }
410 }
411
412 /// getOrCreateContentCache - Create or return a cached ContentCache for the
413 /// specified file.
414 const ContentCache *
getOrCreateContentCache(const FileEntry * FileEnt,bool isSystemFile)415 SourceManager::getOrCreateContentCache(const FileEntry *FileEnt,
416 bool isSystemFile) {
417 assert(FileEnt && "Didn't specify a file entry to use?");
418
419 // Do we already have information about this file?
420 ContentCache *&Entry = FileInfos[FileEnt];
421 if (Entry) return Entry;
422
423 // Nope, create a new Cache entry.
424 Entry = ContentCacheAlloc.Allocate<ContentCache>();
425
426 if (OverriddenFilesInfo) {
427 // If the file contents are overridden with contents from another file,
428 // pass that file to ContentCache.
429 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
430 overI = OverriddenFilesInfo->OverriddenFiles.find(FileEnt);
431 if (overI == OverriddenFilesInfo->OverriddenFiles.end())
432 new (Entry) ContentCache(FileEnt);
433 else
434 new (Entry) ContentCache(OverridenFilesKeepOriginalName ? FileEnt
435 : overI->second,
436 overI->second);
437 } else {
438 new (Entry) ContentCache(FileEnt);
439 }
440
441 Entry->IsSystemFile = isSystemFile;
442 Entry->IsTransient = FilesAreTransient;
443
444 return Entry;
445 }
446
447 /// Create a new ContentCache for the specified memory buffer.
448 /// This does no caching.
449 const ContentCache *
createMemBufferContentCache(const llvm::MemoryBuffer * Buffer,bool DoNotFree)450 SourceManager::createMemBufferContentCache(const llvm::MemoryBuffer *Buffer,
451 bool DoNotFree) {
452 // Add a new ContentCache to the MemBufferInfos list and return it.
453 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>();
454 new (Entry) ContentCache();
455 MemBufferInfos.push_back(Entry);
456 Entry->replaceBuffer(Buffer, DoNotFree);
457 return Entry;
458 }
459
loadSLocEntry(unsigned Index,bool * Invalid) const460 const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index,
461 bool *Invalid) const {
462 assert(!SLocEntryLoaded[Index]);
463 if (ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2))) {
464 if (Invalid)
465 *Invalid = true;
466 // If the file of the SLocEntry changed we could still have loaded it.
467 if (!SLocEntryLoaded[Index]) {
468 // Try to recover; create a SLocEntry so the rest of clang can handle it.
469 LoadedSLocEntryTable[Index] = SLocEntry::get(0,
470 FileInfo::get(SourceLocation(),
471 getFakeContentCacheForRecovery(),
472 SrcMgr::C_User));
473 }
474 }
475
476 return LoadedSLocEntryTable[Index];
477 }
478
479 std::pair<int, unsigned>
AllocateLoadedSLocEntries(unsigned NumSLocEntries,unsigned TotalSize)480 SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries,
481 unsigned TotalSize) {
482 assert(ExternalSLocEntries && "Don't have an external sloc source");
483 // Make sure we're not about to run out of source locations.
484 if (CurrentLoadedOffset - TotalSize < NextLocalOffset)
485 return std::make_pair(0, 0);
486 LoadedSLocEntryTable.resize(LoadedSLocEntryTable.size() + NumSLocEntries);
487 SLocEntryLoaded.resize(LoadedSLocEntryTable.size());
488 CurrentLoadedOffset -= TotalSize;
489 int ID = LoadedSLocEntryTable.size();
490 return std::make_pair(-ID - 1, CurrentLoadedOffset);
491 }
492
493 /// As part of recovering from missing or changed content, produce a
494 /// fake, non-empty buffer.
getFakeBufferForRecovery() const495 llvm::MemoryBuffer *SourceManager::getFakeBufferForRecovery() const {
496 if (!FakeBufferForRecovery)
497 FakeBufferForRecovery =
498 llvm::MemoryBuffer::getMemBuffer("<<<INVALID BUFFER>>");
499
500 return FakeBufferForRecovery.get();
501 }
502
503 /// As part of recovering from missing or changed content, produce a
504 /// fake content cache.
505 const SrcMgr::ContentCache *
getFakeContentCacheForRecovery() const506 SourceManager::getFakeContentCacheForRecovery() const {
507 if (!FakeContentCacheForRecovery) {
508 FakeContentCacheForRecovery = llvm::make_unique<SrcMgr::ContentCache>();
509 FakeContentCacheForRecovery->replaceBuffer(getFakeBufferForRecovery(),
510 /*DoNotFree=*/true);
511 }
512 return FakeContentCacheForRecovery.get();
513 }
514
515 /// Returns the previous in-order FileID or an invalid FileID if there
516 /// is no previous one.
getPreviousFileID(FileID FID) const517 FileID SourceManager::getPreviousFileID(FileID FID) const {
518 if (FID.isInvalid())
519 return FileID();
520
521 int ID = FID.ID;
522 if (ID == -1)
523 return FileID();
524
525 if (ID > 0) {
526 if (ID-1 == 0)
527 return FileID();
528 } else if (unsigned(-(ID-1) - 2) >= LoadedSLocEntryTable.size()) {
529 return FileID();
530 }
531
532 return FileID::get(ID-1);
533 }
534
535 /// Returns the next in-order FileID or an invalid FileID if there is
536 /// no next one.
getNextFileID(FileID FID) const537 FileID SourceManager::getNextFileID(FileID FID) const {
538 if (FID.isInvalid())
539 return FileID();
540
541 int ID = FID.ID;
542 if (ID > 0) {
543 if (unsigned(ID+1) >= local_sloc_entry_size())
544 return FileID();
545 } else if (ID+1 >= -1) {
546 return FileID();
547 }
548
549 return FileID::get(ID+1);
550 }
551
552 //===----------------------------------------------------------------------===//
553 // Methods to create new FileID's and macro expansions.
554 //===----------------------------------------------------------------------===//
555
556 /// createFileID - Create a new FileID for the specified ContentCache and
557 /// include position. This works regardless of whether the ContentCache
558 /// corresponds to a file or some other input source.
createFileID(const ContentCache * File,SourceLocation IncludePos,SrcMgr::CharacteristicKind FileCharacter,int LoadedID,unsigned LoadedOffset)559 FileID SourceManager::createFileID(const ContentCache *File,
560 SourceLocation IncludePos,
561 SrcMgr::CharacteristicKind FileCharacter,
562 int LoadedID, unsigned LoadedOffset) {
563 if (LoadedID < 0) {
564 assert(LoadedID != -1 && "Loading sentinel FileID");
565 unsigned Index = unsigned(-LoadedID) - 2;
566 assert(Index < LoadedSLocEntryTable.size() && "FileID out of range");
567 assert(!SLocEntryLoaded[Index] && "FileID already loaded");
568 LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset,
569 FileInfo::get(IncludePos, File, FileCharacter));
570 SLocEntryLoaded[Index] = true;
571 return FileID::get(LoadedID);
572 }
573 LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset,
574 FileInfo::get(IncludePos, File,
575 FileCharacter)));
576 unsigned FileSize = File->getSize();
577 assert(NextLocalOffset + FileSize + 1 > NextLocalOffset &&
578 NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset &&
579 "Ran out of source locations!");
580 // We do a +1 here because we want a SourceLocation that means "the end of the
581 // file", e.g. for the "no newline at the end of the file" diagnostic.
582 NextLocalOffset += FileSize + 1;
583
584 // Set LastFileIDLookup to the newly created file. The next getFileID call is
585 // almost guaranteed to be from that file.
586 FileID FID = FileID::get(LocalSLocEntryTable.size()-1);
587 return LastFileIDLookup = FID;
588 }
589
590 SourceLocation
createMacroArgExpansionLoc(SourceLocation SpellingLoc,SourceLocation ExpansionLoc,unsigned TokLength)591 SourceManager::createMacroArgExpansionLoc(SourceLocation SpellingLoc,
592 SourceLocation ExpansionLoc,
593 unsigned TokLength) {
594 ExpansionInfo Info = ExpansionInfo::createForMacroArg(SpellingLoc,
595 ExpansionLoc);
596 return createExpansionLocImpl(Info, TokLength);
597 }
598
599 SourceLocation
createExpansionLoc(SourceLocation SpellingLoc,SourceLocation ExpansionLocStart,SourceLocation ExpansionLocEnd,unsigned TokLength,bool ExpansionIsTokenRange,int LoadedID,unsigned LoadedOffset)600 SourceManager::createExpansionLoc(SourceLocation SpellingLoc,
601 SourceLocation ExpansionLocStart,
602 SourceLocation ExpansionLocEnd,
603 unsigned TokLength,
604 bool ExpansionIsTokenRange,
605 int LoadedID,
606 unsigned LoadedOffset) {
607 ExpansionInfo Info = ExpansionInfo::create(
608 SpellingLoc, ExpansionLocStart, ExpansionLocEnd, ExpansionIsTokenRange);
609 return createExpansionLocImpl(Info, TokLength, LoadedID, LoadedOffset);
610 }
611
createTokenSplitLoc(SourceLocation Spelling,SourceLocation TokenStart,SourceLocation TokenEnd)612 SourceLocation SourceManager::createTokenSplitLoc(SourceLocation Spelling,
613 SourceLocation TokenStart,
614 SourceLocation TokenEnd) {
615 assert(getFileID(TokenStart) == getFileID(TokenEnd) &&
616 "token spans multiple files");
617 return createExpansionLocImpl(
618 ExpansionInfo::createForTokenSplit(Spelling, TokenStart, TokenEnd),
619 TokenEnd.getOffset() - TokenStart.getOffset());
620 }
621
622 SourceLocation
createExpansionLocImpl(const ExpansionInfo & Info,unsigned TokLength,int LoadedID,unsigned LoadedOffset)623 SourceManager::createExpansionLocImpl(const ExpansionInfo &Info,
624 unsigned TokLength,
625 int LoadedID,
626 unsigned LoadedOffset) {
627 if (LoadedID < 0) {
628 assert(LoadedID != -1 && "Loading sentinel FileID");
629 unsigned Index = unsigned(-LoadedID) - 2;
630 assert(Index < LoadedSLocEntryTable.size() && "FileID out of range");
631 assert(!SLocEntryLoaded[Index] && "FileID already loaded");
632 LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset, Info);
633 SLocEntryLoaded[Index] = true;
634 return SourceLocation::getMacroLoc(LoadedOffset);
635 }
636 LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, Info));
637 assert(NextLocalOffset + TokLength + 1 > NextLocalOffset &&
638 NextLocalOffset + TokLength + 1 <= CurrentLoadedOffset &&
639 "Ran out of source locations!");
640 // See createFileID for that +1.
641 NextLocalOffset += TokLength + 1;
642 return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1));
643 }
644
645 const llvm::MemoryBuffer *
getMemoryBufferForFile(const FileEntry * File,bool * Invalid)646 SourceManager::getMemoryBufferForFile(const FileEntry *File, bool *Invalid) {
647 const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
648 assert(IR && "getOrCreateContentCache() cannot return NULL");
649 return IR->getBuffer(Diag, *this, SourceLocation(), Invalid);
650 }
651
overrideFileContents(const FileEntry * SourceFile,llvm::MemoryBuffer * Buffer,bool DoNotFree)652 void SourceManager::overrideFileContents(const FileEntry *SourceFile,
653 llvm::MemoryBuffer *Buffer,
654 bool DoNotFree) {
655 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
656 assert(IR && "getOrCreateContentCache() cannot return NULL");
657
658 const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree);
659 const_cast<SrcMgr::ContentCache *>(IR)->BufferOverridden = true;
660
661 getOverriddenFilesInfo().OverriddenFilesWithBuffer.insert(SourceFile);
662 }
663
overrideFileContents(const FileEntry * SourceFile,const FileEntry * NewFile)664 void SourceManager::overrideFileContents(const FileEntry *SourceFile,
665 const FileEntry *NewFile) {
666 assert(SourceFile->getSize() == NewFile->getSize() &&
667 "Different sizes, use the FileManager to create a virtual file with "
668 "the correct size");
669 assert(FileInfos.count(SourceFile) == 0 &&
670 "This function should be called at the initialization stage, before "
671 "any parsing occurs.");
672 getOverriddenFilesInfo().OverriddenFiles[SourceFile] = NewFile;
673 }
674
disableFileContentsOverride(const FileEntry * File)675 void SourceManager::disableFileContentsOverride(const FileEntry *File) {
676 if (!isFileOverridden(File))
677 return;
678
679 const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
680 const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(nullptr);
681 const_cast<SrcMgr::ContentCache *>(IR)->ContentsEntry = IR->OrigEntry;
682
683 assert(OverriddenFilesInfo);
684 OverriddenFilesInfo->OverriddenFiles.erase(File);
685 OverriddenFilesInfo->OverriddenFilesWithBuffer.erase(File);
686 }
687
setFileIsTransient(const FileEntry * File)688 void SourceManager::setFileIsTransient(const FileEntry *File) {
689 const SrcMgr::ContentCache *CC = getOrCreateContentCache(File);
690 const_cast<SrcMgr::ContentCache *>(CC)->IsTransient = true;
691 }
692
getBufferData(FileID FID,bool * Invalid) const693 StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
694 bool MyInvalid = false;
695 const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid);
696 if (!SLoc.isFile() || MyInvalid) {
697 if (Invalid)
698 *Invalid = true;
699 return "<<<<<INVALID SOURCE LOCATION>>>>>";
700 }
701
702 const llvm::MemoryBuffer *Buf = SLoc.getFile().getContentCache()->getBuffer(
703 Diag, *this, SourceLocation(), &MyInvalid);
704 if (Invalid)
705 *Invalid = MyInvalid;
706
707 if (MyInvalid)
708 return "<<<<<INVALID SOURCE LOCATION>>>>>";
709
710 return Buf->getBuffer();
711 }
712
713 //===----------------------------------------------------------------------===//
714 // SourceLocation manipulation methods.
715 //===----------------------------------------------------------------------===//
716
717 /// Return the FileID for a SourceLocation.
718 ///
719 /// This is the cache-miss path of getFileID. Not as hot as that function, but
720 /// still very important. It is responsible for finding the entry in the
721 /// SLocEntry tables that contains the specified location.
getFileIDSlow(unsigned SLocOffset) const722 FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
723 if (!SLocOffset)
724 return FileID::get(0);
725
726 // Now it is time to search for the correct file. See where the SLocOffset
727 // sits in the global view and consult local or loaded buffers for it.
728 if (SLocOffset < NextLocalOffset)
729 return getFileIDLocal(SLocOffset);
730 return getFileIDLoaded(SLocOffset);
731 }
732
733 /// Return the FileID for a SourceLocation with a low offset.
734 ///
735 /// This function knows that the SourceLocation is in a local buffer, not a
736 /// loaded one.
getFileIDLocal(unsigned SLocOffset) const737 FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const {
738 assert(SLocOffset < NextLocalOffset && "Bad function choice");
739
740 // After the first and second level caches, I see two common sorts of
741 // behavior: 1) a lot of searched FileID's are "near" the cached file
742 // location or are "near" the cached expansion location. 2) others are just
743 // completely random and may be a very long way away.
744 //
745 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
746 // then we fall back to a less cache efficient, but more scalable, binary
747 // search to find the location.
748
749 // See if this is near the file point - worst case we start scanning from the
750 // most newly created FileID.
751 const SrcMgr::SLocEntry *I;
752
753 if (LastFileIDLookup.ID < 0 ||
754 LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
755 // Neither loc prunes our search.
756 I = LocalSLocEntryTable.end();
757 } else {
758 // Perhaps it is near the file point.
759 I = LocalSLocEntryTable.begin()+LastFileIDLookup.ID;
760 }
761
762 // Find the FileID that contains this. "I" is an iterator that points to a
763 // FileID whose offset is known to be larger than SLocOffset.
764 unsigned NumProbes = 0;
765 while (true) {
766 --I;
767 if (I->getOffset() <= SLocOffset) {
768 FileID Res = FileID::get(int(I - LocalSLocEntryTable.begin()));
769
770 // If this isn't an expansion, remember it. We have good locality across
771 // FileID lookups.
772 if (!I->isExpansion())
773 LastFileIDLookup = Res;
774 NumLinearScans += NumProbes+1;
775 return Res;
776 }
777 if (++NumProbes == 8)
778 break;
779 }
780
781 // Convert "I" back into an index. We know that it is an entry whose index is
782 // larger than the offset we are looking for.
783 unsigned GreaterIndex = I - LocalSLocEntryTable.begin();
784 // LessIndex - This is the lower bound of the range that we're searching.
785 // We know that the offset corresponding to the FileID is is less than
786 // SLocOffset.
787 unsigned LessIndex = 0;
788 NumProbes = 0;
789 while (true) {
790 bool Invalid = false;
791 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
792 unsigned MidOffset = getLocalSLocEntry(MiddleIndex, &Invalid).getOffset();
793 if (Invalid)
794 return FileID::get(0);
795
796 ++NumProbes;
797
798 // If the offset of the midpoint is too large, chop the high side of the
799 // range to the midpoint.
800 if (MidOffset > SLocOffset) {
801 GreaterIndex = MiddleIndex;
802 continue;
803 }
804
805 // If the middle index contains the value, succeed and return.
806 // FIXME: This could be made faster by using a function that's aware of
807 // being in the local area.
808 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
809 FileID Res = FileID::get(MiddleIndex);
810
811 // If this isn't a macro expansion, remember it. We have good locality
812 // across FileID lookups.
813 if (!LocalSLocEntryTable[MiddleIndex].isExpansion())
814 LastFileIDLookup = Res;
815 NumBinaryProbes += NumProbes;
816 return Res;
817 }
818
819 // Otherwise, move the low-side up to the middle index.
820 LessIndex = MiddleIndex;
821 }
822 }
823
824 /// Return the FileID for a SourceLocation with a high offset.
825 ///
826 /// This function knows that the SourceLocation is in a loaded buffer, not a
827 /// local one.
getFileIDLoaded(unsigned SLocOffset) const828 FileID SourceManager::getFileIDLoaded(unsigned SLocOffset) const {
829 // Sanity checking, otherwise a bug may lead to hanging in release build.
830 if (SLocOffset < CurrentLoadedOffset) {
831 assert(0 && "Invalid SLocOffset or bad function choice");
832 return FileID();
833 }
834
835 // Essentially the same as the local case, but the loaded array is sorted
836 // in the other direction.
837
838 // First do a linear scan from the last lookup position, if possible.
839 unsigned I;
840 int LastID = LastFileIDLookup.ID;
841 if (LastID >= 0 || getLoadedSLocEntryByID(LastID).getOffset() < SLocOffset)
842 I = 0;
843 else
844 I = (-LastID - 2) + 1;
845
846 unsigned NumProbes;
847 for (NumProbes = 0; NumProbes < 8; ++NumProbes, ++I) {
848 // Make sure the entry is loaded!
849 const SrcMgr::SLocEntry &E = getLoadedSLocEntry(I);
850 if (E.getOffset() <= SLocOffset) {
851 FileID Res = FileID::get(-int(I) - 2);
852
853 if (!E.isExpansion())
854 LastFileIDLookup = Res;
855 NumLinearScans += NumProbes + 1;
856 return Res;
857 }
858 }
859
860 // Linear scan failed. Do the binary search. Note the reverse sorting of the
861 // table: GreaterIndex is the one where the offset is greater, which is
862 // actually a lower index!
863 unsigned GreaterIndex = I;
864 unsigned LessIndex = LoadedSLocEntryTable.size();
865 NumProbes = 0;
866 while (true) {
867 ++NumProbes;
868 unsigned MiddleIndex = (LessIndex - GreaterIndex) / 2 + GreaterIndex;
869 const SrcMgr::SLocEntry &E = getLoadedSLocEntry(MiddleIndex);
870 if (E.getOffset() == 0)
871 return FileID(); // invalid entry.
872
873 ++NumProbes;
874
875 if (E.getOffset() > SLocOffset) {
876 // Sanity checking, otherwise a bug may lead to hanging in release build.
877 if (GreaterIndex == MiddleIndex) {
878 assert(0 && "binary search missed the entry");
879 return FileID();
880 }
881 GreaterIndex = MiddleIndex;
882 continue;
883 }
884
885 if (isOffsetInFileID(FileID::get(-int(MiddleIndex) - 2), SLocOffset)) {
886 FileID Res = FileID::get(-int(MiddleIndex) - 2);
887 if (!E.isExpansion())
888 LastFileIDLookup = Res;
889 NumBinaryProbes += NumProbes;
890 return Res;
891 }
892
893 // Sanity checking, otherwise a bug may lead to hanging in release build.
894 if (LessIndex == MiddleIndex) {
895 assert(0 && "binary search missed the entry");
896 return FileID();
897 }
898 LessIndex = MiddleIndex;
899 }
900 }
901
902 SourceLocation SourceManager::
getExpansionLocSlowCase(SourceLocation Loc) const903 getExpansionLocSlowCase(SourceLocation Loc) const {
904 do {
905 // Note: If Loc indicates an offset into a token that came from a macro
906 // expansion (e.g. the 5th character of the token) we do not want to add
907 // this offset when going to the expansion location. The expansion
908 // location is the macro invocation, which the offset has nothing to do
909 // with. This is unlike when we get the spelling loc, because the offset
910 // directly correspond to the token whose spelling we're inspecting.
911 Loc = getSLocEntry(getFileID(Loc)).getExpansion().getExpansionLocStart();
912 } while (!Loc.isFileID());
913
914 return Loc;
915 }
916
getSpellingLocSlowCase(SourceLocation Loc) const917 SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
918 do {
919 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
920 Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc();
921 Loc = Loc.getLocWithOffset(LocInfo.second);
922 } while (!Loc.isFileID());
923 return Loc;
924 }
925
getFileLocSlowCase(SourceLocation Loc) const926 SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const {
927 do {
928 if (isMacroArgExpansion(Loc))
929 Loc = getImmediateSpellingLoc(Loc);
930 else
931 Loc = getImmediateExpansionRange(Loc).getBegin();
932 } while (!Loc.isFileID());
933 return Loc;
934 }
935
936
937 std::pair<FileID, unsigned>
getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry * E) const938 SourceManager::getDecomposedExpansionLocSlowCase(
939 const SrcMgr::SLocEntry *E) const {
940 // If this is an expansion record, walk through all the expansion points.
941 FileID FID;
942 SourceLocation Loc;
943 unsigned Offset;
944 do {
945 Loc = E->getExpansion().getExpansionLocStart();
946
947 FID = getFileID(Loc);
948 E = &getSLocEntry(FID);
949 Offset = Loc.getOffset()-E->getOffset();
950 } while (!Loc.isFileID());
951
952 return std::make_pair(FID, Offset);
953 }
954
955 std::pair<FileID, unsigned>
getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry * E,unsigned Offset) const956 SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
957 unsigned Offset) const {
958 // If this is an expansion record, walk through all the expansion points.
959 FileID FID;
960 SourceLocation Loc;
961 do {
962 Loc = E->getExpansion().getSpellingLoc();
963 Loc = Loc.getLocWithOffset(Offset);
964
965 FID = getFileID(Loc);
966 E = &getSLocEntry(FID);
967 Offset = Loc.getOffset()-E->getOffset();
968 } while (!Loc.isFileID());
969
970 return std::make_pair(FID, Offset);
971 }
972
973 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
974 /// spelling location referenced by the ID. This is the first level down
975 /// towards the place where the characters that make up the lexed token can be
976 /// found. This should not generally be used by clients.
getImmediateSpellingLoc(SourceLocation Loc) const977 SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
978 if (Loc.isFileID()) return Loc;
979 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
980 Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc();
981 return Loc.getLocWithOffset(LocInfo.second);
982 }
983
984 /// getImmediateExpansionRange - Loc is required to be an expansion location.
985 /// Return the start/end of the expansion information.
986 CharSourceRange
getImmediateExpansionRange(SourceLocation Loc) const987 SourceManager::getImmediateExpansionRange(SourceLocation Loc) const {
988 assert(Loc.isMacroID() && "Not a macro expansion loc!");
989 const ExpansionInfo &Expansion = getSLocEntry(getFileID(Loc)).getExpansion();
990 return Expansion.getExpansionLocRange();
991 }
992
getTopMacroCallerLoc(SourceLocation Loc) const993 SourceLocation SourceManager::getTopMacroCallerLoc(SourceLocation Loc) const {
994 while (isMacroArgExpansion(Loc))
995 Loc = getImmediateSpellingLoc(Loc);
996 return Loc;
997 }
998
999 /// getExpansionRange - Given a SourceLocation object, return the range of
1000 /// tokens covered by the expansion in the ultimate file.
getExpansionRange(SourceLocation Loc) const1001 CharSourceRange SourceManager::getExpansionRange(SourceLocation Loc) const {
1002 if (Loc.isFileID())
1003 return CharSourceRange(SourceRange(Loc, Loc), true);
1004
1005 CharSourceRange Res = getImmediateExpansionRange(Loc);
1006
1007 // Fully resolve the start and end locations to their ultimate expansion
1008 // points.
1009 while (!Res.getBegin().isFileID())
1010 Res.setBegin(getImmediateExpansionRange(Res.getBegin()).getBegin());
1011 while (!Res.getEnd().isFileID()) {
1012 CharSourceRange EndRange = getImmediateExpansionRange(Res.getEnd());
1013 Res.setEnd(EndRange.getEnd());
1014 Res.setTokenRange(EndRange.isTokenRange());
1015 }
1016 return Res;
1017 }
1018
isMacroArgExpansion(SourceLocation Loc,SourceLocation * StartLoc) const1019 bool SourceManager::isMacroArgExpansion(SourceLocation Loc,
1020 SourceLocation *StartLoc) const {
1021 if (!Loc.isMacroID()) return false;
1022
1023 FileID FID = getFileID(Loc);
1024 const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion();
1025 if (!Expansion.isMacroArgExpansion()) return false;
1026
1027 if (StartLoc)
1028 *StartLoc = Expansion.getExpansionLocStart();
1029 return true;
1030 }
1031
isMacroBodyExpansion(SourceLocation Loc) const1032 bool SourceManager::isMacroBodyExpansion(SourceLocation Loc) const {
1033 if (!Loc.isMacroID()) return false;
1034
1035 FileID FID = getFileID(Loc);
1036 const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion();
1037 return Expansion.isMacroBodyExpansion();
1038 }
1039
isAtStartOfImmediateMacroExpansion(SourceLocation Loc,SourceLocation * MacroBegin) const1040 bool SourceManager::isAtStartOfImmediateMacroExpansion(SourceLocation Loc,
1041 SourceLocation *MacroBegin) const {
1042 assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc");
1043
1044 std::pair<FileID, unsigned> DecompLoc = getDecomposedLoc(Loc);
1045 if (DecompLoc.second > 0)
1046 return false; // Does not point at the start of expansion range.
1047
1048 bool Invalid = false;
1049 const SrcMgr::ExpansionInfo &ExpInfo =
1050 getSLocEntry(DecompLoc.first, &Invalid).getExpansion();
1051 if (Invalid)
1052 return false;
1053 SourceLocation ExpLoc = ExpInfo.getExpansionLocStart();
1054
1055 if (ExpInfo.isMacroArgExpansion()) {
1056 // For macro argument expansions, check if the previous FileID is part of
1057 // the same argument expansion, in which case this Loc is not at the
1058 // beginning of the expansion.
1059 FileID PrevFID = getPreviousFileID(DecompLoc.first);
1060 if (!PrevFID.isInvalid()) {
1061 const SrcMgr::SLocEntry &PrevEntry = getSLocEntry(PrevFID, &Invalid);
1062 if (Invalid)
1063 return false;
1064 if (PrevEntry.isExpansion() &&
1065 PrevEntry.getExpansion().getExpansionLocStart() == ExpLoc)
1066 return false;
1067 }
1068 }
1069
1070 if (MacroBegin)
1071 *MacroBegin = ExpLoc;
1072 return true;
1073 }
1074
isAtEndOfImmediateMacroExpansion(SourceLocation Loc,SourceLocation * MacroEnd) const1075 bool SourceManager::isAtEndOfImmediateMacroExpansion(SourceLocation Loc,
1076 SourceLocation *MacroEnd) const {
1077 assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc");
1078
1079 FileID FID = getFileID(Loc);
1080 SourceLocation NextLoc = Loc.getLocWithOffset(1);
1081 if (isInFileID(NextLoc, FID))
1082 return false; // Does not point at the end of expansion range.
1083
1084 bool Invalid = false;
1085 const SrcMgr::ExpansionInfo &ExpInfo =
1086 getSLocEntry(FID, &Invalid).getExpansion();
1087 if (Invalid)
1088 return false;
1089
1090 if (ExpInfo.isMacroArgExpansion()) {
1091 // For macro argument expansions, check if the next FileID is part of the
1092 // same argument expansion, in which case this Loc is not at the end of the
1093 // expansion.
1094 FileID NextFID = getNextFileID(FID);
1095 if (!NextFID.isInvalid()) {
1096 const SrcMgr::SLocEntry &NextEntry = getSLocEntry(NextFID, &Invalid);
1097 if (Invalid)
1098 return false;
1099 if (NextEntry.isExpansion() &&
1100 NextEntry.getExpansion().getExpansionLocStart() ==
1101 ExpInfo.getExpansionLocStart())
1102 return false;
1103 }
1104 }
1105
1106 if (MacroEnd)
1107 *MacroEnd = ExpInfo.getExpansionLocEnd();
1108 return true;
1109 }
1110
1111 //===----------------------------------------------------------------------===//
1112 // Queries about the code at a SourceLocation.
1113 //===----------------------------------------------------------------------===//
1114
1115 /// getCharacterData - Return a pointer to the start of the specified location
1116 /// in the appropriate MemoryBuffer.
getCharacterData(SourceLocation SL,bool * Invalid) const1117 const char *SourceManager::getCharacterData(SourceLocation SL,
1118 bool *Invalid) const {
1119 // Note that this is a hot function in the getSpelling() path, which is
1120 // heavily used by -E mode.
1121 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
1122
1123 // Note that calling 'getBuffer()' may lazily page in a source file.
1124 bool CharDataInvalid = false;
1125 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &CharDataInvalid);
1126 if (CharDataInvalid || !Entry.isFile()) {
1127 if (Invalid)
1128 *Invalid = true;
1129
1130 return "<<<<INVALID BUFFER>>>>";
1131 }
1132 const llvm::MemoryBuffer *Buffer =
1133 Entry.getFile().getContentCache()->getBuffer(
1134 Diag, *this, SourceLocation(), &CharDataInvalid);
1135 if (Invalid)
1136 *Invalid = CharDataInvalid;
1137 return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second);
1138 }
1139
1140 /// getColumnNumber - Return the column # for the specified file position.
1141 /// this is significantly cheaper to compute than the line number.
getColumnNumber(FileID FID,unsigned FilePos,bool * Invalid) const1142 unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
1143 bool *Invalid) const {
1144 bool MyInvalid = false;
1145 const llvm::MemoryBuffer *MemBuf = getBuffer(FID, &MyInvalid);
1146 if (Invalid)
1147 *Invalid = MyInvalid;
1148
1149 if (MyInvalid)
1150 return 1;
1151
1152 // It is okay to request a position just past the end of the buffer.
1153 if (FilePos > MemBuf->getBufferSize()) {
1154 if (Invalid)
1155 *Invalid = true;
1156 return 1;
1157 }
1158
1159 const char *Buf = MemBuf->getBufferStart();
1160 // See if we just calculated the line number for this FilePos and can use
1161 // that to lookup the start of the line instead of searching for it.
1162 if (LastLineNoFileIDQuery == FID &&
1163 LastLineNoContentCache->SourceLineCache != nullptr &&
1164 LastLineNoResult < LastLineNoContentCache->NumLines) {
1165 unsigned *SourceLineCache = LastLineNoContentCache->SourceLineCache;
1166 unsigned LineStart = SourceLineCache[LastLineNoResult - 1];
1167 unsigned LineEnd = SourceLineCache[LastLineNoResult];
1168 if (FilePos >= LineStart && FilePos < LineEnd) {
1169 // LineEnd is the LineStart of the next line.
1170 // A line ends with separator LF or CR+LF on Windows.
1171 // FilePos might point to the last separator,
1172 // but we need a column number at most 1 + the last column.
1173 if (FilePos + 1 == LineEnd && FilePos > LineStart) {
1174 if (Buf[FilePos - 1] == '\r' || Buf[FilePos - 1] == '\n')
1175 --FilePos;
1176 }
1177 return FilePos - LineStart + 1;
1178 }
1179 }
1180
1181 unsigned LineStart = FilePos;
1182 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
1183 --LineStart;
1184 return FilePos-LineStart+1;
1185 }
1186
1187 // isInvalid - Return the result of calling loc.isInvalid(), and
1188 // if Invalid is not null, set its value to same.
1189 template<typename LocType>
isInvalid(LocType Loc,bool * Invalid)1190 static bool isInvalid(LocType Loc, bool *Invalid) {
1191 bool MyInvalid = Loc.isInvalid();
1192 if (Invalid)
1193 *Invalid = MyInvalid;
1194 return MyInvalid;
1195 }
1196
getSpellingColumnNumber(SourceLocation Loc,bool * Invalid) const1197 unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
1198 bool *Invalid) const {
1199 if (isInvalid(Loc, Invalid)) return 0;
1200 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
1201 return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
1202 }
1203
getExpansionColumnNumber(SourceLocation Loc,bool * Invalid) const1204 unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc,
1205 bool *Invalid) const {
1206 if (isInvalid(Loc, Invalid)) return 0;
1207 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1208 return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
1209 }
1210
getPresumedColumnNumber(SourceLocation Loc,bool * Invalid) const1211 unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc,
1212 bool *Invalid) const {
1213 PresumedLoc PLoc = getPresumedLoc(Loc);
1214 if (isInvalid(PLoc, Invalid)) return 0;
1215 return PLoc.getColumn();
1216 }
1217
1218 #ifdef __SSE2__
1219 #include <emmintrin.h>
1220 #endif
1221
1222 static LLVM_ATTRIBUTE_NOINLINE void
1223 ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI,
1224 llvm::BumpPtrAllocator &Alloc,
1225 const SourceManager &SM, bool &Invalid);
ComputeLineNumbers(DiagnosticsEngine & Diag,ContentCache * FI,llvm::BumpPtrAllocator & Alloc,const SourceManager & SM,bool & Invalid)1226 static void ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI,
1227 llvm::BumpPtrAllocator &Alloc,
1228 const SourceManager &SM, bool &Invalid) {
1229 // Note that calling 'getBuffer()' may lazily page in the file.
1230 const MemoryBuffer *Buffer =
1231 FI->getBuffer(Diag, SM, SourceLocation(), &Invalid);
1232 if (Invalid)
1233 return;
1234
1235 // Find the file offsets of all of the *physical* source lines. This does
1236 // not look at trigraphs, escaped newlines, or anything else tricky.
1237 SmallVector<unsigned, 256> LineOffsets;
1238
1239 // Line #1 starts at char 0.
1240 LineOffsets.push_back(0);
1241
1242 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
1243 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
1244 unsigned I = 0;
1245 while (true) {
1246 // Skip over the contents of the line.
1247 while (Buf[I] != '\n' && Buf[I] != '\r' && Buf[I] != '\0')
1248 ++I;
1249
1250 if (Buf[I] == '\n' || Buf[I] == '\r') {
1251 // If this is \r\n, skip both characters.
1252 if (Buf[I] == '\r' && Buf[I+1] == '\n')
1253 ++I;
1254 ++I;
1255 LineOffsets.push_back(I);
1256 } else {
1257 // Otherwise, this is a NUL. If end of file, exit.
1258 if (Buf+I == End) break;
1259 ++I;
1260 }
1261 }
1262
1263 // Copy the offsets into the FileInfo structure.
1264 FI->NumLines = LineOffsets.size();
1265 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
1266 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
1267 }
1268
1269 /// getLineNumber - Given a SourceLocation, return the spelling line number
1270 /// for the position indicated. This requires building and caching a table of
1271 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
1272 /// about to emit a diagnostic.
getLineNumber(FileID FID,unsigned FilePos,bool * Invalid) const1273 unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
1274 bool *Invalid) const {
1275 if (FID.isInvalid()) {
1276 if (Invalid)
1277 *Invalid = true;
1278 return 1;
1279 }
1280
1281 ContentCache *Content;
1282 if (LastLineNoFileIDQuery == FID)
1283 Content = LastLineNoContentCache;
1284 else {
1285 bool MyInvalid = false;
1286 const SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
1287 if (MyInvalid || !Entry.isFile()) {
1288 if (Invalid)
1289 *Invalid = true;
1290 return 1;
1291 }
1292
1293 Content = const_cast<ContentCache*>(Entry.getFile().getContentCache());
1294 }
1295
1296 // If this is the first use of line information for this buffer, compute the
1297 /// SourceLineCache for it on demand.
1298 if (!Content->SourceLineCache) {
1299 bool MyInvalid = false;
1300 ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
1301 if (Invalid)
1302 *Invalid = MyInvalid;
1303 if (MyInvalid)
1304 return 1;
1305 } else if (Invalid)
1306 *Invalid = false;
1307
1308 // Okay, we know we have a line number table. Do a binary search to find the
1309 // line number that this character position lands on.
1310 unsigned *SourceLineCache = Content->SourceLineCache;
1311 unsigned *SourceLineCacheStart = SourceLineCache;
1312 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
1313
1314 unsigned QueriedFilePos = FilePos+1;
1315
1316 // FIXME: I would like to be convinced that this code is worth being as
1317 // complicated as it is, binary search isn't that slow.
1318 //
1319 // If it is worth being optimized, then in my opinion it could be more
1320 // performant, simpler, and more obviously correct by just "galloping" outward
1321 // from the queried file position. In fact, this could be incorporated into a
1322 // generic algorithm such as lower_bound_with_hint.
1323 //
1324 // If someone gives me a test case where this matters, and I will do it! - DWD
1325
1326 // If the previous query was to the same file, we know both the file pos from
1327 // that query and the line number returned. This allows us to narrow the
1328 // search space from the entire file to something near the match.
1329 if (LastLineNoFileIDQuery == FID) {
1330 if (QueriedFilePos >= LastLineNoFilePos) {
1331 // FIXME: Potential overflow?
1332 SourceLineCache = SourceLineCache+LastLineNoResult-1;
1333
1334 // The query is likely to be nearby the previous one. Here we check to
1335 // see if it is within 5, 10 or 20 lines. It can be far away in cases
1336 // where big comment blocks and vertical whitespace eat up lines but
1337 // contribute no tokens.
1338 if (SourceLineCache+5 < SourceLineCacheEnd) {
1339 if (SourceLineCache[5] > QueriedFilePos)
1340 SourceLineCacheEnd = SourceLineCache+5;
1341 else if (SourceLineCache+10 < SourceLineCacheEnd) {
1342 if (SourceLineCache[10] > QueriedFilePos)
1343 SourceLineCacheEnd = SourceLineCache+10;
1344 else if (SourceLineCache+20 < SourceLineCacheEnd) {
1345 if (SourceLineCache[20] > QueriedFilePos)
1346 SourceLineCacheEnd = SourceLineCache+20;
1347 }
1348 }
1349 }
1350 } else {
1351 if (LastLineNoResult < Content->NumLines)
1352 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
1353 }
1354 }
1355
1356 unsigned *Pos
1357 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
1358 unsigned LineNo = Pos-SourceLineCacheStart;
1359
1360 LastLineNoFileIDQuery = FID;
1361 LastLineNoContentCache = Content;
1362 LastLineNoFilePos = QueriedFilePos;
1363 LastLineNoResult = LineNo;
1364 return LineNo;
1365 }
1366
getSpellingLineNumber(SourceLocation Loc,bool * Invalid) const1367 unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
1368 bool *Invalid) const {
1369 if (isInvalid(Loc, Invalid)) return 0;
1370 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
1371 return getLineNumber(LocInfo.first, LocInfo.second);
1372 }
getExpansionLineNumber(SourceLocation Loc,bool * Invalid) const1373 unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc,
1374 bool *Invalid) const {
1375 if (isInvalid(Loc, Invalid)) return 0;
1376 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1377 return getLineNumber(LocInfo.first, LocInfo.second);
1378 }
getPresumedLineNumber(SourceLocation Loc,bool * Invalid) const1379 unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
1380 bool *Invalid) const {
1381 PresumedLoc PLoc = getPresumedLoc(Loc);
1382 if (isInvalid(PLoc, Invalid)) return 0;
1383 return PLoc.getLine();
1384 }
1385
1386 /// getFileCharacteristic - return the file characteristic of the specified
1387 /// source location, indicating whether this is a normal file, a system
1388 /// header, or an "implicit extern C" system header.
1389 ///
1390 /// This state can be modified with flags on GNU linemarker directives like:
1391 /// # 4 "foo.h" 3
1392 /// which changes all source locations in the current file after that to be
1393 /// considered to be from a system header.
1394 SrcMgr::CharacteristicKind
getFileCharacteristic(SourceLocation Loc) const1395 SourceManager::getFileCharacteristic(SourceLocation Loc) const {
1396 assert(Loc.isValid() && "Can't get file characteristic of invalid loc!");
1397 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1398 bool Invalid = false;
1399 const SLocEntry &SEntry = getSLocEntry(LocInfo.first, &Invalid);
1400 if (Invalid || !SEntry.isFile())
1401 return C_User;
1402
1403 const SrcMgr::FileInfo &FI = SEntry.getFile();
1404
1405 // If there are no #line directives in this file, just return the whole-file
1406 // state.
1407 if (!FI.hasLineDirectives())
1408 return FI.getFileCharacteristic();
1409
1410 assert(LineTable && "Can't have linetable entries without a LineTable!");
1411 // See if there is a #line directive before the location.
1412 const LineEntry *Entry =
1413 LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second);
1414
1415 // If this is before the first line marker, use the file characteristic.
1416 if (!Entry)
1417 return FI.getFileCharacteristic();
1418
1419 return Entry->FileKind;
1420 }
1421
1422 /// Return the filename or buffer identifier of the buffer the location is in.
1423 /// Note that this name does not respect \#line directives. Use getPresumedLoc
1424 /// for normal clients.
getBufferName(SourceLocation Loc,bool * Invalid) const1425 StringRef SourceManager::getBufferName(SourceLocation Loc,
1426 bool *Invalid) const {
1427 if (isInvalid(Loc, Invalid)) return "<invalid loc>";
1428
1429 return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier();
1430 }
1431
1432 /// getPresumedLoc - This method returns the "presumed" location of a
1433 /// SourceLocation specifies. A "presumed location" can be modified by \#line
1434 /// or GNU line marker directives. This provides a view on the data that a
1435 /// user should see in diagnostics, for example.
1436 ///
1437 /// Note that a presumed location is always given as the expansion point of an
1438 /// expansion location, not at the spelling location.
getPresumedLoc(SourceLocation Loc,bool UseLineDirectives) const1439 PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc,
1440 bool UseLineDirectives) const {
1441 if (Loc.isInvalid()) return PresumedLoc();
1442
1443 // Presumed locations are always for expansion points.
1444 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1445
1446 bool Invalid = false;
1447 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
1448 if (Invalid || !Entry.isFile())
1449 return PresumedLoc();
1450
1451 const SrcMgr::FileInfo &FI = Entry.getFile();
1452 const SrcMgr::ContentCache *C = FI.getContentCache();
1453
1454 // To get the source name, first consult the FileEntry (if one exists)
1455 // before the MemBuffer as this will avoid unnecessarily paging in the
1456 // MemBuffer.
1457 FileID FID = LocInfo.first;
1458 StringRef Filename;
1459 if (C->OrigEntry)
1460 Filename = C->OrigEntry->getName();
1461 else
1462 Filename = C->getBuffer(Diag, *this)->getBufferIdentifier();
1463
1464 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid);
1465 if (Invalid)
1466 return PresumedLoc();
1467 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid);
1468 if (Invalid)
1469 return PresumedLoc();
1470
1471 SourceLocation IncludeLoc = FI.getIncludeLoc();
1472
1473 // If we have #line directives in this file, update and overwrite the physical
1474 // location info if appropriate.
1475 if (UseLineDirectives && FI.hasLineDirectives()) {
1476 assert(LineTable && "Can't have linetable entries without a LineTable!");
1477 // See if there is a #line directive before this. If so, get it.
1478 if (const LineEntry *Entry =
1479 LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second)) {
1480 // If the LineEntry indicates a filename, use it.
1481 if (Entry->FilenameID != -1) {
1482 Filename = LineTable->getFilename(Entry->FilenameID);
1483 // The contents of files referenced by #line are not in the
1484 // SourceManager
1485 FID = FileID::get(0);
1486 }
1487
1488 // Use the line number specified by the LineEntry. This line number may
1489 // be multiple lines down from the line entry. Add the difference in
1490 // physical line numbers from the query point and the line marker to the
1491 // total.
1492 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
1493 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
1494
1495 // Note that column numbers are not molested by line markers.
1496
1497 // Handle virtual #include manipulation.
1498 if (Entry->IncludeOffset) {
1499 IncludeLoc = getLocForStartOfFile(LocInfo.first);
1500 IncludeLoc = IncludeLoc.getLocWithOffset(Entry->IncludeOffset);
1501 }
1502 }
1503 }
1504
1505 return PresumedLoc(Filename.data(), FID, LineNo, ColNo, IncludeLoc);
1506 }
1507
1508 /// Returns whether the PresumedLoc for a given SourceLocation is
1509 /// in the main file.
1510 ///
1511 /// This computes the "presumed" location for a SourceLocation, then checks
1512 /// whether it came from a file other than the main file. This is different
1513 /// from isWrittenInMainFile() because it takes line marker directives into
1514 /// account.
isInMainFile(SourceLocation Loc) const1515 bool SourceManager::isInMainFile(SourceLocation Loc) const {
1516 if (Loc.isInvalid()) return false;
1517
1518 // Presumed locations are always for expansion points.
1519 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1520
1521 bool Invalid = false;
1522 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
1523 if (Invalid || !Entry.isFile())
1524 return false;
1525
1526 const SrcMgr::FileInfo &FI = Entry.getFile();
1527
1528 // Check if there is a line directive for this location.
1529 if (FI.hasLineDirectives())
1530 if (const LineEntry *Entry =
1531 LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second))
1532 if (Entry->IncludeOffset)
1533 return false;
1534
1535 return FI.getIncludeLoc().isInvalid();
1536 }
1537
1538 /// The size of the SLocEntry that \p FID represents.
getFileIDSize(FileID FID) const1539 unsigned SourceManager::getFileIDSize(FileID FID) const {
1540 bool Invalid = false;
1541 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1542 if (Invalid)
1543 return 0;
1544
1545 int ID = FID.ID;
1546 unsigned NextOffset;
1547 if ((ID > 0 && unsigned(ID+1) == local_sloc_entry_size()))
1548 NextOffset = getNextLocalOffset();
1549 else if (ID+1 == -1)
1550 NextOffset = MaxLoadedOffset;
1551 else
1552 NextOffset = getSLocEntry(FileID::get(ID+1)).getOffset();
1553
1554 return NextOffset - Entry.getOffset() - 1;
1555 }
1556
1557 //===----------------------------------------------------------------------===//
1558 // Other miscellaneous methods.
1559 //===----------------------------------------------------------------------===//
1560
1561 /// Retrieve the inode for the given file entry, if possible.
1562 ///
1563 /// This routine involves a system call, and therefore should only be used
1564 /// in non-performance-critical code.
1565 static Optional<llvm::sys::fs::UniqueID>
getActualFileUID(const FileEntry * File)1566 getActualFileUID(const FileEntry *File) {
1567 if (!File)
1568 return None;
1569
1570 llvm::sys::fs::UniqueID ID;
1571 if (llvm::sys::fs::getUniqueID(File->getName(), ID))
1572 return None;
1573
1574 return ID;
1575 }
1576
1577 /// Get the source location for the given file:line:col triplet.
1578 ///
1579 /// If the source file is included multiple times, the source location will
1580 /// be based upon an arbitrary inclusion.
translateFileLineCol(const FileEntry * SourceFile,unsigned Line,unsigned Col) const1581 SourceLocation SourceManager::translateFileLineCol(const FileEntry *SourceFile,
1582 unsigned Line,
1583 unsigned Col) const {
1584 assert(SourceFile && "Null source file!");
1585 assert(Line && Col && "Line and column should start from 1!");
1586
1587 FileID FirstFID = translateFile(SourceFile);
1588 return translateLineCol(FirstFID, Line, Col);
1589 }
1590
1591 /// Get the FileID for the given file.
1592 ///
1593 /// If the source file is included multiple times, the FileID will be the
1594 /// first inclusion.
translateFile(const FileEntry * SourceFile) const1595 FileID SourceManager::translateFile(const FileEntry *SourceFile) const {
1596 assert(SourceFile && "Null source file!");
1597
1598 // Find the first file ID that corresponds to the given file.
1599 FileID FirstFID;
1600
1601 // First, check the main file ID, since it is common to look for a
1602 // location in the main file.
1603 Optional<llvm::sys::fs::UniqueID> SourceFileUID;
1604 Optional<StringRef> SourceFileName;
1605 if (MainFileID.isValid()) {
1606 bool Invalid = false;
1607 const SLocEntry &MainSLoc = getSLocEntry(MainFileID, &Invalid);
1608 if (Invalid)
1609 return FileID();
1610
1611 if (MainSLoc.isFile()) {
1612 const ContentCache *MainContentCache
1613 = MainSLoc.getFile().getContentCache();
1614 if (!MainContentCache || !MainContentCache->OrigEntry) {
1615 // Can't do anything
1616 } else if (MainContentCache->OrigEntry == SourceFile) {
1617 FirstFID = MainFileID;
1618 } else {
1619 // Fall back: check whether we have the same base name and inode
1620 // as the main file.
1621 const FileEntry *MainFile = MainContentCache->OrigEntry;
1622 SourceFileName = llvm::sys::path::filename(SourceFile->getName());
1623 if (*SourceFileName == llvm::sys::path::filename(MainFile->getName())) {
1624 SourceFileUID = getActualFileUID(SourceFile);
1625 if (SourceFileUID) {
1626 if (Optional<llvm::sys::fs::UniqueID> MainFileUID =
1627 getActualFileUID(MainFile)) {
1628 if (*SourceFileUID == *MainFileUID) {
1629 FirstFID = MainFileID;
1630 SourceFile = MainFile;
1631 }
1632 }
1633 }
1634 }
1635 }
1636 }
1637 }
1638
1639 if (FirstFID.isInvalid()) {
1640 // The location we're looking for isn't in the main file; look
1641 // through all of the local source locations.
1642 for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) {
1643 bool Invalid = false;
1644 const SLocEntry &SLoc = getLocalSLocEntry(I, &Invalid);
1645 if (Invalid)
1646 return FileID();
1647
1648 if (SLoc.isFile() &&
1649 SLoc.getFile().getContentCache() &&
1650 SLoc.getFile().getContentCache()->OrigEntry == SourceFile) {
1651 FirstFID = FileID::get(I);
1652 break;
1653 }
1654 }
1655 // If that still didn't help, try the modules.
1656 if (FirstFID.isInvalid()) {
1657 for (unsigned I = 0, N = loaded_sloc_entry_size(); I != N; ++I) {
1658 const SLocEntry &SLoc = getLoadedSLocEntry(I);
1659 if (SLoc.isFile() &&
1660 SLoc.getFile().getContentCache() &&
1661 SLoc.getFile().getContentCache()->OrigEntry == SourceFile) {
1662 FirstFID = FileID::get(-int(I) - 2);
1663 break;
1664 }
1665 }
1666 }
1667 }
1668
1669 // If we haven't found what we want yet, try again, but this time stat()
1670 // each of the files in case the files have changed since we originally
1671 // parsed the file.
1672 if (FirstFID.isInvalid() &&
1673 (SourceFileName ||
1674 (SourceFileName = llvm::sys::path::filename(SourceFile->getName()))) &&
1675 (SourceFileUID || (SourceFileUID = getActualFileUID(SourceFile)))) {
1676 bool Invalid = false;
1677 for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) {
1678 FileID IFileID;
1679 IFileID.ID = I;
1680 const SLocEntry &SLoc = getSLocEntry(IFileID, &Invalid);
1681 if (Invalid)
1682 return FileID();
1683
1684 if (SLoc.isFile()) {
1685 const ContentCache *FileContentCache
1686 = SLoc.getFile().getContentCache();
1687 const FileEntry *Entry = FileContentCache ? FileContentCache->OrigEntry
1688 : nullptr;
1689 if (Entry &&
1690 *SourceFileName == llvm::sys::path::filename(Entry->getName())) {
1691 if (Optional<llvm::sys::fs::UniqueID> EntryUID =
1692 getActualFileUID(Entry)) {
1693 if (*SourceFileUID == *EntryUID) {
1694 FirstFID = FileID::get(I);
1695 SourceFile = Entry;
1696 break;
1697 }
1698 }
1699 }
1700 }
1701 }
1702 }
1703
1704 (void) SourceFile;
1705 return FirstFID;
1706 }
1707
1708 /// Get the source location in \arg FID for the given line:col.
1709 /// Returns null location if \arg FID is not a file SLocEntry.
translateLineCol(FileID FID,unsigned Line,unsigned Col) const1710 SourceLocation SourceManager::translateLineCol(FileID FID,
1711 unsigned Line,
1712 unsigned Col) const {
1713 // Lines are used as a one-based index into a zero-based array. This assert
1714 // checks for possible buffer underruns.
1715 assert(Line && Col && "Line and column should start from 1!");
1716
1717 if (FID.isInvalid())
1718 return SourceLocation();
1719
1720 bool Invalid = false;
1721 const SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1722 if (Invalid)
1723 return SourceLocation();
1724
1725 if (!Entry.isFile())
1726 return SourceLocation();
1727
1728 SourceLocation FileLoc = SourceLocation::getFileLoc(Entry.getOffset());
1729
1730 if (Line == 1 && Col == 1)
1731 return FileLoc;
1732
1733 ContentCache *Content
1734 = const_cast<ContentCache *>(Entry.getFile().getContentCache());
1735 if (!Content)
1736 return SourceLocation();
1737
1738 // If this is the first use of line information for this buffer, compute the
1739 // SourceLineCache for it on demand.
1740 if (!Content->SourceLineCache) {
1741 bool MyInvalid = false;
1742 ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
1743 if (MyInvalid)
1744 return SourceLocation();
1745 }
1746
1747 if (Line > Content->NumLines) {
1748 unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize();
1749 if (Size > 0)
1750 --Size;
1751 return FileLoc.getLocWithOffset(Size);
1752 }
1753
1754 const llvm::MemoryBuffer *Buffer = Content->getBuffer(Diag, *this);
1755 unsigned FilePos = Content->SourceLineCache[Line - 1];
1756 const char *Buf = Buffer->getBufferStart() + FilePos;
1757 unsigned BufLength = Buffer->getBufferSize() - FilePos;
1758 if (BufLength == 0)
1759 return FileLoc.getLocWithOffset(FilePos);
1760
1761 unsigned i = 0;
1762
1763 // Check that the given column is valid.
1764 while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
1765 ++i;
1766 return FileLoc.getLocWithOffset(FilePos + i);
1767 }
1768
1769 /// Compute a map of macro argument chunks to their expanded source
1770 /// location. Chunks that are not part of a macro argument will map to an
1771 /// invalid source location. e.g. if a file contains one macro argument at
1772 /// offset 100 with length 10, this is how the map will be formed:
1773 /// 0 -> SourceLocation()
1774 /// 100 -> Expanded macro arg location
1775 /// 110 -> SourceLocation()
computeMacroArgsCache(MacroArgsMap & MacroArgsCache,FileID FID) const1776 void SourceManager::computeMacroArgsCache(MacroArgsMap &MacroArgsCache,
1777 FileID FID) const {
1778 assert(FID.isValid());
1779
1780 // Initially no macro argument chunk is present.
1781 MacroArgsCache.insert(std::make_pair(0, SourceLocation()));
1782
1783 int ID = FID.ID;
1784 while (true) {
1785 ++ID;
1786 // Stop if there are no more FileIDs to check.
1787 if (ID > 0) {
1788 if (unsigned(ID) >= local_sloc_entry_size())
1789 return;
1790 } else if (ID == -1) {
1791 return;
1792 }
1793
1794 bool Invalid = false;
1795 const SrcMgr::SLocEntry &Entry = getSLocEntryByID(ID, &Invalid);
1796 if (Invalid)
1797 return;
1798 if (Entry.isFile()) {
1799 SourceLocation IncludeLoc = Entry.getFile().getIncludeLoc();
1800 if (IncludeLoc.isInvalid())
1801 continue;
1802 if (!isInFileID(IncludeLoc, FID))
1803 return; // No more files/macros that may be "contained" in this file.
1804
1805 // Skip the files/macros of the #include'd file, we only care about macros
1806 // that lexed macro arguments from our file.
1807 if (Entry.getFile().NumCreatedFIDs)
1808 ID += Entry.getFile().NumCreatedFIDs - 1/*because of next ++ID*/;
1809 continue;
1810 }
1811
1812 const ExpansionInfo &ExpInfo = Entry.getExpansion();
1813
1814 if (ExpInfo.getExpansionLocStart().isFileID()) {
1815 if (!isInFileID(ExpInfo.getExpansionLocStart(), FID))
1816 return; // No more files/macros that may be "contained" in this file.
1817 }
1818
1819 if (!ExpInfo.isMacroArgExpansion())
1820 continue;
1821
1822 associateFileChunkWithMacroArgExp(MacroArgsCache, FID,
1823 ExpInfo.getSpellingLoc(),
1824 SourceLocation::getMacroLoc(Entry.getOffset()),
1825 getFileIDSize(FileID::get(ID)));
1826 }
1827 }
1828
associateFileChunkWithMacroArgExp(MacroArgsMap & MacroArgsCache,FileID FID,SourceLocation SpellLoc,SourceLocation ExpansionLoc,unsigned ExpansionLength) const1829 void SourceManager::associateFileChunkWithMacroArgExp(
1830 MacroArgsMap &MacroArgsCache,
1831 FileID FID,
1832 SourceLocation SpellLoc,
1833 SourceLocation ExpansionLoc,
1834 unsigned ExpansionLength) const {
1835 if (!SpellLoc.isFileID()) {
1836 unsigned SpellBeginOffs = SpellLoc.getOffset();
1837 unsigned SpellEndOffs = SpellBeginOffs + ExpansionLength;
1838
1839 // The spelling range for this macro argument expansion can span multiple
1840 // consecutive FileID entries. Go through each entry contained in the
1841 // spelling range and if one is itself a macro argument expansion, recurse
1842 // and associate the file chunk that it represents.
1843
1844 FileID SpellFID; // Current FileID in the spelling range.
1845 unsigned SpellRelativeOffs;
1846 std::tie(SpellFID, SpellRelativeOffs) = getDecomposedLoc(SpellLoc);
1847 while (true) {
1848 const SLocEntry &Entry = getSLocEntry(SpellFID);
1849 unsigned SpellFIDBeginOffs = Entry.getOffset();
1850 unsigned SpellFIDSize = getFileIDSize(SpellFID);
1851 unsigned SpellFIDEndOffs = SpellFIDBeginOffs + SpellFIDSize;
1852 const ExpansionInfo &Info = Entry.getExpansion();
1853 if (Info.isMacroArgExpansion()) {
1854 unsigned CurrSpellLength;
1855 if (SpellFIDEndOffs < SpellEndOffs)
1856 CurrSpellLength = SpellFIDSize - SpellRelativeOffs;
1857 else
1858 CurrSpellLength = ExpansionLength;
1859 associateFileChunkWithMacroArgExp(MacroArgsCache, FID,
1860 Info.getSpellingLoc().getLocWithOffset(SpellRelativeOffs),
1861 ExpansionLoc, CurrSpellLength);
1862 }
1863
1864 if (SpellFIDEndOffs >= SpellEndOffs)
1865 return; // we covered all FileID entries in the spelling range.
1866
1867 // Move to the next FileID entry in the spelling range.
1868 unsigned advance = SpellFIDSize - SpellRelativeOffs + 1;
1869 ExpansionLoc = ExpansionLoc.getLocWithOffset(advance);
1870 ExpansionLength -= advance;
1871 ++SpellFID.ID;
1872 SpellRelativeOffs = 0;
1873 }
1874 }
1875
1876 assert(SpellLoc.isFileID());
1877
1878 unsigned BeginOffs;
1879 if (!isInFileID(SpellLoc, FID, &BeginOffs))
1880 return;
1881
1882 unsigned EndOffs = BeginOffs + ExpansionLength;
1883
1884 // Add a new chunk for this macro argument. A previous macro argument chunk
1885 // may have been lexed again, so e.g. if the map is
1886 // 0 -> SourceLocation()
1887 // 100 -> Expanded loc #1
1888 // 110 -> SourceLocation()
1889 // and we found a new macro FileID that lexed from offset 105 with length 3,
1890 // the new map will be:
1891 // 0 -> SourceLocation()
1892 // 100 -> Expanded loc #1
1893 // 105 -> Expanded loc #2
1894 // 108 -> Expanded loc #1
1895 // 110 -> SourceLocation()
1896 //
1897 // Since re-lexed macro chunks will always be the same size or less of
1898 // previous chunks, we only need to find where the ending of the new macro
1899 // chunk is mapped to and update the map with new begin/end mappings.
1900
1901 MacroArgsMap::iterator I = MacroArgsCache.upper_bound(EndOffs);
1902 --I;
1903 SourceLocation EndOffsMappedLoc = I->second;
1904 MacroArgsCache[BeginOffs] = ExpansionLoc;
1905 MacroArgsCache[EndOffs] = EndOffsMappedLoc;
1906 }
1907
1908 /// If \arg Loc points inside a function macro argument, the returned
1909 /// location will be the macro location in which the argument was expanded.
1910 /// If a macro argument is used multiple times, the expanded location will
1911 /// be at the first expansion of the argument.
1912 /// e.g.
1913 /// MY_MACRO(foo);
1914 /// ^
1915 /// Passing a file location pointing at 'foo', will yield a macro location
1916 /// where 'foo' was expanded into.
1917 SourceLocation
getMacroArgExpandedLocation(SourceLocation Loc) const1918 SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) const {
1919 if (Loc.isInvalid() || !Loc.isFileID())
1920 return Loc;
1921
1922 FileID FID;
1923 unsigned Offset;
1924 std::tie(FID, Offset) = getDecomposedLoc(Loc);
1925 if (FID.isInvalid())
1926 return Loc;
1927
1928 std::unique_ptr<MacroArgsMap> &MacroArgsCache = MacroArgsCacheMap[FID];
1929 if (!MacroArgsCache) {
1930 MacroArgsCache = llvm::make_unique<MacroArgsMap>();
1931 computeMacroArgsCache(*MacroArgsCache, FID);
1932 }
1933
1934 assert(!MacroArgsCache->empty());
1935 MacroArgsMap::iterator I = MacroArgsCache->upper_bound(Offset);
1936 --I;
1937
1938 unsigned MacroArgBeginOffs = I->first;
1939 SourceLocation MacroArgExpandedLoc = I->second;
1940 if (MacroArgExpandedLoc.isValid())
1941 return MacroArgExpandedLoc.getLocWithOffset(Offset - MacroArgBeginOffs);
1942
1943 return Loc;
1944 }
1945
1946 std::pair<FileID, unsigned>
getDecomposedIncludedLoc(FileID FID) const1947 SourceManager::getDecomposedIncludedLoc(FileID FID) const {
1948 if (FID.isInvalid())
1949 return std::make_pair(FileID(), 0);
1950
1951 // Uses IncludedLocMap to retrieve/cache the decomposed loc.
1952
1953 using DecompTy = std::pair<FileID, unsigned>;
1954 auto InsertOp = IncludedLocMap.try_emplace(FID);
1955 DecompTy &DecompLoc = InsertOp.first->second;
1956 if (!InsertOp.second)
1957 return DecompLoc; // already in map.
1958
1959 SourceLocation UpperLoc;
1960 bool Invalid = false;
1961 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1962 if (!Invalid) {
1963 if (Entry.isExpansion())
1964 UpperLoc = Entry.getExpansion().getExpansionLocStart();
1965 else
1966 UpperLoc = Entry.getFile().getIncludeLoc();
1967 }
1968
1969 if (UpperLoc.isValid())
1970 DecompLoc = getDecomposedLoc(UpperLoc);
1971
1972 return DecompLoc;
1973 }
1974
1975 /// Given a decomposed source location, move it up the include/expansion stack
1976 /// to the parent source location. If this is possible, return the decomposed
1977 /// version of the parent in Loc and return false. If Loc is the top-level
1978 /// entry, return true and don't modify it.
MoveUpIncludeHierarchy(std::pair<FileID,unsigned> & Loc,const SourceManager & SM)1979 static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc,
1980 const SourceManager &SM) {
1981 std::pair<FileID, unsigned> UpperLoc = SM.getDecomposedIncludedLoc(Loc.first);
1982 if (UpperLoc.first.isInvalid())
1983 return true; // We reached the top.
1984
1985 Loc = UpperLoc;
1986 return false;
1987 }
1988
1989 /// Return the cache entry for comparing the given file IDs
1990 /// for isBeforeInTranslationUnit.
getInBeforeInTUCache(FileID LFID,FileID RFID) const1991 InBeforeInTUCacheEntry &SourceManager::getInBeforeInTUCache(FileID LFID,
1992 FileID RFID) const {
1993 // This is a magic number for limiting the cache size. It was experimentally
1994 // derived from a small Objective-C project (where the cache filled
1995 // out to ~250 items). We can make it larger if necessary.
1996 enum { MagicCacheSize = 300 };
1997 IsBeforeInTUCacheKey Key(LFID, RFID);
1998
1999 // If the cache size isn't too large, do a lookup and if necessary default
2000 // construct an entry. We can then return it to the caller for direct
2001 // use. When they update the value, the cache will get automatically
2002 // updated as well.
2003 if (IBTUCache.size() < MagicCacheSize)
2004 return IBTUCache[Key];
2005
2006 // Otherwise, do a lookup that will not construct a new value.
2007 InBeforeInTUCache::iterator I = IBTUCache.find(Key);
2008 if (I != IBTUCache.end())
2009 return I->second;
2010
2011 // Fall back to the overflow value.
2012 return IBTUCacheOverflow;
2013 }
2014
2015 /// Determines the order of 2 source locations in the translation unit.
2016 ///
2017 /// \returns true if LHS source location comes before RHS, false otherwise.
isBeforeInTranslationUnit(SourceLocation LHS,SourceLocation RHS) const2018 bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
2019 SourceLocation RHS) const {
2020 assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
2021 if (LHS == RHS)
2022 return false;
2023
2024 std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
2025 std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
2026
2027 // getDecomposedLoc may have failed to return a valid FileID because, e.g. it
2028 // is a serialized one referring to a file that was removed after we loaded
2029 // the PCH.
2030 if (LOffs.first.isInvalid() || ROffs.first.isInvalid())
2031 return LOffs.first.isInvalid() && !ROffs.first.isInvalid();
2032
2033 std::pair<bool, bool> InSameTU = isInTheSameTranslationUnit(LOffs, ROffs);
2034 if (InSameTU.first)
2035 return InSameTU.second;
2036
2037 // If we arrived here, the location is either in a built-ins buffer or
2038 // associated with global inline asm. PR5662 and PR22576 are examples.
2039
2040 StringRef LB = getBuffer(LOffs.first)->getBufferIdentifier();
2041 StringRef RB = getBuffer(ROffs.first)->getBufferIdentifier();
2042 bool LIsBuiltins = LB == "<built-in>";
2043 bool RIsBuiltins = RB == "<built-in>";
2044 // Sort built-in before non-built-in.
2045 if (LIsBuiltins || RIsBuiltins) {
2046 if (LIsBuiltins != RIsBuiltins)
2047 return LIsBuiltins;
2048 // Both are in built-in buffers, but from different files. We just claim that
2049 // lower IDs come first.
2050 return LOffs.first < ROffs.first;
2051 }
2052 bool LIsAsm = LB == "<inline asm>";
2053 bool RIsAsm = RB == "<inline asm>";
2054 // Sort assembler after built-ins, but before the rest.
2055 if (LIsAsm || RIsAsm) {
2056 if (LIsAsm != RIsAsm)
2057 return RIsAsm;
2058 assert(LOffs.first == ROffs.first);
2059 return false;
2060 }
2061 bool LIsScratch = LB == "<scratch space>";
2062 bool RIsScratch = RB == "<scratch space>";
2063 // Sort scratch after inline asm, but before the rest.
2064 if (LIsScratch || RIsScratch) {
2065 if (LIsScratch != RIsScratch)
2066 return LIsScratch;
2067 return LOffs.second < ROffs.second;
2068 }
2069 llvm_unreachable("Unsortable locations found");
2070 }
2071
isInTheSameTranslationUnit(std::pair<FileID,unsigned> & LOffs,std::pair<FileID,unsigned> & ROffs) const2072 std::pair<bool, bool> SourceManager::isInTheSameTranslationUnit(
2073 std::pair<FileID, unsigned> &LOffs,
2074 std::pair<FileID, unsigned> &ROffs) const {
2075 // If the source locations are in the same file, just compare offsets.
2076 if (LOffs.first == ROffs.first)
2077 return std::make_pair(true, LOffs.second < ROffs.second);
2078
2079 // If we are comparing a source location with multiple locations in the same
2080 // file, we get a big win by caching the result.
2081 InBeforeInTUCacheEntry &IsBeforeInTUCache =
2082 getInBeforeInTUCache(LOffs.first, ROffs.first);
2083
2084 // If we are comparing a source location with multiple locations in the same
2085 // file, we get a big win by caching the result.
2086 if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first))
2087 return std::make_pair(
2088 true, IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second));
2089
2090 // Okay, we missed in the cache, start updating the cache for this query.
2091 IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first,
2092 /*isLFIDBeforeRFID=*/LOffs.first.ID < ROffs.first.ID);
2093
2094 // We need to find the common ancestor. The only way of doing this is to
2095 // build the complete include chain for one and then walking up the chain
2096 // of the other looking for a match.
2097 // We use a map from FileID to Offset to store the chain. Easier than writing
2098 // a custom set hash info that only depends on the first part of a pair.
2099 using LocSet = llvm::SmallDenseMap<FileID, unsigned, 16>;
2100 LocSet LChain;
2101 do {
2102 LChain.insert(LOffs);
2103 // We catch the case where LOffs is in a file included by ROffs and
2104 // quit early. The other way round unfortunately remains suboptimal.
2105 } while (LOffs.first != ROffs.first && !MoveUpIncludeHierarchy(LOffs, *this));
2106 LocSet::iterator I;
2107 while((I = LChain.find(ROffs.first)) == LChain.end()) {
2108 if (MoveUpIncludeHierarchy(ROffs, *this))
2109 break; // Met at topmost file.
2110 }
2111 if (I != LChain.end())
2112 LOffs = *I;
2113
2114 // If we exited because we found a nearest common ancestor, compare the
2115 // locations within the common file and cache them.
2116 if (LOffs.first == ROffs.first) {
2117 IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second);
2118 return std::make_pair(
2119 true, IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second));
2120 }
2121 // Clear the lookup cache, it depends on a common location.
2122 IsBeforeInTUCache.clear();
2123 return std::make_pair(false, false);
2124 }
2125
PrintStats() const2126 void SourceManager::PrintStats() const {
2127 llvm::errs() << "\n*** Source Manager Stats:\n";
2128 llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
2129 << " mem buffers mapped.\n";
2130 llvm::errs() << LocalSLocEntryTable.size() << " local SLocEntry's allocated ("
2131 << llvm::capacity_in_bytes(LocalSLocEntryTable)
2132 << " bytes of capacity), "
2133 << NextLocalOffset << "B of Sloc address space used.\n";
2134 llvm::errs() << LoadedSLocEntryTable.size()
2135 << " loaded SLocEntries allocated, "
2136 << MaxLoadedOffset - CurrentLoadedOffset
2137 << "B of Sloc address space used.\n";
2138
2139 unsigned NumLineNumsComputed = 0;
2140 unsigned NumFileBytesMapped = 0;
2141 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
2142 NumLineNumsComputed += I->second->SourceLineCache != nullptr;
2143 NumFileBytesMapped += I->second->getSizeBytesMapped();
2144 }
2145 unsigned NumMacroArgsComputed = MacroArgsCacheMap.size();
2146
2147 llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
2148 << NumLineNumsComputed << " files with line #'s computed, "
2149 << NumMacroArgsComputed << " files with macro args computed.\n";
2150 llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
2151 << NumBinaryProbes << " binary.\n";
2152 }
2153
dump() const2154 LLVM_DUMP_METHOD void SourceManager::dump() const {
2155 llvm::raw_ostream &out = llvm::errs();
2156
2157 auto DumpSLocEntry = [&](int ID, const SrcMgr::SLocEntry &Entry,
2158 llvm::Optional<unsigned> NextStart) {
2159 out << "SLocEntry <FileID " << ID << "> " << (Entry.isFile() ? "file" : "expansion")
2160 << " <SourceLocation " << Entry.getOffset() << ":";
2161 if (NextStart)
2162 out << *NextStart << ">\n";
2163 else
2164 out << "???\?>\n";
2165 if (Entry.isFile()) {
2166 auto &FI = Entry.getFile();
2167 if (FI.NumCreatedFIDs)
2168 out << " covers <FileID " << ID << ":" << int(ID + FI.NumCreatedFIDs)
2169 << ">\n";
2170 if (FI.getIncludeLoc().isValid())
2171 out << " included from " << FI.getIncludeLoc().getOffset() << "\n";
2172 if (auto *CC = FI.getContentCache()) {
2173 out << " for " << (CC->OrigEntry ? CC->OrigEntry->getName() : "<none>")
2174 << "\n";
2175 if (CC->BufferOverridden)
2176 out << " contents overridden\n";
2177 if (CC->ContentsEntry != CC->OrigEntry) {
2178 out << " contents from "
2179 << (CC->ContentsEntry ? CC->ContentsEntry->getName() : "<none>")
2180 << "\n";
2181 }
2182 }
2183 } else {
2184 auto &EI = Entry.getExpansion();
2185 out << " spelling from " << EI.getSpellingLoc().getOffset() << "\n";
2186 out << " macro " << (EI.isMacroArgExpansion() ? "arg" : "body")
2187 << " range <" << EI.getExpansionLocStart().getOffset() << ":"
2188 << EI.getExpansionLocEnd().getOffset() << ">\n";
2189 }
2190 };
2191
2192 // Dump local SLocEntries.
2193 for (unsigned ID = 0, NumIDs = LocalSLocEntryTable.size(); ID != NumIDs; ++ID) {
2194 DumpSLocEntry(ID, LocalSLocEntryTable[ID],
2195 ID == NumIDs - 1 ? NextLocalOffset
2196 : LocalSLocEntryTable[ID + 1].getOffset());
2197 }
2198 // Dump loaded SLocEntries.
2199 llvm::Optional<unsigned> NextStart;
2200 for (unsigned Index = 0; Index != LoadedSLocEntryTable.size(); ++Index) {
2201 int ID = -(int)Index - 2;
2202 if (SLocEntryLoaded[Index]) {
2203 DumpSLocEntry(ID, LoadedSLocEntryTable[Index], NextStart);
2204 NextStart = LoadedSLocEntryTable[Index].getOffset();
2205 } else {
2206 NextStart = None;
2207 }
2208 }
2209 }
2210
2211 ExternalSLocEntrySource::~ExternalSLocEntrySource() = default;
2212
2213 /// Return the amount of memory used by memory buffers, breaking down
2214 /// by heap-backed versus mmap'ed memory.
getMemoryBufferSizes() const2215 SourceManager::MemoryBufferSizes SourceManager::getMemoryBufferSizes() const {
2216 size_t malloc_bytes = 0;
2217 size_t mmap_bytes = 0;
2218
2219 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i)
2220 if (size_t sized_mapped = MemBufferInfos[i]->getSizeBytesMapped())
2221 switch (MemBufferInfos[i]->getMemoryBufferKind()) {
2222 case llvm::MemoryBuffer::MemoryBuffer_MMap:
2223 mmap_bytes += sized_mapped;
2224 break;
2225 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
2226 malloc_bytes += sized_mapped;
2227 break;
2228 }
2229
2230 return MemoryBufferSizes(malloc_bytes, mmap_bytes);
2231 }
2232
getDataStructureSizes() const2233 size_t SourceManager::getDataStructureSizes() const {
2234 size_t size = llvm::capacity_in_bytes(MemBufferInfos)
2235 + llvm::capacity_in_bytes(LocalSLocEntryTable)
2236 + llvm::capacity_in_bytes(LoadedSLocEntryTable)
2237 + llvm::capacity_in_bytes(SLocEntryLoaded)
2238 + llvm::capacity_in_bytes(FileInfos);
2239
2240 if (OverriddenFilesInfo)
2241 size += llvm::capacity_in_bytes(OverriddenFilesInfo->OverriddenFiles);
2242
2243 return size;
2244 }
2245
SourceManagerForFile(StringRef FileName,StringRef Content)2246 SourceManagerForFile::SourceManagerForFile(StringRef FileName,
2247 StringRef Content) {
2248 // This is referenced by `FileMgr` and will be released by `FileMgr` when it
2249 // is deleted.
2250 IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
2251 new llvm::vfs::InMemoryFileSystem);
2252 InMemoryFileSystem->addFile(
2253 FileName, 0,
2254 llvm::MemoryBuffer::getMemBuffer(Content, FileName,
2255 /*RequiresNullTerminator=*/false));
2256 // This is passed to `SM` as reference, so the pointer has to be referenced
2257 // in `Environment` so that `FileMgr` can out-live this function scope.
2258 FileMgr =
2259 llvm::make_unique<FileManager>(FileSystemOptions(), InMemoryFileSystem);
2260 // This is passed to `SM` as reference, so the pointer has to be referenced
2261 // by `Environment` due to the same reason above.
2262 Diagnostics = llvm::make_unique<DiagnosticsEngine>(
2263 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
2264 new DiagnosticOptions);
2265 SourceMgr = llvm::make_unique<SourceManager>(*Diagnostics, *FileMgr);
2266 FileID ID = SourceMgr->createFileID(FileMgr->getFile(FileName),
2267 SourceLocation(), clang::SrcMgr::C_User);
2268 assert(ID.isValid());
2269 SourceMgr->setMainFileID(ID);
2270 }
2271