1 //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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"
56 #ifdef CLANG_HAVE_RLIMITS
57 #include <sys/resource.h>
60 using namespace clang
;
61 using namespace llvm::opt
;
63 //===----------------------------------------------------------------------===//
65 //===----------------------------------------------------------------------===//
67 static void LLVMErrorHandler(void *UserData
, const char *Message
,
69 DiagnosticsEngine
&Diags
= *static_cast<DiagnosticsEngine
*>(UserData
);
71 Diags
.Report(diag::err_fe_error_backend
) << Message
;
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();
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);
83 #ifdef CLANG_HAVE_RLIMITS
84 /// Attempt to ensure that we have at least 8MiB of usable stack space.
85 static void ensureSufficientStack() {
87 if (getrlimit(RLIMIT_STACK
, &rlim
) != 0)
90 // Increase the soft stack limit to our desired level, if necessary and
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
)
101 rlim
.rlim_cur
= rlim
.rlim_max
;
103 if (setrlimit(RLIMIT_STACK
, &rlim
) != 0 ||
104 rlim
.rlim_cur
!= DesiredStackSize
)
109 static void ensureSufficientStack() {}
112 /// Print supported cpus of the given target.
113 static int PrintSupportedCPUs(std::string TargetStr
) {
115 const llvm::Target
*TheTarget
=
116 llvm::TargetRegistry::lookupTarget(TargetStr
, Error
);
118 llvm::errs() << Error
;
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
,
130 static int PrintSupportedExtensions(std::string TargetStr
) {
132 const llvm::Target
*TheTarget
=
133 llvm::TargetRegistry::lookupTarget(TargetStr
, Error
);
135 llvm::errs() << Error
;
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();
147 llvm::StringMap
<llvm::StringRef
> DescMap
;
148 for (const llvm::SubtargetFeatureKV
&feature
: Features
)
149 DescMap
.insert({feature
.Key
, feature
.Desc
});
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
);
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.");
167 static int PrintEnabledExtensions(const TargetOptions
& TargetOpts
) {
169 const llvm::Target
*TheTarget
=
170 llvm::TargetRegistry::lookupTarget(TargetOpts
.Triple
, Error
);
172 llvm::errs() << Error
;
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();
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
);
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
);
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.");
213 int cc1_main(ArrayRef
<const char *> Argv
, const char *Argv0
, void *MainAddr
) {
214 ensureSufficientStack();
216 std::unique_ptr
<CompilerInstance
> Clang(new CompilerInstance());
217 IntrusiveRefCntPtr
<DiagnosticIDs
> DiagID(new DiagnosticIDs());
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
>());
224 // Initialize targets first, so that --version shows registered targets.
225 llvm::InitializeAllTargets();
226 llvm::InitializeAllTargetMCs();
227 llvm::InitializeAllAsmPrinters();
228 llvm::InitializeAllAsmParsers();
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
);
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
, {});
241 bool Success
= CompilerInvocation::CreateFromArgs(Clang
->getInvocation(),
244 if (!Clang
->getFrontendOpts().TimeTracePath
.empty()) {
245 llvm::timeTraceProfilerInitialize(
246 Clang
->getFrontendOpts().TimeTraceGranularity
, Argv0
,
247 Clang
->getFrontendOpts().TimeTraceVerbose
);
249 // --print-supported-cpus takes priority over the actual compilation.
250 if (Clang
->getFrontendOpts().PrintSupportedCPUs
)
251 return PrintSupportedCPUs(Clang
->getTargetOpts().Triple
);
253 // --print-supported-extensions takes priority over the actual compilation.
254 if (Clang
->getFrontendOpts().PrintSupportedExtensions
)
255 return PrintSupportedExtensions(Clang
->getTargetOpts().Triple
);
257 // --print-enabled-extensions takes priority over the actual compilation.
258 if (Clang
->getFrontendOpts().PrintEnabledExtensions
)
259 return PrintEnabledExtensions(Clang
->getTargetOpts());
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
);
267 // Create the actual diagnostics engine.
268 Clang
->createDiagnostics(*llvm::vfs::getRealFileSystem());
269 if (!Clang
->hasDiagnostics())
272 // Set an error handler, so that any LLVM backend diagnostics go through our
274 llvm::install_fatal_error_handler(LLVMErrorHandler
,
275 static_cast<void*>(&Clang
->getDiagnostics()));
277 DiagsBuffer
->FlushDiagnostics(Clang
->getDiagnostics());
279 Clang
->getDiagnosticClient().finish();
283 // Execute the frontend actions.
285 llvm::TimeTraceScope
TimeScope("ExecuteCompiler");
286 Success
= ExecuteCompilerInvocation(Clang
.get());
289 // If any timers were active but haven't been destroyed yet, print their
290 // results now. This happens in -disable-free mode.
291 llvm::TimerGroup::printAll(llvm::errs());
292 llvm::TimerGroup::clearAll();
294 if (llvm::timeTraceProfilerEnabled()) {
295 // It is possible that the compiler instance doesn't own a file manager here
296 // if we're compiling a module unit. Since the file manager are owned by AST
297 // when we're compiling a module unit. So the file manager may be invalid
300 // It should be fine to create file manager here since the file system
301 // options are stored in the compiler invocation and we can recreate the VFS
302 // from the compiler invocation.
303 if (!Clang
->hasFileManager())
304 Clang
->createFileManager(createVFSFromCompilerInvocation(
305 Clang
->getInvocation(), Clang
->getDiagnostics()));
307 if (auto profilerOutput
= Clang
->createOutputFile(
308 Clang
->getFrontendOpts().TimeTracePath
, /*Binary=*/false,
309 /*RemoveFileOnSignal=*/false,
310 /*useTemporary=*/false)) {
311 llvm::timeTraceProfilerWrite(*profilerOutput
);
312 profilerOutput
.reset();
313 llvm::timeTraceProfilerCleanup();
314 Clang
->clearOutputFiles(false);
318 // Our error handler depends on the Diagnostics object, which we're
319 // potentially about to delete. Uninstall the handler now so that any
320 // later errors use the default handling behavior instead.
321 llvm::remove_fatal_error_handler();
323 // When running with -disable-free, don't do any destruction or shutdown.
324 if (Clang
->getFrontendOpts().DisableFree
) {
325 llvm::BuryPointer(std::move(Clang
));