[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-26 Thread Alex Lorenz via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
arphaman marked an inline comment as done.
Closed by commit rG8679ef4e46a4: [driver] add a new option 
`-gen-cdb-fragment-path` to emit a fragment of a… (authored by arphaman).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66555/new/

https://reviews.llvm.org/D66555

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Clang.h
  clang/test/Driver/gen-cdb-fragment.c

Index: clang/test/Driver/gen-cdb-fragment.c
===
--- /dev/null
+++ clang/test/Driver/gen-cdb-fragment.c
@@ -0,0 +1,37 @@
+// REQUIRES: x86-registered-target
+// RUN: rm -rf %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -c %s -o -  -gen-cdb-fragment-path %t.cdb
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+// CHECK-LS: gen-cdb-fragment.c.{{.*}}.json
+
+// RUN: cat %t.cdb/*.json | FileCheck --check-prefix=CHECK %s
+// CHECK: { "directory": "{{.*}}", "file": "{{.*}}gen-cdb-fragment.c", "output": "-", "arguments": [{{.*}}, "--target=x86_64-apple-macos10.15"{{.*}}]},
+// RUN: cat %t.cdb/*.json | FileCheck --check-prefix=CHECK-FLAG %s
+// CHECK-FLAG-NOT: -gen-cdb-fragment-path
+
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: ls %t.cdb | not FileCheck --check-prefix=CHECK-LS %s
+// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o -  -gen-cdb-fragment-path %t.cdb
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+
+// Empty path is equivalent to '.'
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -working-directory %t.cdb -c %s -o -  -gen-cdb-fragment-path ""
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+// RUN: cat %t.cdb/*.json | FileCheck --check-prefix=CHECK-CWD %s
+// CHECK-CWD: "directory": "{{.*}}.cdb"
+
+// -### does not emit the CDB fragment
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o -  -gen-cdb-fragment-path %t.cdb -###
+// RUN: ls %t.cdb | not FileCheck --check-prefix=CHECK-LS %s
+
+// -MJ is preferred over -gen-cdb-fragment-path
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o -  -gen-cdb-fragment-path %t.cdb -MJ %t.out
+// RUN: ls %t.cdb | not FileCheck --check-prefix=CHECK-LS %s
+// RUN: FileCheck %s < %t.out
Index: clang/lib/Driver/ToolChains/Clang.h
===
--- clang/lib/Driver/ToolChains/Clang.h
+++ clang/lib/Driver/ToolChains/Clang.h
@@ -95,6 +95,10 @@
const InputInfo , const InputInfo ,
const llvm::opt::ArgList ) const;
 
+  void DumpCompilationDatabaseFragmentToDir(
+  StringRef Dir, Compilation , StringRef Target, const InputInfo ,
+  const InputInfo , const llvm::opt::ArgList ) const;
+
 public:
   Clang(const ToolChain );
   ~Clang() override;
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2013,13 +2013,14 @@
 CompilationDatabase = std::move(File);
   }
   auto  = *CompilationDatabase;
-  SmallString<128> Buf;
-  if (llvm::sys::fs::current_path(Buf))
-Buf = ".";
-  CDB << "{ \"directory\": \"" << escape(Buf) << "\"";
+  auto CWD = D.getVFS().getCurrentWorkingDirectory();
+  if (!CWD)
+CWD = ".";
+  CDB << "{ \"directory\": \"" << escape(*CWD) << "\"";
   CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\"";
   CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
   CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\"";
+  SmallString<128> Buf;
   Buf = "-x";
   Buf += types::getTypeName(Input.getType());
   CDB << ", \"" << escape(Buf) << "\"";
@@ -2037,6 +2038,8 @@
 // Skip writing dependency output and the compilation database itself.
 if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group)
   continue;
+if (O.getID() == options::OPT_gen_cdb_fragment_path)
+  continue;
 // Skip inputs.
 if (O.getKind() == Option::InputClass)
   continue;
@@ -2051,6 +2054,40 @@
   CDB << ", \"" << escape(Buf) << "\"]},\n";
 }
 
