1 //===- SourceManager.cpp - Track and cache source files -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the SourceManager interface.
11 //===----------------------------------------------------------------------===//
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/MapVector.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/Capacity.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/Endian.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"
45 using namespace clang
;
46 using namespace SrcMgr
;
47 using llvm::MemoryBuffer
;
49 //===----------------------------------------------------------------------===//
50 // SourceManager Helper Classes
51 //===----------------------------------------------------------------------===//
53 /// getSizeBytesMapped - Returns the number of bytes actually mapped for this
54 /// ContentCache. This can be 0 if the MemBuffer was not actually expanded.
55 unsigned ContentCache::getSizeBytesMapped() const {
56 return Buffer
? Buffer
->getBufferSize() : 0;
59 /// Returns the kind of memory used to back the memory buffer for
60 /// this content cache. This is used for performance analysis.
61 llvm::MemoryBuffer::BufferKind
ContentCache::getMemoryBufferKind() const {
62 if (Buffer
== nullptr) {
63 assert(0 && "Buffer should never be null");
64 return llvm::MemoryBuffer::MemoryBuffer_Malloc
;
66 return Buffer
->getBufferKind();
69 /// getSize - Returns the size of the content encapsulated by this ContentCache.
70 /// This can be the size of the source file or the size of an arbitrary
71 /// scratch buffer. If the ContentCache encapsulates a source file, that
72 /// file is not lazily brought in from disk to satisfy this query.
73 unsigned ContentCache::getSize() const {
74 return Buffer
? (unsigned)Buffer
->getBufferSize()
75 : (unsigned)ContentsEntry
->getSize();
78 const char *ContentCache::getInvalidBOM(StringRef BufStr
) {
79 // If the buffer is valid, check to see if it has a UTF Byte Order Mark
80 // (BOM). We only support UTF-8 with and without a BOM right now. See
81 // http://en.wikipedia.org/wiki/Byte_order_mark for more information.
82 const char *InvalidBOM
=
83 llvm::StringSwitch
<const char *>(BufStr
)
84 .StartsWith(llvm::StringLiteral::withInnerNUL("\x00\x00\xFE\xFF"),
86 .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"),
88 .StartsWith("\xFE\xFF", "UTF-16 (BE)")
89 .StartsWith("\xFF\xFE", "UTF-16 (LE)")
90 .StartsWith("\x2B\x2F\x76", "UTF-7")
91 .StartsWith("\xF7\x64\x4C", "UTF-1")
92 .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC")
93 .StartsWith("\x0E\xFE\xFF", "SCSU")
94 .StartsWith("\xFB\xEE\x28", "BOCU-1")
95 .StartsWith("\x84\x31\x95\x33", "GB-18030")
101 std::optional
<llvm::MemoryBufferRef
>
102 ContentCache::getBufferOrNone(DiagnosticsEngine
&Diag
, FileManager
&FM
,
103 SourceLocation Loc
) const {
104 // Lazily create the Buffer for ContentCaches that wrap files. If we already
105 // computed it, just return what we have.
109 return Buffer
->getMemBufferRef();
113 // Start with the assumption that the buffer is invalid to simplify early
115 IsBufferInvalid
= true;
117 auto BufferOrError
= FM
.getBufferForFile(*ContentsEntry
, IsFileVolatile
);
119 // If we were unable to open the file, then we are in an inconsistent
120 // situation where the content cache referenced a file which no longer
121 // exists. Most likely, we were using a stat cache with an invalid entry but
122 // the file could also have been removed during processing. Since we can't
123 // really deal with this situation, just create an empty buffer.
124 if (!BufferOrError
) {
125 if (Diag
.isDiagnosticInFlight())
126 Diag
.SetDelayedDiagnostic(diag::err_cannot_open_file
,
127 ContentsEntry
->getName(),
128 BufferOrError
.getError().message());
130 Diag
.Report(Loc
, diag::err_cannot_open_file
)
131 << ContentsEntry
->getName() << BufferOrError
.getError().message();
136 Buffer
= std::move(*BufferOrError
);
138 // Check that the file's size fits in an 'unsigned' (with room for a
139 // past-the-end value). This is deeply regrettable, but various parts of
140 // Clang (including elsewhere in this file!) use 'unsigned' to represent file
141 // offsets, line numbers, string literal lengths, and so on, and fail
142 // miserably on large source files.
144 // Note: ContentsEntry could be a named pipe, in which case
145 // ContentsEntry::getSize() could have the wrong size. Use
146 // MemoryBuffer::getBufferSize() instead.
147 if (Buffer
->getBufferSize() >= std::numeric_limits
<unsigned>::max()) {
148 if (Diag
.isDiagnosticInFlight())
149 Diag
.SetDelayedDiagnostic(diag::err_file_too_large
,
150 ContentsEntry
->getName());
152 Diag
.Report(Loc
, diag::err_file_too_large
)
153 << ContentsEntry
->getName();
158 // Unless this is a named pipe (in which case we can handle a mismatch),
159 // check that the file's size is the same as in the file entry (which may
160 // have come from a stat cache).
161 if (!ContentsEntry
->isNamedPipe() &&
162 Buffer
->getBufferSize() != (size_t)ContentsEntry
->getSize()) {
163 if (Diag
.isDiagnosticInFlight())
164 Diag
.SetDelayedDiagnostic(diag::err_file_modified
,
165 ContentsEntry
->getName());
167 Diag
.Report(Loc
, diag::err_file_modified
)
168 << ContentsEntry
->getName();
173 // If the buffer is valid, check to see if it has a UTF Byte Order Mark
174 // (BOM). We only support UTF-8 with and without a BOM right now. See
175 // http://en.wikipedia.org/wiki/Byte_order_mark for more information.
176 StringRef BufStr
= Buffer
->getBuffer();
177 const char *InvalidBOM
= getInvalidBOM(BufStr
);
180 Diag
.Report(Loc
, diag::err_unsupported_bom
)
181 << InvalidBOM
<< ContentsEntry
->getName();
185 // Buffer has been validated.
186 IsBufferInvalid
= false;
187 return Buffer
->getMemBufferRef();
190 unsigned LineTableInfo::getLineTableFilenameID(StringRef Name
) {
191 auto IterBool
= FilenameIDs
.try_emplace(Name
, FilenamesByID
.size());
193 FilenamesByID
.push_back(&*IterBool
.first
);
194 return IterBool
.first
->second
;
197 /// Add a line note to the line table that indicates that there is a \#line or
198 /// GNU line marker at the specified FID/Offset location which changes the
199 /// presumed location to LineNo/FilenameID. If EntryExit is 0, then this doesn't
200 /// change the presumed \#include stack. If it is 1, this is a file entry, if
201 /// it is 2 then this is a file exit. FileKind specifies whether this is a
202 /// system header or extern C system header.
203 void LineTableInfo::AddLineNote(FileID FID
, unsigned Offset
, unsigned LineNo
,
204 int FilenameID
, unsigned EntryExit
,
205 SrcMgr::CharacteristicKind FileKind
) {
206 std::vector
<LineEntry
> &Entries
= LineEntries
[FID
];
208 assert((Entries
.empty() || Entries
.back().FileOffset
< Offset
) &&
209 "Adding line entries out of order!");
211 unsigned IncludeOffset
= 0;
212 if (EntryExit
== 1) {
214 IncludeOffset
= Offset
-1;
216 const auto *PrevEntry
= Entries
.empty() ? nullptr : &Entries
.back();
217 if (EntryExit
== 2) {
219 assert(PrevEntry
&& PrevEntry
->IncludeOffset
&&
220 "PPDirectives should have caught case when popping empty include "
222 PrevEntry
= FindNearestLineEntry(FID
, PrevEntry
->IncludeOffset
);
225 IncludeOffset
= PrevEntry
->IncludeOffset
;
226 if (FilenameID
== -1) {
227 // An unspecified FilenameID means use the previous (or containing)
228 // filename if available, or the main source file otherwise.
229 FilenameID
= PrevEntry
->FilenameID
;
234 Entries
.push_back(LineEntry::get(Offset
, LineNo
, FilenameID
, FileKind
,
238 /// FindNearestLineEntry - Find the line entry nearest to FID that is before
239 /// it. If there is no line entry before Offset in FID, return null.
240 const LineEntry
*LineTableInfo::FindNearestLineEntry(FileID FID
,
242 const std::vector
<LineEntry
> &Entries
= LineEntries
[FID
];
243 assert(!Entries
.empty() && "No #line entries for this FID after all!");
245 // It is very common for the query to be after the last #line, check this
247 if (Entries
.back().FileOffset
<= Offset
)
248 return &Entries
.back();
250 // Do a binary search to find the maximal element that is still before Offset.
251 std::vector
<LineEntry
>::const_iterator I
= llvm::upper_bound(Entries
, Offset
);
252 if (I
== Entries
.begin())
257 /// Add a new line entry that has already been encoded into
258 /// the internal representation of the line table.
259 void LineTableInfo::AddEntry(FileID FID
,
260 const std::vector
<LineEntry
> &Entries
) {
261 LineEntries
[FID
] = Entries
;
264 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
265 unsigned SourceManager::getLineTableFilenameID(StringRef Name
) {
266 return getLineTable().getLineTableFilenameID(Name
);
269 /// AddLineNote - Add a line note to the line table for the FileID and offset
270 /// specified by Loc. If FilenameID is -1, it is considered to be
272 void SourceManager::AddLineNote(SourceLocation Loc
, unsigned LineNo
,
273 int FilenameID
, bool IsFileEntry
,
275 SrcMgr::CharacteristicKind FileKind
) {
276 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedExpansionLoc(Loc
);
278 bool Invalid
= false;
279 const SLocEntry
&Entry
= getSLocEntry(LocInfo
.first
, &Invalid
);
280 if (!Entry
.isFile() || Invalid
)
283 const SrcMgr::FileInfo
&FileInfo
= Entry
.getFile();
285 // Remember that this file has #line directives now if it doesn't already.
286 const_cast<SrcMgr::FileInfo
&>(FileInfo
).setHasLineDirectives();
288 (void) getLineTable();
290 unsigned EntryExit
= 0;
296 LineTable
->AddLineNote(LocInfo
.first
, LocInfo
.second
, LineNo
, FilenameID
,
297 EntryExit
, FileKind
);
300 LineTableInfo
&SourceManager::getLineTable() {
302 LineTable
.reset(new LineTableInfo());
306 //===----------------------------------------------------------------------===//
307 // Private 'Create' methods.
308 //===----------------------------------------------------------------------===//
310 SourceManager::SourceManager(DiagnosticsEngine
&Diag
, FileManager
&FileMgr
,
311 bool UserFilesAreVolatile
)
312 : Diag(Diag
), FileMgr(FileMgr
), UserFilesAreVolatile(UserFilesAreVolatile
) {
314 Diag
.setSourceManager(this);
317 SourceManager::~SourceManager() {
318 // Delete FileEntry objects corresponding to content caches. Since the actual
319 // content cache objects are bump pointer allocated, we just have to run the
320 // dtors, but we call the deallocate method for completeness.
321 for (unsigned i
= 0, e
= MemBufferInfos
.size(); i
!= e
; ++i
) {
322 if (MemBufferInfos
[i
]) {
323 MemBufferInfos
[i
]->~ContentCache();
324 ContentCacheAlloc
.Deallocate(MemBufferInfos
[i
]);
327 for (auto I
= FileInfos
.begin(), E
= FileInfos
.end(); I
!= E
; ++I
) {
329 I
->second
->~ContentCache();
330 ContentCacheAlloc
.Deallocate(I
->second
);
335 void SourceManager::clearIDTables() {
336 MainFileID
= FileID();
337 LocalSLocEntryTable
.clear();
338 LoadedSLocEntryTable
.clear();
339 SLocEntryLoaded
.clear();
340 SLocEntryOffsetLoaded
.clear();
341 LastLineNoFileIDQuery
= FileID();
342 LastLineNoContentCache
= nullptr;
343 LastFileIDLookup
= FileID();
348 // Use up FileID #0 as an invalid expansion.
350 CurrentLoadedOffset
= MaxLoadedOffset
;
351 createExpansionLoc(SourceLocation(), SourceLocation(), SourceLocation(), 1);
354 bool SourceManager::isMainFile(const FileEntry
&SourceFile
) {
355 assert(MainFileID
.isValid() && "expected initialized SourceManager");
356 if (auto *FE
= getFileEntryForID(MainFileID
))
357 return FE
->getUID() == SourceFile
.getUID();
361 void SourceManager::initializeForReplay(const SourceManager
&Old
) {
362 assert(MainFileID
.isInvalid() && "expected uninitialized SourceManager");
364 auto CloneContentCache
= [&](const ContentCache
*Cache
) -> ContentCache
* {
365 auto *Clone
= new (ContentCacheAlloc
.Allocate
<ContentCache
>()) ContentCache
;
366 Clone
->OrigEntry
= Cache
->OrigEntry
;
367 Clone
->ContentsEntry
= Cache
->ContentsEntry
;
368 Clone
->BufferOverridden
= Cache
->BufferOverridden
;
369 Clone
->IsFileVolatile
= Cache
->IsFileVolatile
;
370 Clone
->IsTransient
= Cache
->IsTransient
;
371 Clone
->setUnownedBuffer(Cache
->getBufferIfLoaded());
375 // Ensure all SLocEntries are loaded from the external source.
376 for (unsigned I
= 0, N
= Old
.LoadedSLocEntryTable
.size(); I
!= N
; ++I
)
377 if (!Old
.SLocEntryLoaded
[I
])
378 Old
.loadSLocEntry(I
, nullptr);
380 // Inherit any content cache data from the old source manager.
381 for (auto &FileInfo
: Old
.FileInfos
) {
382 SrcMgr::ContentCache
*&Slot
= FileInfos
[FileInfo
.first
];
385 Slot
= CloneContentCache(FileInfo
.second
);
389 ContentCache
&SourceManager::getOrCreateContentCache(FileEntryRef FileEnt
,
391 // Do we already have information about this file?
392 ContentCache
*&Entry
= FileInfos
[FileEnt
];
396 // Nope, create a new Cache entry.
397 Entry
= ContentCacheAlloc
.Allocate
<ContentCache
>();
399 if (OverriddenFilesInfo
) {
400 // If the file contents are overridden with contents from another file,
401 // pass that file to ContentCache.
402 auto overI
= OverriddenFilesInfo
->OverriddenFiles
.find(FileEnt
);
403 if (overI
== OverriddenFilesInfo
->OverriddenFiles
.end())
404 new (Entry
) ContentCache(FileEnt
);
406 new (Entry
) ContentCache(OverridenFilesKeepOriginalName
? FileEnt
410 new (Entry
) ContentCache(FileEnt
);
413 Entry
->IsFileVolatile
= UserFilesAreVolatile
&& !isSystemFile
;
414 Entry
->IsTransient
= FilesAreTransient
;
415 Entry
->BufferOverridden
|= FileEnt
.isNamedPipe();
420 /// Create a new ContentCache for the specified memory buffer.
421 /// This does no caching.
422 ContentCache
&SourceManager::createMemBufferContentCache(
423 std::unique_ptr
<llvm::MemoryBuffer
> Buffer
) {
424 // Add a new ContentCache to the MemBufferInfos list and return it.
425 ContentCache
*Entry
= ContentCacheAlloc
.Allocate
<ContentCache
>();
426 new (Entry
) ContentCache();
427 MemBufferInfos
.push_back(Entry
);
428 Entry
->setBuffer(std::move(Buffer
));
432 const SrcMgr::SLocEntry
&SourceManager::loadSLocEntry(unsigned Index
,
433 bool *Invalid
) const {
434 assert(!SLocEntryLoaded
[Index
]);
435 if (ExternalSLocEntries
->ReadSLocEntry(-(static_cast<int>(Index
) + 2))) {
438 // If the file of the SLocEntry changed we could still have loaded it.
439 if (!SLocEntryLoaded
[Index
]) {
440 // Try to recover; create a SLocEntry so the rest of clang can handle it.
441 if (!FakeSLocEntryForRecovery
)
442 FakeSLocEntryForRecovery
= std::make_unique
<SLocEntry
>(SLocEntry::get(
443 0, FileInfo::get(SourceLocation(), getFakeContentCacheForRecovery(),
444 SrcMgr::C_User
, "")));
445 return *FakeSLocEntryForRecovery
;
449 return LoadedSLocEntryTable
[Index
];
452 std::pair
<int, SourceLocation::UIntTy
>
453 SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries
,
454 SourceLocation::UIntTy TotalSize
) {
455 assert(ExternalSLocEntries
&& "Don't have an external sloc source");
456 // Make sure we're not about to run out of source locations.
457 if (CurrentLoadedOffset
< TotalSize
||
458 CurrentLoadedOffset
- TotalSize
< NextLocalOffset
) {
459 return std::make_pair(0, 0);
461 LoadedSLocEntryTable
.resize(LoadedSLocEntryTable
.size() + NumSLocEntries
);
462 SLocEntryLoaded
.resize(LoadedSLocEntryTable
.size());
463 SLocEntryOffsetLoaded
.resize(LoadedSLocEntryTable
.size());
464 CurrentLoadedOffset
-= TotalSize
;
465 int BaseID
= -int(LoadedSLocEntryTable
.size()) - 1;
466 LoadedSLocEntryAllocBegin
.push_back(FileID::get(BaseID
));
467 return std::make_pair(BaseID
, CurrentLoadedOffset
);
470 /// As part of recovering from missing or changed content, produce a
471 /// fake, non-empty buffer.
472 llvm::MemoryBufferRef
SourceManager::getFakeBufferForRecovery() const {
473 if (!FakeBufferForRecovery
)
474 FakeBufferForRecovery
=
475 llvm::MemoryBuffer::getMemBuffer("<<<INVALID BUFFER>>");
477 return *FakeBufferForRecovery
;
480 /// As part of recovering from missing or changed content, produce a
481 /// fake content cache.
482 SrcMgr::ContentCache
&SourceManager::getFakeContentCacheForRecovery() const {
483 if (!FakeContentCacheForRecovery
) {
484 FakeContentCacheForRecovery
= std::make_unique
<SrcMgr::ContentCache
>();
485 FakeContentCacheForRecovery
->setUnownedBuffer(getFakeBufferForRecovery());
487 return *FakeContentCacheForRecovery
;
490 /// Returns the previous in-order FileID or an invalid FileID if there
491 /// is no previous one.
492 FileID
SourceManager::getPreviousFileID(FileID FID
) const {
503 } else if (unsigned(-(ID
-1) - 2) >= LoadedSLocEntryTable
.size()) {
507 return FileID::get(ID
-1);
510 /// Returns the next in-order FileID or an invalid FileID if there is
512 FileID
SourceManager::getNextFileID(FileID FID
) const {
518 if (unsigned(ID
+1) >= local_sloc_entry_size())
520 } else if (ID
+1 >= -1) {
524 return FileID::get(ID
+1);
527 //===----------------------------------------------------------------------===//
528 // Methods to create new FileID's and macro expansions.
529 //===----------------------------------------------------------------------===//
531 /// Create a new FileID that represents the specified file
532 /// being \#included from the specified IncludePosition.
533 FileID
SourceManager::createFileID(FileEntryRef SourceFile
,
534 SourceLocation IncludePos
,
535 SrcMgr::CharacteristicKind FileCharacter
,
537 SourceLocation::UIntTy LoadedOffset
) {
538 SrcMgr::ContentCache
&IR
= getOrCreateContentCache(SourceFile
,
539 isSystem(FileCharacter
));
541 // If this is a named pipe, immediately load the buffer to ensure subsequent
542 // calls to ContentCache::getSize() are accurate.
543 if (IR
.ContentsEntry
->isNamedPipe())
544 (void)IR
.getBufferOrNone(Diag
, getFileManager(), SourceLocation());
546 return createFileIDImpl(IR
, SourceFile
.getName(), IncludePos
, FileCharacter
,
547 LoadedID
, LoadedOffset
);
550 /// Create a new FileID that represents the specified memory buffer.
552 /// This does no caching of the buffer and takes ownership of the
553 /// MemoryBuffer, so only pass a MemoryBuffer to this once.
554 FileID
SourceManager::createFileID(std::unique_ptr
<llvm::MemoryBuffer
> Buffer
,
555 SrcMgr::CharacteristicKind FileCharacter
,
557 SourceLocation::UIntTy LoadedOffset
,
558 SourceLocation IncludeLoc
) {
559 StringRef Name
= Buffer
->getBufferIdentifier();
560 return createFileIDImpl(createMemBufferContentCache(std::move(Buffer
)), Name
,
561 IncludeLoc
, FileCharacter
, LoadedID
, LoadedOffset
);
564 /// Create a new FileID that represents the specified memory buffer.
566 /// This does not take ownership of the MemoryBuffer. The memory buffer must
567 /// outlive the SourceManager.
568 FileID
SourceManager::createFileID(const llvm::MemoryBufferRef
&Buffer
,
569 SrcMgr::CharacteristicKind FileCharacter
,
571 SourceLocation::UIntTy LoadedOffset
,
572 SourceLocation IncludeLoc
) {
573 return createFileID(llvm::MemoryBuffer::getMemBuffer(Buffer
), FileCharacter
,
574 LoadedID
, LoadedOffset
, IncludeLoc
);
577 /// Get the FileID for \p SourceFile if it exists. Otherwise, create a
578 /// new FileID for the \p SourceFile.
580 SourceManager::getOrCreateFileID(FileEntryRef SourceFile
,
581 SrcMgr::CharacteristicKind FileCharacter
) {
582 FileID ID
= translateFile(SourceFile
);
583 return ID
.isValid() ? ID
: createFileID(SourceFile
, SourceLocation(),
587 /// createFileID - Create a new FileID for the specified ContentCache and
588 /// include position. This works regardless of whether the ContentCache
589 /// corresponds to a file or some other input source.
590 FileID
SourceManager::createFileIDImpl(ContentCache
&File
, StringRef Filename
,
591 SourceLocation IncludePos
,
592 SrcMgr::CharacteristicKind FileCharacter
,
594 SourceLocation::UIntTy LoadedOffset
) {
596 assert(LoadedID
!= -1 && "Loading sentinel FileID");
597 unsigned Index
= unsigned(-LoadedID
) - 2;
598 assert(Index
< LoadedSLocEntryTable
.size() && "FileID out of range");
599 assert(!SLocEntryLoaded
[Index
] && "FileID already loaded");
600 LoadedSLocEntryTable
[Index
] = SLocEntry::get(
601 LoadedOffset
, FileInfo::get(IncludePos
, File
, FileCharacter
, Filename
));
602 SLocEntryLoaded
[Index
] = SLocEntryOffsetLoaded
[Index
] = true;
603 return FileID::get(LoadedID
);
605 unsigned FileSize
= File
.getSize();
606 if (!(NextLocalOffset
+ FileSize
+ 1 > NextLocalOffset
&&
607 NextLocalOffset
+ FileSize
+ 1 <= CurrentLoadedOffset
)) {
608 Diag
.Report(IncludePos
, diag::err_sloc_space_too_large
);
609 noteSLocAddressSpaceUsage(Diag
);
612 LocalSLocEntryTable
.push_back(
613 SLocEntry::get(NextLocalOffset
,
614 FileInfo::get(IncludePos
, File
, FileCharacter
, Filename
)));
615 // We do a +1 here because we want a SourceLocation that means "the end of the
616 // file", e.g. for the "no newline at the end of the file" diagnostic.
617 NextLocalOffset
+= FileSize
+ 1;
619 // Set LastFileIDLookup to the newly created file. The next getFileID call is
620 // almost guaranteed to be from that file.
621 FileID FID
= FileID::get(LocalSLocEntryTable
.size()-1);
622 return LastFileIDLookup
= FID
;
625 SourceLocation
SourceManager::createMacroArgExpansionLoc(
626 SourceLocation SpellingLoc
, SourceLocation ExpansionLoc
, unsigned Length
) {
627 ExpansionInfo Info
= ExpansionInfo::createForMacroArg(SpellingLoc
,
629 return createExpansionLocImpl(Info
, Length
);
632 SourceLocation
SourceManager::createExpansionLoc(
633 SourceLocation SpellingLoc
, SourceLocation ExpansionLocStart
,
634 SourceLocation ExpansionLocEnd
, unsigned Length
,
635 bool ExpansionIsTokenRange
, int LoadedID
,
636 SourceLocation::UIntTy LoadedOffset
) {
637 ExpansionInfo Info
= ExpansionInfo::create(
638 SpellingLoc
, ExpansionLocStart
, ExpansionLocEnd
, ExpansionIsTokenRange
);
639 return createExpansionLocImpl(Info
, Length
, LoadedID
, LoadedOffset
);
642 SourceLocation
SourceManager::createTokenSplitLoc(SourceLocation Spelling
,
643 SourceLocation TokenStart
,
644 SourceLocation TokenEnd
) {
645 assert(getFileID(TokenStart
) == getFileID(TokenEnd
) &&
646 "token spans multiple files");
647 return createExpansionLocImpl(
648 ExpansionInfo::createForTokenSplit(Spelling
, TokenStart
, TokenEnd
),
649 TokenEnd
.getOffset() - TokenStart
.getOffset());
653 SourceManager::createExpansionLocImpl(const ExpansionInfo
&Info
,
654 unsigned Length
, int LoadedID
,
655 SourceLocation::UIntTy LoadedOffset
) {
657 assert(LoadedID
!= -1 && "Loading sentinel FileID");
658 unsigned Index
= unsigned(-LoadedID
) - 2;
659 assert(Index
< LoadedSLocEntryTable
.size() && "FileID out of range");
660 assert(!SLocEntryLoaded
[Index
] && "FileID already loaded");
661 LoadedSLocEntryTable
[Index
] = SLocEntry::get(LoadedOffset
, Info
);
662 SLocEntryLoaded
[Index
] = SLocEntryOffsetLoaded
[Index
] = true;
663 return SourceLocation::getMacroLoc(LoadedOffset
);
665 LocalSLocEntryTable
.push_back(SLocEntry::get(NextLocalOffset
, Info
));
666 if (NextLocalOffset
+ Length
+ 1 <= NextLocalOffset
||
667 NextLocalOffset
+ Length
+ 1 > CurrentLoadedOffset
) {
668 Diag
.Report(SourceLocation(), diag::err_sloc_space_too_large
);
669 // FIXME: call `noteSLocAddressSpaceUsage` to report details to users and
670 // use a source location from `Info` to point at an error.
671 // Currently, both cause Clang to run indefinitely, this needs to be fixed.
672 // FIXME: return an error instead of crashing. Returning invalid source
673 // locations causes compiler to run indefinitely.
674 llvm::report_fatal_error("ran out of source locations");
676 // See createFileID for that +1.
677 NextLocalOffset
+= Length
+ 1;
678 return SourceLocation::getMacroLoc(NextLocalOffset
- (Length
+ 1));
681 std::optional
<llvm::MemoryBufferRef
>
682 SourceManager::getMemoryBufferForFileOrNone(FileEntryRef File
) {
683 SrcMgr::ContentCache
&IR
= getOrCreateContentCache(File
);
684 return IR
.getBufferOrNone(Diag
, getFileManager(), SourceLocation());
687 void SourceManager::overrideFileContents(
688 FileEntryRef SourceFile
, std::unique_ptr
<llvm::MemoryBuffer
> Buffer
) {
689 SrcMgr::ContentCache
&IR
= getOrCreateContentCache(SourceFile
);
691 IR
.setBuffer(std::move(Buffer
));
692 IR
.BufferOverridden
= true;
694 getOverriddenFilesInfo().OverriddenFilesWithBuffer
.insert(SourceFile
);
697 void SourceManager::overrideFileContents(const FileEntry
*SourceFile
,
698 FileEntryRef NewFile
) {
699 assert(SourceFile
->getSize() == NewFile
.getSize() &&
700 "Different sizes, use the FileManager to create a virtual file with "
702 assert(FileInfos
.find_as(SourceFile
) == FileInfos
.end() &&
703 "This function should be called at the initialization stage, before "
704 "any parsing occurs.");
705 // FileEntryRef is not default-constructible.
706 auto Pair
= getOverriddenFilesInfo().OverriddenFiles
.insert(
707 std::make_pair(SourceFile
, NewFile
));
709 Pair
.first
->second
= NewFile
;
713 SourceManager::bypassFileContentsOverride(FileEntryRef File
) {
714 assert(isFileOverridden(&File
.getFileEntry()));
715 OptionalFileEntryRef BypassFile
= FileMgr
.getBypassFile(File
);
717 // If the file can't be found in the FS, give up.
721 (void)getOrCreateContentCache(*BypassFile
);
725 void SourceManager::setFileIsTransient(FileEntryRef File
) {
726 getOrCreateContentCache(File
).IsTransient
= true;
729 std::optional
<StringRef
>
730 SourceManager::getNonBuiltinFilenameForID(FileID FID
) const {
731 if (const SrcMgr::SLocEntry
*Entry
= getSLocEntryForFile(FID
))
732 if (Entry
->getFile().getContentCache().OrigEntry
)
733 return Entry
->getFile().getName();
737 StringRef
SourceManager::getBufferData(FileID FID
, bool *Invalid
) const {
738 auto B
= getBufferDataOrNone(FID
);
741 return B
? *B
: "<<<<<INVALID SOURCE LOCATION>>>>>";
744 std::optional
<StringRef
>
745 SourceManager::getBufferDataIfLoaded(FileID FID
) const {
746 if (const SrcMgr::SLocEntry
*Entry
= getSLocEntryForFile(FID
))
747 return Entry
->getFile().getContentCache().getBufferDataIfLoaded();
751 std::optional
<StringRef
> SourceManager::getBufferDataOrNone(FileID FID
) const {
752 if (const SrcMgr::SLocEntry
*Entry
= getSLocEntryForFile(FID
))
753 if (auto B
= Entry
->getFile().getContentCache().getBufferOrNone(
754 Diag
, getFileManager(), SourceLocation()))
755 return B
->getBuffer();
759 //===----------------------------------------------------------------------===//
760 // SourceLocation manipulation methods.
761 //===----------------------------------------------------------------------===//
763 /// Return the FileID for a SourceLocation.
765 /// This is the cache-miss path of getFileID. Not as hot as that function, but
766 /// still very important. It is responsible for finding the entry in the
767 /// SLocEntry tables that contains the specified location.
768 FileID
SourceManager::getFileIDSlow(SourceLocation::UIntTy SLocOffset
) const {
770 return FileID::get(0);
772 // Now it is time to search for the correct file. See where the SLocOffset
773 // sits in the global view and consult local or loaded buffers for it.
774 if (SLocOffset
< NextLocalOffset
)
775 return getFileIDLocal(SLocOffset
);
776 return getFileIDLoaded(SLocOffset
);
779 /// Return the FileID for a SourceLocation with a low offset.
781 /// This function knows that the SourceLocation is in a local buffer, not a
783 FileID
SourceManager::getFileIDLocal(SourceLocation::UIntTy SLocOffset
) const {
784 assert(SLocOffset
< NextLocalOffset
&& "Bad function choice");
786 // After the first and second level caches, I see two common sorts of
787 // behavior: 1) a lot of searched FileID's are "near" the cached file
788 // location or are "near" the cached expansion location. 2) others are just
789 // completely random and may be a very long way away.
791 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
792 // then we fall back to a less cache efficient, but more scalable, binary
793 // search to find the location.
795 // See if this is near the file point - worst case we start scanning from the
796 // most newly created FileID.
798 // LessIndex - This is the lower bound of the range that we're searching.
799 // We know that the offset corresponding to the FileID is less than
801 unsigned LessIndex
= 0;
802 // upper bound of the search range.
803 unsigned GreaterIndex
= LocalSLocEntryTable
.size();
804 if (LastFileIDLookup
.ID
>= 0) {
805 // Use the LastFileIDLookup to prune the search space.
806 if (LocalSLocEntryTable
[LastFileIDLookup
.ID
].getOffset() < SLocOffset
)
807 LessIndex
= LastFileIDLookup
.ID
;
809 GreaterIndex
= LastFileIDLookup
.ID
;
812 // Find the FileID that contains this.
813 unsigned NumProbes
= 0;
816 assert(GreaterIndex
< LocalSLocEntryTable
.size());
817 if (LocalSLocEntryTable
[GreaterIndex
].getOffset() <= SLocOffset
) {
818 FileID Res
= FileID::get(int(GreaterIndex
));
819 // Remember it. We have good locality across FileID lookups.
820 LastFileIDLookup
= Res
;
821 NumLinearScans
+= NumProbes
+1;
824 if (++NumProbes
== 8)
830 unsigned MiddleIndex
= (GreaterIndex
-LessIndex
)/2+LessIndex
;
831 SourceLocation::UIntTy MidOffset
=
832 getLocalSLocEntry(MiddleIndex
).getOffset();
836 // If the offset of the midpoint is too large, chop the high side of the
837 // range to the midpoint.
838 if (MidOffset
> SLocOffset
) {
839 GreaterIndex
= MiddleIndex
;
843 // If the middle index contains the value, succeed and return.
844 if (MiddleIndex
+ 1 == LocalSLocEntryTable
.size() ||
845 SLocOffset
< getLocalSLocEntry(MiddleIndex
+ 1).getOffset()) {
846 FileID Res
= FileID::get(MiddleIndex
);
848 // Remember it. We have good locality across FileID lookups.
849 LastFileIDLookup
= Res
;
850 NumBinaryProbes
+= NumProbes
;
854 // Otherwise, move the low-side up to the middle index.
855 LessIndex
= MiddleIndex
;
859 /// Return the FileID for a SourceLocation with a high offset.
861 /// This function knows that the SourceLocation is in a loaded buffer, not a
863 FileID
SourceManager::getFileIDLoaded(SourceLocation::UIntTy SLocOffset
) const {
864 if (SLocOffset
< CurrentLoadedOffset
) {
865 assert(0 && "Invalid SLocOffset or bad function choice");
869 return FileID::get(ExternalSLocEntries
->getSLocEntryID(SLocOffset
));
872 SourceLocation
SourceManager::
873 getExpansionLocSlowCase(SourceLocation Loc
) const {
875 // Note: If Loc indicates an offset into a token that came from a macro
876 // expansion (e.g. the 5th character of the token) we do not want to add
877 // this offset when going to the expansion location. The expansion
878 // location is the macro invocation, which the offset has nothing to do
879 // with. This is unlike when we get the spelling loc, because the offset
880 // directly correspond to the token whose spelling we're inspecting.
881 Loc
= getSLocEntry(getFileID(Loc
)).getExpansion().getExpansionLocStart();
882 } while (!Loc
.isFileID());
887 SourceLocation
SourceManager::getSpellingLocSlowCase(SourceLocation Loc
) const {
889 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedLoc(Loc
);
890 Loc
= getSLocEntry(LocInfo
.first
).getExpansion().getSpellingLoc();
891 Loc
= Loc
.getLocWithOffset(LocInfo
.second
);
892 } while (!Loc
.isFileID());
896 SourceLocation
SourceManager::getFileLocSlowCase(SourceLocation Loc
) const {
898 if (isMacroArgExpansion(Loc
))
899 Loc
= getImmediateSpellingLoc(Loc
);
901 Loc
= getImmediateExpansionRange(Loc
).getBegin();
902 } while (!Loc
.isFileID());
907 std::pair
<FileID
, unsigned>
908 SourceManager::getDecomposedExpansionLocSlowCase(
909 const SrcMgr::SLocEntry
*E
) const {
910 // If this is an expansion record, walk through all the expansion points.
915 Loc
= E
->getExpansion().getExpansionLocStart();
917 FID
= getFileID(Loc
);
918 E
= &getSLocEntry(FID
);
919 Offset
= Loc
.getOffset()-E
->getOffset();
920 } while (!Loc
.isFileID());
922 return std::make_pair(FID
, Offset
);
925 std::pair
<FileID
, unsigned>
926 SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry
*E
,
927 unsigned Offset
) const {
928 // If this is an expansion record, walk through all the expansion points.
932 Loc
= E
->getExpansion().getSpellingLoc();
933 Loc
= Loc
.getLocWithOffset(Offset
);
935 FID
= getFileID(Loc
);
936 E
= &getSLocEntry(FID
);
937 Offset
= Loc
.getOffset()-E
->getOffset();
938 } while (!Loc
.isFileID());
940 return std::make_pair(FID
, Offset
);
943 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
944 /// spelling location referenced by the ID. This is the first level down
945 /// towards the place where the characters that make up the lexed token can be
946 /// found. This should not generally be used by clients.
947 SourceLocation
SourceManager::getImmediateSpellingLoc(SourceLocation Loc
) const{
948 if (Loc
.isFileID()) return Loc
;
949 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedLoc(Loc
);
950 Loc
= getSLocEntry(LocInfo
.first
).getExpansion().getSpellingLoc();
951 return Loc
.getLocWithOffset(LocInfo
.second
);
954 /// Return the filename of the file containing a SourceLocation.
955 StringRef
SourceManager::getFilename(SourceLocation SpellingLoc
) const {
956 if (OptionalFileEntryRef F
= getFileEntryRefForID(getFileID(SpellingLoc
)))
961 /// getImmediateExpansionRange - Loc is required to be an expansion location.
962 /// Return the start/end of the expansion information.
964 SourceManager::getImmediateExpansionRange(SourceLocation Loc
) const {
965 assert(Loc
.isMacroID() && "Not a macro expansion loc!");
966 const ExpansionInfo
&Expansion
= getSLocEntry(getFileID(Loc
)).getExpansion();
967 return Expansion
.getExpansionLocRange();
970 SourceLocation
SourceManager::getTopMacroCallerLoc(SourceLocation Loc
) const {
971 while (isMacroArgExpansion(Loc
))
972 Loc
= getImmediateSpellingLoc(Loc
);
976 /// getExpansionRange - Given a SourceLocation object, return the range of
977 /// tokens covered by the expansion in the ultimate file.
978 CharSourceRange
SourceManager::getExpansionRange(SourceLocation Loc
) const {
980 return CharSourceRange(SourceRange(Loc
, Loc
), true);
982 CharSourceRange Res
= getImmediateExpansionRange(Loc
);
984 // Fully resolve the start and end locations to their ultimate expansion
986 while (!Res
.getBegin().isFileID())
987 Res
.setBegin(getImmediateExpansionRange(Res
.getBegin()).getBegin());
988 while (!Res
.getEnd().isFileID()) {
989 CharSourceRange EndRange
= getImmediateExpansionRange(Res
.getEnd());
990 Res
.setEnd(EndRange
.getEnd());
991 Res
.setTokenRange(EndRange
.isTokenRange());
996 bool SourceManager::isMacroArgExpansion(SourceLocation Loc
,
997 SourceLocation
*StartLoc
) const {
998 if (!Loc
.isMacroID()) return false;
1000 FileID FID
= getFileID(Loc
);
1001 const SrcMgr::ExpansionInfo
&Expansion
= getSLocEntry(FID
).getExpansion();
1002 if (!Expansion
.isMacroArgExpansion()) return false;
1005 *StartLoc
= Expansion
.getExpansionLocStart();
1009 bool SourceManager::isMacroBodyExpansion(SourceLocation Loc
) const {
1010 if (!Loc
.isMacroID()) return false;
1012 FileID FID
= getFileID(Loc
);
1013 const SrcMgr::ExpansionInfo
&Expansion
= getSLocEntry(FID
).getExpansion();
1014 return Expansion
.isMacroBodyExpansion();
1017 bool SourceManager::isAtStartOfImmediateMacroExpansion(SourceLocation Loc
,
1018 SourceLocation
*MacroBegin
) const {
1019 assert(Loc
.isValid() && Loc
.isMacroID() && "Expected a valid macro loc");
1021 std::pair
<FileID
, unsigned> DecompLoc
= getDecomposedLoc(Loc
);
1022 if (DecompLoc
.second
> 0)
1023 return false; // Does not point at the start of expansion range.
1025 bool Invalid
= false;
1026 const SrcMgr::ExpansionInfo
&ExpInfo
=
1027 getSLocEntry(DecompLoc
.first
, &Invalid
).getExpansion();
1030 SourceLocation ExpLoc
= ExpInfo
.getExpansionLocStart();
1032 if (ExpInfo
.isMacroArgExpansion()) {
1033 // For macro argument expansions, check if the previous FileID is part of
1034 // the same argument expansion, in which case this Loc is not at the
1035 // beginning of the expansion.
1036 FileID PrevFID
= getPreviousFileID(DecompLoc
.first
);
1037 if (!PrevFID
.isInvalid()) {
1038 const SrcMgr::SLocEntry
&PrevEntry
= getSLocEntry(PrevFID
, &Invalid
);
1041 if (PrevEntry
.isExpansion() &&
1042 PrevEntry
.getExpansion().getExpansionLocStart() == ExpLoc
)
1048 *MacroBegin
= ExpLoc
;
1052 bool SourceManager::isAtEndOfImmediateMacroExpansion(SourceLocation Loc
,
1053 SourceLocation
*MacroEnd
) const {
1054 assert(Loc
.isValid() && Loc
.isMacroID() && "Expected a valid macro loc");
1056 FileID FID
= getFileID(Loc
);
1057 SourceLocation NextLoc
= Loc
.getLocWithOffset(1);
1058 if (isInFileID(NextLoc
, FID
))
1059 return false; // Does not point at the end of expansion range.
1061 bool Invalid
= false;
1062 const SrcMgr::ExpansionInfo
&ExpInfo
=
1063 getSLocEntry(FID
, &Invalid
).getExpansion();
1067 if (ExpInfo
.isMacroArgExpansion()) {
1068 // For macro argument expansions, check if the next FileID is part of the
1069 // same argument expansion, in which case this Loc is not at the end of the
1071 FileID NextFID
= getNextFileID(FID
);
1072 if (!NextFID
.isInvalid()) {
1073 const SrcMgr::SLocEntry
&NextEntry
= getSLocEntry(NextFID
, &Invalid
);
1076 if (NextEntry
.isExpansion() &&
1077 NextEntry
.getExpansion().getExpansionLocStart() ==
1078 ExpInfo
.getExpansionLocStart())
1084 *MacroEnd
= ExpInfo
.getExpansionLocEnd();
1088 //===----------------------------------------------------------------------===//
1089 // Queries about the code at a SourceLocation.
1090 //===----------------------------------------------------------------------===//
1092 /// getCharacterData - Return a pointer to the start of the specified location
1093 /// in the appropriate MemoryBuffer.
1094 const char *SourceManager::getCharacterData(SourceLocation SL
,
1095 bool *Invalid
) const {
1096 // Note that this is a hot function in the getSpelling() path, which is
1097 // heavily used by -E mode.
1098 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedSpellingLoc(SL
);
1100 // Note that calling 'getBuffer()' may lazily page in a source file.
1101 bool CharDataInvalid
= false;
1102 const SLocEntry
&Entry
= getSLocEntry(LocInfo
.first
, &CharDataInvalid
);
1103 if (CharDataInvalid
|| !Entry
.isFile()) {
1107 return "<<<<INVALID BUFFER>>>>";
1109 std::optional
<llvm::MemoryBufferRef
> Buffer
=
1110 Entry
.getFile().getContentCache().getBufferOrNone(Diag
, getFileManager(),
1114 return Buffer
? Buffer
->getBufferStart() + LocInfo
.second
1115 : "<<<<INVALID BUFFER>>>>";
1118 /// getColumnNumber - Return the column # for the specified file position.
1119 /// this is significantly cheaper to compute than the line number.
1120 unsigned SourceManager::getColumnNumber(FileID FID
, unsigned FilePos
,
1121 bool *Invalid
) const {
1122 std::optional
<llvm::MemoryBufferRef
> MemBuf
= getBufferOrNone(FID
);
1129 // It is okay to request a position just past the end of the buffer.
1130 if (FilePos
> MemBuf
->getBufferSize()) {
1136 const char *Buf
= MemBuf
->getBufferStart();
1137 // See if we just calculated the line number for this FilePos and can use
1138 // that to lookup the start of the line instead of searching for it.
1139 if (LastLineNoFileIDQuery
== FID
&& LastLineNoContentCache
->SourceLineCache
&&
1140 LastLineNoResult
< LastLineNoContentCache
->SourceLineCache
.size()) {
1141 const unsigned *SourceLineCache
=
1142 LastLineNoContentCache
->SourceLineCache
.begin();
1143 unsigned LineStart
= SourceLineCache
[LastLineNoResult
- 1];
1144 unsigned LineEnd
= SourceLineCache
[LastLineNoResult
];
1145 if (FilePos
>= LineStart
&& FilePos
< LineEnd
) {
1146 // LineEnd is the LineStart of the next line.
1147 // A line ends with separator LF or CR+LF on Windows.
1148 // FilePos might point to the last separator,
1149 // but we need a column number at most 1 + the last column.
1150 if (FilePos
+ 1 == LineEnd
&& FilePos
> LineStart
) {
1151 if (Buf
[FilePos
- 1] == '\r' || Buf
[FilePos
- 1] == '\n')
1154 return FilePos
- LineStart
+ 1;
1158 unsigned LineStart
= FilePos
;
1159 while (LineStart
&& Buf
[LineStart
-1] != '\n' && Buf
[LineStart
-1] != '\r')
1161 return FilePos
-LineStart
+1;
1164 // isInvalid - Return the result of calling loc.isInvalid(), and
1165 // if Invalid is not null, set its value to same.
1166 template<typename LocType
>
1167 static bool isInvalid(LocType Loc
, bool *Invalid
) {
1168 bool MyInvalid
= Loc
.isInvalid();
1170 *Invalid
= MyInvalid
;
1174 unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc
,
1175 bool *Invalid
) const {
1176 if (isInvalid(Loc
, Invalid
)) return 0;
1177 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedSpellingLoc(Loc
);
1178 return getColumnNumber(LocInfo
.first
, LocInfo
.second
, Invalid
);
1181 unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc
,
1182 bool *Invalid
) const {
1183 if (isInvalid(Loc
, Invalid
)) return 0;
1184 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedExpansionLoc(Loc
);
1185 return getColumnNumber(LocInfo
.first
, LocInfo
.second
, Invalid
);
1188 unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc
,
1189 bool *Invalid
) const {
1190 PresumedLoc PLoc
= getPresumedLoc(Loc
);
1191 if (isInvalid(PLoc
, Invalid
)) return 0;
1192 return PLoc
.getColumn();
1195 // Check if mutli-byte word x has bytes between m and n, included. This may also
1196 // catch bytes equal to n + 1.
1197 // The returned value holds a 0x80 at each byte position that holds a match.
1198 // see http://graphics.stanford.edu/~seander/bithacks.html#HasBetweenInWord
1200 static constexpr inline T
likelyhasbetween(T x
, unsigned char m
,
1202 return ((x
- ~static_cast<T
>(0) / 255 * (n
+ 1)) & ~x
&
1203 ((x
& ~static_cast<T
>(0) / 255 * 127) +
1204 (~static_cast<T
>(0) / 255 * (127 - (m
- 1))))) &
1205 ~static_cast<T
>(0) / 255 * 128;
1208 LineOffsetMapping
LineOffsetMapping::get(llvm::MemoryBufferRef Buffer
,
1209 llvm::BumpPtrAllocator
&Alloc
) {
1211 // Find the file offsets of all of the *physical* source lines. This does
1212 // not look at trigraphs, escaped newlines, or anything else tricky.
1213 SmallVector
<unsigned, 256> LineOffsets
;
1215 // Line #1 starts at char 0.
1216 LineOffsets
.push_back(0);
1218 const unsigned char *Start
= (const unsigned char *)Buffer
.getBufferStart();
1219 const unsigned char *End
= (const unsigned char *)Buffer
.getBufferEnd();
1220 const unsigned char *Buf
= Start
;
1224 // scan sizeof(Word) bytes at a time for new lines.
1225 // This is much faster than scanning each byte independently.
1226 if ((unsigned long)(End
- Start
) > sizeof(Word
)) {
1228 Word
= llvm::support::endian::read64(Buf
, llvm::endianness::little
);
1229 // no new line => jump over sizeof(Word) bytes.
1230 auto Mask
= likelyhasbetween(Word
, '\n', '\r');
1232 Buf
+= sizeof(Word
);
1236 // At that point, Mask contains 0x80 set at each byte that holds a value
1239 // Scan for the next newline - it's very likely there's one.
1240 unsigned N
= llvm::countr_zero(Mask
) - 7; // -7 because 0x80 is the marker
1243 unsigned char Byte
= Word
;
1246 // If this is \r\n, skip both characters.
1252 LineOffsets
.push_back(Buf
- Start
);
1254 } while (Buf
< End
- sizeof(Word
) - 1);
1257 // Handle tail using a regular check.
1260 LineOffsets
.push_back(Buf
- Start
+ 1);
1261 } else if (*Buf
== '\r') {
1262 // If this is \r\n, skip both characters.
1263 if (Buf
+ 1 < End
&& Buf
[1] == '\n') {
1266 LineOffsets
.push_back(Buf
- Start
+ 1);
1271 return LineOffsetMapping(LineOffsets
, Alloc
);
1274 LineOffsetMapping::LineOffsetMapping(ArrayRef
<unsigned> LineOffsets
,
1275 llvm::BumpPtrAllocator
&Alloc
)
1276 : Storage(Alloc
.Allocate
<unsigned>(LineOffsets
.size() + 1)) {
1277 Storage
[0] = LineOffsets
.size();
1278 std::copy(LineOffsets
.begin(), LineOffsets
.end(), Storage
+ 1);
1281 /// getLineNumber - Given a SourceLocation, return the spelling line number
1282 /// for the position indicated. This requires building and caching a table of
1283 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
1284 /// about to emit a diagnostic.
1285 unsigned SourceManager::getLineNumber(FileID FID
, unsigned FilePos
,
1286 bool *Invalid
) const {
1287 if (FID
.isInvalid()) {
1293 const ContentCache
*Content
;
1294 if (LastLineNoFileIDQuery
== FID
)
1295 Content
= LastLineNoContentCache
;
1297 bool MyInvalid
= false;
1298 const SLocEntry
&Entry
= getSLocEntry(FID
, &MyInvalid
);
1299 if (MyInvalid
|| !Entry
.isFile()) {
1305 Content
= &Entry
.getFile().getContentCache();
1308 // If this is the first use of line information for this buffer, compute the
1309 // SourceLineCache for it on demand.
1310 if (!Content
->SourceLineCache
) {
1311 std::optional
<llvm::MemoryBufferRef
> Buffer
=
1312 Content
->getBufferOrNone(Diag
, getFileManager(), SourceLocation());
1318 Content
->SourceLineCache
=
1319 LineOffsetMapping::get(*Buffer
, ContentCacheAlloc
);
1323 // Okay, we know we have a line number table. Do a binary search to find the
1324 // line number that this character position lands on.
1325 const unsigned *SourceLineCache
= Content
->SourceLineCache
.begin();
1326 const unsigned *SourceLineCacheStart
= SourceLineCache
;
1327 const unsigned *SourceLineCacheEnd
= Content
->SourceLineCache
.end();
1329 unsigned QueriedFilePos
= FilePos
+1;
1331 // FIXME: I would like to be convinced that this code is worth being as
1332 // complicated as it is, binary search isn't that slow.
1334 // If it is worth being optimized, then in my opinion it could be more
1335 // performant, simpler, and more obviously correct by just "galloping" outward
1336 // from the queried file position. In fact, this could be incorporated into a
1337 // generic algorithm such as lower_bound_with_hint.
1339 // If someone gives me a test case where this matters, and I will do it! - DWD
1341 // If the previous query was to the same file, we know both the file pos from
1342 // that query and the line number returned. This allows us to narrow the
1343 // search space from the entire file to something near the match.
1344 if (LastLineNoFileIDQuery
== FID
) {
1345 if (QueriedFilePos
>= LastLineNoFilePos
) {
1346 // FIXME: Potential overflow?
1347 SourceLineCache
= SourceLineCache
+LastLineNoResult
-1;
1349 // The query is likely to be nearby the previous one. Here we check to
1350 // see if it is within 5, 10 or 20 lines. It can be far away in cases
1351 // where big comment blocks and vertical whitespace eat up lines but
1352 // contribute no tokens.
1353 if (SourceLineCache
+5 < SourceLineCacheEnd
) {
1354 if (SourceLineCache
[5] > QueriedFilePos
)
1355 SourceLineCacheEnd
= SourceLineCache
+5;
1356 else if (SourceLineCache
+10 < SourceLineCacheEnd
) {
1357 if (SourceLineCache
[10] > QueriedFilePos
)
1358 SourceLineCacheEnd
= SourceLineCache
+10;
1359 else if (SourceLineCache
+20 < SourceLineCacheEnd
) {
1360 if (SourceLineCache
[20] > QueriedFilePos
)
1361 SourceLineCacheEnd
= SourceLineCache
+20;
1366 if (LastLineNoResult
< Content
->SourceLineCache
.size())
1367 SourceLineCacheEnd
= SourceLineCache
+LastLineNoResult
+1;
1371 const unsigned *Pos
=
1372 std::lower_bound(SourceLineCache
, SourceLineCacheEnd
, QueriedFilePos
);
1373 unsigned LineNo
= Pos
-SourceLineCacheStart
;
1375 LastLineNoFileIDQuery
= FID
;
1376 LastLineNoContentCache
= Content
;
1377 LastLineNoFilePos
= QueriedFilePos
;
1378 LastLineNoResult
= LineNo
;
1382 unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc
,
1383 bool *Invalid
) const {
1384 if (isInvalid(Loc
, Invalid
)) return 0;
1385 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedSpellingLoc(Loc
);
1386 return getLineNumber(LocInfo
.first
, LocInfo
.second
);
1388 unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc
,
1389 bool *Invalid
) const {
1390 if (isInvalid(Loc
, Invalid
)) return 0;
1391 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedExpansionLoc(Loc
);
1392 return getLineNumber(LocInfo
.first
, LocInfo
.second
);
1394 unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc
,
1395 bool *Invalid
) const {
1396 PresumedLoc PLoc
= getPresumedLoc(Loc
);
1397 if (isInvalid(PLoc
, Invalid
)) return 0;
1398 return PLoc
.getLine();
1401 /// getFileCharacteristic - return the file characteristic of the specified
1402 /// source location, indicating whether this is a normal file, a system
1403 /// header, or an "implicit extern C" system header.
1405 /// This state can be modified with flags on GNU linemarker directives like:
1407 /// which changes all source locations in the current file after that to be
1408 /// considered to be from a system header.
1409 SrcMgr::CharacteristicKind
1410 SourceManager::getFileCharacteristic(SourceLocation Loc
) const {
1411 assert(Loc
.isValid() && "Can't get file characteristic of invalid loc!");
1412 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedExpansionLoc(Loc
);
1413 const SLocEntry
*SEntry
= getSLocEntryForFile(LocInfo
.first
);
1417 const SrcMgr::FileInfo
&FI
= SEntry
->getFile();
1419 // If there are no #line directives in this file, just return the whole-file
1421 if (!FI
.hasLineDirectives())
1422 return FI
.getFileCharacteristic();
1424 assert(LineTable
&& "Can't have linetable entries without a LineTable!");
1425 // See if there is a #line directive before the location.
1426 const LineEntry
*Entry
=
1427 LineTable
->FindNearestLineEntry(LocInfo
.first
, LocInfo
.second
);
1429 // If this is before the first line marker, use the file characteristic.
1431 return FI
.getFileCharacteristic();
1433 return Entry
->FileKind
;
1436 /// Return the filename or buffer identifier of the buffer the location is in.
1437 /// Note that this name does not respect \#line directives. Use getPresumedLoc
1438 /// for normal clients.
1439 StringRef
SourceManager::getBufferName(SourceLocation Loc
,
1440 bool *Invalid
) const {
1441 if (isInvalid(Loc
, Invalid
)) return "<invalid loc>";
1443 auto B
= getBufferOrNone(getFileID(Loc
));
1446 return B
? B
->getBufferIdentifier() : "<invalid buffer>";
1449 /// getPresumedLoc - This method returns the "presumed" location of a
1450 /// SourceLocation specifies. A "presumed location" can be modified by \#line
1451 /// or GNU line marker directives. This provides a view on the data that a
1452 /// user should see in diagnostics, for example.
1454 /// Note that a presumed location is always given as the expansion point of an
1455 /// expansion location, not at the spelling location.
1456 PresumedLoc
SourceManager::getPresumedLoc(SourceLocation Loc
,
1457 bool UseLineDirectives
) const {
1458 if (Loc
.isInvalid()) return PresumedLoc();
1460 // Presumed locations are always for expansion points.
1461 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedExpansionLoc(Loc
);
1463 bool Invalid
= false;
1464 const SLocEntry
&Entry
= getSLocEntry(LocInfo
.first
, &Invalid
);
1465 if (Invalid
|| !Entry
.isFile())
1466 return PresumedLoc();
1468 const SrcMgr::FileInfo
&FI
= Entry
.getFile();
1469 const SrcMgr::ContentCache
*C
= &FI
.getContentCache();
1471 // To get the source name, first consult the FileEntry (if one exists)
1472 // before the MemBuffer as this will avoid unnecessarily paging in the
1474 FileID FID
= LocInfo
.first
;
1477 Filename
= C
->OrigEntry
->getName();
1478 else if (auto Buffer
= C
->getBufferOrNone(Diag
, getFileManager()))
1479 Filename
= Buffer
->getBufferIdentifier();
1481 unsigned LineNo
= getLineNumber(LocInfo
.first
, LocInfo
.second
, &Invalid
);
1483 return PresumedLoc();
1484 unsigned ColNo
= getColumnNumber(LocInfo
.first
, LocInfo
.second
, &Invalid
);
1486 return PresumedLoc();
1488 SourceLocation IncludeLoc
= FI
.getIncludeLoc();
1490 // If we have #line directives in this file, update and overwrite the physical
1491 // location info if appropriate.
1492 if (UseLineDirectives
&& FI
.hasLineDirectives()) {
1493 assert(LineTable
&& "Can't have linetable entries without a LineTable!");
1494 // See if there is a #line directive before this. If so, get it.
1495 if (const LineEntry
*Entry
=
1496 LineTable
->FindNearestLineEntry(LocInfo
.first
, LocInfo
.second
)) {
1497 // If the LineEntry indicates a filename, use it.
1498 if (Entry
->FilenameID
!= -1) {
1499 Filename
= LineTable
->getFilename(Entry
->FilenameID
);
1500 // The contents of files referenced by #line are not in the
1502 FID
= FileID::get(0);
1505 // Use the line number specified by the LineEntry. This line number may
1506 // be multiple lines down from the line entry. Add the difference in
1507 // physical line numbers from the query point and the line marker to the
1509 unsigned MarkerLineNo
= getLineNumber(LocInfo
.first
, Entry
->FileOffset
);
1510 LineNo
= Entry
->LineNo
+ (LineNo
-MarkerLineNo
-1);
1512 // Note that column numbers are not molested by line markers.
1514 // Handle virtual #include manipulation.
1515 if (Entry
->IncludeOffset
) {
1516 IncludeLoc
= getLocForStartOfFile(LocInfo
.first
);
1517 IncludeLoc
= IncludeLoc
.getLocWithOffset(Entry
->IncludeOffset
);
1522 return PresumedLoc(Filename
.data(), FID
, LineNo
, ColNo
, IncludeLoc
);
1525 /// Returns whether the PresumedLoc for a given SourceLocation is
1526 /// in the main file.
1528 /// This computes the "presumed" location for a SourceLocation, then checks
1529 /// whether it came from a file other than the main file. This is different
1530 /// from isWrittenInMainFile() because it takes line marker directives into
1532 bool SourceManager::isInMainFile(SourceLocation Loc
) const {
1533 if (Loc
.isInvalid()) return false;
1535 // Presumed locations are always for expansion points.
1536 std::pair
<FileID
, unsigned> LocInfo
= getDecomposedExpansionLoc(Loc
);
1538 const SLocEntry
*Entry
= getSLocEntryForFile(LocInfo
.first
);
1542 const SrcMgr::FileInfo
&FI
= Entry
->getFile();
1544 // Check if there is a line directive for this location.
1545 if (FI
.hasLineDirectives())
1546 if (const LineEntry
*Entry
=
1547 LineTable
->FindNearestLineEntry(LocInfo
.first
, LocInfo
.second
))
1548 if (Entry
->IncludeOffset
)
1551 return FI
.getIncludeLoc().isInvalid();
1554 /// The size of the SLocEntry that \p FID represents.
1555 unsigned SourceManager::getFileIDSize(FileID FID
) const {
1556 bool Invalid
= false;
1557 const SrcMgr::SLocEntry
&Entry
= getSLocEntry(FID
, &Invalid
);
1562 SourceLocation::UIntTy NextOffset
;
1563 if ((ID
> 0 && unsigned(ID
+1) == local_sloc_entry_size()))
1564 NextOffset
= getNextLocalOffset();
1565 else if (ID
+1 == -1)
1566 NextOffset
= MaxLoadedOffset
;
1568 NextOffset
= getSLocEntry(FileID::get(ID
+1)).getOffset();
1570 return NextOffset
- Entry
.getOffset() - 1;
1573 //===----------------------------------------------------------------------===//
1574 // Other miscellaneous methods.
1575 //===----------------------------------------------------------------------===//
1577 /// Get the source location for the given file:line:col triplet.
1579 /// If the source file is included multiple times, the source location will
1580 /// be based upon an arbitrary inclusion.
1581 SourceLocation
SourceManager::translateFileLineCol(const FileEntry
*SourceFile
,
1583 unsigned Col
) const {
1584 assert(SourceFile
&& "Null source file!");
1585 assert(Line
&& Col
&& "Line and column should start from 1!");
1587 FileID FirstFID
= translateFile(SourceFile
);
1588 return translateLineCol(FirstFID
, Line
, Col
);
1591 /// Get the FileID for the given file.
1593 /// If the source file is included multiple times, the FileID will be the
1594 /// first inclusion.
1595 FileID
SourceManager::translateFile(const FileEntry
*SourceFile
) const {
1596 assert(SourceFile
&& "Null source file!");
1598 // First, check the main file ID, since it is common to look for a
1599 // location in the main file.
1600 if (MainFileID
.isValid()) {
1601 bool Invalid
= false;
1602 const SLocEntry
&MainSLoc
= getSLocEntry(MainFileID
, &Invalid
);
1606 if (MainSLoc
.isFile()) {
1607 if (MainSLoc
.getFile().getContentCache().OrigEntry
== SourceFile
)
1612 // The location we're looking for isn't in the main file; look
1613 // through all of the local source locations.
1614 for (unsigned I
= 0, N
= local_sloc_entry_size(); I
!= N
; ++I
) {
1615 const SLocEntry
&SLoc
= getLocalSLocEntry(I
);
1616 if (SLoc
.isFile() &&
1617 SLoc
.getFile().getContentCache().OrigEntry
== SourceFile
)
1618 return FileID::get(I
);
1621 // If that still didn't help, try the modules.
1622 for (unsigned I
= 0, N
= loaded_sloc_entry_size(); I
!= N
; ++I
) {
1623 const SLocEntry
&SLoc
= getLoadedSLocEntry(I
);
1624 if (SLoc
.isFile() &&
1625 SLoc
.getFile().getContentCache().OrigEntry
== SourceFile
)
1626 return FileID::get(-int(I
) - 2);
1632 /// Get the source location in \arg FID for the given line:col.
1633 /// Returns null location if \arg FID is not a file SLocEntry.
1634 SourceLocation
SourceManager::translateLineCol(FileID FID
,
1636 unsigned Col
) const {
1637 // Lines are used as a one-based index into a zero-based array. This assert
1638 // checks for possible buffer underruns.
1639 assert(Line
&& Col
&& "Line and column should start from 1!");
1641 if (FID
.isInvalid())
1642 return SourceLocation();
1644 bool Invalid
= false;
1645 const SLocEntry
&Entry
= getSLocEntry(FID
, &Invalid
);
1647 return SourceLocation();
1649 if (!Entry
.isFile())
1650 return SourceLocation();
1652 SourceLocation FileLoc
= SourceLocation::getFileLoc(Entry
.getOffset());
1654 if (Line
== 1 && Col
== 1)
1657 const ContentCache
*Content
= &Entry
.getFile().getContentCache();
1659 // If this is the first use of line information for this buffer, compute the
1660 // SourceLineCache for it on demand.
1661 std::optional
<llvm::MemoryBufferRef
> Buffer
=
1662 Content
->getBufferOrNone(Diag
, getFileManager());
1664 return SourceLocation();
1665 if (!Content
->SourceLineCache
)
1666 Content
->SourceLineCache
=
1667 LineOffsetMapping::get(*Buffer
, ContentCacheAlloc
);
1669 if (Line
> Content
->SourceLineCache
.size()) {
1670 unsigned Size
= Buffer
->getBufferSize();
1673 return FileLoc
.getLocWithOffset(Size
);
1676 unsigned FilePos
= Content
->SourceLineCache
[Line
- 1];
1677 const char *Buf
= Buffer
->getBufferStart() + FilePos
;
1678 unsigned BufLength
= Buffer
->getBufferSize() - FilePos
;
1680 return FileLoc
.getLocWithOffset(FilePos
);
1684 // Check that the given column is valid.
1685 while (i
< BufLength
-1 && i
< Col
-1 && Buf
[i
] != '\n' && Buf
[i
] != '\r')
1687 return FileLoc
.getLocWithOffset(FilePos
+ i
);
1690 /// Compute a map of macro argument chunks to their expanded source
1691 /// location. Chunks that are not part of a macro argument will map to an
1692 /// invalid source location. e.g. if a file contains one macro argument at
1693 /// offset 100 with length 10, this is how the map will be formed:
1694 /// 0 -> SourceLocation()
1695 /// 100 -> Expanded macro arg location
1696 /// 110 -> SourceLocation()
1697 void SourceManager::computeMacroArgsCache(MacroArgsMap
&MacroArgsCache
,
1699 assert(FID
.isValid());
1701 // Initially no macro argument chunk is present.
1702 MacroArgsCache
.insert(std::make_pair(0, SourceLocation()));
1707 // Stop if there are no more FileIDs to check.
1709 if (unsigned(ID
) >= local_sloc_entry_size())
1711 } else if (ID
== -1) {
1715 bool Invalid
= false;
1716 const SrcMgr::SLocEntry
&Entry
= getSLocEntryByID(ID
, &Invalid
);
1719 if (Entry
.isFile()) {
1720 auto& File
= Entry
.getFile();
1721 if (File
.getFileCharacteristic() == C_User_ModuleMap
||
1722 File
.getFileCharacteristic() == C_System_ModuleMap
)
1725 SourceLocation IncludeLoc
= File
.getIncludeLoc();
1726 bool IncludedInFID
=
1727 (IncludeLoc
.isValid() && isInFileID(IncludeLoc
, FID
)) ||
1728 // Predefined header doesn't have a valid include location in main
1729 // file, but any files created by it should still be skipped when
1730 // computing macro args expanded in the main file.
1731 (FID
== MainFileID
&& Entry
.getFile().getName() == "<built-in>");
1732 if (IncludedInFID
) {
1733 // Skip the files/macros of the #include'd file, we only care about
1734 // macros that lexed macro arguments from our file.
1735 if (Entry
.getFile().NumCreatedFIDs
)
1736 ID
+= Entry
.getFile().NumCreatedFIDs
- 1 /*because of next ++ID*/;
1739 // If file was included but not from FID, there is no more files/macros
1740 // that may be "contained" in this file.
1741 if (IncludeLoc
.isValid())
1746 const ExpansionInfo
&ExpInfo
= Entry
.getExpansion();
1748 if (ExpInfo
.getExpansionLocStart().isFileID()) {
1749 if (!isInFileID(ExpInfo
.getExpansionLocStart(), FID
))
1750 return; // No more files/macros that may be "contained" in this file.
1753 if (!ExpInfo
.isMacroArgExpansion())
1756 associateFileChunkWithMacroArgExp(MacroArgsCache
, FID
,
1757 ExpInfo
.getSpellingLoc(),
1758 SourceLocation::getMacroLoc(Entry
.getOffset()),
1759 getFileIDSize(FileID::get(ID
)));
1763 void SourceManager::associateFileChunkWithMacroArgExp(
1764 MacroArgsMap
&MacroArgsCache
,
1766 SourceLocation SpellLoc
,
1767 SourceLocation ExpansionLoc
,
1768 unsigned ExpansionLength
) const {
1769 if (!SpellLoc
.isFileID()) {
1770 SourceLocation::UIntTy SpellBeginOffs
= SpellLoc
.getOffset();
1771 SourceLocation::UIntTy SpellEndOffs
= SpellBeginOffs
+ ExpansionLength
;
1773 // The spelling range for this macro argument expansion can span multiple
1774 // consecutive FileID entries. Go through each entry contained in the
1775 // spelling range and if one is itself a macro argument expansion, recurse
1776 // and associate the file chunk that it represents.
1778 FileID SpellFID
; // Current FileID in the spelling range.
1779 unsigned SpellRelativeOffs
;
1780 std::tie(SpellFID
, SpellRelativeOffs
) = getDecomposedLoc(SpellLoc
);
1782 const SLocEntry
&Entry
= getSLocEntry(SpellFID
);
1783 SourceLocation::UIntTy SpellFIDBeginOffs
= Entry
.getOffset();
1784 unsigned SpellFIDSize
= getFileIDSize(SpellFID
);
1785 SourceLocation::UIntTy SpellFIDEndOffs
= SpellFIDBeginOffs
+ SpellFIDSize
;
1786 const ExpansionInfo
&Info
= Entry
.getExpansion();
1787 if (Info
.isMacroArgExpansion()) {
1788 unsigned CurrSpellLength
;
1789 if (SpellFIDEndOffs
< SpellEndOffs
)
1790 CurrSpellLength
= SpellFIDSize
- SpellRelativeOffs
;
1792 CurrSpellLength
= ExpansionLength
;
1793 associateFileChunkWithMacroArgExp(MacroArgsCache
, FID
,
1794 Info
.getSpellingLoc().getLocWithOffset(SpellRelativeOffs
),
1795 ExpansionLoc
, CurrSpellLength
);
1798 if (SpellFIDEndOffs
>= SpellEndOffs
)
1799 return; // we covered all FileID entries in the spelling range.
1801 // Move to the next FileID entry in the spelling range.
1802 unsigned advance
= SpellFIDSize
- SpellRelativeOffs
+ 1;
1803 ExpansionLoc
= ExpansionLoc
.getLocWithOffset(advance
);
1804 ExpansionLength
-= advance
;
1806 SpellRelativeOffs
= 0;
1810 assert(SpellLoc
.isFileID());
1813 if (!isInFileID(SpellLoc
, FID
, &BeginOffs
))
1816 unsigned EndOffs
= BeginOffs
+ ExpansionLength
;
1818 // Add a new chunk for this macro argument. A previous macro argument chunk
1819 // may have been lexed again, so e.g. if the map is
1820 // 0 -> SourceLocation()
1821 // 100 -> Expanded loc #1
1822 // 110 -> SourceLocation()
1823 // and we found a new macro FileID that lexed from offset 105 with length 3,
1824 // the new map will be:
1825 // 0 -> SourceLocation()
1826 // 100 -> Expanded loc #1
1827 // 105 -> Expanded loc #2
1828 // 108 -> Expanded loc #1
1829 // 110 -> SourceLocation()
1831 // Since re-lexed macro chunks will always be the same size or less of
1832 // previous chunks, we only need to find where the ending of the new macro
1833 // chunk is mapped to and update the map with new begin/end mappings.
1835 MacroArgsMap::iterator I
= MacroArgsCache
.upper_bound(EndOffs
);
1837 SourceLocation EndOffsMappedLoc
= I
->second
;
1838 MacroArgsCache
[BeginOffs
] = ExpansionLoc
;
1839 MacroArgsCache
[EndOffs
] = EndOffsMappedLoc
;
1842 /// If \arg Loc points inside a function macro argument, the returned
1843 /// location will be the macro location in which the argument was expanded.
1844 /// If a macro argument is used multiple times, the expanded location will
1845 /// be at the first expansion of the argument.
1849 /// Passing a file location pointing at 'foo', will yield a macro location
1850 /// where 'foo' was expanded into.
1852 SourceManager::getMacroArgExpandedLocation(SourceLocation Loc
) const {
1853 if (Loc
.isInvalid() || !Loc
.isFileID())
1858 std::tie(FID
, Offset
) = getDecomposedLoc(Loc
);
1859 if (FID
.isInvalid())
1862 std::unique_ptr
<MacroArgsMap
> &MacroArgsCache
= MacroArgsCacheMap
[FID
];
1863 if (!MacroArgsCache
) {
1864 MacroArgsCache
= std::make_unique
<MacroArgsMap
>();
1865 computeMacroArgsCache(*MacroArgsCache
, FID
);
1868 assert(!MacroArgsCache
->empty());
1869 MacroArgsMap::iterator I
= MacroArgsCache
->upper_bound(Offset
);
1870 // In case every element in MacroArgsCache is greater than Offset we can't
1871 // decrement the iterator.
1872 if (I
== MacroArgsCache
->begin())
1877 SourceLocation::UIntTy MacroArgBeginOffs
= I
->first
;
1878 SourceLocation MacroArgExpandedLoc
= I
->second
;
1879 if (MacroArgExpandedLoc
.isValid())
1880 return MacroArgExpandedLoc
.getLocWithOffset(Offset
- MacroArgBeginOffs
);
1885 std::pair
<FileID
, unsigned>
1886 SourceManager::getDecomposedIncludedLoc(FileID FID
) const {
1887 if (FID
.isInvalid())
1888 return std::make_pair(FileID(), 0);
1890 // Uses IncludedLocMap to retrieve/cache the decomposed loc.
1892 using DecompTy
= std::pair
<FileID
, unsigned>;
1893 auto InsertOp
= IncludedLocMap
.try_emplace(FID
);
1894 DecompTy
&DecompLoc
= InsertOp
.first
->second
;
1895 if (!InsertOp
.second
)
1896 return DecompLoc
; // already in map.
1898 SourceLocation UpperLoc
;
1899 bool Invalid
= false;
1900 const SrcMgr::SLocEntry
&Entry
= getSLocEntry(FID
, &Invalid
);
1902 if (Entry
.isExpansion())
1903 UpperLoc
= Entry
.getExpansion().getExpansionLocStart();
1905 UpperLoc
= Entry
.getFile().getIncludeLoc();
1908 if (UpperLoc
.isValid())
1909 DecompLoc
= getDecomposedLoc(UpperLoc
);
1914 bool SourceManager::isInTheSameTranslationUnitImpl(
1915 const std::pair
<FileID
, unsigned> &LOffs
,
1916 const std::pair
<FileID
, unsigned> &ROffs
) const {
1917 // If one is local while the other is loaded.
1918 if (isLoadedFileID(LOffs
.first
) != isLoadedFileID(ROffs
.first
))
1921 if (isLoadedFileID(LOffs
.first
) && isLoadedFileID(ROffs
.first
)) {
1922 auto FindSLocEntryAlloc
= [this](FileID FID
) {
1923 // Loaded FileIDs are negative, we store the lowest FileID from each
1924 // allocation, later allocations have lower FileIDs.
1925 return llvm::lower_bound(LoadedSLocEntryAllocBegin
, FID
,
1926 std::greater
<FileID
>{});
1929 // If both are loaded from different AST files.
1930 if (FindSLocEntryAlloc(LOffs
.first
) != FindSLocEntryAlloc(ROffs
.first
))
1937 /// Given a decomposed source location, move it up the include/expansion stack
1938 /// to the parent source location within the same translation unit. If this is
1939 /// possible, return the decomposed version of the parent in Loc and return
1940 /// false. If Loc is a top-level entry, return true and don't modify it.
1942 MoveUpTranslationUnitIncludeHierarchy(std::pair
<FileID
, unsigned> &Loc
,
1943 const SourceManager
&SM
) {
1944 std::pair
<FileID
, unsigned> UpperLoc
= SM
.getDecomposedIncludedLoc(Loc
.first
);
1945 if (UpperLoc
.first
.isInvalid() ||
1946 !SM
.isInTheSameTranslationUnitImpl(UpperLoc
, Loc
))
1947 return true; // We reached the top.
1953 /// Return the cache entry for comparing the given file IDs
1954 /// for isBeforeInTranslationUnit.
1955 InBeforeInTUCacheEntry
&SourceManager::getInBeforeInTUCache(FileID LFID
,
1956 FileID RFID
) const {
1957 // This is a magic number for limiting the cache size. It was experimentally
1958 // derived from a small Objective-C project (where the cache filled
1959 // out to ~250 items). We can make it larger if necessary.
1960 // FIXME: this is almost certainly full these days. Use an LRU cache?
1961 enum { MagicCacheSize
= 300 };
1962 IsBeforeInTUCacheKey
Key(LFID
, RFID
);
1964 // If the cache size isn't too large, do a lookup and if necessary default
1965 // construct an entry. We can then return it to the caller for direct
1966 // use. When they update the value, the cache will get automatically
1968 if (IBTUCache
.size() < MagicCacheSize
)
1969 return IBTUCache
.try_emplace(Key
, LFID
, RFID
).first
->second
;
1971 // Otherwise, do a lookup that will not construct a new value.
1972 InBeforeInTUCache::iterator I
= IBTUCache
.find(Key
);
1973 if (I
!= IBTUCache
.end())
1976 // Fall back to the overflow value.
1977 IBTUCacheOverflow
.setQueryFIDs(LFID
, RFID
);
1978 return IBTUCacheOverflow
;
1981 /// Determines the order of 2 source locations in the translation unit.
1983 /// \returns true if LHS source location comes before RHS, false otherwise.
1984 bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS
,
1985 SourceLocation RHS
) const {
1986 assert(LHS
.isValid() && RHS
.isValid() && "Passed invalid source location!");
1990 std::pair
<FileID
, unsigned> LOffs
= getDecomposedLoc(LHS
);
1991 std::pair
<FileID
, unsigned> ROffs
= getDecomposedLoc(RHS
);
1993 // getDecomposedLoc may have failed to return a valid FileID because, e.g. it
1994 // is a serialized one referring to a file that was removed after we loaded
1996 if (LOffs
.first
.isInvalid() || ROffs
.first
.isInvalid())
1997 return LOffs
.first
.isInvalid() && !ROffs
.first
.isInvalid();
1999 std::pair
<bool, bool> InSameTU
= isInTheSameTranslationUnit(LOffs
, ROffs
);
2001 return InSameTU
.second
;
2002 // TODO: This should be unreachable, but some clients are calling this
2003 // function before making sure LHS and RHS are in the same TU.
2004 return LOffs
.first
< ROffs
.first
;
2007 std::pair
<bool, bool> SourceManager::isInTheSameTranslationUnit(
2008 std::pair
<FileID
, unsigned> &LOffs
,
2009 std::pair
<FileID
, unsigned> &ROffs
) const {
2010 // If the source locations are not in the same TU, return early.
2011 if (!isInTheSameTranslationUnitImpl(LOffs
, ROffs
))
2012 return std::make_pair(false, false);
2014 // If the source locations are in the same file, just compare offsets.
2015 if (LOffs
.first
== ROffs
.first
)
2016 return std::make_pair(true, LOffs
.second
< ROffs
.second
);
2018 // If we are comparing a source location with multiple locations in the same
2019 // file, we get a big win by caching the result.
2020 InBeforeInTUCacheEntry
&IsBeforeInTUCache
=
2021 getInBeforeInTUCache(LOffs
.first
, ROffs
.first
);
2023 // If we are comparing a source location with multiple locations in the same
2024 // file, we get a big win by caching the result.
2025 if (IsBeforeInTUCache
.isCacheValid())
2026 return std::make_pair(
2027 true, IsBeforeInTUCache
.getCachedResult(LOffs
.second
, ROffs
.second
));
2029 // Okay, we missed in the cache, we'll compute the answer and populate it.
2030 // We need to find the common ancestor. The only way of doing this is to
2031 // build the complete include chain for one and then walking up the chain
2032 // of the other looking for a match.
2034 // A location within a FileID on the path up from LOffs to the main file.
2036 std::pair
<FileID
, unsigned> DecomposedLoc
; // FileID redundant, but clearer.
2037 FileID ChildFID
; // Used for breaking ties. Invalid for the initial loc.
2039 llvm::SmallDenseMap
<FileID
, Entry
, 16> LChain
;
2043 LChain
.try_emplace(LOffs
.first
, Entry
{LOffs
, LChild
});
2044 // We catch the case where LOffs is in a file included by ROffs and
2045 // quit early. The other way round unfortunately remains suboptimal.
2046 if (LOffs
.first
== ROffs
.first
)
2048 LChild
= LOffs
.first
;
2049 } while (!MoveUpTranslationUnitIncludeHierarchy(LOffs
, *this));
2053 auto LIt
= LChain
.find(ROffs
.first
);
2054 if (LIt
!= LChain
.end()) {
2055 // Compare the locations within the common file and cache them.
2056 LOffs
= LIt
->second
.DecomposedLoc
;
2057 LChild
= LIt
->second
.ChildFID
;
2058 // The relative order of LChild and RChild is a tiebreaker when
2059 // - locs expand to the same location (occurs in macro arg expansion)
2060 // - one loc is a parent of the other (we consider the parent as "first")
2061 // For the parent entry to be first, its invalid child file ID must
2062 // compare smaller to the valid child file ID of the other entry.
2063 // However loaded FileIDs are <0, so we perform *unsigned* comparison!
2064 // This changes the relative order of local vs loaded FileIDs, but it
2065 // doesn't matter as these are never mixed in macro expansion.
2066 unsigned LChildID
= LChild
.ID
;
2067 unsigned RChildID
= RChild
.ID
;
2068 assert(((LOffs
.second
!= ROffs
.second
) ||
2069 (LChildID
== 0 || RChildID
== 0) ||
2070 isInSameSLocAddrSpace(getComposedLoc(LChild
, 0),
2071 getComposedLoc(RChild
, 0), nullptr)) &&
2072 "Mixed local/loaded FileIDs with same include location?");
2073 IsBeforeInTUCache
.setCommonLoc(LOffs
.first
, LOffs
.second
, ROffs
.second
,
2074 LChildID
< RChildID
);
2075 return std::make_pair(
2076 true, IsBeforeInTUCache
.getCachedResult(LOffs
.second
, ROffs
.second
));
2078 RChild
= ROffs
.first
;
2079 } while (!MoveUpTranslationUnitIncludeHierarchy(ROffs
, *this));
2081 // If we found no match, the location is either in a built-ins buffer or
2082 // associated with global inline asm. PR5662 and PR22576 are examples.
2084 StringRef LB
= getBufferOrFake(LOffs
.first
).getBufferIdentifier();
2085 StringRef RB
= getBufferOrFake(ROffs
.first
).getBufferIdentifier();
2087 bool LIsBuiltins
= LB
== "<built-in>";
2088 bool RIsBuiltins
= RB
== "<built-in>";
2089 // Sort built-in before non-built-in.
2090 if (LIsBuiltins
|| RIsBuiltins
) {
2091 if (LIsBuiltins
!= RIsBuiltins
)
2092 return std::make_pair(true, LIsBuiltins
);
2093 // Both are in built-in buffers, but from different files. We just claim
2094 // that lower IDs come first.
2095 return std::make_pair(true, LOffs
.first
< ROffs
.first
);
2098 bool LIsAsm
= LB
== "<inline asm>";
2099 bool RIsAsm
= RB
== "<inline asm>";
2100 // Sort assembler after built-ins, but before the rest.
2101 if (LIsAsm
|| RIsAsm
) {
2102 if (LIsAsm
!= RIsAsm
)
2103 return std::make_pair(true, RIsAsm
);
2104 assert(LOffs
.first
== ROffs
.first
);
2105 return std::make_pair(true, false);
2108 bool LIsScratch
= LB
== "<scratch space>";
2109 bool RIsScratch
= RB
== "<scratch space>";
2110 // Sort scratch after inline asm, but before the rest.
2111 if (LIsScratch
|| RIsScratch
) {
2112 if (LIsScratch
!= RIsScratch
)
2113 return std::make_pair(true, LIsScratch
);
2114 return std::make_pair(true, LOffs
.second
< ROffs
.second
);
2117 llvm_unreachable("Unsortable locations found");
2120 void SourceManager::PrintStats() const {
2121 llvm::errs() << "\n*** Source Manager Stats:\n";
2122 llvm::errs() << FileInfos
.size() << " files mapped, " << MemBufferInfos
.size()
2123 << " mem buffers mapped.\n";
2124 llvm::errs() << LocalSLocEntryTable
.size() << " local SLocEntries allocated ("
2125 << llvm::capacity_in_bytes(LocalSLocEntryTable
)
2126 << " bytes of capacity), " << NextLocalOffset
2127 << "B of SLoc address space used.\n";
2128 llvm::errs() << LoadedSLocEntryTable
.size()
2129 << " loaded SLocEntries allocated ("
2130 << llvm::capacity_in_bytes(LoadedSLocEntryTable
)
2131 << " bytes of capacity), "
2132 << MaxLoadedOffset
- CurrentLoadedOffset
2133 << "B of SLoc address space used.\n";
2135 unsigned NumLineNumsComputed
= 0;
2136 unsigned NumFileBytesMapped
= 0;
2137 for (fileinfo_iterator I
= fileinfo_begin(), E
= fileinfo_end(); I
!= E
; ++I
){
2138 NumLineNumsComputed
+= bool(I
->second
->SourceLineCache
);
2139 NumFileBytesMapped
+= I
->second
->getSizeBytesMapped();
2141 unsigned NumMacroArgsComputed
= MacroArgsCacheMap
.size();
2143 llvm::errs() << NumFileBytesMapped
<< " bytes of files mapped, "
2144 << NumLineNumsComputed
<< " files with line #'s computed, "
2145 << NumMacroArgsComputed
<< " files with macro args computed.\n";
2146 llvm::errs() << "FileID scans: " << NumLinearScans
<< " linear, "
2147 << NumBinaryProbes
<< " binary.\n";
2150 LLVM_DUMP_METHOD
void SourceManager::dump() const {
2151 llvm::raw_ostream
&out
= llvm::errs();
2153 auto DumpSLocEntry
= [&](int ID
, const SrcMgr::SLocEntry
&Entry
,
2154 std::optional
<SourceLocation::UIntTy
> NextStart
) {
2155 out
<< "SLocEntry <FileID " << ID
<< "> " << (Entry
.isFile() ? "file" : "expansion")
2156 << " <SourceLocation " << Entry
.getOffset() << ":";
2158 out
<< *NextStart
<< ">\n";
2161 if (Entry
.isFile()) {
2162 auto &FI
= Entry
.getFile();
2163 if (FI
.NumCreatedFIDs
)
2164 out
<< " covers <FileID " << ID
<< ":" << int(ID
+ FI
.NumCreatedFIDs
)
2166 if (FI
.getIncludeLoc().isValid())
2167 out
<< " included from " << FI
.getIncludeLoc().getOffset() << "\n";
2168 auto &CC
= FI
.getContentCache();
2169 out
<< " for " << (CC
.OrigEntry
? CC
.OrigEntry
->getName() : "<none>")
2171 if (CC
.BufferOverridden
)
2172 out
<< " contents overridden\n";
2173 if (CC
.ContentsEntry
!= CC
.OrigEntry
) {
2174 out
<< " contents from "
2175 << (CC
.ContentsEntry
? CC
.ContentsEntry
->getName() : "<none>")
2179 auto &EI
= Entry
.getExpansion();
2180 out
<< " spelling from " << EI
.getSpellingLoc().getOffset() << "\n";
2181 out
<< " macro " << (EI
.isMacroArgExpansion() ? "arg" : "body")
2182 << " range <" << EI
.getExpansionLocStart().getOffset() << ":"
2183 << EI
.getExpansionLocEnd().getOffset() << ">\n";
2187 // Dump local SLocEntries.
2188 for (unsigned ID
= 0, NumIDs
= LocalSLocEntryTable
.size(); ID
!= NumIDs
; ++ID
) {
2189 DumpSLocEntry(ID
, LocalSLocEntryTable
[ID
],
2190 ID
== NumIDs
- 1 ? NextLocalOffset
2191 : LocalSLocEntryTable
[ID
+ 1].getOffset());
2193 // Dump loaded SLocEntries.
2194 std::optional
<SourceLocation::UIntTy
> NextStart
;
2195 for (unsigned Index
= 0; Index
!= LoadedSLocEntryTable
.size(); ++Index
) {
2196 int ID
= -(int)Index
- 2;
2197 if (SLocEntryLoaded
[Index
]) {
2198 DumpSLocEntry(ID
, LoadedSLocEntryTable
[Index
], NextStart
);
2199 NextStart
= LoadedSLocEntryTable
[Index
].getOffset();
2201 NextStart
= std::nullopt
;
2206 void SourceManager::noteSLocAddressSpaceUsage(
2207 DiagnosticsEngine
&Diag
, std::optional
<unsigned> MaxNotes
) const {
2209 // A location where this file was entered.
2211 // Number of times this FileEntry was entered.
2212 unsigned Inclusions
= 0;
2213 // Size usage from the file itself.
2214 uint64_t DirectSize
= 0;
2215 // Total size usage from the file and its macro expansions.
2216 uint64_t TotalSize
= 0;
2218 using UsageMap
= llvm::MapVector
<const FileEntry
*, Info
>;
2221 uint64_t CountedSize
= 0;
2223 auto AddUsageForFileID
= [&](FileID ID
) {
2224 // The +1 here is because getFileIDSize doesn't include the extra byte for
2225 // the one-past-the-end location.
2226 unsigned Size
= getFileIDSize(ID
) + 1;
2228 // Find the file that used this address space, either directly or by
2230 SourceLocation FileStart
= getFileLoc(getComposedLoc(ID
, 0));
2231 FileID FileLocID
= getFileID(FileStart
);
2232 const FileEntry
*Entry
= getFileEntryForID(FileLocID
);
2234 Info
&EntryInfo
= Usage
[Entry
];
2235 if (EntryInfo
.Loc
.isInvalid())
2236 EntryInfo
.Loc
= FileStart
;
2237 if (ID
== FileLocID
) {
2238 ++EntryInfo
.Inclusions
;
2239 EntryInfo
.DirectSize
+= Size
;
2241 EntryInfo
.TotalSize
+= Size
;
2242 CountedSize
+= Size
;
2245 // Loaded SLocEntries have indexes counting downwards from -2.
2246 for (size_t Index
= 0; Index
!= LoadedSLocEntryTable
.size(); ++Index
) {
2247 AddUsageForFileID(FileID::get(-2 - Index
));
2249 // Local SLocEntries have indexes counting upwards from 0.
2250 for (size_t Index
= 0; Index
!= LocalSLocEntryTable
.size(); ++Index
) {
2251 AddUsageForFileID(FileID::get(Index
));
2254 // Sort the usage by size from largest to smallest. Break ties by raw source
2256 auto SortedUsage
= Usage
.takeVector();
2257 auto Cmp
= [](const UsageMap::value_type
&A
, const UsageMap::value_type
&B
) {
2258 return A
.second
.TotalSize
> B
.second
.TotalSize
||
2259 (A
.second
.TotalSize
== B
.second
.TotalSize
&&
2260 A
.second
.Loc
< B
.second
.Loc
);
2262 auto SortedEnd
= SortedUsage
.end();
2263 if (MaxNotes
&& SortedUsage
.size() > *MaxNotes
) {
2264 SortedEnd
= SortedUsage
.begin() + *MaxNotes
;
2265 std::nth_element(SortedUsage
.begin(), SortedEnd
, SortedUsage
.end(), Cmp
);
2267 std::sort(SortedUsage
.begin(), SortedEnd
, Cmp
);
2269 // Produce note on sloc address space usage total.
2270 uint64_t LocalUsage
= NextLocalOffset
;
2271 uint64_t LoadedUsage
= MaxLoadedOffset
- CurrentLoadedOffset
;
2272 int UsagePercent
= static_cast<int>(100.0 * double(LocalUsage
+ LoadedUsage
) /
2274 Diag
.Report(SourceLocation(), diag::note_total_sloc_usage
)
2275 << LocalUsage
<< LoadedUsage
<< (LocalUsage
+ LoadedUsage
) << UsagePercent
;
2277 // Produce notes on sloc address space usage for each file with a high usage.
2278 uint64_t ReportedSize
= 0;
2279 for (auto &[Entry
, FileInfo
] :
2280 llvm::make_range(SortedUsage
.begin(), SortedEnd
)) {
2281 Diag
.Report(FileInfo
.Loc
, diag::note_file_sloc_usage
)
2282 << FileInfo
.Inclusions
<< FileInfo
.DirectSize
2283 << (FileInfo
.TotalSize
- FileInfo
.DirectSize
);
2284 ReportedSize
+= FileInfo
.TotalSize
;
2287 // Describe any remaining usage not reported in the per-file usage.
2288 if (ReportedSize
!= CountedSize
) {
2289 Diag
.Report(SourceLocation(), diag::note_file_misc_sloc_usage
)
2290 << (SortedUsage
.end() - SortedEnd
) << CountedSize
- ReportedSize
;
2294 ExternalSLocEntrySource::~ExternalSLocEntrySource() = default;
2296 /// Return the amount of memory used by memory buffers, breaking down
2297 /// by heap-backed versus mmap'ed memory.
2298 SourceManager::MemoryBufferSizes
SourceManager::getMemoryBufferSizes() const {
2299 size_t malloc_bytes
= 0;
2300 size_t mmap_bytes
= 0;
2302 for (unsigned i
= 0, e
= MemBufferInfos
.size(); i
!= e
; ++i
)
2303 if (size_t sized_mapped
= MemBufferInfos
[i
]->getSizeBytesMapped())
2304 switch (MemBufferInfos
[i
]->getMemoryBufferKind()) {
2305 case llvm::MemoryBuffer::MemoryBuffer_MMap
:
2306 mmap_bytes
+= sized_mapped
;
2308 case llvm::MemoryBuffer::MemoryBuffer_Malloc
:
2309 malloc_bytes
+= sized_mapped
;
2313 return MemoryBufferSizes(malloc_bytes
, mmap_bytes
);
2316 size_t SourceManager::getDataStructureSizes() const {
2317 size_t size
= llvm::capacity_in_bytes(MemBufferInfos
) +
2318 llvm::capacity_in_bytes(LocalSLocEntryTable
) +
2319 llvm::capacity_in_bytes(LoadedSLocEntryTable
) +
2320 llvm::capacity_in_bytes(SLocEntryLoaded
) +
2321 llvm::capacity_in_bytes(FileInfos
);
2323 if (OverriddenFilesInfo
)
2324 size
+= llvm::capacity_in_bytes(OverriddenFilesInfo
->OverriddenFiles
);
2329 SourceManagerForFile::SourceManagerForFile(StringRef FileName
,
2330 StringRef Content
) {
2331 // This is referenced by `FileMgr` and will be released by `FileMgr` when it
2333 IntrusiveRefCntPtr
<llvm::vfs::InMemoryFileSystem
> InMemoryFileSystem(
2334 new llvm::vfs::InMemoryFileSystem
);
2335 InMemoryFileSystem
->addFile(
2337 llvm::MemoryBuffer::getMemBuffer(Content
, FileName
,
2338 /*RequiresNullTerminator=*/false));
2339 // This is passed to `SM` as reference, so the pointer has to be referenced
2340 // in `Environment` so that `FileMgr` can out-live this function scope.
2342 std::make_unique
<FileManager
>(FileSystemOptions(), InMemoryFileSystem
);
2343 // This is passed to `SM` as reference, so the pointer has to be referenced
2344 // by `Environment` due to the same reason above.
2345 Diagnostics
= std::make_unique
<DiagnosticsEngine
>(
2346 IntrusiveRefCntPtr
<DiagnosticIDs
>(new DiagnosticIDs
),
2347 new DiagnosticOptions
);
2348 SourceMgr
= std::make_unique
<SourceManager
>(*Diagnostics
, *FileMgr
);
2349 FileEntryRef FE
= llvm::cantFail(FileMgr
->getFileRef(FileName
));
2351 SourceMgr
->createFileID(FE
, SourceLocation(), clang::SrcMgr::C_User
);
2352 assert(ID
.isValid());
2353 SourceMgr
->setMainFileID(ID
);