aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Tooling/CompilationDatabaseTest.cpp
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2021-02-24 02:43:53 +0100
committerSam McCall <sam.mccall@gmail.com>2021-03-01 09:43:59 +0100
commit588db1ccff713332c1f9358f423e682f18c06e8e (patch)
tree971386c9dace62e87e2f732364b7d38fca368d81 /clang/unittests/Tooling/CompilationDatabaseTest.cpp
parent9fac8496eae809c288096037d7a3f5a1a3d04c7a (diff)
downloadllvm-588db1ccff713332c1f9358f423e682f18c06e8e.zip
llvm-588db1ccff713332c1f9358f423e682f18c06e8e.tar.gz
llvm-588db1ccff713332c1f9358f423e682f18c06e8e.tar.bz2
[clangd] Use flags from open files when opening headers they include
Currently our strategy for getting header compile flags is something like: A) look for flags for the header in compile_commands.json This basically never works, build systems don't generate this info. B) try to match to an impl file in compile_commands.json and use its flags This only (mostly) works if the headers are in the same project. C) give up and use fallback flags This kind of works for stdlib in the default configuration, and otherwise doesn't. Obviously there are big gaps here. This patch inserts a new attempt between A and B: if the header is transitively included by any open file (whether same project or not), then we use its compile command. This doesn't make any attempt to solve some related problems: - parsing non-self-contained header files in context (importing PP state) - using the compile flags of non-opened candidate files found in the index Fixes https://github.com/clangd/clangd/issues/123 Fixes https://github.com/clangd/clangd/issues/695 See https://github.com/clangd/clangd/issues/519 Differential Revision: https://reviews.llvm.org/D97351
Diffstat (limited to 'clang/unittests/Tooling/CompilationDatabaseTest.cpp')
-rw-r--r--clang/unittests/Tooling/CompilationDatabaseTest.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/clang/unittests/Tooling/CompilationDatabaseTest.cpp b/clang/unittests/Tooling/CompilationDatabaseTest.cpp
index a3ea899..ba40a7a 100644
--- a/clang/unittests/Tooling/CompilationDatabaseTest.cpp
+++ b/clang/unittests/Tooling/CompilationDatabaseTest.cpp
@@ -843,6 +843,18 @@ TEST_F(InterpolateTest, DriverModes) {
EXPECT_EQ(getCommand("bar.h"), "clang -D bar.cpp --driver-mode=cl /TP");
}
+TEST(TransferCompileCommandTest, Smoke) {
+ CompileCommand Cmd;
+ Cmd.Filename = "foo.cc";
+ Cmd.CommandLine = {"clang", "-Wall", "foo.cc"};
+ Cmd.Directory = "dir";
+ CompileCommand Transferred = transferCompileCommand(std::move(Cmd), "foo.h");
+ EXPECT_EQ(Transferred.Filename, "foo.h");
+ EXPECT_THAT(Transferred.CommandLine,
+ ElementsAre("clang", "-Wall", "-x", "c++-header", "foo.h"));
+ EXPECT_EQ(Transferred.Directory, "dir");
+}
+
TEST(CompileCommandTest, EqualityOperator) {
CompileCommand CCRef("/foo/bar", "hello.c", {"a", "b"}, "hello.o");
CompileCommand CCTest = CCRef;