xref: /aosp_15_r20/external/clang/tools/driver/cc1_main.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This is the entry point to the clang -cc1 functionality, which implements the
11*67e74705SXin Li // core compiler functionality along with a number of additional tools for
12*67e74705SXin Li // demonstration and testing purposes.
13*67e74705SXin Li //
14*67e74705SXin Li //===----------------------------------------------------------------------===//
15*67e74705SXin Li 
16*67e74705SXin Li #include "llvm/Option/Arg.h"
17*67e74705SXin Li #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
18*67e74705SXin Li #include "clang/Driver/DriverDiagnostic.h"
19*67e74705SXin Li #include "clang/Driver/Options.h"
20*67e74705SXin Li #include "clang/Frontend/CompilerInstance.h"
21*67e74705SXin Li #include "clang/Frontend/CompilerInvocation.h"
22*67e74705SXin Li #include "clang/Frontend/FrontendDiagnostic.h"
23*67e74705SXin Li #include "clang/Frontend/TextDiagnosticBuffer.h"
24*67e74705SXin Li #include "clang/Frontend/TextDiagnosticPrinter.h"
25*67e74705SXin Li #include "clang/Frontend/Utils.h"
26*67e74705SXin Li #include "clang/FrontendTool/Utils.h"
27*67e74705SXin Li #include "llvm/ADT/Statistic.h"
28*67e74705SXin Li #include "llvm/LinkAllPasses.h"
29*67e74705SXin Li #include "llvm/Option/ArgList.h"
30*67e74705SXin Li #include "llvm/Option/OptTable.h"
31*67e74705SXin Li #include "llvm/Support/ErrorHandling.h"
32*67e74705SXin Li #include "llvm/Support/ManagedStatic.h"
33*67e74705SXin Li #include "llvm/Support/Signals.h"
34*67e74705SXin Li #include "llvm/Support/TargetSelect.h"
35*67e74705SXin Li #include "llvm/Support/Timer.h"
36*67e74705SXin Li #include "llvm/Support/raw_ostream.h"
37*67e74705SXin Li #include <cstdio>
38*67e74705SXin Li using namespace clang;
39*67e74705SXin Li using namespace llvm::opt;
40*67e74705SXin Li 
41*67e74705SXin Li //===----------------------------------------------------------------------===//
42*67e74705SXin Li // Main driver
43*67e74705SXin Li //===----------------------------------------------------------------------===//
44*67e74705SXin Li 
LLVMErrorHandler(void * UserData,const std::string & Message,bool GenCrashDiag)45*67e74705SXin Li static void LLVMErrorHandler(void *UserData, const std::string &Message,
46*67e74705SXin Li                              bool GenCrashDiag) {
47*67e74705SXin Li   DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
48*67e74705SXin Li 
49*67e74705SXin Li   Diags.Report(diag::err_fe_error_backend) << Message;
50*67e74705SXin Li 
51*67e74705SXin Li   // Run the interrupt handlers to make sure any special cleanups get done, in
52*67e74705SXin Li   // particular that we remove files registered with RemoveFileOnSignal.
53*67e74705SXin Li   llvm::sys::RunInterruptHandlers();
54*67e74705SXin Li 
55*67e74705SXin Li   // We cannot recover from llvm errors.  When reporting a fatal error, exit
56*67e74705SXin Li   // with status 70 to generate crash diagnostics.  For BSD systems this is
57*67e74705SXin Li   // defined as an internal software error.  Otherwise, exit with status 1.
58*67e74705SXin Li   exit(GenCrashDiag ? 70 : 1);
59*67e74705SXin Li }
60*67e74705SXin Li 
61*67e74705SXin Li #ifdef LINK_POLLY_INTO_TOOLS
62*67e74705SXin Li namespace polly {
63*67e74705SXin Li void initializePollyPasses(llvm::PassRegistry &Registry);
64*67e74705SXin Li }
65*67e74705SXin Li #endif
66*67e74705SXin Li 
cc1_main(ArrayRef<const char * > Argv,const char * Argv0,void * MainAddr)67*67e74705SXin Li int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
68*67e74705SXin Li   std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
69*67e74705SXin Li   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
70*67e74705SXin Li 
71*67e74705SXin Li   // Register the support for object-file-wrapped Clang modules.
72*67e74705SXin Li   auto PCHOps = Clang->getPCHContainerOperations();
73*67e74705SXin Li   PCHOps->registerWriter(llvm::make_unique<ObjectFilePCHContainerWriter>());
74*67e74705SXin Li   PCHOps->registerReader(llvm::make_unique<ObjectFilePCHContainerReader>());
75*67e74705SXin Li 
76*67e74705SXin Li   // Initialize targets first, so that --version shows registered targets.
77*67e74705SXin Li   llvm::InitializeAllTargets();
78*67e74705SXin Li   llvm::InitializeAllTargetMCs();
79*67e74705SXin Li   llvm::InitializeAllAsmPrinters();
80*67e74705SXin Li   llvm::InitializeAllAsmParsers();
81*67e74705SXin Li 
82*67e74705SXin Li #ifdef LINK_POLLY_INTO_TOOLS
83*67e74705SXin Li   llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry();
84*67e74705SXin Li   polly::initializePollyPasses(Registry);
85*67e74705SXin Li #endif
86*67e74705SXin Li 
87*67e74705SXin Li   // Buffer diagnostics from argument parsing so that we can output them using a
88*67e74705SXin Li   // well formed diagnostic object.
89*67e74705SXin Li   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
90*67e74705SXin Li   TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
91*67e74705SXin Li   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
92*67e74705SXin Li   bool Success = CompilerInvocation::CreateFromArgs(
93*67e74705SXin Li       Clang->getInvocation(), Argv.begin(), Argv.end(), Diags);
94*67e74705SXin Li 
95*67e74705SXin Li   // Infer the builtin include path if unspecified.
96*67e74705SXin Li   if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
97*67e74705SXin Li       Clang->getHeaderSearchOpts().ResourceDir.empty())
98*67e74705SXin Li     Clang->getHeaderSearchOpts().ResourceDir =
99*67e74705SXin Li       CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
100*67e74705SXin Li 
101*67e74705SXin Li   // Create the actual diagnostics engine.
102*67e74705SXin Li   Clang->createDiagnostics();
103*67e74705SXin Li   if (!Clang->hasDiagnostics())
104*67e74705SXin Li     return 1;
105*67e74705SXin Li 
106*67e74705SXin Li   // Set an error handler, so that any LLVM backend diagnostics go through our
107*67e74705SXin Li   // error handler.
108*67e74705SXin Li   llvm::install_fatal_error_handler(LLVMErrorHandler,
109*67e74705SXin Li                                   static_cast<void*>(&Clang->getDiagnostics()));
110*67e74705SXin Li 
111*67e74705SXin Li   DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
112*67e74705SXin Li   if (!Success)
113*67e74705SXin Li     return 1;
114*67e74705SXin Li 
115*67e74705SXin Li   // Execute the frontend actions.
116*67e74705SXin Li   Success = ExecuteCompilerInvocation(Clang.get());
117*67e74705SXin Li 
118*67e74705SXin Li   // If any timers were active but haven't been destroyed yet, print their
119*67e74705SXin Li   // results now.  This happens in -disable-free mode.
120*67e74705SXin Li   llvm::TimerGroup::printAll(llvm::errs());
121*67e74705SXin Li 
122*67e74705SXin Li   // Our error handler depends on the Diagnostics object, which we're
123*67e74705SXin Li   // potentially about to delete. Uninstall the handler now so that any
124*67e74705SXin Li   // later errors use the default handling behavior instead.
125*67e74705SXin Li   llvm::remove_fatal_error_handler();
126*67e74705SXin Li 
127*67e74705SXin Li   // When running with -disable-free, don't do any destruction or shutdown.
128*67e74705SXin Li   if (Clang->getFrontendOpts().DisableFree) {
129*67e74705SXin Li     BuryPointer(std::move(Clang));
130*67e74705SXin Li     return !Success;
131*67e74705SXin Li   }
132*67e74705SXin Li 
133*67e74705SXin Li   return !Success;
134*67e74705SXin Li }
135