/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/Host.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 | | |
27 | | using namespace clang; |
28 | | |
29 | | namespace { |
30 | | |
31 | | struct UnsavedFileHash { |
32 | | std::string Name; |
33 | | std::string MD5; |
34 | | }; |
35 | | |
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; |
43 | | bool Dump = false; |
44 | | }; |
45 | | |
46 | | } // end anonymous namespace |
47 | | |
48 | | LLVM_YAML_IS_SEQUENCE_VECTOR(UnsavedFileHash) |
49 | | |
50 | | namespace llvm { |
51 | | namespace yaml { |
52 | | |
53 | | template <> struct MappingTraits<UnsavedFileHash> { |
54 | 2 | static void mapping(IO &IO, UnsavedFileHash &Info) { |
55 | 2 | IO.mapRequired("name", Info.Name); |
56 | 2 | IO.mapRequired("md5", Info.MD5); |
57 | 2 | } |
58 | | }; |
59 | | |
60 | | template <> struct MappingTraits<ClangInvocationInfo> { |
61 | 3 | static void mapping(IO &IO, ClangInvocationInfo &Info) { |
62 | 3 | IO.mapRequired("toolchain", Info.Toolchain); |
63 | 3 | IO.mapOptional("libclang.operation", Info.LibclangOperation); |
64 | 3 | IO.mapOptional("libclang.opts", Info.LibclangOptions); |
65 | 3 | IO.mapRequired("args", Info.Arguments); |
66 | 3 | IO.mapOptional("invocation-args", Info.InvocationArguments); |
67 | 3 | IO.mapOptional("unsaved_file_hashes", Info.UnsavedFileHashes); |
68 | 3 | } |
69 | | }; |
70 | | |
71 | | } // end namespace yaml |
72 | | } // end namespace llvm |
73 | | |
74 | 3 | static std::string generateReproducerMetaInfo(const ClangInvocationInfo &Info) { |
75 | 3 | std::string Result; |
76 | 3 | llvm::raw_string_ostream OS(Result); |
77 | 3 | OS << '{'; |
78 | 3 | bool NeedComma = false; |
79 | 7 | auto EmitKey = [&](StringRef Key) { |
80 | 7 | if (NeedComma) |
81 | 4 | OS << ", "; |
82 | 7 | NeedComma = true; |
83 | 7 | OS << '"' << Key << "\": "; |
84 | 7 | }; |
85 | 6 | auto EmitStringKey = [&](StringRef Key, StringRef Value) { |
86 | 6 | if (Value.empty()) |
87 | 0 | return; |
88 | 6 | EmitKey(Key); |
89 | 6 | OS << '"' << Value << '"'; |
90 | 6 | }; |
91 | 3 | EmitStringKey("libclang.operation", Info.LibclangOperation); |
92 | 3 | EmitStringKey("libclang.opts", Info.LibclangOptions); |
93 | 3 | if (!Info.InvocationArguments.empty()) { |
94 | 1 | EmitKey("invocation-args"); |
95 | 1 | OS << '['; |
96 | 1 | for (const auto &Arg : llvm::enumerate(Info.InvocationArguments)) { |
97 | 1 | if (Arg.index()) |
98 | 0 | OS << ','; |
99 | 1 | OS << '"' << Arg.value() << '"'; |
100 | 1 | } |
101 | 1 | OS << ']'; |
102 | 1 | } |
103 | 3 | OS << '}'; |
104 | | // FIXME: Compare unsaved file hashes and report mismatch in the reproducer. |
105 | 3 | if (Info.Dump) |
106 | 3 | llvm::outs() << "REPRODUCER METAINFO: " << OS.str() << "\n"; |
107 | 3 | return std::move(OS.str()); |
108 | 3 | } |
109 | | |
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 | 3 | const ClangInvocationInfo &Info) { |
114 | 3 | using namespace driver; |
115 | 3 | auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(Argv[0]); |
116 | | |
117 | 3 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions; |
118 | | |
119 | 3 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
120 | 3 | DiagnosticsEngine Diags(DiagID, &*DiagOpts, new IgnoringDiagConsumer()); |
121 | 3 | ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false); |
122 | 3 | Driver TheDriver(Argv[0], llvm::sys::getDefaultTargetTriple(), Diags); |
123 | 3 | TheDriver.setTargetAndMode(TargetAndMode); |
124 | | |
125 | 3 | std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Argv)); |
126 | 3 | if (C && !C->containsError()) { |
127 | 3 | for (const auto &J : C->getJobs()) { |
128 | 3 | if (const Command *Cmd = dyn_cast<Command>(&J)) { |
129 | 3 | Driver::CompilationDiagnosticReport Report; |
130 | 3 | TheDriver.generateCompilationDiagnostics( |
131 | 3 | *C, *Cmd, generateReproducerMetaInfo(Info), &Report); |
132 | 3 | return Report; |
133 | 3 | } |
134 | 3 | } |
135 | 3 | } |
136 | | |
137 | 0 | return None; |
138 | 3 | } |
139 | | |
140 | | std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes); |
141 | | |
142 | | static void printReproducerInformation( |
143 | | llvm::raw_ostream &OS, const ClangInvocationInfo &Info, |
144 | 3 | const driver::Driver::CompilationDiagnosticReport &Report) { |
145 | 3 | OS << "REPRODUCER:\n"; |
146 | 3 | OS << "{\n"; |
147 | 3 | OS << R"("files":[)"; |
148 | 6 | for (const auto &File : llvm::enumerate(Report.TemporaryFiles)) { |
149 | 6 | if (File.index()) |
150 | 3 | OS << ','; |
151 | 6 | OS << '"' << File.value() << '"'; |
152 | 6 | } |
153 | 3 | OS << "]\n}\n"; |
154 | 3 | } |
155 | | |
156 | | int cc1gen_reproducer_main(ArrayRef<const char *> Argv, const char *Argv0, |
157 | 3 | void *MainAddr) { |
158 | 3 | if (Argv.size() < 1) { |
159 | 0 | llvm::errs() << "error: missing invocation file\n"; |
160 | 0 | return 1; |
161 | 0 | } |
162 | | // Parse the invocation descriptor. |
163 | 3 | StringRef Input = Argv[0]; |
164 | 3 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buffer = |
165 | 3 | llvm::MemoryBuffer::getFile(Input, /*IsText=*/true); |
166 | 3 | if (!Buffer) { |
167 | 0 | llvm::errs() << "error: failed to read " << Input << ": " |
168 | 0 | << Buffer.getError().message() << "\n"; |
169 | 0 | return 1; |
170 | 0 | } |
171 | 3 | llvm::yaml::Input YAML(Buffer.get()->getBuffer()); |
172 | 3 | ClangInvocationInfo InvocationInfo; |
173 | 3 | YAML >> InvocationInfo; |
174 | 3 | if (Argv.size() > 1 && Argv[1] == StringRef("-v")) |
175 | 3 | InvocationInfo.Dump = true; |
176 | | |
177 | | // Create an invocation that will produce the reproducer. |
178 | 3 | std::vector<const char *> DriverArgs; |
179 | 3 | for (const auto &Arg : InvocationInfo.Arguments) |
180 | 18 | DriverArgs.push_back(Arg.c_str()); |
181 | 3 | std::string Path = GetExecutablePath(Argv0, /*CanonicalPrefixes=*/true); |
182 | 3 | DriverArgs[0] = Path.c_str(); |
183 | 3 | llvm::Optional<driver::Driver::CompilationDiagnosticReport> Report = |
184 | 3 | generateReproducerForInvocationArguments(DriverArgs, InvocationInfo); |
185 | | |
186 | | // Emit the information about the reproduce files to stdout. |
187 | 3 | int Result = 1; |
188 | 3 | if (Report) { |
189 | 3 | printReproducerInformation(llvm::outs(), InvocationInfo, *Report); |
190 | 3 | Result = 0; |
191 | 3 | } |
192 | | |
193 | | // Remove the input file. |
194 | 3 | llvm::sys::fs::remove(Input); |
195 | 3 | return Result; |
196 | 3 | } |