Use FileEntryRef for PPCallbacks::HasInclude

This fixes the issue where a filename dependendency was missing if the file that
was referenced with __has_include() was accessed through a symlink in an earlier run,
if the file manager was reused between runs.

llvm-svn: 370081
This commit is contained in:
Alex Lorenz 2019-08-27 17:32:42 +00:00
parent 3d9b39b733
commit 1c8a4b7204
6 changed files with 41 additions and 7 deletions

View File

@ -210,7 +210,7 @@ void ExpandModularHeadersPPCallbacks::PragmaDiagnostic(SourceLocation Loc,
parseToLocation(Loc);
}
void ExpandModularHeadersPPCallbacks::HasInclude(SourceLocation Loc, StringRef,
bool, const FileEntry *,
bool, Optional<FileEntryRef>,
SrcMgr::CharacteristicKind) {
parseToLocation(Loc);
}

View File

@ -83,7 +83,7 @@ private:
void PragmaDiagnosticPop(SourceLocation Loc, StringRef) override;
void PragmaDiagnostic(SourceLocation Loc, StringRef, diag::Severity,
StringRef) override;
void HasInclude(SourceLocation Loc, StringRef, bool, const FileEntry *,
void HasInclude(SourceLocation Loc, StringRef, bool, Optional<FileEntryRef> ,
SrcMgr::CharacteristicKind) override;
void PragmaOpenCLExtension(SourceLocation NameLoc, const IdentifierInfo *,
SourceLocation StateLoc, unsigned) override;

View File

@ -307,7 +307,7 @@ public:
/// Hook called when a '__has_include' or '__has_include_next' directive is
/// read.
virtual void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled,
const FileEntry *File,
Optional<FileEntryRef> File,
SrcMgr::CharacteristicKind FileType) {}
/// Hook called when a source range is skipped.
@ -489,7 +489,7 @@ public:
}
void HasInclude(SourceLocation Loc, StringRef FileName, bool IsAngled,
const FileEntry *File,
Optional<FileEntryRef> File,
SrcMgr::CharacteristicKind FileType) override {
First->HasInclude(Loc, FileName, IsAngled, File, FileType);
Second->HasInclude(Loc, FileName, IsAngled, File, FileType);

View File

@ -83,7 +83,7 @@ struct DepCollectorPPCallbacks : public PPCallbacks {
}
void HasInclude(SourceLocation Loc, StringRef SpelledFilename, bool IsAngled,
const FileEntry *File,
Optional<FileEntryRef> File,
SrcMgr::CharacteristicKind FileType) override {
if (!File)
return;

View File

@ -1219,8 +1219,7 @@ static bool EvaluateHasIncludeCommon(Token &Tok,
if (File)
FileType =
PP.getHeaderSearchInfo().getFileDirFlavor(&File->getFileEntry());
Callbacks->HasInclude(FilenameLoc, Filename, isAngled,
File ? &File->getFileEntry() : nullptr, FileType);
Callbacks->HasInclude(FilenameLoc, Filename, isAngled, File, FileType);
}
// Get the result value. A result of true means the file exists.

View File

@ -161,5 +161,40 @@ TEST(DependencyScanner, ScanDepsReuseFilemanagerSkippedFile) {
EXPECT_EQ(convert_to_slash(Deps[5]), "/root/header.h");
}
TEST(DependencyScanner, ScanDepsReuseFilemanagerHasInclude) {
std::vector<std::string> Compilation = {"-c", "-E", "-MT", "test.cpp.o"};
StringRef CWD = "/root";
FixedCompilationDatabase CDB(CWD, Compilation);
auto VFS = new llvm::vfs::InMemoryFileSystem();
VFS->setCurrentWorkingDirectory(CWD);
auto Sept = llvm::sys::path::get_separator();
std::string HeaderPath = llvm::formatv("{0}root{0}header.h", Sept);
std::string SymlinkPath = llvm::formatv("{0}root{0}symlink.h", Sept);
std::string TestPath = llvm::formatv("{0}root{0}test.cpp", Sept);
VFS->addFile(HeaderPath, 0, llvm::MemoryBuffer::getMemBuffer("\n"));
VFS->addHardLink(SymlinkPath, HeaderPath);
VFS->addFile(
TestPath, 0,
llvm::MemoryBuffer::getMemBuffer("#if __has_include(\"header.h\") && "
"__has_include(\"symlink.h\")\n#endif"));
ClangTool Tool(CDB, {"test.cpp", "test.cpp"},
std::make_shared<PCHContainerOperations>(), VFS);
Tool.clearArgumentsAdjusters();
std::vector<std::string> Deps;
TestDependencyScanningAction Action(Deps);
Tool.run(&Action);
using llvm::sys::path::convert_to_slash;
ASSERT_EQ(Deps.size(), 6u);
EXPECT_EQ(convert_to_slash(Deps[0]), "/root/test.cpp");
EXPECT_EQ(convert_to_slash(Deps[1]), "/root/header.h");
EXPECT_EQ(convert_to_slash(Deps[2]), "/root/symlink.h");
EXPECT_EQ(convert_to_slash(Deps[3]), "/root/test.cpp");
EXPECT_EQ(convert_to_slash(Deps[4]), "/root/header.h");
EXPECT_EQ(convert_to_slash(Deps[5]), "/root/symlink.h");
}
} // end namespace tooling
} // end namespace clang