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