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