mirror of https://github.com/microsoft/clang.git
[Tooling] JSONCompilationDatabasePlugin infers compile commands for missing files
Summary: See the existing InterpolatingCompilationDatabase for details on how this works. We've been using this in clangd for a while, the heuristics seem to work well. Reviewers: bkramer Subscribers: ilya-biryukov, ioeric, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D51729 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@342228 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
fdf354a5a0
commit
bff78a489e
|
@ -157,13 +157,16 @@ std::vector<std::string> unescapeCommandLine(JSONCommandLineSyntax Syntax,
|
||||||
return parser.parse();
|
return parser.parse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This plugin locates a nearby compile_command.json file, and also infers
|
||||||
|
// compile commands for files not present in the database.
|
||||||
class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin {
|
class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin {
|
||||||
std::unique_ptr<CompilationDatabase>
|
std::unique_ptr<CompilationDatabase>
|
||||||
loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override {
|
loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override {
|
||||||
SmallString<1024> JSONDatabasePath(Directory);
|
SmallString<1024> JSONDatabasePath(Directory);
|
||||||
llvm::sys::path::append(JSONDatabasePath, "compile_commands.json");
|
llvm::sys::path::append(JSONDatabasePath, "compile_commands.json");
|
||||||
return JSONCompilationDatabase::loadFromFile(
|
auto Base = JSONCompilationDatabase::loadFromFile(
|
||||||
JSONDatabasePath, ErrorMessage, JSONCommandLineSyntax::AutoDetect);
|
JSONDatabasePath, ErrorMessage, JSONCommandLineSyntax::AutoDetect);
|
||||||
|
return Base ? inferMissingCompileCommands(std::move(Base)) : nullptr;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
// RUN: rm -rf %t
|
// RUN: rm -rf %t
|
||||||
// RUN: mkdir %t
|
// RUN: mkdir %t
|
||||||
// RUN: echo "[{\"directory\":\".\",\"command\":\"clang++ -c %/t/test.cpp\",\"file\":\"%/t/test.cpp\"}]" | sed -e 's/\\/\\\\/g' > %t/compile_commands.json
|
// RUN: echo "[{\"directory\":\".\",\"command\":\"clang++ -DSECRET=XYZZY -c %/t/test.cpp\",\"file\":\"%/t/test.cpp\"}]" | sed -e 's/\\/\\\\/g' > %t/compile_commands.json
|
||||||
// RUN: cp "%s" "%t/test.cpp"
|
// RUN: cp "%s" "%t/test.cpp"
|
||||||
// RUN: not clang-check "%t/test.cpp" 2>&1 | FileCheck %s
|
// RUN: not clang-check "%t/test.cpp" 2>&1 | FileCheck %s
|
||||||
|
|
||||||
// CHECK: C++ requires
|
// CHECK: XYZZY
|
||||||
invalid;
|
SECRET;
|
||||||
|
|
||||||
|
// Copy to a different file, and rely on the command being inferred.
|
||||||
|
// RUN: cp "%s" "%t/other.cpp"
|
||||||
|
// RUN: not clang-check "%t/other.cpp" 2>&1 | FileCheck %s
|
||||||
|
|
Loading…
Reference in New Issue