+void Clang::DumpCompilationDatabaseFragmentToDir(
+StringRef Dir, Compilation , StringRef Target, const InputInfo ,
+const InputInfo , const llvm::opt::ArgList ) const {
+  // If this is a dry run, do not create the compilation database file.
+  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
+return;
+
+  if (CompilationDatabase)
+DumpCompilationDatabase(C, "", Target, Output, Input, Args);
+
+  SmallString<256> Path = Dir;
+  const auto  = C.getDriver();
+  Driver.getVFS().makeAbsolute(Path);
+  auto Err = llvm::sys::fs::create_directory(Path, 

[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-23 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman marked 5 inline comments as done.
arphaman added inline comments.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:2067
+  const auto  = C.getDriver();
+  auto CWD = Driver.getVFS().getCurrentWorkingDirectory();
+  if (!CWD) {

jkorous wrote:
> Do we still need this now?
No, the CWD should actually be read in `DumpCompilationDatabase`. I updated 
that method.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:2089
+  if (Err) {
+Driver.Diag(diag::err_drv_compilationdatabase) << Err.message();
+return;

jkorous wrote:
> Just to make sure - the error different on purpose here?
Not really needed I guess, I reused the error.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:2092
+  }
+  CompilationDatabase =
+  std::make_unique(FD, /*shouldClose=*/true);

jkorous wrote:
> I'm not familiar with intended lifecycle of `CompilationDatabase ` member. 
> Just to make sure - we need neither to preserve the original value nor reset 
> the stream after we're done here (as in - nobody is going to write anything 
> after we're done here), right?
Yes, we should either preserve or not reset the stream once it's created. 
Multiple clang invocations can be constructed  from one driver run, so we want 
to keep the stream open while the driver is running.



Comment at: clang/lib/Driver/ToolChains/Clang.h:99
+  void DumpCompilationDatabaseFragmentToDir(
+  StringRef Dir, Compilation , StringRef Target, const InputInfo ,
+  const InputInfo , const llvm::opt::ArgList ) const;

jkorous wrote:
> BTW this looks kind of funny `const InputInfo , const InputInfo 
> `.
Yes, that's unfortunate type naming in Clang :(


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66555/new/

https://reviews.llvm.org/D66555



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-23 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman updated this revision to Diff 216898.
arphaman added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66555/new/

https://reviews.llvm.org/D66555

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Clang.h
  clang/test/Driver/gen-cdb-fragment.c

Index: clang/test/Driver/gen-cdb-fragment.c
===
--- /dev/null
+++ clang/test/Driver/gen-cdb-fragment.c
@@ -0,0 +1,37 @@
+// REQUIRES: x86-registered-target
+// RUN: rm -rf %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -c %s -o -  -gen-cdb-fragment-path %t.cdb
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+// CHECK-LS: gen-cdb-fragment.c.{{.*}}.json
+
+// RUN: cat %t.cdb/*.json | FileCheck --check-prefix=CHECK %s
+// CHECK: { "directory": "{{.*}}", "file": "{{.*}}gen-cdb-fragment.c", "output": "-", "arguments": [{{.*}}, "--target=x86_64-apple-macos10.15"{{.*}}]},
+// RUN: cat %t.cdb/*.json | FileCheck --check-prefix=CHECK-FLAG %s
+// CHECK-FLAG-NOT: -gen-cdb-fragment-path
+
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: ls %t.cdb | not FileCheck --check-prefix=CHECK-LS %s
+// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o -  -gen-cdb-fragment-path %t.cdb
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+
+// Empty path is equivalent to '.'
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -working-directory %t.cdb -c %s -o -  -gen-cdb-fragment-path ""
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+// RUN: cat %t.cdb/*.json | FileCheck --check-prefix=CHECK-CWD %s
+// CHECK-CWD: "directory": "{{.*}}.cdb"
+
+// -### does not emit the CDB fragment
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o -  -gen-cdb-fragment-path %t.cdb -###
+// RUN: ls %t.cdb | not FileCheck --check-prefix=CHECK-LS %s
+
+// -MJ is preferred over -gen-cdb-fragment-path
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o -  -gen-cdb-fragment-path %t.cdb -MJ %t.out
+// RUN: ls %t.cdb | not FileCheck --check-prefix=CHECK-LS %s
+// RUN: FileCheck %s < %t.out
Index: clang/lib/Driver/ToolChains/Clang.h
===
--- clang/lib/Driver/ToolChains/Clang.h
+++ clang/lib/Driver/ToolChains/Clang.h
@@ -95,6 +95,10 @@
const InputInfo , const InputInfo ,
const llvm::opt::ArgList ) const;
 
+  void DumpCompilationDatabaseFragmentToDir(
+  StringRef Dir, Compilation , StringRef Target, const InputInfo ,
+  const InputInfo , const llvm::opt::ArgList ) const;
+
 public:
   Clang(const ToolChain );
   ~Clang() override;
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2013,13 +2013,14 @@
 CompilationDatabase = std::move(File);
   }
   auto  = *CompilationDatabase;
-  SmallString<128> Buf;
-  if (llvm::sys::fs::current_path(Buf))
-Buf = ".";
-  CDB << "{ \"directory\": \"" << escape(Buf) << "\"";
+  auto CWD = D.getVFS().getCurrentWorkingDirectory();
+  if (!CWD)
+CWD = ".";
+  CDB << "{ \"directory\": \"" << escape(*CWD) << "\"";
   CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\"";
   CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
   CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\"";
+  SmallString<128> Buf;
   Buf = "-x";
   Buf += types::getTypeName(Input.getType());
   CDB << ", \"" << escape(Buf) << "\"";
@@ -2037,6 +2038,8 @@
 // Skip writing dependency output and the compilation database itself.
 if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group)
   continue;
+if (O.getID() == options::OPT_gen_cdb_fragment_path)
+  continue;
 // Skip inputs.
 if (O.getKind() == Option::InputClass)
   continue;
@@ -2051,6 +2054,40 @@
   CDB << ", \"" << escape(Buf) << "\"]},\n";
 }
 
