[PATCH] D40983: Generate Libclang invocation reproducers using a new -cc1gen-reproducer option

2018-04-06 Thread Alex Lorenz via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL329442: Generate Libclang invocation reproducers using a new 
-cc1gen-reproducer (authored by arphaman, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D40983?vs=127935=141391#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D40983

Files:
  cfe/trunk/include/clang/Driver/Driver.h
  cfe/trunk/lib/Driver/Driver.cpp
  cfe/trunk/test/Index/create-libclang-completion-reproducer.c
  cfe/trunk/test/Index/create-libclang-parsing-reproducer.c
  cfe/trunk/tools/driver/CMakeLists.txt
  cfe/trunk/tools/driver/cc1gen_reproducer_main.cpp
  cfe/trunk/tools/driver/driver.cpp

Index: cfe/trunk/lib/Driver/Driver.cpp
===
--- cfe/trunk/lib/Driver/Driver.cpp
+++ cfe/trunk/lib/Driver/Driver.cpp
@@ -,8 +,9 @@
 // When clang crashes, produce diagnostic information including the fully
 // preprocessed source file(s).  Request that the developer attach the
 // diagnostic information to a bug report.
-void Driver::generateCompilationDiagnostics(Compilation ,
-const Command ) {
+void Driver::generateCompilationDiagnostics(
+Compilation , const Command ,
+StringRef AdditionalInformation, CompilationDiagnosticReport *Report) {
   if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics))
 return;
 
@@ -1238,6 +1239,8 @@
   SmallString<128> ReproCrashFilename;
   for (const char *TempFile : TempFiles) {
 Diag(clang::diag::note_drv_command_failed_diag_msg) << TempFile;
+if (Report)
+  Report->TemporaryFiles.push_back(TempFile);
 if (ReproCrashFilename.empty()) {
   ReproCrashFilename = TempFile;
   llvm::sys::path::replace_extension(ReproCrashFilename, ".crash");
@@ -1266,6 +1269,11 @@
 ScriptOS << "# Original command: ";
 Cmd.Print(ScriptOS, "\n", /*Quote=*/true);
 Cmd.Print(ScriptOS, "\n", /*Quote=*/true, );
+if (!AdditionalInformation.empty())
+  ScriptOS << "\n# Additional information: " << AdditionalInformation
+   << "\n";
+if (Report)
+  Report->TemporaryFiles.push_back(Script);
 Diag(clang::diag::note_drv_command_failed_diag_msg) << Script;
   }
 
Index: cfe/trunk/tools/driver/cc1gen_reproducer_main.cpp
===
--- cfe/trunk/tools/driver/cc1gen_reproducer_main.cpp
+++ cfe/trunk/tools/driver/cc1gen_reproducer_main.cpp
@@ -0,0 +1,196 @@
+//===-- cc1gen_reproducer_main.cpp - Clang reproducer generator  --===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// This is the entry point to the clang -cc1gen-reproducer functionality, which
+// generates reproducers for invocations for clang-based tools.
+//
+//===--===//
+
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/VirtualFileSystem.h"
+#include "clang/Driver/Compilation.h"
+#include "clang/Driver/Driver.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/YAMLTraits.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace clang;
+
+namespace {
+
+struct UnsavedFileHash {
+  std::string Name;
+  std::string MD5;
+};
+
+struct ClangInvocationInfo {
+  std::string Toolchain;
+  std::string LibclangOperation;
+  std::string LibclangOptions;
+  std::vector Arguments;
+  std::vector InvocationArguments;
+  std::vector UnsavedFileHashes;
+  bool Dump = false;
+};
+
+} // end anonymous namespace
+
+LLVM_YAML_IS_SEQUENCE_VECTOR(UnsavedFileHash)
+
+namespace llvm {
+namespace yaml {
+
+template <> struct MappingTraits {
+  static void mapping(IO , UnsavedFileHash ) {
+IO.mapRequired("name", Info.Name);
+IO.mapRequired("md5", Info.MD5);
+  }
+};
+
+template <> struct MappingTraits {
+  static void mapping(IO , ClangInvocationInfo ) {
+IO.mapRequired("toolchain", Info.Toolchain);
+IO.mapOptional("libclang.operation", Info.LibclangOperation);
+IO.mapOptional("libclang.opts", Info.LibclangOptions);
+IO.mapRequired("args", Info.Arguments);
+IO.mapOptional("invocation-args", Info.InvocationArguments);
+IO.mapOptional("unsaved_file_hashes", Info.UnsavedFileHashes);
+  }
+};
+
+} // end namespace yaml
+} // end namespace llvm
+
+static std::string generateReproducerMetaInfo(const ClangInvocationInfo ) {
+  std::string Result;
+  llvm::raw_string_ostream OS(Result);
+  OS << '{';
+  bool NeedComma = false;
+  auto EmitKey = [&](StringRef Key) {
+if (NeedComma)
+  OS << ", ";
+NeedComma = 

[PATCH] D40983: Generate Libclang invocation reproducers using a new -cc1gen-reproducer option

2018-04-05 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman added a comment.

In https://reviews.llvm.org/D40983#1059087, @arphaman wrote:

> In https://reviews.llvm.org/D40983#968796, @bruno wrote:
>
> > Makes sense, LGTM.
> >
> > Should we add documentation explaining how to use this? I'm fine if it 
> > comes in a follow up commit.
>
>
> Sorry, just got time to get back to this now. Thanks for the review! I'll add 
> a document for this new functionality tomorrow.




- In a follow-up commit tomorrow.


https://reviews.llvm.org/D40983



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


[PATCH] D40983: Generate Libclang invocation reproducers using a new -cc1gen-reproducer option

2018-04-05 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman added a comment.

In https://reviews.llvm.org/D40983#968796, @bruno wrote:

> Makes sense, LGTM.
>
> Should we add documentation explaining how to use this? I'm fine if it comes 
> in a follow up commit.


Sorry, just got time to get back to this now. Thanks for the review! I'll add a 
document for this new functionality tomorrow.


https://reviews.llvm.org/D40983



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


[PATCH] D40983: Generate Libclang invocation reproducers using a new -cc1gen-reproducer option

2018-01-05 Thread Bruno Cardoso Lopes via Phabricator via cfe-commits
bruno accepted this revision.
bruno added a comment.
This revision is now accepted and ready to land.

Makes sense, LGTM.

Should we add documentation explaining how to use this? I'm fine if it comes in 
a follow up commit.


https://reviews.llvm.org/D40983



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


[PATCH] D40983: Generate Libclang invocation reproducers using a new -cc1gen-reproducer option

2017-12-21 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman updated this revision to Diff 127935.
arphaman marked an inline comment as done.
arphaman added a comment.

Address review comments


https://reviews.llvm.org/D40983

Files:
  include/clang/Driver/Driver.h
  lib/Driver/Driver.cpp
  test/Index/create-libclang-completion-reproducer.c
  test/Index/create-libclang-parsing-reproducer.c
  tools/driver/CMakeLists.txt
  tools/driver/cc1gen_reproducer_main.cpp
  tools/driver/driver.cpp

Index: tools/driver/driver.cpp
===
--- tools/driver/driver.cpp
+++ tools/driver/driver.cpp
@@ -205,6 +205,8 @@
 void *MainAddr);
 extern int cc1as_main(ArrayRef Argv, const char *Argv0,
   void *MainAddr);
+extern int cc1gen_reproducer_main(ArrayRef Argv,
+  const char *Argv0, void *MainAddr);
 
 static void insertTargetAndModeArgs(const ParsedClangName ,
 SmallVectorImpl ,
@@ -309,6 +311,8 @@
 return cc1_main(argv.slice(2), argv[0], GetExecutablePathVP);
   if (Tool == "as")
 return cc1as_main(argv.slice(2), argv[0], GetExecutablePathVP);
+  if (Tool == "gen-reproducer")
+return cc1gen_reproducer_main(argv.slice(2), argv[0], GetExecutablePathVP);
 
   // Reject unknown tools.
   llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n";
Index: tools/driver/cc1gen_reproducer_main.cpp
===
--- /dev/null
+++ tools/driver/cc1gen_reproducer_main.cpp
@@ -0,0 +1,196 @@
+//===-- cc1gen_reproducer_main.cpp - Clang reproducer generator  --===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// This is the entry point to the clang -cc1gen-reproducer functionality, which
+// generates reproducers for invocations for clang-based tools.
+//
+//===--===//
+
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/VirtualFileSystem.h"
+#include "clang/Driver/Compilation.h"
+#include "clang/Driver/Driver.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/YAMLTraits.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace clang;
+
+namespace {
+
+struct UnsavedFileHash {
+  std::string Name;
+  std::string MD5;
+};
+
+struct ClangInvocationInfo {
+  std::string Toolchain;
+  std::string LibclangOperation;
+  std::string LibclangOptions;
+  std::vector Arguments;
+  std::vector InvocationArguments;
+  std::vector UnsavedFileHashes;
+  bool Dump = false;
+};
+
+} // end anonymous namespace
+
+LLVM_YAML_IS_SEQUENCE_VECTOR(UnsavedFileHash)
+
+namespace llvm {
+namespace yaml {
+
+template <> struct MappingTraits {
+  static void mapping(IO , UnsavedFileHash ) {
+IO.mapRequired("name", Info.Name);
+IO.mapRequired("md5", Info.MD5);
+  }
+};
+
+template <> struct MappingTraits {
+  static void mapping(IO , ClangInvocationInfo ) {
+IO.mapRequired("toolchain", Info.Toolchain);
+IO.mapOptional("libclang.operation", Info.LibclangOperation);
+IO.mapOptional("libclang.opts", Info.LibclangOptions);
+IO.mapRequired("args", Info.Arguments);
+IO.mapOptional("invocation-args", Info.InvocationArguments);
+IO.mapOptional("unsaved_file_hashes", Info.UnsavedFileHashes);
+  }
+};
+
+} // end namespace yaml
+} // end namespace llvm
+
+static std::string generateReproducerMetaInfo(const ClangInvocationInfo ) {
+  std::string Result;
+  llvm::raw_string_ostream OS(Result);
+  OS << '{';
+  bool NeedComma = false;
+  auto EmitKey = [&](StringRef Key) {
+if (NeedComma)
+  OS << ", ";
+NeedComma = true;
+OS << '"' << Key << "\": ";
+  };
+  auto EmitStringKey = [&](StringRef Key, StringRef Value) {
+if (Value.empty())
+  return;
+EmitKey(Key);
+OS << '"' << Value << '"';
+  };
+  EmitStringKey("libclang.operation", Info.LibclangOperation);
+  EmitStringKey("libclang.opts", Info.LibclangOptions);
+  if (!Info.InvocationArguments.empty()) {
+EmitKey("invocation-args");
+OS << '[';
+for (const auto  : llvm::enumerate(Info.InvocationArguments)) {
+  if (Arg.index())
+OS << ',';
+  OS << '"' << Arg.value() << '"';
+}
+OS << ']';
+  }
+  OS << '}';
+  // FIXME: Compare unsaved file hashes and report mismatch in the reproducer.
+  if (Info.Dump)
+llvm::outs() << "REPRODUCER METAINFO: " << OS.str() << "\n";
+  return std::move(OS.str());
+}
+
+/// Generates a reproducer for a set of arguments from a specific invocation.
+static llvm::Optional
+generateReproducerForInvocationArguments(ArrayRef Argv,
+

[PATCH] D40983: Generate Libclang invocation reproducers using a new -cc1gen-reproducer option

2017-12-21 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman marked 2 inline comments as done.
arphaman added a comment.

In https://reviews.llvm.org/D40983#958809, @bruno wrote:

> Hi Alex,
>
> Thanks for improving this.
>
> - Instead of adding `-cc1gen-reproducer`, why can't you run that through 
> `-cc1` and have a flag similar to `-###`, which just prints the reproducer 
> line?


It would be difficult to add this logic to the regular `-cc1`, as it would need 
to construct a real compiler invocation on top of the pseudo one which takes 
the libclang invocation file (because it has to load the actual compiler 
arguments from the libclang file). This would be difficult and quite disruptive 
to the code in the cc1 driver and the compiler invocation. The approach in this 
patch is much simpler to implement and maintain, and does not disrupt the code 
in the `cc1` driver. This patch makes it more of a tool rather than a clang 
driver invocation.

> - I didn't  understand how you can use the final output information, can you 
> give an example to illustrate?

Given a sample output like:

REPRODUCER:
{
 "files":["/tmp/a.c","/tmp/a.sh"]
}

The tool's client (also a libclang client) will parse the JSON object with the 
files that make up the reproducer. It will then be able to do whatever it needs 
with those specific files.




Comment at: include/clang/Driver/Driver.h:394
+  struct CompilationDiagnosticReport {
+std::vector TemporaryFiles;
+  };

bruno wrote:
> I assume the number of temporary files are usually small, can you switch to 
> SmallVector here?
Yep.


Repository:
  rC Clang

https://reviews.llvm.org/D40983



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


[PATCH] D40983: Generate Libclang invocation reproducers using a new -cc1gen-reproducer option

2017-12-18 Thread Bruno Cardoso Lopes via Phabricator via cfe-commits
bruno added a comment.

Hi Alex,

Thanks for improving this.

- Instead of adding `-cc1gen-reproducer`, why can't you run that through `-cc1` 
and have a flag similar to `-###`, which just prints the reproducer line?
- I didn't  understand how you can use the final output information, can you 
give an example to illustrate?




Comment at: include/clang/Driver/Driver.h:394
+  struct CompilationDiagnosticReport {
+std::vector TemporaryFiles;
+  };

I assume the number of temporary files are usually small, can you switch to 
SmallVector here?



Comment at: tools/driver/cc1gen_reproducer_main.cpp:187
+  // Emit the information about the reproduce files to stdout.
+  int Result;
+  if (Report) {

int Result = 1; 

(no need for the else clause)


Repository:
  rC Clang

https://reviews.llvm.org/D40983



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


[PATCH] D40983: Generate Libclang invocation reproducers using a new -cc1gen-reproducer option

2017-12-07 Thread Alex Lorenz via Phabricator via cfe-commits
arphaman created this revision.
Herald added a subscriber: mgorny.

This patch is a follow up to the previous work that recorded Libclang 
invocations into temporary files: https://reviews.llvm.org/D40527.

It adds a new -cc1 mode to clang: `-cc1gen-reproducer`. The goal of this mode 
is to generate Clang reproducer files for Libclang tool invocation. The JSON 
format in the invocation files is not really intended to be stable, so Libclang 
and Clang should be of the same version when generating reproducers.
The new mode emits the information about the temporary files in the reproducers 
to stdout using JSON. It also injects additional Libclang-specific information 
about the reproducer to the reproducer's .sh files.

Thanks for taking a look!


Repository:
  rC Clang

https://reviews.llvm.org/D40983

Files:
  include/clang/Driver/Driver.h
  lib/Driver/Driver.cpp
  test/Index/create-libclang-completion-reproducer.c
  test/Index/create-libclang-parsing-reproducer.c
  tools/driver/CMakeLists.txt
  tools/driver/cc1gen_reproducer_main.cpp
  tools/driver/driver.cpp

Index: tools/driver/driver.cpp
===
--- tools/driver/driver.cpp
+++ tools/driver/driver.cpp
@@ -205,6 +205,8 @@
 void *MainAddr);
 extern int cc1as_main(ArrayRef Argv, const char *Argv0,
   void *MainAddr);
+extern int cc1gen_reproducer_main(ArrayRef Argv,
+  const char *Argv0, void *MainAddr);
 
 static void insertTargetAndModeArgs(const ParsedClangName ,
 SmallVectorImpl ,
@@ -309,6 +311,8 @@
 return cc1_main(argv.slice(2), argv[0], GetExecutablePathVP);
   if (Tool == "as")
 return cc1as_main(argv.slice(2), argv[0], GetExecutablePathVP);
+  if (Tool == "gen-reproducer")
+return cc1gen_reproducer_main(argv.slice(2), argv[0], GetExecutablePathVP);
 
   // Reject unknown tools.
   llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n";
Index: tools/driver/cc1gen_reproducer_main.cpp
===
--- /dev/null
+++ tools/driver/cc1gen_reproducer_main.cpp
@@ -0,0 +1,198 @@
+//===-- cc1gen_reproducer_main.cpp - Clang reproducer generator  --===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// This is the entry point to the clang -cc1gen-reproducer functionality, which
+// generates reproducers for invocations for clang-based tools.
+//
+//===--===//
+
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/VirtualFileSystem.h"
+#include "clang/Driver/Compilation.h"
+#include "clang/Driver/Driver.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/YAMLTraits.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace clang;
+
+namespace {
+
+struct UnsavedFileHash {
+  std::string Name;
+  std::string MD5;
+};
+
+struct ClangInvocationInfo {
+  std::string Toolchain;
+  std::string LibclangOperation;
+  std::string LibclangOptions;
+  std::vector Arguments;
+  std::vector InvocationArguments;
+  std::vector UnsavedFileHashes;
+  bool Dump = false;
+};
+
+} // end anonymous namespace
+
+LLVM_YAML_IS_SEQUENCE_VECTOR(UnsavedFileHash)
+
+namespace llvm {
+namespace yaml {
+
+template <> struct MappingTraits {
+  static void mapping(IO , UnsavedFileHash ) {
+IO.mapRequired("name", Info.Name);
+IO.mapRequired("md5", Info.MD5);
+  }
+};
+
+template <> struct MappingTraits {
+  static void mapping(IO , ClangInvocationInfo ) {
+IO.mapRequired("toolchain", Info.Toolchain);
+IO.mapOptional("libclang.operation", Info.LibclangOperation);
+IO.mapOptional("libclang.opts", Info.LibclangOptions);
+IO.mapRequired("args", Info.Arguments);
+IO.mapOptional("invocation-args", Info.InvocationArguments);
+IO.mapOptional("unsaved_file_hashes", Info.UnsavedFileHashes);
+  }
+};
+
+} // end namespace yaml
+} // end namespace llvm
+
+static std::string generateReproducerMetaInfo(const ClangInvocationInfo ) {
+  std::string Result;
+  llvm::raw_string_ostream OS(Result);
+  OS << '{';
+  bool NeedComma = false;
+  auto EmitKey = [&](StringRef Key) {
+if (NeedComma)
+  OS << ", ";
+NeedComma = true;
+OS << '"' << Key << "\": ";
+  };
+  auto EmitStringKey = [&](StringRef Key, StringRef Value) {
+if (Value.empty())
+  return;
+EmitKey(Key);
+OS << '"' << Value << '"';
+  };
+  EmitStringKey("libclang.operation", Info.LibclangOperation);
+  EmitStringKey("libclang.opts", Info.LibclangOptions);
+  if