[PATCH] D54657: [clang] Add -MJJ for appending to compilation databases.
Tom Roeder via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 16 15:54:50 PST 2018
tmroeder created this revision.
tmroeder added a reviewer: klimek.
Herald added a subscriber: cfe-commits.
Some build systems compile files more than once (e.g., Kbuild in the
Linux kernel compiles with -c once and with -E once). In these cases,
the -MJ flag will only record the latest compilation, since it will
overwrite the output file. This is a problem for libTooling because the
latest compilation-database entry is not always the right one to use for
libTooling-based tools.
This commit adds a new flag, called -MJJ, that appends to its argument
instead of overwriting it. This allows the compilation database to
accumulate all entries. It also allows a build to specify a single
compile_commands.json file as the argument and have all builds append to
it.
Repository:
rC Clang
https://reviews.llvm.org/D54657
Files:
include/clang/Driver/Options.td
lib/Driver/ToolChains/Clang.cpp
lib/Driver/ToolChains/Clang.h
test/Driver/compilation_database_append.c
Index: test/Driver/compilation_database_append.c
===================================================================
--- /dev/null
+++ test/Driver/compilation_database_append.c
@@ -0,0 +1,16 @@
+// RUN: mkdir -p %t && cd %t
+// RUN: %clang -MD -MP --sysroot=somewhere -c -x c %s -xc++ %s -Wall -MJJ - -no-canonical-prefixes 2>&1 | FileCheck %s
+// RUN: %clang -c -x c %s -MJJ json
+// RUN: %clang -E -x c %s -MJJ json
+// RUN: cat json | FileCheck --check-prefix=DOUBLE %s
+// RUN: not %clang -c -x c %s -MJJ %s/non-existant -no-canonical-prefixes 2>&1 | FileCheck --check-prefix=ERROR %s
+
+// CHECK: { "directory": "{{.*}}", "file": "[[SRC:[^"]+[/|\\]compilation_database_append.c]]", "output": "compilation_database_append.o", "arguments": ["{{[^"]*}}clang{{[^"]*}}", "-xc", "[[SRC]]", "--sysroot=somewhere", "-c", "-Wall",{{.*}} "--target={{[^"]+}}"]},
+// CHECK: { "directory": "{{.*}}", "file": "[[SRC:[^"]+[/|\\]compilation_database_append.c]]", "output": "compilation_database_append.o", "arguments": ["{{[^"]*}}clang{{[^"]*}}", "-xc++", "[[SRC]]", "--sysroot=somewhere", "-c", "-Wall",{{.*}} "--target={{[^"]+}}"]},
+// DOUBLE: { "directory": "{{.*}}", "file": "[[SRC:[^"]+[/|\\]compilation_database_append.c]]", "output": "compilation_database_append.o", "arguments": ["{{[^"]*}}clang{{[^"]*}}", "-xc", "[[SRC]]", "-c",{{.*}} "--target={{[^"]+}}"]},
+// DOUBLE-NEXT: { "directory": "{{.*}}", "file": "[[SRC:[^"]+[/|\\]compilation_database_append.c]]", "output": "-", "arguments": ["{{[^"]*}}clang{{[^"]*}}", "-xc", "[[SRC]]", "-E",{{.*}} "--target={{[^"]+}}"]},
+// ERROR: error: compilation database '{{.*}}/non-existant' could not be opened:
+
+int main(void) {
+ return 0;
+}
Index: lib/Driver/ToolChains/Clang.h
===================================================================
--- lib/Driver/ToolChains/Clang.h
+++ lib/Driver/ToolChains/Clang.h
@@ -94,7 +94,8 @@
void DumpCompilationDatabase(Compilation &C, StringRef Filename,
StringRef Target,
const InputInfo &Output, const InputInfo &Input,
- const llvm::opt::ArgList &Args) const;
+ const llvm::opt::ArgList &Args,
+ bool Append) const;
public:
Clang(const ToolChain &TC);
Index: lib/Driver/ToolChains/Clang.cpp
===================================================================
--- lib/Driver/ToolChains/Clang.cpp
+++ lib/Driver/ToolChains/Clang.cpp
@@ -1909,7 +1909,8 @@
void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
StringRef Target, const InputInfo &Output,
- const InputInfo &Input, const ArgList &Args) const {
+ const InputInfo &Input, const ArgList &Args,
+ bool Append) const {
// If this is a dry run, do not create the compilation database file.
if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
return;
@@ -1919,7 +1920,11 @@
if (!CompilationDatabase) {
std::error_code EC;
- auto File = llvm::make_unique<llvm::raw_fd_ostream>(Filename, EC, llvm::sys::fs::F_Text);
+ llvm::sys::fs::OpenFlags Flags = llvm::sys::fs::F_Text;
+ if (Append) {
+ Flags |= llvm::sys::fs::OF_Append;
+ }
+ auto File = llvm::make_unique<llvm::raw_fd_ostream>(Filename, EC, Flags);
if (EC) {
D.Diag(clang::diag::err_drv_compilationdatabase) << Filename
<< EC.message();
@@ -3365,8 +3370,12 @@
CmdArgs.push_back("-triple");
CmdArgs.push_back(Args.MakeArgString(TripleStr));
- if (const Arg *MJ = Args.getLastArg(options::OPT_MJ)) {
- DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args);
+ Arg *MJ = nullptr;
+ if ((MJ = Args.getLastArg(options::OPT_MJ)) ||
+ (MJ = Args.getLastArg(options::OPT_MJJ))) {
+ bool Append = MJ->getOption().matches(options::OPT_MJJ);
+ DumpCompilationDatabase(C, MJ->getValue(), TripleStr, Output, Input, Args,
+ Append);
Args.ClaimAllArgs(options::OPT_MJ);
}
Index: include/clang/Driver/Options.td
===================================================================
--- include/clang/Driver/Options.td
+++ include/clang/Driver/Options.td
@@ -380,6 +380,8 @@
HelpText<"Add missing headers to depfile">;
def MJ : JoinedOrSeparate<["-"], "MJ">, Group<M_Group>,
HelpText<"Write a compilation database entry per input">;
+def MJJ : JoinedOrSeparate<["-"], "MJJ">, Group<M_Group>,
+ HelpText<"Write or append to a compilation database entry per input">;
def MP : Flag<["-"], "MP">, Group<M_Group>, Flags<[CC1Option]>,
HelpText<"Create phony target for each dependency (other than main file)">;
def MQ : JoinedOrSeparate<["-"], "MQ">, Group<M_Group>, Flags<[CC1Option]>,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54657.174470.patch
Type: text/x-patch
Size: 4910 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181116/7800ca63/attachment.bin>
More information about the cfe-commits
mailing list