Go to the documentation of this file.
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/PointerIntPair.h"
47 #include "llvm/ADT/SmallVector.h"
48 #include "llvm/ADT/StringRef.h"
49 #include "llvm/Support/Allocator.h"
50 #include "llvm/Support/Compiler.h"
51 #include "llvm/Support/MemoryBuffer.h"
101 explicit operator bool()
const {
return Storage; }
115 llvm::BumpPtrAllocator &Alloc);
119 llvm::BumpPtrAllocator &Alloc);
123 unsigned *Storage =
nullptr;
132 mutable std::unique_ptr<llvm::MemoryBuffer> Buffer;
196 "Passed ContentCache object cannot own a buffer.");
233 return Buffer->getMemBufferRef();
241 return Buffer->getBuffer();
248 Buffer = std::move(B);
255 assert(!Buffer &&
"Expected to be called right after construction");
257 setBuffer(llvm::MemoryBuffer::getMemBuffer(*B));
267 static_assert(
alignof(ContentCache) >= 8,
268 "ContentCache must be 8-byte aligned.");
297 unsigned NumCreatedFIDs : 31;
300 unsigned HasLineDirectives : 1;
303 llvm::PointerIntPair<const ContentCache *, 3, CharacteristicKind>
312 X.NumCreatedFIDs = 0;
313 X.HasLineDirectives =
false;
314 X.ContentAndKind.setPointer(&Con);
315 X.ContentAndKind.setInt(FileCharacter);
325 return *ContentAndKind.getPointer();
330 return ContentAndKind.getInt();
363 bool ExpansionIsTokenRange;
371 return ExpansionLocStart;
409 bool ExpansionIsTokenRange =
true) {
411 X.SpellingLoc = SpellingLoc;
412 X.ExpansionLocStart = Start;
413 X.ExpansionLocEnd =
End;
414 X.ExpansionIsTokenRange = ExpansionIsTokenRange;
453 return create(SpellingLoc, Start,
End,
false);
461 static_assert(
sizeof(FileInfo) <=
sizeof(ExpansionInfo),
462 "FileInfo must be no larger than ExpansionInfo.");
486 assert(
isFile() &&
"Not a file SLocEntry!");
491 assert(
isExpansion() &&
"Not a macro expansion SLocEntry!");
496 assert(!(
Offset & (1ULL << OffsetBits)) &&
"Offset is too large");
499 E.IsExpansion =
false;
506 assert(!(
Offset & (1ULL << OffsetBits)) &&
"Offset is too large");
509 E.IsExpansion =
true;
543 FileID LQueryFID, RQueryFID;
548 bool IsLQFIDBeforeRQFID;
559 unsigned LCommonOffset, RCommonOffset;
567 return LQueryFID == LHS && RQueryFID == RHS;
575 if (LQueryFID != CommonFID) LOffset = LCommonOffset;
576 if (RQueryFID != CommonFID) ROffset = RCommonOffset;
583 if (LOffset == ROffset)
584 return IsLQFIDBeforeRQFID;
586 return LOffset < ROffset;
594 IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
598 LQueryFID = RQueryFID =
FileID();
599 IsLQFIDBeforeRQFID =
false;
603 unsigned rCommonOffset) {
604 CommonFID = commonFID;
605 LCommonOffset = lCommonOffset;
606 RCommonOffset = rCommonOffset;
633 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
641 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
645 bool OverridenFilesKeepOriginalName =
true;
649 bool UserFilesAreVolatile;
654 bool FilesAreTransient =
false;
656 struct OverriddenFilesInfoTy {
659 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
667 std::unique_ptr<OverriddenFilesInfoTy> OverriddenFilesInfo;
669 OverriddenFilesInfoTy &getOverriddenFilesInfo() {
670 if (!OverriddenFilesInfo)
671 OverriddenFilesInfo.reset(
new OverriddenFilesInfoTy);
672 return *OverriddenFilesInfo;
679 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
713 llvm::BitVector SLocEntryLoaded;
722 mutable FileID LastFileIDLookup;
727 std::unique_ptr<LineTableInfo> LineTable;
731 mutable FileID LastLineNoFileIDQuery;
733 mutable unsigned LastLineNoFilePos;
734 mutable unsigned LastLineNoResult;
743 mutable unsigned NumLinearScans = 0;
744 mutable unsigned NumBinaryProbes = 0;
751 mutable llvm::DenseMap<FileID, std::pair<FileID, unsigned>> IncludedLocMap;
754 using IsBeforeInTUCacheKey = std::pair<FileID, FileID>;
758 using InBeforeInTUCache =
759 llvm::DenseMap<IsBeforeInTUCacheKey, InBeforeInTUCacheEntry>;
762 mutable InBeforeInTUCache IBTUCache;
770 mutable std::unique_ptr<llvm::MemoryBuffer> FakeBufferForRecovery;
772 mutable std::unique_ptr<SrcMgr::ContentCache> FakeContentCacheForRecovery;
774 mutable std::unique_ptr<SrcMgr::SLocEntry> FakeSLocEntryForRecovery;
778 using MacroArgsMap = std::map<unsigned, SourceLocation>;
780 mutable llvm::DenseMap<FileID, std::unique_ptr<MacroArgsMap>>
794 bool UserFilesAreVolatile =
false);
813 OverridenFilesKeepOriginalName = value;
822 return StoredModuleBuildStack;
827 StoredModuleBuildStack.clear();
828 StoredModuleBuildStack.append(stack.begin(), stack.end());
833 StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc));
855 assert(PreambleFileID.
isInvalid() &&
"PreambleFileID already set!");
920 bool ExpansionIsTokenRange =
true,
942 return getFakeBufferForRecovery();
953 const llvm::MemoryBufferRef &Buffer) {
965 std::unique_ptr<llvm::MemoryBuffer> Buffer);
967 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
982 if (OverriddenFilesInfo) {
983 if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(
File))
985 if (OverriddenFilesInfo->OverriddenFiles.find(
File) !=
986 OverriddenFilesInfo->OverriddenFiles.end())
1005 FilesAreTransient = Transient;
1017 if (
auto *Entry = getSLocEntryForFile(FID))
1018 return Entry->getFile().getContentCache().getBufferOrNone(
1027 llvm::MemoryBufferRef
1031 return getFakeBufferForRecovery();
1036 if (
auto *Entry = getSLocEntryForFile(FID))
1037 return Entry->getFile().getContentCache().OrigEntry;
1044 return Entry->getLastRef();
1082 if (
auto *Entry = getSLocEntryForFile(FID))
1083 return Entry->getFile().NumCreatedFIDs;
1090 bool Force =
false)
const {
1091 auto *Entry = getSLocEntryForFile(FID);
1094 assert((Force || Entry->getFile().NumCreatedFIDs == 0) &&
"Already set!");
1112 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
1113 return LastFileIDLookup;
1115 return getFileIDSlow(SLocOffset);
1124 if (
auto *Entry = getSLocEntryForFile(FID))
1125 return SourceLocation::getFileLoc(Entry->getOffset());
1132 if (
auto *Entry = getSLocEntryForFile(FID))
1133 return SourceLocation::getFileLoc(Entry->getOffset() +
1141 if (
auto *Entry = getSLocEntryForFile(FID))
1142 return Entry->getFile().getIncludeLoc();
1149 std::pair<SourceLocation, StringRef>
1167 return getExpansionLocSlowCase(Loc);
1175 return getFileLocSlowCase(Loc);
1194 End.isTokenRange());
1201 if (Expansion.
getEnd() == Range.getEnd())
1215 return getSpellingLocSlowCase(Loc);
1228 auto *Entry = getSLocEntryOrNull(FID);
1233 return Entry->isFile() ? SourceLocation::getFileLoc(GlobalOffset)
1234 : SourceLocation::getMacroLoc(GlobalOffset);
1243 auto *Entry = getSLocEntryOrNull(FID);
1245 return std::make_pair(
FileID(), 0);
1246 return std::make_pair(FID, Loc.getOffset() - Entry->getOffset());
1253 std::pair<FileID, unsigned>
1256 auto *E = getSLocEntryOrNull(FID);
1258 return std::make_pair(
FileID(), 0);
1260 unsigned Offset = Loc.getOffset()-E->getOffset();
1262 return std::make_pair(FID,
Offset);
1264 return getDecomposedExpansionLocSlowCase(E);
1271 std::pair<FileID, unsigned>
1274 auto *E = getSLocEntryOrNull(FID);
1276 return std::make_pair(
FileID(), 0);
1278 unsigned Offset = Loc.getOffset()-E->getOffset();
1280 return std::make_pair(FID,
Offset);
1281 return getDecomposedSpellingLocSlowCase(E,
Offset);
1340 assert(((Start.getOffset() < NextLocalOffset &&
1341 Start.getOffset()+Length <= NextLocalOffset) ||
1342 (Start.getOffset() >= CurrentLoadedOffset &&
1343 Start.getOffset()+Length < MaxLoadedOffset)) &&
1344 "Chunk is not valid SLoc address space");
1348 if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1350 *RelativeOffset = LocOffs - BeginOffs;
1365 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1366 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1368 if (LHSLoaded == RHSLoaded) {
1370 *RelativeOffset = RHSOffs - LHSOffs;
1386 bool *Invalid =
nullptr)
const;
1395 bool *Invalid =
nullptr)
const;
1397 bool *Invalid =
nullptr)
const;
1399 bool *Invalid =
nullptr)
const;
1401 bool *Invalid =
nullptr)
const;
1447 bool UseLineDirectives =
true)
const;
1480 return Filename.equals(
"<built-in>");
1489 return Filename.equals(
"<command line>");
1498 return Filename.equals(
"<scratch space>");
1538 unsigned *RelativeOffset =
nullptr)
const {
1540 if (isOffsetInFileID(FID, Offs)) {
1561 bool IsFileEntry,
bool IsFileExit,
1577 return ContentCacheAlloc.getTotalMemory();
1605 unsigned Line,
unsigned Col)
const;
1619 unsigned Line,
unsigned Col)
const;
1644 std::pair<bool, bool>
1646 std::pair<FileID, unsigned> &ROffs)
const;
1661 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1662 bool RHSLoaded = RHS >= CurrentLoadedOffset;
1663 if (LHSLoaded == RHSLoaded)
1664 return LHSOffset < RHS;
1672 return Location == Start || Location ==
End ||
1679 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::const_iterator;
1684 return FileInfos.find(
File) != FileInfos.end();
1697 assert(Index < LocalSLocEntryTable.size() &&
"Invalid index");
1698 return LocalSLocEntryTable[Index];
1706 bool *Invalid =
nullptr)
const {
1707 assert(Index < LoadedSLocEntryTable.size() &&
"Invalid index");
1708 if (SLocEntryLoaded[Index])
1709 return LoadedSLocEntryTable[Index];
1710 return loadSLocEntry(Index, Invalid);
1714 bool *Invalid =
nullptr)
const {
1715 if (FID.ID == 0 || FID.ID == -1) {
1716 if (Invalid) *Invalid =
true;
1717 return LocalSLocEntryTable[0];
1719 return getSLocEntryByID(FID.ID, Invalid);
1725 assert(LoadedSLocEntryTable.empty() &&
1726 "Invalidating existing loaded entries");
1727 ExternalSLocEntries = Source;
1736 std::pair<int, SourceLocation::UIntTy>
1742 return Loc.getOffset() >= CurrentLoadedOffset;
1747 return Loc.getOffset() < NextLocalOffset;
1752 assert(FID.ID != -1 &&
"Using FileID sentinel value");
1784 llvm::MemoryBufferRef getFakeBufferForRecovery()
const;
1792 return Invalid ? nullptr : &Entry;
1796 if (
auto *Entry = getSLocEntryOrNull(FID))
1797 if (Entry->isFile())
1804 const SrcMgr::SLocEntry &getSLocEntryByID(
int ID,
1805 bool *
Invalid =
nullptr)
const {
1806 assert(
ID != -1 &&
"Using FileID sentinel value");
1808 return getLoadedSLocEntryByID(
ID,
Invalid);
1812 const SrcMgr::SLocEntry &
1813 getLoadedSLocEntryByID(
int ID,
bool *
Invalid =
nullptr)
const {
1820 createExpansionLocImpl(
const SrcMgr::ExpansionInfo &Expansion,
1821 unsigned Length,
int LoadedID = 0,
1826 inline bool isOffsetInFileID(FileID FID,
1830 if (SLocOffset < Entry.getOffset())
return false;
1837 if (FID.ID+1 ==
static_cast<int>(LocalSLocEntryTable.size()))
1838 return SLocOffset < NextLocalOffset;
1842 return SLocOffset < getSLocEntryByID(FID.ID+1).
getOffset();
1847 FileID getPreviousFileID(FileID FID)
const;
1851 FileID getNextFileID(FileID FID)
const;
1858 FileID createFileIDImpl(SrcMgr::ContentCache &File, StringRef
Filename,
1859 SourceLocation IncludePos,
1863 SrcMgr::ContentCache &getOrCreateContentCache(FileEntryRef SourceFile,
1864 bool isSystemFile =
false);
1867 SrcMgr::ContentCache &
1868 createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf);
1874 SourceLocation getExpansionLocSlowCase(SourceLocation Loc)
const;
1875 SourceLocation getSpellingLocSlowCase(SourceLocation Loc)
const;
1876 SourceLocation getFileLocSlowCase(SourceLocation Loc)
const;
1878 std::pair<FileID, unsigned>
1879 getDecomposedExpansionLocSlowCase(
const SrcMgr::SLocEntry *E)
const;
1880 std::pair<FileID, unsigned>
1881 getDecomposedSpellingLocSlowCase(
const SrcMgr::SLocEntry *E,
1883 void computeMacroArgsCache(MacroArgsMap &MacroArgsCache, FileID FID)
const;
1884 void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
1886 SourceLocation SpellLoc,
1887 SourceLocation ExpansionLoc,
1888 unsigned ExpansionLength)
const;
1892 template<
typename T>
1904 return SM.isBeforeInTranslationUnit(LHS, RHS);
1938 std::unique_ptr<FileManager> FileMgr;
1939 std::unique_ptr<DiagnosticsEngine> Diagnostics;
1940 std::unique_ptr<SourceManager> SourceMgr;
1945 #endif // LLVM_CLANG_BASIC_SOURCEMANAGER_H
void overrideFileContents(FileEntryRef SourceFile, std::unique_ptr< llvm::MemoryBuffer > Buffer)
fileinfo_iterator fileinfo_begin() const
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.
bool isMacroBodyExpansion(SourceLocation Loc) const
Tests whether the given source location represents the expansion of a macro body.
CharSourceRange getExpansionLocRange() const
const FileEntry * ContentsEntry
References the file which the contents were actually loaded from.
static const char * getInvalidBOM(StringRef BufStr)
CharSourceRange getExpansionRange(SourceRange Range) const
Given a SourceRange object, return the range of tokens or characters covered by the expansion in the ...
bool isMainFile(const FileEntry &SourceFile)
Returns true when the given FileEntry corresponds to the main file.
SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr, bool UserFilesAreVolatile=false)
ContentCache(const ContentCache &RHS)
The copy ctor does not allow copies where source object has either a non-NULL Buffer or SourceLineCac...
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const
Return the file characteristic of the specified source location, indicating whether this is a normal ...
BeforeThanCompare(SourceManager &SM)
FileID getOrCreateFileID(const FileEntry *SourceFile, SrcMgr::CharacteristicKind FileCharacter)
Get the FileID for SourceFile if it exists.
const SrcMgr::SLocEntry & getLocalSLocEntry(unsigned Index) const
Get a local SLocEntry. This is exposed for indexing.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const
Returns the kind of memory used to back the memory buffer for this content cache.
FileID getPreambleFileID() const
Get the file ID for the precompiled preamble if there is one.
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.
bool isInExternCSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in an "extern C" system header.
std::pair< SourceLocation, StringRef > getModuleImportLoc(SourceLocation Loc) const
SourceLocation getBegin() const
A SourceLocation and its associated SourceManager.
SourceLocation::UIntTy getOffset() const
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...
ContentCache & operator=(const ContentCache &RHS)=delete
A trivial tuple used to represent a source range.
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.
SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const
Given a SourceLocation object, return the spelling location referenced by the ID.
bool isMacroBodyExpansion() const
bool isInMainFile(SourceLocation Loc) const
Returns whether the PresumedLoc for a given SourceLocation is in the main file.
bool operator()(SourceLocation LHS, SourceLocation RHS) const
bool isMacroArgExpansion() const
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
Encodes a location in the source.
void setBuffer(std::unique_ptr< llvm::MemoryBuffer > B)
Set the buffer.
void setExternalSLocEntrySource(ExternalSLocEntrySource *Source)
static LineOffsetMapping get(llvm::MemoryBufferRef Buffer, llvm::BumpPtrAllocator &Alloc)
SourceLocation getBegin() const
bool isCacheValid(FileID LHS, FileID RHS) const
Return true if the currently cached values match up with the specified LHS/RHS query.
SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const
If Loc points inside a function macro argument, the returned location will be the macro location in w...
llvm::DenseMap< const FileEntry *, SrcMgr::ContentCache * >::const_iterator fileinfo_iterator
unsigned IsFileVolatile
True if this content cache was initially created for a source file considered to be volatile (likely ...
bool isFunctionMacroExpansion() const
SourceLocation::UIntTy getNextLocalOffset() const
std::pair< FileID, unsigned > getDecomposedSpellingLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
llvm::Optional< llvm::MemoryBufferRef > getBufferOrNone(FileID FID, SourceLocation Loc=SourceLocation()) const
Return the buffer for the specified FileID.
void setCommonLoc(FileID commonFID, unsigned lCommonOffset, unsigned rCommonOffset)
SourceManager & operator=(const SourceManager &)=delete
Concrete class used by the front-end to report problems and issues.
void setOverridenFilesKeepOriginalName(bool value)
Set true if the SourceManager should report the original file name for contents of files that were ov...
CharSourceRange getImmediateExpansionRange(SourceLocation Loc) const
Return the start/end of the expansion information for an expansion location.
unsigned getExpansionColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
bool isWrittenInBuiltinFile(SourceLocation Loc) const
Returns whether Loc is located in a <built-in> file.
const unsigned * end() const
CharSourceRange getExpansionRange(SourceLocation Loc) const
Given a SourceLocation object, return the range of tokens covered by the expansion in the ultimate fi...
SourceLocation getFileLoc(SourceLocation Loc) const
Given Loc, if it is a macro location return the expansion location or the spelling location,...
bool isWrittenInScratchSpace(SourceLocation Loc) const
Returns whether Loc is located in a <scratch space> file.
const SrcMgr::SLocEntry & getSLocEntry(FileID FID, bool *Invalid=nullptr) const
SourceLocation getIncludeLoc(FileID FID) const
Returns the include location if FID is a #include'd file otherwise it returns an invalid location.
void PrintStats() const
Print statistics to stderr.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
ContentCache(const FileEntry *Ent=nullptr)
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.
bool isAtStartOfImmediateMacroExpansion(SourceLocation Loc, SourceLocation *MacroBegin=nullptr) const
Returns true if the given MacroID location points at the beginning of the immediate macro expansion.
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 setTokenRange(bool TR)
void setModuleBuildStack(ModuleBuildStack stack)
Set the module build stack.
bool isMacroArgExpansion(SourceLocation Loc, SourceLocation *StartLoc=nullptr) const
Tests whether the given source location represents a macro argument's expansion into the function-lik...
LineOffsetMapping SourceLineCache
A bump pointer allocated array of offsets for each source line.
Mapping of line offsets into a source file.
This class handles loading and caching of source files into memory.
const FileEntry * getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
Returns the FileEntry record for the provided SLocEntry.
unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
const ContentCache & getContentCache() const
void setAllFilesAreTransient(bool Transient)
Specify that all files that are read during this compilation are transient.
unsigned loaded_sloc_entry_size() const
Get the number of loaded SLocEntries we have.
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...
CharSourceRange getExpansionRange(CharSourceRange Range) const
Given a CharSourceRange object, return the range of tokens or characters covered by the expansion in ...
llvm::Optional< llvm::MemoryBufferRef > getBufferIfLoaded() const
Return the buffer, only if it has been loaded.
void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID)
Set up a new query.
const size_t malloc_bytes
SourceLocation translateLineCol(FileID FID, unsigned Line, unsigned Col) const
Get the source location in FID for the given line:col.
void setUnownedBuffer(llvm::Optional< llvm::MemoryBufferRef > B)
Set the buffer to one that's not owned (or to nullptr).
SourceLocation getTopMacroCallerLoc(SourceLocation Loc) const
Each ExpansionInfo encodes the expansion location - where the token was ultimately expanded,...
StringRef getName() const
Returns the name of the file that was used when the file was loaded from the underlying file system.
BeforeThanCompare(SourceManager &SM)
const char * getFilename() const
Return the presumed filename of this location.
unsigned getLineTableFilenameID(StringRef Str)
Return the uniqued ID for the specified filename.
bool hasLineDirectives() const
Return true if this FileID has #line directives in it.
FileID getMainFileID() const
Returns the FileID of the main source file.
bool isPointWithin(SourceLocation Location, SourceLocation Start, SourceLocation End) const
Return true if the Point is within Start and End.
llvm::Optional< StringRef > getBufferDataIfLoaded() const
Return a StringRef to the source buffer data, only if it has already been loaded.
const FileInfo & getFile() const
virtual bool ReadSLocEntry(int ID)=0
Read the source location entry with index ID, which will always be less than -1.
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 FileInfo get(SourceLocation IL, ContentCache &Con, CharacteristicKind FileCharacter, StringRef Filename)
Return a FileInfo object.
virtual ~ExternalSLocEntrySource()
bool isLoadedSourceLocation(SourceLocation Loc) const
Returns true if Loc came from a PCH/Module.
SourceLocation createTokenSplitLoc(SourceLocation SpellingLoc, SourceLocation TokenStart, SourceLocation TokenEnd)
Return a new SourceLocation that encodes that the token starting at TokenStart ends prematurely at To...
unsigned getSizeBytesMapped() const
Returns the number of bytes actually mapped for this ContentCache.
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 getSpellingLoc() const
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
unsigned getColumnNumber(FileID FID, unsigned FilePos, bool *Invalid=nullptr) const
Return the column # for the specified file position.
bool isWrittenInCommandLineFile(SourceLocation Loc) const
Returns whether Loc is located in a <command line> file.
CharacteristicKind getFileCharacteristic() const
Return whether this is a system header or not.
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 userFilesAreVolatile() const
True if non-system source files should be treated as volatile (likely to change while trying to use t...
SourceManager and necessary depdencies (e.g.
bool isInSystemMacro(SourceLocation loc) const
Returns whether Loc is expanded from a macro in a system header.
const SrcMgr::SLocEntry & getLoadedSLocEntry(unsigned Index, bool *Invalid=nullptr) const
Get a loaded SLocEntry. This is exposed for indexing.
SourceLocation getLocForEndOfFile(FileID FID) const
Return the source location corresponding to the last byte of the specified file.
Writes an AST file containing the contents of a translation unit.
const AnnotatedLine * Line
llvm::Optional< StringRef > getBufferDataIfLoaded(FileID FID) const
Return a StringRef to the source buffer data for the specified FileID, returning None if it's not yet...
ArrayRef< unsigned > getLines() const
Cached information about one file (either on disk or in the virtual file system).
std::pair< FileID, unsigned > getDecomposedLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
One instance of this struct is kept for every file loaded or used.
unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
LineTableInfo & getLineTable()
Retrieve the stored line table.
const unsigned & operator[](int I) const
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
bool isWrittenInSameFile(SourceLocation Loc1, SourceLocation Loc2) const
Returns true if the spelling locations for both SourceLocations are part of the same file buffer.
LineOffsetMapping()=default
FileID translateFile(FileEntryRef SourceFile) const
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file.
MemoryBufferSizes getMemoryBufferSizes() const
Return the amount of memory used by memory buffers, breaking down by heap-backed versus mmap'ed memor...
SourceLocation getIncludeLoc() const
bool isInvalid() const
Return true if this object is invalid or uninitialized.
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.
const ExpansionInfo & getExpansion() const
llvm::Optional< llvm::MemoryBufferRef > getMemoryBufferForFileOrNone(const FileEntry *File)
Retrieve the memory buffer associated with the given file.
bool isExpansionTokenRange() const
Comparison function object.
llvm::Optional< llvm::MemoryBufferRef > getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM, SourceLocation Loc=SourceLocation()) const
Returns the memory buffer for the associated content.
bool hasLineTable() const
Determine if the source manager has a line table.
bool operator()(SourceRange LHS, SourceRange RHS) const
Holds the cache used by isBeforeInTranslationUnit.
bool isLocalSourceLocation(SourceLocation Loc) const
Returns true if Loc did not come from a PCH/Module.
unsigned getSize() const
Returns the size of the content encapsulated by this ContentCache.
SourceLocation getExpansionLocStart() const
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...
unsigned IsTransient
True if this file may be transient, that is, if it might not exist at some later point in time when t...
ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
const char * getCharacterData(SourceLocation SL, bool *Invalid=nullptr) const
Return a pointer to the start of the specified location in the appropriate spelling MemoryBuffer.
SourceManagerForFile(StringRef FileName, StringRef Content)
Creates SourceManager and necessary depdencies (e.g.
Represents a character-granular source range.
void setMainFileID(FileID FID)
Set the file ID for the main source file.
SourceLocation getExpansionLocEnd() const
StringRef getBufferData(FileID FID, bool *Invalid=nullptr) const
Return a StringRef to the source buffer data for the specified FileID.
SourceLocation getEnd() const
const unsigned * begin() const
void overrideFileContents(const FileEntry *SourceFile, const llvm::MemoryBufferRef &Buffer)
Override the contents of the given source file by providing an already-allocated buffer.
fileinfo_iterator fileinfo_end() const
bool isLoadedFileID(FileID FID) const
Returns true if FID came from a PCH/Module.
Reads an AST files chain containing the contents of a translation unit.
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.
bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the "source location address space".
void initializeForReplay(const SourceManager &Old)
Initialize this source manager suitably to replay the compilation described by Old.
unsigned getNumCreatedFIDsForFileID(FileID FID) const
Get the number of FileIDs (files and macros) that were created during preprocessing of FID,...
llvm::MemoryBufferRef getMemoryBufferForFileOrFake(const FileEntry *File)
Retrieve the memory buffer associated with the given file.
Implements support for file system lookup, file system caching, and directory search management.
FileID createFileID(const FileEntry *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 setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs, bool Force=false) const
Set the number of FileIDs (files and macros) that were created during preprocessing of FID,...
std::pair< FileID, unsigned > getDecomposedIncludedLoc(FileID FID) const
Returns the "included/expanded in" decomposed location of the given FileID.
SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const
Gets the location of the immediate macro caller, one level up the stack toward the initial macro type...
const FileEntry * OrigEntry
Reference to the file entry representing this ContentCache.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
void setPreambleFileID(FileID Preamble)
Set the file ID for the precompiled preamble.
DiagnosticsEngine & getDiagnostics() const
static SLocEntry get(SourceLocation::UIntTy Offset, const FileInfo &FI)
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool isValid() const
Return true if this is a valid SourceLocation object.
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
bool isSystem(CharacteristicKind CK)
Determine whether a file / directory characteristic is for system code.
SourceLocation getComposedLoc(FileID FID, unsigned Offset) const
Form a SourceLocation from a FileID and Offset pair.
const FileEntry & getFileEntry() const
Used to hold and unique data used to represent #line information.
ModuleBuildStack getModuleBuildStack() const
Retrieve the module build stack.
FileManager & getFileManager() const
bool isModuleMap(CharacteristicKind CK)
Determine whether a file characteristic is for a module map.
unsigned local_sloc_entry_size() const
Get the number of local SLocEntries we have.
unsigned BufferOverridden
Indicates whether the buffer itself was provided to override the actual file contents.
Optional< FileEntryRef > getFileEntryRefForID(FileID FID) const
Returns the FileEntryRef for the provided FileID.
Information about a FileID, basically just the logical file that it represents and include stack info...
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID.
static ExpansionInfo create(SourceLocation SpellingLoc, SourceLocation Start, SourceLocation End, bool ExpansionIsTokenRange=true)
Return a ExpansionInfo for an expansion.
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...
Represents an unpacked "presumed" location which can be presented to the user.
SourceLocation translateFileLineCol(const FileEntry *SourceFile, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid=nullptr) const
bool isLocalFileID(FileID FID) const
Returns true if FID did not come from a PCH/Module.
bool hasFileInfo(const FileEntry *File) const
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.
unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid=nullptr) const
unsigned getFileIDSize(FileID FID) const
The size of the SLocEntry that FID represents.
Optional< FileEntryRef > bypassFileContentsOverride(FileEntryRef File)
Bypass the overridden contents of a file.
void setHasLineDirectives()
Set the flag that indicates that this FileID has line table entries associated with it.
std::pair< FileID, unsigned > getDecomposedExpansionLoc(SourceLocation Loc) const
Decompose the specified location into a raw FileID + Offset pair.
External source of source location entries.
llvm::MemoryBufferRef getBufferOrFake(FileID FID, SourceLocation Loc=SourceLocation()) const
Return the buffer for the specified FileID.
FileID translateFile(const FileEntry *SourceFile) const
Get the FileID for the given file.
static SLocEntry get(SourceLocation::UIntTy Offset, const ExpansionInfo &Expansion)
bool isWrittenInMainFile(SourceLocation Loc) const
Returns true if the spelling location for the given location is in the main file buffer.
size_t getDataStructureSizes() const
Return the amount of memory used for various side tables and data structures in the SourceManager.
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.
MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
bool isFileOverridden(const FileEntry *File) const
Returns true if the file contents have been overridden.
StringRef Filename
The filename that is used to access OrigEntry.
llvm::Optional< StringRef > getBufferDataOrNone(FileID FID) const
Return a StringRef to the source buffer data for the specified FileID, returning None if invalid.
static ExpansionInfo createForTokenSplit(SourceLocation SpellingLoc, SourceLocation Start, SourceLocation End)
Return a special ExpansionInfo representing a token that ends prematurely.
void setFileIsTransient(const FileEntry *SourceFile)
Specify that a file is transient.
unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid=nullptr) const
unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid=nullptr) const
Given a SourceLocation, return the spelling line number for the position indicated.
This is a discriminated union of FileInfo and ExpansionInfo.
SourceLocation getSpellingLoc(SourceLocation Loc) const
Given a SourceLocation object, return the spelling location referenced by the ID.