summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Tooling/DependencyScannerTest.cpp
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2019-08-22 18:15:50 +0000
committerAlex Lorenz <arphaman@gmail.com>2019-08-22 18:15:50 +0000
commit4dc5573acc0d2e7c59d8bac2543eb25cb4b32984 (patch)
tree79d7d47df40888fc8802d0f98f1dee747a10ff25 /clang/unittests/Tooling/DependencyScannerTest.cpp
parent15ee5ba6e75759f5ca3d58f941fb6f8023741491 (diff)
downloadbcm5719-llvm-4dc5573acc0d2e7c59d8bac2543eb25cb4b32984.tar.gz
bcm5719-llvm-4dc5573acc0d2e7c59d8bac2543eb25cb4b32984.zip
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 llvm-svn: 369680
Diffstat (limited to 'clang/unittests/Tooling/DependencyScannerTest.cpp')
-rw-r--r--clang/unittests/Tooling/DependencyScannerTest.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/clang/unittests/Tooling/DependencyScannerTest.cpp b/clang/unittests/Tooling/DependencyScannerTest.cpp
new file mode 100644
index 00000000000..fa60d0a7524
--- /dev/null
+++ b/clang/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
OpenPOWER on IntegriCloud