Author: sammccall Date: Fri Sep 14 05:24:09 2018 New Revision: 342228 URL: http://llvm.org/viewvc/llvm-project?rev=342228&view=rev Log: [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 Modified: cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp cfe/trunk/test/Tooling/auto-detect-from-source.cpp Modified: cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp?rev=342228&r1=342227&r2=342228&view=diff ============================================================================== --- cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp (original) +++ cfe/trunk/lib/Tooling/JSONCompilationDatabase.cpp Fri Sep 14 05:24:09 2018 @@ -157,13 +157,16 @@ std::vector<std::string> unescapeCommand 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 { std::unique_ptr<CompilationDatabase> loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override { SmallString<1024> JSONDatabasePath(Directory); llvm::sys::path::append(JSONDatabasePath, "compile_commands.json"); - return JSONCompilationDatabase::loadFromFile( + auto Base = JSONCompilationDatabase::loadFromFile( JSONDatabasePath, ErrorMessage, JSONCommandLineSyntax::AutoDetect); + return Base ? inferMissingCompileCommands(std::move(Base)) : nullptr; } }; Modified: cfe/trunk/test/Tooling/auto-detect-from-source.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Tooling/auto-detect-from-source.cpp?rev=342228&r1=342227&r2=342228&view=diff ============================================================================== --- cfe/trunk/test/Tooling/auto-detect-from-source.cpp (original) +++ cfe/trunk/test/Tooling/auto-detect-from-source.cpp Fri Sep 14 05:24:09 2018 @@ -1,8 +1,12 @@ // RUN: rm -rf %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: not clang-check "%t/test.cpp" 2>&1 | FileCheck %s -// CHECK: C++ requires -invalid; +// CHECK: XYZZY +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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits