1 | //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===// |
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 -cc1 functionality, which implements the |
10 | // core compiler functionality along with a number of additional tools for |
11 | // demonstration and testing purposes. |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #include "clang/Basic/Stack.h" |
16 | #include "clang/Basic/TargetOptions.h" |
17 | #include "clang/CodeGen/ObjectFilePCHContainerOperations.h" |
18 | #include "clang/Config/config.h" |
19 | #include "clang/Driver/DriverDiagnostic.h" |
20 | #include "clang/Driver/Options.h" |
21 | #include "clang/Frontend/CompilerInstance.h" |
22 | #include "clang/Frontend/CompilerInvocation.h" |
23 | #include "clang/Frontend/FrontendDiagnostic.h" |
24 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
25 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
26 | #include "clang/Frontend/Utils.h" |
27 | #include "clang/FrontendTool/Utils.h" |
28 | #include "llvm/ADT/Statistic.h" |
29 | #include "llvm/ADT/StringExtras.h" |
30 | #include "llvm/Config/llvm-config.h" |
31 | #include "llvm/LinkAllPasses.h" |
32 | #include "llvm/MC/MCSubtargetInfo.h" |
33 | #include "llvm/MC/TargetRegistry.h" |
34 | #include "llvm/Option/Arg.h" |
35 | #include "llvm/Option/ArgList.h" |
36 | #include "llvm/Option/OptTable.h" |
37 | #include "llvm/Support/BuryPointer.h" |
38 | #include "llvm/Support/Compiler.h" |
39 | #include "llvm/Support/ErrorHandling.h" |
40 | #include "llvm/Support/ManagedStatic.h" |
41 | #include "llvm/Support/Path.h" |
42 | #include "llvm/Support/Process.h" |
43 | #include "llvm/Support/Signals.h" |
44 | #include "llvm/Support/TargetSelect.h" |
45 | #include "llvm/Support/TimeProfiler.h" |
46 | #include "llvm/Support/Timer.h" |
47 | #include "llvm/Support/raw_ostream.h" |
48 | #include "llvm/Target/TargetMachine.h" |
49 | #include "llvm/TargetParser/AArch64TargetParser.h" |
50 | #include "llvm/TargetParser/ARMTargetParser.h" |
51 | #include "llvm/TargetParser/RISCVISAInfo.h" |
52 | #include <cstdio> |
53 | |
54 | #ifdef CLANG_HAVE_RLIMITS |
55 | #include <sys/resource.h> |
56 | #endif |
57 | |
58 | using namespace clang; |
59 | using namespace llvm::opt; |
60 | |
61 | //===----------------------------------------------------------------------===// |
62 | // Main driver |
63 | //===----------------------------------------------------------------------===// |
64 | |
65 | static void LLVMErrorHandler(void *UserData, const char *Message, |
66 | bool GenCrashDiag) { |
67 | DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); |
68 | |
69 | Diags.Report(DiagID: diag::err_fe_error_backend) << Message; |
70 | |
71 | // Run the interrupt handlers to make sure any special cleanups get done, in |
72 | // particular that we remove files registered with RemoveFileOnSignal. |
73 | llvm::sys::RunInterruptHandlers(); |
74 | |
75 | // We cannot recover from llvm errors. When reporting a fatal error, exit |
76 | // with status 70 to generate crash diagnostics. For BSD systems this is |
77 | // defined as an internal software error. Otherwise, exit with status 1. |
78 | llvm::sys::Process::Exit(RetCode: GenCrashDiag ? 70 : 1); |
79 | } |
80 | |
81 | #ifdef CLANG_HAVE_RLIMITS |
82 | /// Attempt to ensure that we have at least 8MiB of usable stack space. |
83 | static void ensureSufficientStack() { |
84 | struct rlimit rlim; |
85 | if (getrlimit(RLIMIT_STACK, rlimits: &rlim) != 0) |
86 | return; |
87 | |
88 | // Increase the soft stack limit to our desired level, if necessary and |
89 | // possible. |
90 | if (rlim.rlim_cur != RLIM_INFINITY && |
91 | rlim.rlim_cur < rlim_t(DesiredStackSize)) { |
92 | // Try to allocate sufficient stack. |
93 | if (rlim.rlim_max == RLIM_INFINITY || |
94 | rlim.rlim_max >= rlim_t(DesiredStackSize)) |
95 | rlim.rlim_cur = DesiredStackSize; |
96 | else if (rlim.rlim_cur == rlim.rlim_max) |
97 | return; |
98 | else |
99 | rlim.rlim_cur = rlim.rlim_max; |
100 | |
101 | if (setrlimit(RLIMIT_STACK, rlimits: &rlim) != 0 || |
102 | rlim.rlim_cur != DesiredStackSize) |
103 | return; |
104 | } |
105 | } |
106 | #else |
107 | static void ensureSufficientStack() {} |
108 | #endif |
109 | |
110 | /// Print supported cpus of the given target. |
111 | static int PrintSupportedCPUs(std::string TargetStr) { |
112 | std::string Error; |
113 | const llvm::Target *TheTarget = |
114 | llvm::TargetRegistry::lookupTarget(Triple: TargetStr, Error); |
115 | if (!TheTarget) { |
116 | llvm::errs() << Error; |
117 | return 1; |
118 | } |
119 | |
120 | // the target machine will handle the mcpu printing |
121 | llvm::TargetOptions Options; |
122 | std::unique_ptr<llvm::TargetMachine> TheTargetMachine( |
123 | TheTarget->createTargetMachine(TT: TargetStr, CPU: "" , Features: "+cpuhelp" , Options, |
124 | RM: std::nullopt)); |
125 | return 0; |
126 | } |
127 | |
128 | static int PrintSupportedExtensions(std::string TargetStr) { |
129 | std::string Error; |
130 | const llvm::Target *TheTarget = |
131 | llvm::TargetRegistry::lookupTarget(Triple: TargetStr, Error); |
132 | if (!TheTarget) { |
133 | llvm::errs() << Error; |
134 | return 1; |
135 | } |
136 | |
137 | llvm::TargetOptions Options; |
138 | std::unique_ptr<llvm::TargetMachine> TheTargetMachine( |
139 | TheTarget->createTargetMachine(TT: TargetStr, CPU: "" , Features: "" , Options, RM: std::nullopt)); |
140 | const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple(); |
141 | const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo(); |
142 | const llvm::ArrayRef<llvm::SubtargetFeatureKV> Features = |
143 | MCInfo->getAllProcessorFeatures(); |
144 | |
145 | llvm::StringMap<llvm::StringRef> DescMap; |
146 | for (const llvm::SubtargetFeatureKV &feature : Features) |
147 | DescMap.insert(KV: {feature.Key, feature.Desc}); |
148 | |
149 | if (MachineTriple.isRISCV()) |
150 | llvm::RISCVISAInfo::printSupportedExtensions(DescMap); |
151 | else if (MachineTriple.isAArch64()) |
152 | llvm::AArch64::PrintSupportedExtensions(); |
153 | else if (MachineTriple.isARM()) |
154 | llvm::ARM::PrintSupportedExtensions(DescMap); |
155 | else { |
156 | // The option was already checked in Driver::HandleImmediateArgs, |
157 | // so we do not expect to get here if we are not a supported architecture. |
158 | assert(0 && "Unhandled triple for --print-supported-extensions option." ); |
159 | return 1; |
160 | } |
161 | |
162 | return 0; |
163 | } |
164 | |
165 | static int PrintEnabledExtensions(const TargetOptions& TargetOpts) { |
166 | std::string Error; |
167 | const llvm::Target *TheTarget = |
168 | llvm::TargetRegistry::lookupTarget(Triple: TargetOpts.Triple, Error); |
169 | if (!TheTarget) { |
170 | llvm::errs() << Error; |
171 | return 1; |
172 | } |
173 | |
174 | // Create a target machine using the input features, the triple information |
175 | // and a dummy instance of llvm::TargetOptions. Note that this is _not_ the |
176 | // same as the `clang::TargetOptions` instance we have access to here. |
177 | llvm::TargetOptions BackendOptions; |
178 | std::string FeaturesStr = llvm::join(R: TargetOpts.FeaturesAsWritten, Separator: "," ); |
179 | std::unique_ptr<llvm::TargetMachine> TheTargetMachine( |
180 | TheTarget->createTargetMachine(TT: TargetOpts.Triple, CPU: TargetOpts.CPU, Features: FeaturesStr, Options: BackendOptions, RM: std::nullopt)); |
181 | const llvm::Triple &MachineTriple = TheTargetMachine->getTargetTriple(); |
182 | const llvm::MCSubtargetInfo *MCInfo = TheTargetMachine->getMCSubtargetInfo(); |
183 | |
184 | // Extract the feature names that are enabled for the given target. |
185 | // We do that by capturing the key from the set of SubtargetFeatureKV entries |
186 | // provided by MCSubtargetInfo, which match the '-target-feature' values. |
187 | const std::vector<llvm::SubtargetFeatureKV> Features = |
188 | MCInfo->getEnabledProcessorFeatures(); |
189 | std::set<llvm::StringRef> EnabledFeatureNames; |
190 | for (const llvm::SubtargetFeatureKV &feature : Features) |
191 | EnabledFeatureNames.insert(x: feature.Key); |
192 | |
193 | if (MachineTriple.isAArch64()) |
194 | llvm::AArch64::printEnabledExtensions(EnabledFeatureNames); |
195 | else if (MachineTriple.isRISCV()) { |
196 | llvm::StringMap<llvm::StringRef> DescMap; |
197 | for (const llvm::SubtargetFeatureKV &feature : Features) |
198 | DescMap.insert(KV: {feature.Key, feature.Desc}); |
199 | llvm::RISCVISAInfo::printEnabledExtensions(IsRV64: MachineTriple.isArch64Bit(), |
200 | EnabledFeatureNames, DescMap); |
201 | } else { |
202 | // The option was already checked in Driver::HandleImmediateArgs, |
203 | // so we do not expect to get here if we are not a supported architecture. |
204 | assert(0 && "Unhandled triple for --print-enabled-extensions option." ); |
205 | return 1; |
206 | } |
207 | |
208 | return 0; |
209 | } |
210 | |
211 | int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { |
212 | ensureSufficientStack(); |
213 | |
214 | std::unique_ptr<CompilerInstance> Clang(new CompilerInstance()); |
215 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
216 | |
217 | // Register the support for object-file-wrapped Clang modules. |
218 | auto PCHOps = Clang->getPCHContainerOperations(); |
219 | PCHOps->registerWriter(Writer: std::make_unique<ObjectFilePCHContainerWriter>()); |
220 | PCHOps->registerReader(Reader: std::make_unique<ObjectFilePCHContainerReader>()); |
221 | |
222 | // Initialize targets first, so that --version shows registered targets. |
223 | llvm::InitializeAllTargets(); |
224 | llvm::InitializeAllTargetMCs(); |
225 | llvm::InitializeAllAsmPrinters(); |
226 | llvm::InitializeAllAsmParsers(); |
227 | |
228 | // Buffer diagnostics from argument parsing so that we can output them using a |
229 | // well formed diagnostic object. |
230 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
231 | TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer; |
232 | DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer); |
233 | |
234 | // Setup round-trip remarks for the DiagnosticsEngine used in CreateFromArgs. |
235 | if (find(Range&: Argv, Val: StringRef("-Rround-trip-cc1-args" )) != Argv.end()) |
236 | Diags.setSeverity(Diag: diag::remark_cc1_round_trip_generated, |
237 | Map: diag::Severity::Remark, Loc: {}); |
238 | |
239 | bool Success = CompilerInvocation::CreateFromArgs(Res&: Clang->getInvocation(), |
240 | CommandLineArgs: Argv, Diags, Argv0); |
241 | |
242 | if (!Clang->getFrontendOpts().TimeTracePath.empty()) { |
243 | llvm::timeTraceProfilerInitialize( |
244 | TimeTraceGranularity: Clang->getFrontendOpts().TimeTraceGranularity, ProcName: Argv0, |
245 | TimeTraceVerbose: Clang->getFrontendOpts().TimeTraceVerbose); |
246 | } |
247 | // --print-supported-cpus takes priority over the actual compilation. |
248 | if (Clang->getFrontendOpts().PrintSupportedCPUs) |
249 | return PrintSupportedCPUs(TargetStr: Clang->getTargetOpts().Triple); |
250 | |
251 | // --print-supported-extensions takes priority over the actual compilation. |
252 | if (Clang->getFrontendOpts().PrintSupportedExtensions) |
253 | return PrintSupportedExtensions(TargetStr: Clang->getTargetOpts().Triple); |
254 | |
255 | // --print-enabled-extensions takes priority over the actual compilation. |
256 | if (Clang->getFrontendOpts().PrintEnabledExtensions) |
257 | return PrintEnabledExtensions(TargetOpts: Clang->getTargetOpts()); |
258 | |
259 | // Infer the builtin include path if unspecified. |
260 | if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && |
261 | Clang->getHeaderSearchOpts().ResourceDir.empty()) |
262 | Clang->getHeaderSearchOpts().ResourceDir = |
263 | CompilerInvocation::GetResourcesPath(Argv0, MainAddr); |
264 | |
265 | // Create the actual diagnostics engine. |
266 | Clang->createDiagnostics(); |
267 | if (!Clang->hasDiagnostics()) |
268 | return 1; |
269 | |
270 | // Set an error handler, so that any LLVM backend diagnostics go through our |
271 | // error handler. |
272 | llvm::install_fatal_error_handler(handler: LLVMErrorHandler, |
273 | user_data: static_cast<void*>(&Clang->getDiagnostics())); |
274 | |
275 | DiagsBuffer->FlushDiagnostics(Diags&: Clang->getDiagnostics()); |
276 | if (!Success) { |
277 | Clang->getDiagnosticClient().finish(); |
278 | return 1; |
279 | } |
280 | |
281 | // Execute the frontend actions. |
282 | { |
283 | llvm::TimeTraceScope TimeScope("ExecuteCompiler" ); |
284 | Success = ExecuteCompilerInvocation(Clang: Clang.get()); |
285 | } |
286 | |
287 | // If any timers were active but haven't been destroyed yet, print their |
288 | // results now. This happens in -disable-free mode. |
289 | llvm::TimerGroup::printAll(OS&: llvm::errs()); |
290 | llvm::TimerGroup::clearAll(); |
291 | |
292 | if (llvm::timeTraceProfilerEnabled()) { |
293 | // It is possible that the compiler instance doesn't own a file manager here |
294 | // if we're compiling a module unit. Since the file manager are owned by AST |
295 | // when we're compiling a module unit. So the file manager may be invalid |
296 | // here. |
297 | // |
298 | // It should be fine to create file manager here since the file system |
299 | // options are stored in the compiler invocation and we can recreate the VFS |
300 | // from the compiler invocation. |
301 | if (!Clang->hasFileManager()) |
302 | Clang->createFileManager(VFS: createVFSFromCompilerInvocation( |
303 | CI: Clang->getInvocation(), Diags&: Clang->getDiagnostics())); |
304 | |
305 | if (auto profilerOutput = Clang->createOutputFile( |
306 | OutputPath: Clang->getFrontendOpts().TimeTracePath, /*Binary=*/false, |
307 | /*RemoveFileOnSignal=*/false, |
308 | /*useTemporary=*/UseTemporary: false)) { |
309 | llvm::timeTraceProfilerWrite(OS&: *profilerOutput); |
310 | profilerOutput.reset(); |
311 | llvm::timeTraceProfilerCleanup(); |
312 | Clang->clearOutputFiles(EraseFiles: false); |
313 | } |
314 | } |
315 | |
316 | // Our error handler depends on the Diagnostics object, which we're |
317 | // potentially about to delete. Uninstall the handler now so that any |
318 | // later errors use the default handling behavior instead. |
319 | llvm::remove_fatal_error_handler(); |
320 | |
321 | // When running with -disable-free, don't do any destruction or shutdown. |
322 | if (Clang->getFrontendOpts().DisableFree) { |
323 | llvm::BuryPointer(Ptr: std::move(Clang)); |
324 | return !Success; |
325 | } |
326 | |
327 | return !Success; |
328 | } |
329 | |