1 //===-- cc1gen_reproducer_main.cpp - Clang reproducer generator ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the entry point to the clang -cc1gen-reproducer functionality, which
11 // generates reproducers for invocations for clang-based tools.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Basic/LLVM.h"
17 #include "clang/Basic/VirtualFileSystem.h"
18 #include "clang/Driver/Compilation.h"
19 #include "clang/Driver/Driver.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/TargetSelect.h"
24 #include "llvm/Support/YAMLTraits.h"
25 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
31 struct UnsavedFileHash {
36 struct ClangInvocationInfo {
37 std::string Toolchain;
38 std::string LibclangOperation;
39 std::string LibclangOptions;
40 std::vector<std::string> Arguments;
41 std::vector<std::string> InvocationArguments;
42 std::vector<UnsavedFileHash> UnsavedFileHashes;
46 } // end anonymous namespace
48 LLVM_YAML_IS_SEQUENCE_VECTOR(UnsavedFileHash)
53 template <> struct MappingTraits<UnsavedFileHash> {
54 static void mapping(IO &IO, UnsavedFileHash &Info) {
55 IO.mapRequired("name", Info.Name);
56 IO.mapRequired("md5", Info.MD5);
60 template <> struct MappingTraits<ClangInvocationInfo> {
61 static void mapping(IO &IO, ClangInvocationInfo &Info) {
62 IO.mapRequired("toolchain", Info.Toolchain);
63 IO.mapOptional("libclang.operation", Info.LibclangOperation);
64 IO.mapOptional("libclang.opts", Info.LibclangOptions);
65 IO.mapRequired("args", Info.Arguments);
66 IO.mapOptional("invocation-args", Info.InvocationArguments);
67 IO.mapOptional("unsaved_file_hashes", Info.UnsavedFileHashes);
71 } // end namespace yaml
72 } // end namespace llvm
74 static std::string generateReproducerMetaInfo(const ClangInvocationInfo &Info) {
76 llvm::raw_string_ostream OS(Result);
78 bool NeedComma = false;
79 auto EmitKey = [&](StringRef Key) {
83 OS << '"' << Key << "\": ";
85 auto EmitStringKey = [&](StringRef Key, StringRef Value) {
89 OS << '"' << Value << '"';
91 EmitStringKey("libclang.operation", Info.LibclangOperation);
92 EmitStringKey("libclang.opts", Info.LibclangOptions);
93 if (!Info.InvocationArguments.empty()) {
94 EmitKey("invocation-args");
96 for (const auto &Arg : llvm::enumerate(Info.InvocationArguments)) {
99 OS << '"' << Arg.value() << '"';
104 // FIXME: Compare unsaved file hashes and report mismatch in the reproducer.
106 llvm::outs() << "REPRODUCER METAINFO: " << OS.str() << "\n";
107 return std::move(OS.str());
110 /// Generates a reproducer for a set of arguments from a specific invocation.
111 static llvm::Optional<driver::Driver::CompilationDiagnosticReport>
112 generateReproducerForInvocationArguments(ArrayRef<const char *> Argv,
113 const ClangInvocationInfo &Info) {
114 using namespace driver;
115 auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(Argv[0]);
117 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions;
119 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
120 DiagnosticsEngine Diags(DiagID, &*DiagOpts, new IgnoringDiagConsumer());
121 ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false);
122 Driver TheDriver(Argv[0], llvm::sys::getDefaultTargetTriple(), Diags);
123 TheDriver.setTargetAndMode(TargetAndMode);
125 std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Argv));
126 if (C && !C->containsError()) {
127 for (const auto &J : C->getJobs()) {
128 if (const Command *Cmd = dyn_cast<Command>(&J)) {
129 Driver::CompilationDiagnosticReport Report;
130 TheDriver.generateCompilationDiagnostics(
131 *C, *Cmd, generateReproducerMetaInfo(Info), &Report);
140 std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes);
142 static void printReproducerInformation(
143 llvm::raw_ostream &OS, const ClangInvocationInfo &Info,
144 const driver::Driver::CompilationDiagnosticReport &Report) {
145 OS << "REPRODUCER:\n";
147 OS << R"("files":[)";
148 for (const auto &File : llvm::enumerate(Report.TemporaryFiles)) {
151 OS << '"' << File.value() << '"';
156 int cc1gen_reproducer_main(ArrayRef<const char *> Argv, const char *Argv0,
158 if (Argv.size() < 1) {
159 llvm::errs() << "error: missing invocation file\n";
162 // Parse the invocation descriptor.
163 StringRef Input = Argv[0];
164 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer =
165 llvm::MemoryBuffer::getFile(Input);
167 llvm::errs() << "error: failed to read " << Input << ": "
168 << Buffer.getError().message() << "\n";
171 llvm::yaml::Input YAML(Buffer.get()->getBuffer());
172 ClangInvocationInfo InvocationInfo;
173 YAML >> InvocationInfo;
174 if (Argv.size() > 1 && Argv[1] == StringRef("-v"))
175 InvocationInfo.Dump = true;
177 // Create an invocation that will produce the reproducer.
178 std::vector<const char *> DriverArgs;
179 for (const auto &Arg : InvocationInfo.Arguments)
180 DriverArgs.push_back(Arg.c_str());
181 std::string Path = GetExecutablePath(Argv0, /*CanonicalPrefixes=*/true);
182 DriverArgs[0] = Path.c_str();
183 llvm::Optional<driver::Driver::CompilationDiagnosticReport> Report =
184 generateReproducerForInvocationArguments(DriverArgs, InvocationInfo);
186 // Emit the information about the reproduce files to stdout.
189 printReproducerInformation(llvm::outs(), InvocationInfo, *Report);
193 // Remove the input file.
194 llvm::sys::fs::remove(Input);