+void Clang::DumpCompilationDatabaseFragmentToDir(
+StringRef Dir, Compilation , StringRef Target, const InputInfo ,
+const InputInfo , const llvm::opt::ArgList ) const {
+  // If this is a dry run, do not create the compilation database file.
+  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
+return;
+
+  if (CompilationDatabase)
+DumpCompilationDatabase(C, "", Target, Output, Input, Args);
+
+  SmallString<256> Path = Dir;
+  const auto  = C.getDriver();
+  Driver.getVFS().makeAbsolute(Path);
+  auto Err = llvm::sys::fs::create_directory(Path, /*IgnoreExisting=*/true);
+  if (Err) {
+Driver.Diag(diag::err_drv_compilationdatabase) << Dir << Err.message();
+return;
+  }
+
+  llvm::sys::path::append(
+  

[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-22 Thread Jan Korous via Phabricator via cfe-commits
jkorous added inline comments.



Comment at: clang/lib/Driver/ToolChains/Clang.h:99
+  void DumpCompilationDatabaseFragmentToDir(
+  StringRef Dir, Compilation , StringRef Target, const InputInfo ,
+  const InputInfo , const llvm::opt::ArgList ) const;

BTW this looks kind of funny `const InputInfo , const InputInfo `.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66555/new/

https://reviews.llvm.org/D66555



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-22 Thread Jan Korous via Phabricator via cfe-commits
jkorous added inline comments.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:2067
+  const auto  = C.getDriver();
+  auto CWD = Driver.getVFS().getCurrentWorkingDirectory();
+  if (!CWD) {

Do we still need this now?



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:2089
+  if (Err) {
+Driver.Diag(diag::err_drv_compilationdatabase) << Err.message();
+return;

Just to make sure - the error different on purpose here?



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:2092
+  }
+  CompilationDatabase =
+  std::make_unique(FD, /*shouldClose=*/true);

I'm not familiar with intended lifecycle of `CompilationDatabase ` member. Just 
to make sure - we need neither to preserve the original value nor reset the 
stream after we're done here (as in - nobody is going to write anything after 
we're done here), right?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66555/new/

https://reviews.llvm.org/D66555



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-22 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman updated this revision to Diff 216717.
arphaman added a comment.
Herald added a subscriber: ributzka.

Reimplement the new option on top of `-MJ`. `-MJ` is still the preferred option 
if both are specified.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66555/new/

https://reviews.llvm.org/D66555

Files:
  clang/include/clang/Basic/DiagnosticDriverKinds.td
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Driver/ToolChains/Clang.h
  clang/test/Driver/gen-cdb-fragment.c

Index: clang/test/Driver/gen-cdb-fragment.c
===
--- /dev/null
+++ clang/test/Driver/gen-cdb-fragment.c
@@ -0,0 +1,35 @@
+// REQUIRES: x86-registered-target
+// RUN: rm -rf %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -c %s -o -  -gen-cdb-fragment-path %t.cdb
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+// CHECK-LS: gen-cdb-fragment.c.{{.*}}.json
+
+// RUN: cat %t.cdb/*.json | FileCheck --check-prefix=CHECK %s
+// CHECK: { "directory": "{{.*}}", "file": "{{.*}}gen-cdb-fragment.c", "output": "-", "arguments": [{{.*}}, "--target=x86_64-apple-macos10.15"{{.*}}]},
+// RUN: cat %t.cdb/*.json | FileCheck --check-prefix=CHECK-FLAG %s
+// CHECK-FLAG-NOT: -gen-cdb-fragment-path
+
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: ls %t.cdb | not FileCheck --check-prefix=CHECK-LS %s
+// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o -  -gen-cdb-fragment-path %t.cdb
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+
+// Empty path is equivalent to '.'
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -working-directory %t.cdb -c %s -o -  -gen-cdb-fragment-path ""
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+
+// -### does not emit the CDB fragment
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o -  -gen-cdb-fragment-path %t.cdb -###
+// RUN: ls %t.cdb | not FileCheck --check-prefix=CHECK-LS %s
+
+// -MJ is preferred over -gen-cdb-fragment-path
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o -  -gen-cdb-fragment-path %t.cdb -MJ %t.out
+// RUN: ls %t.cdb | not FileCheck --check-prefix=CHECK-LS %s
+// RUN: FileCheck %s < %t.out
Index: clang/lib/Driver/ToolChains/Clang.h
===
--- clang/lib/Driver/ToolChains/Clang.h
+++ clang/lib/Driver/ToolChains/Clang.h
@@ -95,6 +95,10 @@
const InputInfo , const InputInfo ,
const llvm::opt::ArgList ) const;
 
+  void DumpCompilationDatabaseFragmentToDir(
+  StringRef Dir, Compilation , StringRef Target, const InputInfo ,
+  const InputInfo , const llvm::opt::ArgList ) const;
+
 public:
   Clang(const ToolChain );
   ~Clang() override;
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2037,6 +2037,8 @@
 // Skip writing dependency output and the compilation database itself.
 if (O.getGroup().isValid() && O.getGroup().getID() == options::OPT_M_Group)
   continue;
+if (O.getID() == options::OPT_gen_cdb_fragment_path)
+  continue;
 // Skip inputs.
 if (O.getKind() == Option::InputClass)
   continue;
@@ -2051,6 +2053,47 @@
   CDB << ", \"" << escape(Buf) << "\"]},\n";
 }
 
+void Clang::DumpCompilationDatabaseFragmentToDir(
+StringRef Dir, Compilation , StringRef Target, const InputInfo ,
+const InputInfo , const llvm::opt::ArgList ) const {
+  // If this is a dry run, do not create the compilation database file.
+  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH))
+return;
+
+  if (CompilationDatabase)
+DumpCompilationDatabase(C, "", Target, Output, Input, Args);
+
+  const auto  = C.getDriver();
+  auto CWD = Driver.getVFS().getCurrentWorkingDirectory();
+  if (!CWD) {
+Driver.Diag(diag::err_drv_emit_cdb_fragment_dir_failure)
+<< CWD.getError().message();
+return;
+  }
+
+  SmallString<256> Path = Dir;
+  Driver.getVFS().makeAbsolute(Path);
+  auto Err = llvm::sys::fs::create_directory(Path, /*IgnoreExisting=*/true);
+  if (Err) {
+Driver.Diag(diag::err_drv_emit_cdb_fragment_dir_failure) << Err.message();
+return;
+  }
+
+  llvm::sys::path::append(
+  Path,
+  Twine(llvm::sys::path::filename(Input.getFilename())) + "..json");
+  int FD;
+  SmallString<256> TempPath;
+  Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath);
+  if (Err) {
+Driver.Diag(diag::err_drv_compilationdatabase) << Err.message();
+return;
+  }
+  CompilationDatabase =
+  std::make_unique(FD, /*shouldClose=*/true);
+  DumpCompilationDatabase(C, "", Target, Output, Input, Args);
+}
+
 static void 

