From 9d3530bd4ebaa7dc02494db440026f87f2bb8bc4 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Fri, 14 Sep 2018 12:24:09 +0000 Subject: [PATCH] [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 llvm-svn: 342228 --- clang/lib/Tooling/JSONCompilationDatabase.cpp | 5 ++++- clang/test/Tooling/auto-detect-from-source.cpp | 10 +++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/clang/lib/Tooling/JSONCompilationDatabase.cpp b/clang/lib/Tooling/JSONCompilationDatabase.cpp index 2fa5fce279d6..b0feaa229c11 100644 --- a/clang/lib/Tooling/JSONCompilationDatabase.cpp +++ b/clang/lib/Tooling/JSONCompilationDatabase.cpp @@ -157,13 +157,16 @@ std::vector unescapeCommandLine(JSONCommandLineSyntax Syntax, 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 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; } }; diff --git a/clang/test/Tooling/auto-detect-from-source.cpp b/clang/test/Tooling/auto-detect-from-source.cpp index 12a660d585a9..e6241a9ec3e5 100644 --- a/clang/test/Tooling/auto-detect-from-source.cpp +++ b/clang/test/Tooling/auto-detect-from-source.cpp @@ -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