diff options
author | Alex Lorenz <arphaman@gmail.com> | 2019-08-22 18:15:50 +0000 |
---|---|---|
committer | Alex Lorenz <arphaman@gmail.com> | 2019-08-22 18:15:50 +0000 |
commit | 5195a35aa7c12eab8cf35edefd0208dc0ddcde00 (patch) | |
tree | 1a470d2caeb437f99161a1d8c35318f5b6bc833a /unittests | |
parent | 979eefe3686b1845d8ecd7be814863a5150c8728 (diff) | |
download | clang-5195a35aa7c12eab8cf35edefd0208dc0ddcde00.tar.gz |
Introduce FileEntryRef and use it when handling includes to report correct dependencies
when the FileManager is reused across invocations
This commit introduces a parallel API to FileManager's getFile: getFileEntryRef, which returns
a reference to the FileEntry, and the name that was used to access the file. In the case of
a VFS with 'use-external-names', the FileEntyRef contains the external name of the file,
not the filename that was used to access it.
The new API is adopted only in the HeaderSearch and Preprocessor for include file lookup, so that the
accessed path can be propagated to SourceManager's FileInfo. SourceManager's FileInfo now can report this accessed path, using
the new getName method. This API is then adopted in the dependency collector, which now correctly reports dependencies when a file
is included both using a symlink and a real path in the case when the FileManager is reused across multiple Preprocessor invocations.
Note that this patch does not fix all dependency collector issues, as the same problem is still present in other cases when dependencies
are obtained using FileSkipped, InclusionDirective, and HasInclude. This will be fixed in follow-up commits.
Differential Revision: https://reviews.llvm.org/D65907
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@369680 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/Tooling/CMakeLists.txt | 1 | ||||
-rw-r--r-- | unittests/Tooling/DependencyScannerTest.cpp | 118 |
2 files changed, 119 insertions, 0 deletions
diff --git a/unittests/Tooling/CMakeLists.txt b/unittests/Tooling/CMakeLists.txt index c699d41c28..3a4094a8b4 100644 --- a/unittests/Tooling/CMakeLists.txt +++ b/unittests/Tooling/CMakeLists.txt @@ -15,6 +15,7 @@ add_clang_unittest(ToolingTests CastExprTest.cpp CommentHandlerTest.cpp CompilationDatabaseTest.cpp + DependencyScannerTest.cpp DiagnosticsYamlTest.cpp ExecutionTest.cpp FixItTest.cpp diff --git a/unittests/Tooling/DependencyScannerTest.cpp b/unittests/Tooling/DependencyScannerTest.cpp new file mode 100644 index 0000000000..fa60d0a752 --- /dev/null +++ b/unittests/Tooling/DependencyScannerTest.cpp @@ -0,0 +1,118 @@ +//===- unittest/Tooling/ToolingTest.cpp - Tooling unit tests --------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/ASTConsumer.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclGroup.h" +#include "clang/Frontend/ASTUnit.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendAction.h" +#include "clang/Frontend/FrontendActions.h" +#include "clang/Tooling/CompilationDatabase.h" +#include "clang/Tooling/Tooling.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "gtest/gtest.h" +#include <algorithm> +#include <string> + +namespace clang { +namespace tooling { + +namespace { + +/// Prints out all of the gathered dependencies into a string. +class TestFileCollector : public DependencyFileGenerator { +public: + TestFileCollector(DependencyOutputOptions &Opts, + std::vector<std::string> &Deps) + : DependencyFileGenerator(Opts), Deps(Deps) {} + + void finishedMainFile(DiagnosticsEngine &Diags) override { + Deps = getDependencies(); + } + +private: + std::vector<std::string> &Deps; +}; + +class TestDependencyScanningAction : public tooling::ToolAction { +public: + TestDependencyScanningAction(std::vector<std::string> &Deps) : Deps(Deps) {} + + bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation, + FileManager *FileMgr, + std::shared_ptr<PCHContainerOperations> PCHContainerOps, + DiagnosticConsumer *DiagConsumer) override { + CompilerInstance Compiler(std::move(PCHContainerOps)); + Compiler.setInvocation(std::move(Invocation)); + Compiler.setFileManager(FileMgr); + + Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false); + if (!Compiler.hasDiagnostics()) + return false; + + Compiler.createSourceManager(*FileMgr); + Compiler.addDependencyCollector(std::make_shared<TestFileCollector>( + Compiler.getInvocation().getDependencyOutputOpts(), Deps)); + + auto Action = std::make_unique<PreprocessOnlyAction>(); + return Compiler.ExecuteAction(*Action); + } + +private: + std::vector<std::string> &Deps; +}; + +} // namespace + +TEST(DependencyScanner, ScanDepsReuseFilemanager) { + 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); + VFS->addFile("/root/header.h", 0, llvm::MemoryBuffer::getMemBuffer("\n")); + VFS->addHardLink("/root/symlink.h", "/root/header.h"); + VFS->addFile("/root/test.cpp", 0, + llvm::MemoryBuffer::getMemBuffer( + "#include \"symlink.h\"\n#include \"header.h\"\n")); + + ClangTool Tool(CDB, {"test.cpp"}, std::make_shared<PCHContainerOperations>(), + VFS); + Tool.clearArgumentsAdjusters(); + std::vector<std::string> Deps; + TestDependencyScanningAction Action(Deps); + Tool.run(&Action); + // The first invocation should return dependencies in order of access. + ASSERT_EQ(Deps.size(), 3u); + EXPECT_EQ(Deps[0], "/root/test.cpp"); + EXPECT_EQ(Deps[1], "/root/symlink.h"); + EXPECT_EQ(Deps[2], "/root/header.h"); + + // The file manager should still have two FileEntries, as one file is a + // hardlink. + FileManager &Files = Tool.getFiles(); + EXPECT_EQ(Files.getNumUniqueRealFiles(), 2u); + + Deps.clear(); + Tool.run(&Action); + // The second invocation should have the same order of dependencies. + ASSERT_EQ(Deps.size(), 3u); + EXPECT_EQ(Deps[0], "/root/test.cpp"); + EXPECT_EQ(Deps[1], "/root/symlink.h"); + EXPECT_EQ(Deps[2], "/root/header.h"); + + EXPECT_EQ(Files.getNumUniqueRealFiles(), 2u); +} + +} // end namespace tooling +} // end namespace clang |