/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/driver/cc1gen_reproducer_main.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- cc1gen_reproducer_main.cpp - Clang reproducer generator ----------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This is the entry point to the clang -cc1gen-reproducer functionality, which |
10 | | // generates reproducers for invocations for clang-based tools. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/Basic/Diagnostic.h" |
15 | | #include "clang/Basic/LLVM.h" |
16 | | #include "clang/Driver/Compilation.h" |
17 | | #include "clang/Driver/Driver.h" |
18 | | #include "llvm/ADT/ArrayRef.h" |
19 | | #include "llvm/ADT/STLExtras.h" |
20 | | #include "llvm/Support/FileSystem.h" |
21 | | #include "llvm/Support/LLVMDriver.h" |
22 | | #include "llvm/Support/TargetSelect.h" |
23 | | #include "llvm/Support/VirtualFileSystem.h" |
24 | | #include "llvm/Support/YAMLTraits.h" |
25 | | #include "llvm/Support/raw_ostream.h" |
26 | | #include "llvm/TargetParser/Host.h" |
27 | | #include <optional> |
28 | | |
29 | | using namespace clang; |
30 | | |
31 | | namespace { |
32 | | |
33 | | struct UnsavedFileHash { |
34 | | std::string Name; |
35 | | std::string MD5; |
36 | | }; |
37 | | |
38 | | struct ClangInvocationInfo { |
39 | | std::string Toolchain; |
40 | | std::string LibclangOperation; |
41 | | std::string LibclangOptions; |
42 | | std::vector<std::string> Arguments; |
43 | | std::vector<std::string> InvocationArguments; |
44 | | std::vector<UnsavedFileHash> UnsavedFileHashes; |
45 | | bool Dump = false; |
46 | | }; |
47 | | |
48 | | } // end anonymous namespace |
49 | | |
50 | | LLVM_YAML_IS_SEQUENCE_VECTOR(UnsavedFileHash) |
51 | | |
52 | | namespace llvm { |
53 | | namespace yaml { |
54 | | |
55 | | template <> struct MappingTraits<UnsavedFileHash> { |
56 | 2 | static void mapping(IO &IO, UnsavedFileHash &Info) { |
57 | 2 | IO.mapRequired("name", Info.Name); |
58 | 2 | IO.mapRequired("md5", Info.MD5); |
59 | 2 | } |
60 | | }; |
61 | | |
62 | | template <> struct MappingTraits<ClangInvocationInfo> { |
63 | 3 | static void mapping(IO &IO, ClangInvocationInfo &Info) { |
64 | 3 | IO.mapRequired("toolchain", Info.Toolchain); |
65 | 3 | IO.mapOptional("libclang.operation", Info.LibclangOperation); |
66 | 3 | IO.mapOptional("libclang.opts", Info.LibclangOptions); |
67 | 3 | IO.mapRequired("args", Info.Arguments); |
68 | 3 | IO.mapOptional("invocation-args", Info.InvocationArguments); |
69 | 3 | IO.mapOptional("unsaved_file_hashes", Info.UnsavedFileHashes); |
70 | 3 | } |
71 | | }; |
72 | | |
73 | | } // end namespace yaml |
74 | | } // end namespace llvm |
75 | | |
76 | 3 | static std::string generateReproducerMetaInfo(const ClangInvocationInfo &Info) { |
77 | 3 | std::string Result; |
78 | 3 | llvm::raw_string_ostream OS(Result); |
79 | 3 | OS << '{'; |
80 | 3 | bool NeedComma = false; |
81 | 7 | auto EmitKey = [&](StringRef Key) { |
82 | 7 | if (NeedComma) |
83 | 4 | OS << ", "; |
84 | 7 | NeedComma = true; |
85 | 7 | OS << '"' << Key << "\": "; |
86 | 7 | }; |
87 | 6 | auto EmitStringKey = [&](StringRef Key, StringRef Value) { |
88 | 6 | if (Value.empty()) |
89 | 0 | return; |
90 | 6 | EmitKey(Key); |
91 | 6 | OS << '"' << Value << '"'; |
92 | 6 | }; |
93 | 3 | EmitStringKey("libclang.operation", Info.LibclangOperation); |
94 | 3 | EmitStringKey("libclang.opts", Info.LibclangOptions); |
95 | 3 | if (!Info.InvocationArguments.empty()) { |
96 | 1 | EmitKey("invocation-args"); |
97 | 1 | OS << '['; |
98 | 1 | for (const auto &Arg : llvm::enumerate(Info.InvocationArguments)) { |
99 | 1 | if (Arg.index()) |
100 | 0 | OS << ','; |
101 | 1 | OS << '"' << Arg.value() << '"'; |
102 | 1 | } |
103 | 1 | OS << ']'; |
104 | 1 | } |
105 | 3 | OS << '}'; |
106 | | // FIXME: Compare unsaved file hashes and report mismatch in the reproducer. |
107 | 3 | if (Info.Dump) |
108 | 3 | llvm::outs() << "REPRODUCER METAINFO: " << OS.str() << "\n"; |
109 | 3 | return std::move(OS.str()); |
110 | 3 | } |
111 | | |
112 | | /// Generates a reproducer for a set of arguments from a specific invocation. |
113 | | static std::optional<driver::Driver::CompilationDiagnosticReport> |
114 | | generateReproducerForInvocationArguments(ArrayRef<const char *> Argv, |
115 | | const ClangInvocationInfo &Info, |
116 | 3 | const llvm::ToolContext &ToolContext) { |
117 | 3 | using namespace driver; |
118 | 3 | auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(Argv[0]); |
119 | | |
120 | 3 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions; |
121 | | |
122 | 3 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
123 | 3 | DiagnosticsEngine Diags(DiagID, &*DiagOpts, new IgnoringDiagConsumer()); |
124 | 3 | ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false); |
125 | 3 | Driver TheDriver(ToolContext.Path, llvm::sys::getDefaultTargetTriple(), |
126 | 3 | Diags); |
127 | 3 | TheDriver.setTargetAndMode(TargetAndMode); |
128 | 3 | if (ToolContext.NeedsPrependArg) |
129 | 0 | TheDriver.setPrependArg(ToolContext.PrependArg); |
130 | | |
131 | 3 | std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Argv)); |
132 | 3 | if (C && !C->containsError()) { |
133 | 3 | for (const auto &J : C->getJobs()) { |
134 | 3 | if (const Command *Cmd = dyn_cast<Command>(&J)) { |
135 | 3 | Driver::CompilationDiagnosticReport Report; |
136 | 3 | TheDriver.generateCompilationDiagnostics( |
137 | 3 | *C, *Cmd, generateReproducerMetaInfo(Info), &Report); |
138 | 3 | return Report; |
139 | 3 | } |
140 | 3 | } |
141 | 3 | } |
142 | | |
143 | 0 | return std::nullopt; |
144 | 3 | } |
145 | | |
146 | | std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes); |
147 | | |
148 | | static void printReproducerInformation( |
149 | | llvm::raw_ostream &OS, const ClangInvocationInfo &Info, |
150 | 3 | const driver::Driver::CompilationDiagnosticReport &Report) { |
151 | 3 | OS << "REPRODUCER:\n"; |
152 | 3 | OS << "{\n"; |
153 | 3 | OS << R"("files":[)"; |
154 | 6 | for (const auto &File : llvm::enumerate(Report.TemporaryFiles)) { |
155 | 6 | if (File.index()) |
156 | 3 | OS << ','; |
157 | 6 | OS << '"' << File.value() << '"'; |
158 | 6 | } |
159 | 3 | OS << "]\n}\n"; |
160 | 3 | } |
161 | | |
162 | | int cc1gen_reproducer_main(ArrayRef<const char *> Argv, const char *Argv0, |
163 | | void *MainAddr, |
164 | 3 | const llvm::ToolContext &ToolContext) { |
165 | 3 | if (Argv.size() < 1) { |
166 | 0 | llvm::errs() << "error: missing invocation file\n"; |
167 | 0 | return 1; |
168 | 0 | } |
169 | | // Parse the invocation descriptor. |
170 | 3 | StringRef Input = Argv[0]; |
171 | 3 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer = |
172 | 3 | llvm::MemoryBuffer::getFile(Input, /*IsText=*/true); |
173 | 3 | if (!Buffer) { |
174 | 0 | llvm::errs() << "error: failed to read " << Input << ": " |
175 | 0 | << Buffer.getError().message() << "\n"; |
176 | 0 | return 1; |
177 | 0 | } |
178 | 3 | llvm::yaml::Input YAML(Buffer.get()->getBuffer()); |
179 | 3 | ClangInvocationInfo InvocationInfo; |
180 | 3 | YAML >> InvocationInfo; |
181 | 3 | if (Argv.size() > 1 && Argv[1] == StringRef("-v")) |
182 | 3 | InvocationInfo.Dump = true; |
183 | | |
184 | | // Create an invocation that will produce the reproducer. |
185 | 3 | std::vector<const char *> DriverArgs; |
186 | 3 | for (const auto &Arg : InvocationInfo.Arguments) |
187 | 18 | DriverArgs.push_back(Arg.c_str()); |
188 | 3 | std::string Path = GetExecutablePath(Argv0, /*CanonicalPrefixes=*/true); |
189 | 3 | DriverArgs[0] = Path.c_str(); |
190 | 3 | std::optional<driver::Driver::CompilationDiagnosticReport> Report = |
191 | 3 | generateReproducerForInvocationArguments(DriverArgs, InvocationInfo, |
192 | 3 | ToolContext); |
193 | | |
194 | | // Emit the information about the reproduce files to stdout. |
195 | 3 | int Result = 1; |
196 | 3 | if (Report) { |
197 | 3 | printReproducerInformation(llvm::outs(), InvocationInfo, *Report); |
198 | 3 | Result = 0; |
199 | 3 | } |
200 | | |
201 | | // Remove the input file. |
202 | 3 | llvm::sys::fs::remove(Input); |
203 | 3 | return Result; |
204 | 3 | } |