34#ifndef LLVM_CLANG_BASIC_SOURCEMANAGER_H
35#define LLVM_CLANG_BASIC_SOURCEMANAGER_H
41#include "llvm/ADT/ArrayRef.h"
42#include "llvm/ADT/BitVector.h"
43#include "llvm/ADT/DenseMap.h"
44#include "llvm/ADT/DenseSet.h"
45#include "llvm/ADT/IntrusiveRefCntPtr.h"
46#include "llvm/ADT/PagedVector.h"
47#include "llvm/ADT/PointerIntPair.h"
48#include "llvm/ADT/SmallVector.h"
49#include "llvm/ADT/StringRef.h"
50#include "llvm/Support/Allocator.h"
51#include "llvm/Support/Compiler.h"
52#include "llvm/Support/MemoryBuffer.h"
103 explicit operator bool()
const {
return Storage; }
117 llvm::BumpPtrAllocator &Alloc);
121 llvm::BumpPtrAllocator &Alloc);
125 unsigned *Storage =
nullptr;
134 mutable std::unique_ptr<llvm::MemoryBuffer> Buffer;
170 LLVM_PREFERRED_TYPE(
bool)
175 LLVM_PREFERRED_TYPE(
bool)
181 LLVM_PREFERRED_TYPE(
bool)
184 LLVM_PREFERRED_TYPE(
bool)
208 "Passed ContentCache object cannot own a buffer.");
220 std::optional<llvm::MemoryBufferRef>
245 return Buffer->getMemBufferRef();
253 return Buffer->getBuffer();
260 Buffer = std::move(B);
267 assert(!Buffer &&
"Expected to be called right after construction");
269 setBuffer(llvm::MemoryBuffer::getMemBuffer(*B));
279static_assert(
alignof(ContentCache) >= 8,
280 "ContentCache must be 8-byte aligned.");
309 unsigned NumCreatedFIDs : 31;
312 LLVM_PREFERRED_TYPE(
bool)
313 unsigned HasLineDirectives : 1;
316 llvm::PointerIntPair<const ContentCache *, 3, CharacteristicKind>
325 X.NumCreatedFIDs = 0;
326 X.HasLineDirectives =
false;
327 X.ContentAndKind.setPointer(&Con);
328 X.ContentAndKind.setInt(FileCharacter);
338 return *ContentAndKind.getPointer();
343 return ContentAndKind.getInt();
376 bool ExpansionIsTokenRange;
384 return ExpansionLocStart;
422 bool ExpansionIsTokenRange =
true) {
424 X.SpellingLoc = SpellingLoc;
425 X.ExpansionLocStart = Start;
426 X.ExpansionLocEnd = End;
427 X.ExpansionIsTokenRange = ExpansionIsTokenRange;
466 return create(SpellingLoc, Start, End,
false);
474static_assert(
sizeof(FileInfo) <=
sizeof(ExpansionInfo),
475 "FileInfo must be no larger than ExpansionInfo.");
484 LLVM_PREFERRED_TYPE(
bool)
504 assert(
isFile() &&
"Not a file SLocEntry!");
509 assert(
isExpansion() &&
"Not a macro expansion SLocEntry!");
515 assert(!(Offset & (1ULL << OffsetBits)) &&
"Offset is too large");
522 assert(!(Offset & (1ULL << OffsetBits)) &&
"Offset is too large");
525 E.IsExpansion =
false;
532 assert(!(Offset & (1ULL << OffsetBits)) &&
"Offset is too large");
535 E.IsExpansion =
true;
575 FileID LQueryFID, RQueryFID;
580 bool LChildBeforeRChild;
591 unsigned LCommonOffset, RCommonOffset;
612 if (LQueryFID != CommonFID) LOffset = LCommonOffset;
613 if (RQueryFID != CommonFID) ROffset = RCommonOffset;
620 if (LOffset == ROffset)
621 return LChildBeforeRChild;
623 return LOffset < ROffset;
630 if (LQueryFID != LHS || RQueryFID != RHS) {
638 unsigned rCommonOffset,
bool LParentBeforeRParent) {
639 CommonFID = commonFID;
640 LCommonOffset = lCommonOffset;
641 RCommonOffset = rCommonOffset;
642 LChildBeforeRChild = LParentBeforeRParent;
669 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
677 llvm::DenseMap<FileEntryRef, SrcMgr::ContentCache*> FileInfos;
681 bool OverridenFilesKeepOriginalName =
true;
685 bool UserFilesAreVolatile;
690 bool FilesAreTransient =
false;
692 struct OverriddenFilesInfoTy {
695 llvm::DenseMap<const FileEntry *, FileEntryRef> OverriddenFiles;
698 llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer;
703 std::unique_ptr<OverriddenFilesInfoTy> OverriddenFilesInfo;
705 OverriddenFilesInfoTy &getOverriddenFilesInfo() {
706 if (!OverriddenFilesInfo)
707 OverriddenFilesInfo.reset(
new OverriddenFilesInfoTy);
708 return *OverriddenFilesInfo;
715 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
727 llvm::PagedVector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
754 llvm::BitVector SLocEntryLoaded;
760 llvm::BitVector SLocEntryOffsetLoaded;
769 mutable FileID LastFileIDLookup;
774 std::unique_ptr<LineTableInfo> LineTable;
778 mutable FileID LastLineNoFileIDQuery;
780 mutable unsigned LastLineNoFilePos;
781 mutable unsigned LastLineNoResult;
790 mutable unsigned NumLinearScans = 0;
791 mutable unsigned NumBinaryProbes = 0;
798 mutable llvm::DenseMap<FileID, std::pair<FileID, unsigned>> IncludedLocMap;
801 using IsBeforeInTUCacheKey = std::pair<FileID, FileID>;
805 using InBeforeInTUCache =
806 llvm::DenseMap<IsBeforeInTUCacheKey, InBeforeInTUCacheEntry>;
809 mutable InBeforeInTUCache IBTUCache;
817 mutable std::unique_ptr<llvm::MemoryBuffer> FakeBufferForRecovery;
819 mutable std::unique_ptr<SrcMgr::ContentCache> FakeContentCacheForRecovery;
821 mutable std::unique_ptr<SrcMgr::SLocEntry> FakeSLocEntryForRecovery;
825 using MacroArgsMap = std::map<unsigned, SourceLocation>;
827 mutable llvm::DenseMap<FileID, std::unique_ptr<MacroArgsMap>>
841 bool UserFilesAreVolatile =
false);
860 OverridenFilesKeepOriginalName = value;
869 return StoredModuleBuildStack;
874 StoredModuleBuildStack.clear();
875 StoredModuleBuildStack.append(stack.begin(), stack.end());
880 StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc));
902 assert(PreambleFileID.
isInvalid() &&
"PreambleFileID already set!");
960 bool ExpansionIsTokenRange =
true,
973 std::optional<llvm::MemoryBufferRef>
982 return getFakeBufferForRecovery();
993 const llvm::MemoryBufferRef &Buffer) {
1005 std::unique_ptr<llvm::MemoryBuffer> Buffer);
1017 if (OverriddenFilesInfo) {
1018 if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(
File))
1020 if (OverriddenFilesInfo->OverriddenFiles.contains(
File))
1039 FilesAreTransient = Transient;
1050 std::optional<llvm::MemoryBufferRef>
1052 if (
auto *Entry = getSLocEntryForFile(FID))
1053 return Entry->getFile().getContentCache().getBufferOrNone(
1055 return std::nullopt;
1062 llvm::MemoryBufferRef
1066 return getFakeBufferForRecovery();
1078 if (
auto *Entry = getSLocEntryForFile(FID))
1079 return Entry->getFile().getContentCache().OrigEntry;
1080 return std::nullopt;
1119 if (
auto *Entry = getSLocEntryForFile(FID))
1120 return Entry->getFile().NumCreatedFIDs;
1127 bool Force =
false) {
1128 auto *Entry = getSLocEntryForFile(FID);
1131 assert((Force || Entry->getFile().NumCreatedFIDs == 0) &&
"Already set!");
1132 Entry->getFile().NumCreatedFIDs = NumFIDs;
1146 return getFileID(SpellingLoc.getOffset());
1155 if (
auto *Entry = getSLocEntryForFile(FID))
1156 return SourceLocation::getFileLoc(Entry->getOffset());
1163 if (
auto *Entry = getSLocEntryForFile(FID))
1164 return SourceLocation::getFileLoc(Entry->getOffset() +
1172 if (
auto *Entry = getSLocEntryForFile(FID))
1173 return Entry->getFile().getIncludeLoc();
1180 std::pair<SourceLocation, StringRef>
1197 if (
Loc.isFileID())
return Loc;
1198 return getExpansionLocSlowCase(
Loc);
1205 if (
Loc.isFileID())
return Loc;
1206 return getFileLocSlowCase(
Loc);
1225 End.isTokenRange());
1245 if (
Loc.isFileID())
return Loc;
1246 return getSpellingLocSlowCase(
Loc);
1259 auto *Entry = getSLocEntryOrNull(FID);
1264 return Entry->isFile() ? SourceLocation::getFileLoc(GlobalOffset)
1265 : SourceLocation::getMacroLoc(GlobalOffset);
1274 auto *Entry = getSLocEntryOrNull(FID);
1276 return std::make_pair(
FileID(), 0);
1277 return std::make_pair(FID,
Loc.getOffset() - Entry->getOffset());
1284 std::pair<FileID, unsigned>
1287 auto *
E = getSLocEntryOrNull(FID);
1289 return std::make_pair(
FileID(), 0);
1291 unsigned Offset =
Loc.getOffset()-
E->getOffset();
1293 return std::make_pair(FID, Offset);
1295 return getDecomposedExpansionLocSlowCase(
E);
1302 std::pair<FileID, unsigned>
1305 auto *
E = getSLocEntryOrNull(FID);
1307 return std::make_pair(
FileID(), 0);
1309 unsigned Offset =
Loc.getOffset()-
E->getOffset();
1311 return std::make_pair(FID, Offset);
1312 return getDecomposedSpellingLocSlowCase(
E, Offset);
1371 assert(((Start.getOffset() < NextLocalOffset &&
1372 Start.getOffset()+Length <= NextLocalOffset) ||
1373 (Start.getOffset() >= CurrentLoadedOffset &&
1374 Start.getOffset()+Length < MaxLoadedOffset)) &&
1375 "Chunk is not valid SLoc address space");
1379 if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1381 *RelativeOffset = LocOffs - BeginOffs;
1396 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1397 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1399 if (LHSLoaded == RHSLoaded) {
1401 *RelativeOffset = RHSOffs - LHSOffs;
1417 bool *
Invalid =
nullptr)
const;
1426 bool *
Invalid =
nullptr)
const;
1428 bool *
Invalid =
nullptr)
const;
1430 bool *
Invalid =
nullptr)
const;
1432 bool *
Invalid =
nullptr)
const;
1478 bool UseLineDirectives =
true)
const;
1520 return Filename ==
"<command line>";
1529 return Filename ==
"<scratch space>";
1534 if (
Loc.isInvalid())
1569 unsigned *RelativeOffset =
nullptr)
const {
1571 if (isOffsetInFileID(FID, Offs)) {
1592 bool IsFileEntry,
bool IsFileExit,
1608 return ContentCacheAlloc.getTotalMemory();
1636 unsigned Line,
unsigned Col)
const;
1650 unsigned Line,
unsigned Col)
const;
1675 std::pair<bool, bool>
1677 std::pair<FileID, unsigned> &ROffs)
const;
1686 const std::pair<FileID, unsigned> &LOffs,
1687 const std::pair<FileID, unsigned> &ROffs)
const;
1702 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1703 bool RHSLoaded = RHS >= CurrentLoadedOffset;
1704 if (LHSLoaded == RHSLoaded)
1705 return LHSOffset < RHS;
1713 return Location == Start || Location == End ||
1720 llvm::DenseMap<FileEntryRef, SrcMgr::ContentCache *>::const_iterator;
1725 return FileInfos.find_as(
File) != FileInfos.end();
1735 std::optional<unsigned> MaxNotes = 32)
const;
1747 assert(Index < LocalSLocEntryTable.size() &&
"Invalid index");
1748 return LocalSLocEntryTable[Index];
1756 bool *
Invalid =
nullptr)
const {
1764 assert(Index < LoadedSLocEntryTable.size() &&
"Invalid index");
1765 if (SLocEntryLoaded[Index])
1766 return LoadedSLocEntryTable[Index];
1767 return loadSLocEntry(Index,
Invalid);
1771 bool *
Invalid =
nullptr)
const {
1776 if (FID.ID == 0 || FID.ID == -1) {
1778 return LocalSLocEntryTable[0];
1780 return getSLocEntryByID(FID.ID,
Invalid);
1786 assert(LoadedSLocEntryTable.empty() &&
1787 "Invalidating existing loaded entries");
1788 ExternalSLocEntries = Source;
1797 std::pair<int, SourceLocation::UIntTy>
1803 return isLoadedOffset(
Loc.getOffset());
1808 return isLocalOffset(
Loc.getOffset());
1813 assert(FID.ID != -1 &&
"Using FileID sentinel value");
1825 if (!
Loc.isMacroID())
return Loc;
1845 llvm::MemoryBufferRef getFakeBufferForRecovery()
const;
1852 return const_cast<SourceManager *
>(
this)->getSLocEntryOrNull(FID);
1858 return Invalid ? nullptr : &Entry;
1861 const SrcMgr::SLocEntry *getSLocEntryForFile(FileID FID)
const {
1862 return const_cast<SourceManager *
>(
this)->getSLocEntryForFile(FID);
1865 SrcMgr::SLocEntry *getSLocEntryForFile(FileID FID) {
1866 if (
auto *Entry = getSLocEntryOrNull(FID))
1867 if (Entry->isFile())
1874 const SrcMgr::SLocEntry &getSLocEntryByID(
int ID,
1875 bool *
Invalid =
nullptr)
const {
1879 SrcMgr::SLocEntry &getSLocEntryByID(
int ID,
bool *
Invalid =
nullptr) {
1880 assert(
ID != -1 &&
"Using FileID sentinel value");
1882 return getLoadedSLocEntryByID(
ID,
Invalid);
1886 const SrcMgr::SLocEntry &
1887 getLoadedSLocEntryByID(
int ID,
bool *
Invalid =
nullptr)
const {
1892 SrcMgr::SLocEntry &getLoadedSLocEntryByID(
int ID,
bool *
Invalid =
nullptr) {
1898 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
1899 return LastFileIDLookup;
1901 return getFileIDSlow(SLocOffset);
1905 return SLocOffset < CurrentLoadedOffset;
1909 return SLocOffset >= CurrentLoadedOffset;
1915 createExpansionLocImpl(
const SrcMgr::ExpansionInfo &Expansion,
1916 unsigned Length,
int LoadedID = 0,
1921 inline bool isOffsetInFileID(FileID FID,
1925 if (SLocOffset < Entry.getOffset())
return false;
1932 if (FID.ID+1 ==
static_cast<int>(LocalSLocEntryTable.size()))
1933 return SLocOffset < NextLocalOffset;
1937 return SLocOffset < getSLocEntryByID(FID.ID+1).
getOffset();
1942 FileID getPreviousFileID(FileID FID)
const;
1946 FileID getNextFileID(FileID FID)
const;
1953 FileID createFileIDImpl(SrcMgr::ContentCache &
File, StringRef
Filename,
1954 SourceLocation IncludePos,
1958 SrcMgr::ContentCache &getOrCreateContentCache(FileEntryRef SourceFile,
1959 bool isSystemFile =
false);
1962 SrcMgr::ContentCache &
1963 createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf);
1969 SourceLocation getExpansionLocSlowCase(SourceLocation
Loc)
const;
1970 SourceLocation getSpellingLocSlowCase(SourceLocation
Loc)
const;
1971 SourceLocation getFileLocSlowCase(SourceLocation
Loc)
const;
1973 std::pair<FileID, unsigned>
1974 getDecomposedExpansionLocSlowCase(
const SrcMgr::SLocEntry *
E)
const;
1975 std::pair<FileID, unsigned>
1976 getDecomposedSpellingLocSlowCase(
const SrcMgr::SLocEntry *
E,
1977 unsigned Offset)
const;
1978 void computeMacroArgsCache(MacroArgsMap &MacroArgsCache, FileID FID)
const;
1979 void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
1981 SourceLocation SpellLoc,
1982 SourceLocation ExpansionLoc,
1983 unsigned ExpansionLength)
const;
1984 void updateSlocUsageStats()
const;
2000 return SM.isBeforeInTranslationUnit(LHS, RHS);
2034 std::unique_ptr<FileManager> FileMgr;
2035 std::unique_ptr<DiagnosticsEngine> Diagnostics;
2036 std::unique_ptr<SourceManager> SourceMgr;
Defines the Diagnostic-related interfaces.
Defines interfaces for clang::FileEntry and clang::FileEntryRef.
Defines the clang::FileManager interface and associated types.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Defines the clang::SourceLocation class and associated facilities.
Reads an AST files chain containing the contents of a translation unit.
Writes an AST file containing the contents of a translation unit.
bool operator()(SourceLocation LHS, SourceLocation RHS) const
BeforeThanCompare(SourceManager &SM)
BeforeThanCompare(SourceManager &SM)
bool operator()(SourceRange LHS, SourceRange RHS) const
Comparison function object.
Represents a character-granular source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
void setTokenRange(bool TR)
Concrete class used by the front-end to report problems and issues.
External source of source location entries.
virtual int getSLocEntryID(SourceLocation::UIntTy SLocOffset)=0
Get the index ID for the loaded SourceLocation offset.
virtual std::pair< SourceLocation, StringRef > getModuleImportLoc(int ID)=0
Retrieve the module import location and name for the given ID, if in fact it was loaded from a module...
virtual ~ExternalSLocEntrySource()
virtual bool ReadSLocEntry(int ID)=0
Read the source location entry with index ID, which will always be less than -1.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
const FileEntry & getFileEntry() const
Cached information about one file (either on disk or in the virtual file system).
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Implements support for file system lookup, file system caching, and directory search management.
A SourceLocation and its associated SourceManager.
Holds the cache used by isBeforeInTranslationUnit.
void setCommonLoc(FileID commonFID, unsigned lCommonOffset, unsigned rCommonOffset, bool LParentBeforeRParent)
bool getCachedResult(unsigned LOffset, unsigned ROffset) const
If the cache is valid, compute the result given the specified offsets in the LHS/RHS FileID's.
void setQueryFIDs(FileID LHS, FileID RHS)
Set up a new query.
InBeforeInTUCacheEntry(FileID L, FileID R)
InBeforeInTUCacheEntry()=default
bool isCacheValid() const
Return true if the currently cached values match up with the specified LHS/RHS query.
Used to hold and unique data used to represent #line information.
Represents an unpacked "presumed" location which can be presented to the user.
const char * getFilename() const
Return the presumed filename of this location.
bool isInvalid() const
Return true if this object is invalid or uninitialized.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
SourceManager and necessary dependencies (e.g.
This class handles loading and caching of source files into memory.
std::optional< StringRef > getNonBuiltinFilenameForID(FileID FID) const
Returns the filename for the provided FileID, unless it's a built-in buffer that's not represented by...
bool isMacroBodyExpansion(SourceLocation Loc) const
Tests whether the given source location represents the expansion of a macro body.
unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid=nullptr) const
bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the "source location address space".
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
bool isAtEndOfImmediateMacroExpansion(SourceLocation Loc, SourceLocation *MacroEnd=nullptr) const
Returns true if the given MacroID location points at the character end of the immediate macro expansi...
DiagnosticsEngine & getDiagnostics() const
SourceLocation::UIntTy getNextLocalOffset() const
bool isWrittenInBuiltinFile(SourceLocation Loc) const
Returns whether Loc is located in a <built-in> file.
unsigned getColumnNumber(FileID FID, unsigned FilePos, bool *Invalid=nullptr) const
Return the column # for the specified file position.
void noteSLocAddressSpaceUsage(DiagnosticsEngine &Diag, std::optional< unsigned > MaxNotes=32) const
void setAllFilesAreTransient(bool Transient)
Specify that all files that are read during this compilation are transient.
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
bool isLocalSourceLocation(SourceLocation Loc) const
Returns true if Loc did not come from a PCH/Module.
bool isInMainFile(SourceLocation Loc) const
Returns whether the PresumedLoc for a given SourceLocation is in the main file.
void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, bool IsFileEntry, bool IsFileExit, SrcMgr::CharacteristicKind FileKind)
Add a line note to the line table for the FileID and offset specified by Loc.
SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr, bool UserFilesAreVolatile=false)
SourceLocation createTokenSplitLoc(SourceLocation SpellingLoc, SourceLocation TokenStart, SourceLocation TokenEnd)
Return a new SourceLocation that encodes that the token starting at TokenStart ends prematurely at To...
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
const FileEntry * getFileEntryForSLocEntry(const SrcMgr::SLocEntry &SLocEntry) const
Returns the FileEntry record for the provided SLocEntry.
bool isWrittenInCommandLineFile(SourceLocation Loc) const
Returns whether Loc is located in a <command line> file.
MemoryBufferSizes getMemoryBufferSizes() const
Return the amount of memory used by memory buffers, breaking down by heap-backed versus mmap'ed memor...
void setFileIsTransient(FileEntryRef SourceFile)
Specify that a file is transient.
bool isFileOverridden(const FileEntry *File) const
Returns true if the file contents have been overridden.
OptionalFileEntryRef getFileEntryRefForID(FileID FID) const
Returns the FileEntryRef for the provided FileID.
CharSourceRange getExpansionRange(SourceRange Range) const
Given a SourceRange object, return the range of tokens or characters covered by the expansion in the ...
SourceLocation getFileLoc(SourceLocation Loc) const
Given Loc, if it is a macro location return the expansion location or the spelling location,...
bool isInSLocAddrSpace(SourceLocation Loc, SourceLocation Start, unsigned Length, SourceLocation::UIntTy *RelativeOffset=nullptr) const
Returns true if Loc is inside the [Start, +Length) chunk of the source location address space.
SourceLocation translateLineCol(FileID FID, unsigned Line, unsigned Col) const
Get the source location in FID for the given line:col.
size_t getContentCacheSize() const
Return the total amount of physical memory allocated by the ContentCache allocator.
StringRef getBufferName(SourceLocation Loc, bool *Invalid=nullptr) const
Return the filename or buffer identifier of the buffer the location is in.
SourceLocation getTopMacroCallerLoc(SourceLocation Loc) const
std::optional< StringRef > getBufferDataOrNone(FileID FID) const
Return a StringRef to the source buffer data for the specified FileID, returning std::nullopt if inva...
unsigned getExpansionColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
FileID translateFile(const FileEntry *SourceFile) const
Get the FileID for the given file.
CharSourceRange getExpansionRange(CharSourceRange Range) const
Given a CharSourceRange object, return the range of tokens or characters covered by the expansion in ...
void setModuleBuildStack(ModuleBuildStack stack)
Set the module build stack.
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID=0, SourceLocation::UIntTy LoadedOffset=0)
Create a new FileID that represents the specified file being #included from the specified IncludePosi...
void PrintStats() const
Print statistics to stderr.
FileID getUniqueLoadedASTFileID(SourceLocation Loc) const
SrcMgr::SLocEntry & getSLocEntry(FileID FID, bool *Invalid=nullptr)
bool isMainFile(const FileEntry &SourceFile)
Returns true when the given FileEntry corresponds to the main file.
size_t getDataStructureSizes() const
Return the amount of memory used for various side tables and data structures in the SourceManager.
bool isMacroArgExpansion(SourceLocation Loc, SourceLocation *StartLoc=nullptr) const
Tests whether the given source location represents a macro argument's expansion into the function-lik...
bool isInTheSameTranslationUnitImpl(const std::pair< FileID, unsigned > &LOffs, const std::pair< FileID, unsigned > &ROffs) const
Determines whether the two decomposed source location is in the same TU.
const SrcMgr::SLocEntry & getLocalSLocEntry(unsigned Index) const
Get a local SLocEntry. This is exposed for indexing.
SourceLocation getComposedLoc(FileID FID, unsigned Offset) const
Form a SourceLocation from a FileID and Offset pair.
void setPreambleFileID(FileID Preamble)
Set the file ID for the precompiled preamble.
OptionalFileEntryRef bypassFileContentsOverride(FileEntryRef File)
Bypass the overridden contents of a file.
bool userFilesAreVolatile() const
True if non-system source files should be treated as volatile (likely to change while trying to use t...
const SrcMgr::SLocEntry & getLoadedSLocEntry(unsigned Index, bool *Invalid=nullptr) const
Get a loaded SLocEntry. This is exposed for indexing.
SourceLocation getSpellingLoc(SourceLocation Loc) const
Given a SourceLocation object, return the spelling location referenced by the ID.
FileManager & getFileManager() const
void setOverridenFilesKeepOriginalName(bool value)
Set true if the SourceManager should report the original file name for contents of files that were ov...
fileinfo_iterator fileinfo_end() const
FileID translateFile(FileEntryRef SourceFile) const
ModuleBuildStack getModuleBuildStack() const
Retrieve the module build stack.
unsigned local_sloc_entry_size() const
Get the number of local SLocEntries we have.
std::optional< StringRef > getBufferDataIfLoaded(FileID FID) const
Return a StringRef to the source buffer data for the specified FileID, returning std::nullopt if it's...
SourceLocation getLocForEndOfFile(FileID FID) const
Return the source location corresponding to the last byte of the specified file.
FileID getMainFileID() const
Returns the FileID of the main source file.
SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const
Gets the location of the immediate macro caller, one level up the stack toward the initial macro type...
const char * getCharacterData(SourceLocation SL, bool *Invalid=nullptr) const
Return a pointer to the start of the specified location in the appropriate spelling MemoryBuffer.
std::pair< int, SourceLocation::UIntTy > AllocateLoadedSLocEntries(unsigned NumSLocEntries, SourceLocation::UIntTy TotalSize)
Allocate a number of loaded SLocEntries, which will be actually loaded on demand from the external so...
void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc)
Push an entry to the module build stack.
void overrideFileContents(FileEntryRef SourceFile, const llvm::MemoryBufferRef &Buffer)
Override the contents of the given source file by providing an already-allocated buffer.
SourceLocation getIncludeLoc(FileID FID) const
Returns the include location if FID is a #include'd file otherwise it returns an invalid location.
llvm::MemoryBufferRef getBufferOrFake(FileID FID, SourceLocation Loc=SourceLocation()) const
Return the buffer for the specified FileID.
unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid=nullptr) const
unsigned getFileIDSize(FileID FID) const
The size of the SLocEntry that FID represents.
bool isInSystemMacro(SourceLocation loc) const
Returns whether Loc is expanded from a macro in a system header.
void setMainFileID(FileID FID)
Set the file ID for the main source file.
unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid=nullptr) const
Given a SourceLocation, return the spelling line number for the position indicated.
std::pair< bool, bool > isInTheSameTranslationUnit(std::pair< FileID, unsigned > &LOffs, std::pair< FileID, unsigned > &ROffs) const
Determines whether the two decomposed source location is in the same translation unit.
llvm::DenseMap< FileEntryRef, SrcMgr::ContentCache * >::const_iterator fileinfo_iterator
CharSourceRange getImmediateExpansionRange(SourceLocation Loc) const
Return the start/end of the expansion information for an expansion location.
unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
bool hasLineTable() const
Determine if the source manager has a line table.
CharSourceRange getExpansionRange(SourceLocation Loc) const
Given a SourceLocation object, return the range of tokens covered by the expansion in the ultimate fi...
bool isWrittenInScratchSpace(SourceLocation Loc) const
Returns whether Loc is located in a <scratch space> file.
bool isInFileID(SourceLocation Loc, FileID FID, unsigned *RelativeOffset=nullptr) const
Given a specific FileID, returns true if Loc is inside that FileID chunk and sets relative offset (of...
void setExternalSLocEntrySource(ExternalSLocEntrySource *Source)
unsigned getLineTableFilenameID(StringRef Str)
Return the uniqued ID for the specified filename.
void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs, bool Force=false)
Set the number of FileIDs (files and macros) that were created during preprocessing of FID,...
bool isLocalFileID(FileID FID) const
Returns true if FID did not come from a PCH/Module.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
FileID getPreambleFileID() const
Get the file ID for the precompiled preamble if there is one.
std::pair< FileID, unsigned > getDecomposedExpansionLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
SrcMgr::SLocEntry & getLocalSLocEntry(unsigned Index)
Get a local SLocEntry. This is exposed for indexing.
unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid=nullptr) const
void initializeForReplay(const SourceManager &Old)
Initialize this source manager suitably to replay the compilation described by Old.
bool isLoadedSourceLocation(SourceLocation Loc) const
Returns true if Loc came from a PCH/Module.
unsigned loaded_sloc_entry_size() const
Get the number of loaded SLocEntries we have.
SourceManager & operator=(const SourceManager &)=delete
FileID getOrCreateFileID(FileEntryRef SourceFile, SrcMgr::CharacteristicKind FileCharacter)
Get the FileID for SourceFile if it exists.
bool hasFileInfo(const FileEntry *File) const
SourceLocation translateFileLineCol(const FileEntry *SourceFile, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
std::pair< SourceLocation, StringRef > getModuleImportLoc(SourceLocation Loc) const
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
bool isLoadedFileID(FileID FID) const
Returns true if FID came from a PCH/Module.
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
std::pair< FileID, unsigned > getDecomposedSpellingLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS, SourceLocation::IntTy *RelativeOffset) const
Return true if both LHS and RHS are in the local source location address space or the loaded one.
llvm::MemoryBufferRef getMemoryBufferForFileOrFake(FileEntryRef File)
Retrieve the memory buffer associated with the given file.
SrcMgr::SLocEntry & getLoadedSLocEntry(unsigned Index, bool *Invalid=nullptr)
Get a loaded SLocEntry. This is exposed for indexing.
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file.
bool isAtStartOfImmediateMacroExpansion(SourceLocation Loc, SourceLocation *MacroBegin=nullptr) const
Returns true if the given MacroID location points at the beginning of the immediate macro expansion.
SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const
Return the file characteristic of the specified source location, indicating whether this is a normal ...
SourceLocation createExpansionLoc(SourceLocation SpellingLoc, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd, unsigned Length, bool ExpansionIsTokenRange=true, int LoadedID=0, SourceLocation::UIntTy LoadedOffset=0)
Creates an expansion SLocEntry for a macro use.
bool isWrittenInSameFile(SourceLocation Loc1, SourceLocation Loc2) const
Returns true if the spelling locations for both SourceLocations are part of the same file buffer.
bool isInExternCSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in an "extern C" system header.
unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
bool isWrittenInMainFile(SourceLocation Loc) const
Returns true if the spelling location for the given location is in the main file buffer.
bool isPointWithin(SourceLocation Location, SourceLocation Start, SourceLocation End) const
Return true if the Point is within Start and End.
SourceManager(const SourceManager &)=delete
std::pair< FileID, unsigned > getDecomposedIncludedLoc(FileID FID) const
Returns the "included/expanded in" decomposed location of the given FileID.
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID.
SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const
If Loc points inside a function macro argument, the returned location will be the macro location in w...
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
unsigned getNumCreatedFIDsForFileID(FileID FID) const
Get the number of FileIDs (files and macros) that were created during preprocessing of FID,...
std::optional< llvm::MemoryBufferRef > getBufferOrNone(FileID FID, SourceLocation Loc=SourceLocation()) const
Return the buffer for the specified FileID.
fileinfo_iterator fileinfo_begin() const
bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation::UIntTy RHS) const
Determines the order of a source location and a source location offset in the "source location addres...
LineTableInfo & getLineTable()
Retrieve the stored line table.
SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const
Given a SourceLocation object, return the spelling location referenced by the ID.
std::optional< llvm::MemoryBufferRef > getMemoryBufferForFileOrNone(FileEntryRef File)
Retrieve the memory buffer associated with the given file.
const SrcMgr::SLocEntry & getSLocEntry(FileID FID, bool *Invalid=nullptr) const
SourceLocation createMacroArgExpansionLoc(SourceLocation SpellingLoc, SourceLocation ExpansionLoc, unsigned Length)
Creates an expansion SLocEntry for the substitution of an argument into a function-like macro's body.
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
One instance of this struct is kept for every file loaded or used.
void setBuffer(std::unique_ptr< llvm::MemoryBuffer > B)
Set the buffer.
std::optional< StringRef > getBufferDataIfLoaded() const
Return a StringRef to the source buffer data, only if it has already been loaded.
void setUnownedBuffer(std::optional< llvm::MemoryBufferRef > B)
Set the buffer to one that's not owned (or to nullptr).
OptionalFileEntryRef ContentsEntry
References the file which the contents were actually loaded from.
unsigned getSizeBytesMapped() const
Returns the number of bytes actually mapped for this ContentCache.
unsigned IsTransient
True if this file may be transient, that is, if it might not exist at some later point in time when t...
StringRef Filename
The filename that is used to access OrigEntry.
ContentCache(FileEntryRef Ent)
ContentCache(FileEntryRef Ent, FileEntryRef contentEnt)
unsigned getSize() const
Returns the size of the content encapsulated by this ContentCache.
llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const
Returns the kind of memory used to back the memory buffer for this content cache.
unsigned IsFileVolatile
True if this content cache was initially created for a source file considered to be volatile (likely ...
LineOffsetMapping SourceLineCache
A bump pointer allocated array of offsets for each source line.
std::optional< llvm::MemoryBufferRef > getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM, SourceLocation Loc=SourceLocation()) const
Returns the memory buffer for the associated content.
static const char * getInvalidBOM(StringRef BufStr)
std::optional< llvm::MemoryBufferRef > getBufferIfLoaded() const
Return the buffer, only if it has been loaded.
unsigned BufferOverridden
Indicates whether the buffer itself was provided to override the actual file contents.
ContentCache(const ContentCache &RHS)
The copy ctor does not allow copies where source object has either a non-NULL Buffer or SourceLineCac...
ContentCache & operator=(const ContentCache &RHS)=delete
OptionalFileEntryRef OrigEntry
Reference to the file entry representing this ContentCache.
Each ExpansionInfo encodes the expansion location - where the token was ultimately expanded,...
SourceLocation getExpansionLocStart() const
static ExpansionInfo create(SourceLocation SpellingLoc, SourceLocation Start, SourceLocation End, bool ExpansionIsTokenRange=true)
Return a ExpansionInfo for an expansion.
bool isMacroBodyExpansion() const
bool isExpansionTokenRange() const
bool isFunctionMacroExpansion() const
SourceLocation getSpellingLoc() const
CharSourceRange getExpansionLocRange() const
bool isMacroArgExpansion() const
static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc, SourceLocation ExpansionLoc)
Return a special ExpansionInfo for the expansion of a macro argument into a function-like macro's bod...
static ExpansionInfo createForTokenSplit(SourceLocation SpellingLoc, SourceLocation Start, SourceLocation End)
Return a special ExpansionInfo representing a token that ends prematurely.
SourceLocation getExpansionLocEnd() const
Information about a FileID, basically just the logical file that it represents and include stack info...
const ContentCache & getContentCache() const
CharacteristicKind getFileCharacteristic() const
Return whether this is a system header or not.
static FileInfo get(SourceLocation IL, ContentCache &Con, CharacteristicKind FileCharacter, StringRef Filename)
Return a FileInfo object.
bool hasLineDirectives() const
Return true if this FileID has #line directives in it.
void setHasLineDirectives()
Set the flag that indicates that this FileID has line table entries associated with it.
SourceLocation getIncludeLoc() const
StringRef getName() const
Returns the name of the file that was used when the file was loaded from the underlying file system.
Mapping of line offsets into a source file.
const unsigned * begin() const
LineOffsetMapping()=default
const unsigned * end() const
const unsigned & operator[](int I) const
static LineOffsetMapping get(llvm::MemoryBufferRef Buffer, llvm::BumpPtrAllocator &Alloc)
ArrayRef< unsigned > getLines() const
This is a discriminated union of FileInfo and ExpansionInfo.
SourceLocation::UIntTy getOffset() const
static SLocEntry getOffsetOnly(SourceLocation::UIntTy Offset)
Creates an incomplete SLocEntry that is only able to report its offset.
static SLocEntry get(SourceLocation::UIntTy Offset, const FileInfo &FI)
const FileInfo & getFile() const
static SLocEntry get(SourceLocation::UIntTy Offset, const ExpansionInfo &Expansion)
const ExpansionInfo & getExpansion() const
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
bool isSystem(CharacteristicKind CK)
Determine whether a file / directory characteristic is for system code.
bool isModuleMap(CharacteristicKind CK)
Determine whether a file characteristic is for a module map.
The JSON file list parser is used to communicate input to InstallAPI.
const size_t malloc_bytes
MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)