This patch changes type of the `File` parameter in `PPCallbacks::InclusionDirective()` from `const FileEntry *` to `Optional<FileEntryRef>`.
With the API change in place, this patch then removes some uses of the deprecated `FileEntry::getName()` (e.g. in `DependencyGraph.cpp` and `ModuleDependencyCollector.cpp`).
Reviewed By: dexonsmith, bnbarham
Differential Revision: https://reviews.llvm.org/D123574
void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
- const FileEntry * /*File*/, StringRef SearchPath,
+ Optional<FileEntryRef> /*File*/, StringRef SearchPath,
StringRef /*RelativePath*/,
const Module * /*Imported*/,
SrcMgr::CharacteristicKind /*FileType*/) override {
void ExpandModularHeadersPPCallbacks::InclusionDirective(
SourceLocation DirectiveLoc, const Token &IncludeToken,
StringRef IncludedFilename, bool IsAngled, CharSourceRange FilenameRange,
- const FileEntry *IncludedFile, StringRef SearchPath, StringRef RelativePath,
- const Module *Imported, SrcMgr::CharacteristicKind FileType) {
+ Optional<FileEntryRef> IncludedFile, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
+ SrcMgr::CharacteristicKind FileType) {
if (Imported) {
serialization::ModuleFile *MF =
Compiler.getASTReader()->getModuleManager().lookup(
void InclusionDirective(SourceLocation DirectiveLoc,
const Token &IncludeToken, StringRef IncludedFilename,
bool IsAngled, CharSourceRange FilenameRange,
- const FileEntry *IncludedFile, StringRef SearchPath,
- StringRef RelativePath, const Module *Imported,
+ Optional<FileEntryRef> IncludedFile,
+ StringRef SearchPath, StringRef RelativePath,
+ const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void EndOfMainFile() override;
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FileNameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FileNameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void EndOfMainFile() override;
void KernelNameRestrictionPPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &, StringRef FileName, bool,
- CharSourceRange, const FileEntry *, StringRef, StringRef, const Module *,
- SrcMgr::CharacteristicKind) {
+ CharSourceRange, Optional<FileEntryRef>, StringRef, StringRef,
+ const Module *, SrcMgr::CharacteristicKind) {
IncludeDirective ID = {HashLoc, FileName};
IncludeDirectives.push_back(std::move(ID));
}
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
private:
void SuspiciousIncludePPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
- bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
+ bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import)
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void EndOfMainFile() override;
void IncludeOrderPPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
- bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
+ bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
// We recognize the first include as a special main module header and want
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
private:
void RestrictedIncludesPPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
- bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
+ bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
// Compiler provided headers are allowed (e.g stddef.h).
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
private:
void IncludeModernizePPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
- bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
+ bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
// FIXME: Take care of library symbols from the global namespace.
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
clearCurrentEnum(HashLoc);
}
void RestrictedIncludesPPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
- bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
+ bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
if (!Check.contains(FileName) && SrcMgr::isSystem(FileType)) {
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void EndOfMainFile() override;
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void MacroDefined(const Token &MacroNameTok,
void DuplicateIncludeCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
- bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
+ bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
if (llvm::find(Files.back(), FileName) != Files.back().end()) {
void InclusionDirective(SourceLocation HashLocation,
const Token &IncludeToken, StringRef FileNameRef,
bool IsAngled, CharSourceRange FileNameRange,
- const FileEntry * /*IncludedFile*/,
+ Optional<FileEntryRef> /*IncludedFile*/,
StringRef /*SearchPath*/, StringRef /*RelativePath*/,
const Module * /*ImportedModule*/,
SrcMgr::CharacteristicKind /*FileType*/) override {
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
llvm::StringRef FileName, bool IsAngled,
CharSourceRange /*FilenameRange*/,
- const FileEntry *File, llvm::StringRef /*SearchPath*/,
+ Optional<FileEntryRef> File,
+ llvm::StringRef /*SearchPath*/,
llvm::StringRef /*RelativePath*/,
const clang::Module * /*Imported*/,
SrcMgr::CharacteristicKind FileKind) override {
auto &Inc = Out->MainFileIncludes.back();
Inc.Written =
(IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str();
- Inc.Resolved = std::string(File ? File->tryGetRealPathName() : "");
+ Inc.Resolved =
+ std::string(File ? File->getFileEntry().tryGetRealPathName() : "");
Inc.HashOffset = SM.getFileOffset(HashLoc);
Inc.HashLine =
SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1;
if (LastPragmaKeepInMainFileLine == Inc.HashLine)
Inc.BehindPragmaKeep = true;
if (File) {
- IncludeStructure::HeaderID HID = Out->getOrCreateID(File);
+ IncludeStructure::HeaderID HID = Out->getOrCreateID(*File);
Inc.HeaderID = static_cast<unsigned>(HID);
if (IsAngled)
if (auto StdlibHeader = tooling::stdlib::Header::named(Inc.Written)) {
// Record include graph (not just for main-file includes)
if (File) {
- auto *IncludingFileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc));
+ auto IncludingFileEntry = SM.getFileEntryRefForID(SM.getFileID(HashLoc));
if (!IncludingFileEntry) {
assert(SM.getBufferName(HashLoc).startswith("<") &&
"Expected #include location to be a file or <built-in>");
// Treat as if included from the main file.
- IncludingFileEntry = SM.getFileEntryForID(MainFID);
+ IncludingFileEntry = SM.getFileEntryRefForID(MainFID);
}
- auto IncludingID = Out->getOrCreateID(IncludingFileEntry),
- IncludedID = Out->getOrCreateID(File);
+ auto IncludingID = Out->getOrCreateID(*IncludingFileEntry),
+ IncludedID = Out->getOrCreateID(*File);
Out->IncludeChildren[IncludingID].push_back(IncludedID);
}
}
}
IncludeStructure::HeaderID
-IncludeStructure::getOrCreateID(const FileEntry *Entry) {
+IncludeStructure::getOrCreateID(FileEntryRef Entry) {
// Main file's FileEntry was not known at IncludeStructure creation time.
- if (Entry == MainFileEntry) {
+ if (&Entry.getFileEntry() == MainFileEntry) {
if (RealPathNames.front().empty())
RealPathNames.front() = MainFileEntry->tryGetRealPathName().str();
return MainFileID;
}
auto R = UIDToIndex.try_emplace(
- Entry->getUniqueID(),
+ Entry.getUniqueID(),
static_cast<IncludeStructure::HeaderID>(RealPathNames.size()));
if (R.second)
RealPathNames.emplace_back();
IncludeStructure::HeaderID Result = R.first->getSecond();
std::string &RealPathName = RealPathNames[static_cast<unsigned>(Result)];
if (RealPathName.empty())
- RealPathName = Entry->tryGetRealPathName().str();
+ RealPathName = Entry.getFileEntry().tryGetRealPathName().str();
return Result;
}
enum class HeaderID : unsigned {};
llvm::Optional<HeaderID> getID(const FileEntry *Entry) const;
- HeaderID getOrCreateID(const FileEntry *Entry);
+ HeaderID getOrCreateID(FileEntryRef Entry);
StringRef getRealPath(HeaderID ID) const {
assert(static_cast<unsigned>(ID) <= RealPathNames.size());
SynthesizedFilenameTok.setKind(tok::header_name);
SynthesizedFilenameTok.setLiteralData(Inc.Written.data());
- const FileEntry *FE = File ? &File->getFileEntry() : nullptr;
llvm::StringRef WrittenFilename =
llvm::StringRef(Inc.Written).drop_front().drop_back();
Delegate->InclusionDirective(
syntax::FileRange(SM, SynthesizedFilenameTok.getLocation(),
SynthesizedFilenameTok.getEndLoc())
.toCharRange(SM),
- FE, "SearchPath", "RelPath",
+ File, "SearchPath", "RelPath",
/*Imported=*/nullptr, Inc.FileKind);
if (File)
Delegate->FileSkipped(*File, SynthesizedFilenameTok, Inc.FileKind);
namespace clangd {
namespace {
-llvm::Optional<std::string> toURI(const FileEntry *File) {
+llvm::Optional<std::string> toURI(Optional<FileEntryRef> File) {
if (!File)
return llvm::None;
- auto AbsolutePath = File->tryGetRealPathName();
+ auto AbsolutePath = File->getFileEntry().tryGetRealPathName();
if (AbsolutePath.empty())
return llvm::None;
return URI::create(AbsolutePath).toString();
return;
const auto FileID = SM.getFileID(Loc);
- const auto *File = SM.getFileEntryForID(FileID);
+ auto File = SM.getFileEntryRefForID(FileID);
auto URI = toURI(File);
if (!URI)
return;
// Add edges from including files to includes.
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
llvm::StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
if (!IncludeURI)
return;
- auto IncludingURI = toURI(SM.getFileEntryForID(SM.getFileID(HashLoc)));
+ auto IncludingURI = toURI(SM.getFileEntryRefForID(SM.getFileID(HashLoc)));
if (!IncludingURI)
return;
void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
SrcMgr::CharacteristicKind FileType) override {
#ifndef NDEBUG
- auto URI = toURI(&SkippedFile.getFileEntry());
+ auto URI = toURI(SkippedFile);
if (!URI)
return;
auto I = IG.try_emplace(*URI);
#include "llvm/Support/Error.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Path.h"
+#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
IncludeStructure::HeaderID getID(StringRef Filename,
IncludeStructure &Includes) {
auto &SM = Clang->getSourceManager();
- auto Entry = SM.getFileManager().getFile(Filename);
- EXPECT_TRUE(Entry);
+ auto Entry = SM.getFileManager().getFileRef(Filename);
+ EXPECT_THAT_EXPECTED(Entry, llvm::Succeeded());
return Includes.getOrCreateID(*Entry);
}
#include "clang/Lex/Token.h"
#include "clang/Tooling/Syntax/Tokens.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock-matchers.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *,
- StringRef, StringRef, const clang::Module *,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef>, StringRef, StringRef,
+ const clang::Module *,
SrcMgr::CharacteristicKind) override {
Includes.emplace_back(SM, HashLoc, IncludeTok, FileName, IsAngled,
FilenameRange);
auto &FM = SM.getFileManager();
// Copy so that we can getOrCreateID().
IncludeStructure Includes = ExpectedAST.getIncludeStructure();
- auto MainFE = FM.getFile(testPath("foo.cpp"));
- ASSERT_TRUE(MainFE);
+ auto MainFE = FM.getFileRef(testPath("foo.cpp"));
+ ASSERT_THAT_EXPECTED(MainFE, llvm::Succeeded());
auto MainID = Includes.getOrCreateID(*MainFE);
auto &PatchedFM = PatchedAST->getSourceManager().getFileManager();
IncludeStructure PatchedIncludes = PatchedAST->getIncludeStructure();
- auto PatchedMainFE = PatchedFM.getFile(testPath("foo.cpp"));
- ASSERT_TRUE(PatchedMainFE);
+ auto PatchedMainFE = PatchedFM.getFileRef(testPath("foo.cpp"));
+ ASSERT_THAT_EXPECTED(PatchedMainFE, llvm::Succeeded());
auto PatchedMainID = PatchedIncludes.getOrCreateID(*PatchedMainFE);
EXPECT_EQ(Includes.includeDepth(MainID)[MainID],
PatchedIncludes.includeDepth(PatchedMainID)[PatchedMainID]);
// Include directive callback.
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
Checker.collectUmbrellaHeaderHeader(File->getName());
}
const clang::Token &IncludeTok,
llvm::StringRef FileName, bool IsAngled,
clang::CharSourceRange FilenameRange,
- const clang::FileEntry *File,
+ llvm::Optional<clang::FileEntryRef> File,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath,
const clang::Module *Imported,
void PreprocessorCallbacks::InclusionDirective(
clang::SourceLocation HashLoc, const clang::Token &IncludeTok,
llvm::StringRef FileName, bool IsAngled,
- clang::CharSourceRange FilenameRange, const clang::FileEntry *File,
- llvm::StringRef SearchPath, llvm::StringRef RelativePath,
- const clang::Module *Imported, clang::SrcMgr::CharacteristicKind FileType) {
+ clang::CharSourceRange FilenameRange,
+ llvm::Optional<clang::FileEntryRef> File, llvm::StringRef SearchPath,
+ llvm::StringRef RelativePath, const clang::Module *Imported,
+ clang::SrcMgr::CharacteristicKind FileType) {
int DirectiveLine, DirectiveColumn;
std::string HeaderPath = getSourceLocationFile(PP, HashLoc);
getSourceLocationLineAndColumn(PP, HashLoc, DirectiveLine, DirectiveColumn);
const Token &FilenameTok,
SrcMgr::CharacteristicKind FileType) {
beginCallback("FileSkipped");
- appendArgument("ParentFile", &SkippedFile.getFileEntry());
+ appendArgument("ParentFile", SkippedFile);
appendArgument("FilenameTok", FilenameTok);
appendArgument("FileType", FileType, CharacteristicKindStrings);
}
// of whether the inclusion will actually result in an inclusion.
void PPCallbacksTracker::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, llvm::StringRef FileName,
- bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
+ bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
llvm::StringRef SearchPath, llvm::StringRef RelativePath,
const Module *Imported, SrcMgr::CharacteristicKind FileType) {
beginCallback("InclusionDirective");
// Append a FileEntry argument to the top trace item.
void PPCallbacksTracker::appendArgument(const char *Name,
- const FileEntry *Value) {
+ Optional<FileEntryRef> Value) {
if (!Value) {
appendArgument(Name, "(null)");
return;
}
- appendFilePathArgument(Name, Value->getName());
+ appendArgument(Name, *Value);
+}
+
+void PPCallbacksTracker::appendArgument(const char *Name, FileEntryRef Value) {
+ appendFilePathArgument(Name, Value.getName());
}
// Append a SourceLocation argument to the top trace item.
SrcMgr::CharacteristicKind FileType) override;
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
llvm::StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
/// Append a FileID argument to the top trace item.
void appendArgument(const char *Name, FileID Value);
- /// Append a FileEntry argument to the top trace item.
- void appendArgument(const char *Name, const FileEntry *Value);
+ /// Append a FileEntryRef argument to the top trace item.
+ void appendArgument(const char *Name, Optional<FileEntryRef> Value);
+ void appendArgument(const char *Name, FileEntryRef Value);
/// Append a SourceLocation argument to the top trace item.
void appendArgument(const char *Name, SourceLocation Value);
StringRef FileName,
bool IsAngled,
CharSourceRange FilenameRange,
- const FileEntry *File,
+ Optional<FileEntryRef> File,
StringRef SearchPath,
StringRef RelativePath,
const Module *Imported,
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
First->InclusionDirective(HashLoc, IncludeTok, FileName, IsAngled,
FilenameRange, File, SearchPath, RelativePath,
unsigned ImportedModule : 1;
/// The file that was included.
- const FileEntry *File;
+ Optional<FileEntryRef> File;
public:
- InclusionDirective(PreprocessingRecord &PPRec,
- InclusionKind Kind, StringRef FileName,
- bool InQuotes, bool ImportedModule,
- const FileEntry *File, SourceRange Range);
+ InclusionDirective(PreprocessingRecord &PPRec, InclusionKind Kind,
+ StringRef FileName, bool InQuotes, bool ImportedModule,
+ Optional<FileEntryRef> File, SourceRange Range);
/// Determine what kind of inclusion directive this is.
InclusionKind getKind() const { return static_cast<InclusionKind>(Kind); }
/// Retrieve the file entry for the actual file that was included
/// by this directive.
- const FileEntry *getFile() const { return File; }
+ Optional<FileEntryRef> getFile() const { return File; }
// Implement isa/cast/dyncast/etc.
static bool classof(const PreprocessedEntity *PE) {
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
- const FileEntry *File, StringRef SearchPath,
+ Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
FileID PrevFID) override;
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void moduleImport(SourceLocation ImportLoc, ModuleIdPath Path,
const Module *Imported) override;
void MacroPPCallbacks::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
- bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
+ bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
/// Callback invoked whenever a directive (#xxx) is processed.
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
/// Hook called whenever a macro definition is seen.
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
if (!File)
DepCollector.maybeAddDependency(FileName, /*FromModule*/false,
const Preprocessor *PP;
std::string OutputFile;
std::string SysRoot;
- llvm::SetVector<const FileEntry *> AllFiles;
- typedef llvm::DenseMap<const FileEntry *,
- SmallVector<const FileEntry *, 2> > DependencyMap;
+ llvm::SetVector<FileEntryRef> AllFiles;
+ using DependencyMap =
+ llvm::DenseMap<FileEntryRef, SmallVector<FileEntryRef, 2>>;
DependencyMap Dependencies;
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void EndOfMainFile() override {
StringRef FileName,
bool IsAngled,
CharSourceRange FilenameRange,
- const FileEntry *File,
+ Optional<FileEntryRef> File,
StringRef SearchPath,
StringRef RelativePath,
const Module *Imported,
return;
SourceManager &SM = PP->getSourceManager();
- const FileEntry *FromFile
- = SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
+ Optional<FileEntryRef> FromFile =
+ SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(HashLoc)));
if (!FromFile)
return;
- Dependencies[FromFile].push_back(File);
+ Dependencies[*FromFile].push_back(*File);
- AllFiles.insert(File);
- AllFiles.insert(FromFile);
+ AllFiles.insert(*File);
+ AllFiles.insert(*FromFile);
}
raw_ostream &
OS.indent(2);
writeNodeReference(OS, AllFiles[I]);
OS << " [ shape=\"box\", label=\"";
- StringRef FileName = AllFiles[I]->getName();
+ StringRef FileName = AllFiles[I].getName();
if (FileName.startswith(SysRoot))
FileName = FileName.substr(SysRoot.size());
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
if (!File)
return;
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
- // File is null if it wasn't found.
+ // File is None if it wasn't found.
// (We have some false negatives if PP recovered e.g. <foo> -> "foo")
- if (File != nullptr)
+ if (File)
return;
// If it's a rare absolute include, we know the full path already.
FileID PrevFID) override;
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void Ident(SourceLocation Loc, StringRef str) override;
void PragmaMessage(SourceLocation Loc, StringRef Namespace,
StringRef FileName,
bool IsAngled,
CharSourceRange FilenameRange,
- const FileEntry *File,
+ Optional<FileEntryRef> File,
StringRef SearchPath,
StringRef RelativePath,
const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override;
void If(SourceLocation Loc, SourceRange ConditionRange,
ConditionValueKind ConditionValue) override;
StringRef /*FileName*/,
bool /*IsAngled*/,
CharSourceRange /*FilenameRange*/,
- const FileEntry * /*File*/,
+ Optional<FileEntryRef> /*File*/,
StringRef /*SearchPath*/,
StringRef /*RelativePath*/,
const Module *Imported,
if (Callbacks && !IsImportDecl) {
// Notify the callback object that we've seen an inclusion directive.
// FIXME: Use a different callback for a pp-import?
- Callbacks->InclusionDirective(
- HashLoc, IncludeTok, LookupFilename, isAngled, FilenameRange,
- File ? &File->getFileEntry() : nullptr, SearchPath, RelativePath,
- Action == Import ? SuggestedModule.getModule() : nullptr,
- FileCharacter);
+ Callbacks->InclusionDirective(HashLoc, IncludeTok, LookupFilename, isAngled,
+ FilenameRange, File, SearchPath, RelativePath,
+ Action == Import ? SuggestedModule.getModule()
+ : nullptr,
+ FileCharacter);
if (Action == Skip && File)
Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
}
InclusionDirective::InclusionDirective(PreprocessingRecord &PPRec,
InclusionKind Kind, StringRef FileName,
bool InQuotes, bool ImportedModule,
- const FileEntry *File, SourceRange Range)
+ Optional<FileEntryRef> File,
+ SourceRange Range)
: PreprocessingDirective(InclusionDirectiveKind, Range), InQuotes(InQuotes),
Kind(Kind), ImportedModule(ImportedModule), File(File) {
char *Memory = (char *)PPRec.Allocate(FileName.size() + 1, alignof(char));
StringRef FileName,
bool IsAngled,
CharSourceRange FilenameRange,
- const FileEntry *File,
+ Optional<FileEntryRef> File,
StringRef SearchPath,
StringRef RelativePath,
const Module *Imported,
case PPD_INCLUSION_DIRECTIVE: {
const char *FullFileNameStart = Blob.data() + Record[0];
StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
- const FileEntry *File = nullptr;
+ Optional<FileEntryRef> File;
if (!FullFileName.empty())
- if (auto FE = PP.getFileManager().getFile(FullFileName))
- File = *FE;
+ File = PP.getFileManager().getOptionalFileRef(FullFileName);
// FIXME: Stable encoding
InclusionDirective::InclusionKind Kind
void ModuleDepCollectorPP::InclusionDirective(
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
- bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
+ bool IsAngled, CharSourceRange FilenameRange, Optional<FileEntryRef> File,
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) {
if (!File && !Imported) {
return nullptr;
const InclusionDirective *ID = getCursorInclusionDirective(cursor);
- return const_cast<FileEntry *>(ID->getFile());
+ Optional<FileEntryRef> File = ID->getFile();
+ return const_cast<FileEntry *>(File ? &File->getFileEntry() : nullptr);
}
unsigned clang_Cursor_getObjCPropertyAttributes(CXCursor C, unsigned reserved) {
void CXIndexDataConsumer::ppIncludedFile(SourceLocation hashLoc,
StringRef filename,
- const FileEntry *File,
+ Optional<FileEntryRef> File,
bool isImport, bool isAngled,
bool isModuleImport) {
if (!CB.ppIncludedFile)
return;
+ const FileEntry *FE = File ? &File->getFileEntry() : nullptr;
+
ScratchAlloc SA(*this);
CXIdxIncludedFileInfo Info = { getIndexLoc(hashLoc),
SA.toCStr(filename),
static_cast<CXFile>(
- const_cast<FileEntry *>(File)),
+ const_cast<FileEntry *>(FE)),
isImport, isAngled, isModuleImport };
CXIdxClientFile idxFile = CB.ppIncludedFile(ClientData, &Info);
- FileMap[File] = idxFile;
+ FileMap[FE] = idxFile;
}
void CXIndexDataConsumer::importedModule(const ImportDecl *ImportD) {
void enteredMainFile(const FileEntry *File);
- void ppIncludedFile(SourceLocation hashLoc,
- StringRef filename, const FileEntry *File,
- bool isImport, bool isAngled, bool isModuleImport);
+ void ppIncludedFile(SourceLocation hashLoc, StringRef filename,
+ Optional<FileEntryRef> File, bool isImport, bool isAngled,
+ bool isModuleImport);
void importedModule(const ImportDecl *ImportD);
void importedPCH(const FileEntry *File);
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
bool isImport = (IncludeTok.is(tok::identifier) &&
IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import);
public:
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
- CharSourceRange FilenameRange, const FileEntry *File,
- StringRef SearchPath, StringRef RelativePath,
- const Module *Imported,
+ CharSourceRange FilenameRange,
+ Optional<FileEntryRef> File, StringRef SearchPath,
+ StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
this->HashLoc = HashLoc;
this->IncludeTok = IncludeTok;
SmallString<16> FileName;
bool IsAngled;
CharSourceRange FilenameRange;
- const FileEntry* File;
+ Optional<FileEntryRef> File;
SmallString<16> SearchPath;
SmallString<16> RelativePath;
const Module* Imported;