[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-22 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman added a comment.

@jkorous I addressed your comments as well ("" directory is now allowed, error 
is reported, existing escape used).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66555/new/

https://reviews.llvm.org/D66555



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-22 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman added a comment.

In D66555#1640603 , @tschuett wrote:

> How does it relate to the -MJ option?


Ah, I wasn't aware that such option existed. I should use it instead.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66555/new/

https://reviews.llvm.org/D66555



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-22 Thread Thorsten via Phabricator via cfe-commits
tschuett added a comment.

How does it relate to the -MJ option?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66555/new/

https://reviews.llvm.org/D66555



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-21 Thread Jan Korous via Phabricator via cfe-commits
jkorous added inline comments.



Comment at: clang/lib/Driver/Driver.cpp:1044
+  if (const Arg *A = Args.getLastArg(options::OPT_gen_cdb_fragment_path))
+PathToCDBFragmentDir = A->getValue();
   // FIXME: TargetTriple is used by the target-prefixed calls to as/ld

IIUC this feature might end up being used from some test script. I'm wondering 
- what would happen had the argument been an empty string?

```
clang -c foo.c -gen-cdb-fragment-path ""
```



Comment at: clang/lib/Driver/Driver.cpp:4868
+
+void Driver::emitCompilationDatabaseFragment(StringRef DestDir,
+ StringRef Executable,

I am just wondering whether we shouldn't notice user about failures, possibly 
with some error message.



Comment at: clang/lib/Driver/Driver.cpp:4896
+for (char C : Arg) {
+  if (C == ' ' || C == '\'' || C == '"' || C == '\\')
+OS << '\\';

It feels like this might have been already implemented somewhere else.

Couldn't we use for example `llvm::yaml::escape()`?

https://llvm.org/doxygen/namespacellvm_1_1yaml.html#aefed9cb3f107aee0cff4d325c7d689ae


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66555/new/

https://reviews.llvm.org/D66555



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D66555: [driver] add a new option `-gen-cdb-fragment-path` to emit a fragment of a compilation database for each compilation

2019-08-21 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman created this revision.
arphaman added reviewers: Bigcheese, jkorous, dexonsmith.
Herald added a project: clang.

This patch adds a new option called `-gen-cdb-fragment-path`  to the driver, 
which can be used to specify a directory path to which clang can emit a 
fragment of a CDB for each compilation it needs to invoke.

The CDB fragment is emitted into a unique file in the specified directory. It 
contains the `-cc1` clang invocation in its command. The file itself is 
actually a valid standalone CDB (if you disregard the not yet well supported 
`-cc1` innovation by the CDB, which I'll fix). To load the full CDB that can be 
emitted during a build, I'm going to add a new CDB reader from a directory that 
reads all fragments and aggregates them into one CDB in a follow-up patch.

This is useful to setup verification infrastructure for `clang-scan-deps` for 
some projects that don't have a way of constructing a CDB from their build.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66555

Files:
  clang/include/clang/Driver/Driver.h
  clang/include/clang/Driver/Job.h
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/Compilation.cpp
  clang/lib/Driver/Driver.cpp
  clang/test/Driver/gen-cdb-fragment.c

Index: clang/test/Driver/gen-cdb-fragment.c
===
--- /dev/null
+++ clang/test/Driver/gen-cdb-fragment.c
@@ -0,0 +1,18 @@
+// REQUIRES: x86-registered-target
+// RUN: rm -rf %t.cdb
+// RUN: %clang -target x86_64-apple-macos10.15 -c %s -o -  -gen-cdb-fragment-path %t.cdb
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
+// CHECK-LS: gen-cdb-fragment.c.{{.*}}.json
+
+// RUN: cat %t.cdb/*.json | FileCheck --check-prefix=CHECK %s
+// CHECK: [{
+// CHECK-NEXT: "directory":"{{.*}}",
+// CHECK-NEXT: "command":"{{.*}} -cc1 -triple x86_64-apple-macosx10.15.0 {{.*}}",
+// CHECK-NEXT: "file":"{{.*}}gen-cdb-fragment.c"
+// CHECK: }]
+
+// RUN: rm -rf %t.cdb
+// RUN: mkdir %t.cdb
+// RUN: ls %t.cdb | not FileCheck --check-prefix=CHECK-LS %s
+// RUN: %clang -target x86_64-apple-macos10.15 -S %s -o -  -gen-cdb-fragment-path %t.cdb
+// RUN: ls %t.cdb | FileCheck --check-prefix=CHECK-LS %s
Index: clang/lib/Driver/Driver.cpp
===
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -1040,6 +1040,8 @@
   GenReproducer = Args.hasFlag(options::OPT_gen_reproducer,
options::OPT_fno_crash_diagnostics,
!!::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH"));
+  if (const Arg *A = Args.getLastArg(options::OPT_gen_cdb_fragment_path))
+PathToCDBFragmentDir = A->getValue();
   // FIXME: TargetTriple is used by the target-prefixed calls to as/ld
   // and getToolChain is const.
   if (IsCLMode()) {
@@ -4862,3 +4864,46 @@
 bool clang::driver::isOptimizationLevelFast(const ArgList ) {
   return Args.hasFlag(options::OPT_Ofast, options::OPT_O_Group, false);
 }
+
+void Driver::emitCompilationDatabaseFragment(StringRef DestDir,
+ StringRef Executable,
+ StringRef Filename,
+ const ArgStringList ) const {
+  auto CWD = getVFS().getCurrentWorkingDirectory();
+  if (!CWD)
+return;
+
+  SmallString<256> Path = DestDir;
+  getVFS().makeAbsolute(Path);
+  auto Err = llvm::sys::fs::create_directory(Path, /*IgnoreExisting=*/true);
+  if (Err)
+return;
+
+  llvm::sys::path::append(Path, Twine(llvm::sys::path::filename(Filename)) +
+"..json");
+  int FD;
+  SmallString<256> TempPath;
+  Err = llvm::sys::fs::createUniqueFile(Path, FD, TempPath);
+  if (Err)
+return;
+
+  llvm::raw_fd_ostream OS(FD, /*ShouldClose=*/true);
+  OS << "[{\n";
+  OS << R"("directory":")" << *CWD << "\",\n";
+  OS << R"("command":")";
+  auto EmitCommandLineFragment = [](StringRef Arg) {
+for (char C : Arg) {
+  if (C == ' ' || C == '\'' || C == '"' || C == '\\')
+OS << '\\';
+  OS << C;
+}
+  };
+  EmitCommandLineFragment(Executable);
+  for (const auto  : Args) {
+OS << ' ';
+EmitCommandLineFragment(Arg);
+  }
+  OS << "\",\n";
+  OS << R"("file":")" << Filename << "\"\n";
+  OS << "}]";
+}
Index: clang/lib/Driver/Compilation.cpp
===
--- clang/lib/Driver/Compilation.cpp
+++ clang/lib/Driver/Compilation.cpp
@@ -176,6 +176,14 @@
 
 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
   }
+  if (auto CDBOutputDir = getDriver().getPathToCDBFragmentDir()) {
+if (isa(C.getSource()) ||
+isa(C.getSource())) {
+  for (const auto  : C.getInputFilenames())
+getDriver().emitCompilationDatabaseFragment(
+*CDBOutputDir, C.getExecutable(), F, C.getArguments());
+}
+  }
 
   std::string Error;
   bool ExecutionFailed;
Index: