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/CodeGen/ObjectFilePCHContainerOperations.h"
18 #include "clang/Driver/DriverDiagnostic.h"
19 #include "clang/Driver/Options.h"
20 #include "clang/Frontend/CompilerInstance.h"
21 #include "clang/Frontend/CompilerInvocation.h"
22 #include "clang/Frontend/FrontendDiagnostic.h"
23 #include "clang/Frontend/TextDiagnosticBuffer.h"
24 #include "clang/Frontend/TextDiagnosticPrinter.h"
25 #include "clang/Frontend/Utils.h"
26 #include "clang/FrontendTool/Utils.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/LinkAllPasses.h"
29 #include "llvm/Option/ArgList.h"
30 #include "llvm/Option/OptTable.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/ManagedStatic.h"
33 #include "llvm/Support/Signals.h"
34 #include "llvm/Support/TargetSelect.h"
35 #include "llvm/Support/Timer.h"
36 #include "llvm/Support/raw_ostream.h"
38 using namespace clang;
39 using namespace llvm::opt;
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
45 static void LLVMErrorHandler(void *UserData, const std::string &Message,
47 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
49 Diags.Report(diag::err_fe_error_backend) << Message;
51 // Run the interrupt handlers to make sure any special cleanups get done, in
52 // particular that we remove files registered with RemoveFileOnSignal.
53 llvm::sys::RunInterruptHandlers();
55 // We cannot recover from llvm errors. When reporting a fatal error, exit
56 // with status 70 to generate crash diagnostics. For BSD systems this is
57 // defined as an internal software error. Otherwise, exit with status 1.
58 exit(GenCrashDiag ? 70 : 1);
61 #ifdef LINK_POLLY_INTO_TOOLS
63 void initializePollyPasses(llvm::PassRegistry &Registry);
67 int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
68 std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
69 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
71 // Register the support for object-file-wrapped Clang modules.
72 auto PCHOps = Clang->getPCHContainerOperations();
73 PCHOps->registerWriter(llvm::make_unique<ObjectFilePCHContainerWriter>());
74 PCHOps->registerReader(llvm::make_unique<ObjectFilePCHContainerReader>());
76 // Initialize targets first, so that --version shows registered targets.
77 llvm::InitializeAllTargets();
78 llvm::InitializeAllTargetMCs();
79 llvm::InitializeAllAsmPrinters();
80 llvm::InitializeAllAsmParsers();
82 #ifdef LINK_POLLY_INTO_TOOLS
83 llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry();
84 polly::initializePollyPasses(Registry);
87 // Buffer diagnostics from argument parsing so that we can output them using a
88 // well formed diagnostic object.
89 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
90 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
91 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
92 bool Success = CompilerInvocation::CreateFromArgs(
93 Clang->getInvocation(), Argv.begin(), Argv.end(), Diags);
95 // Infer the builtin include path if unspecified.
96 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
97 Clang->getHeaderSearchOpts().ResourceDir.empty())
98 Clang->getHeaderSearchOpts().ResourceDir =
99 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
101 // Create the actual diagnostics engine.
102 Clang->createDiagnostics();
103 if (!Clang->hasDiagnostics())
106 // Set an error handler, so that any LLVM backend diagnostics go through our
108 llvm::install_fatal_error_handler(LLVMErrorHandler,
109 static_cast<void*>(&Clang->getDiagnostics()));
111 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
115 // Execute the frontend actions.
116 Success = ExecuteCompilerInvocation(Clang.get());
118 // If any timers were active but haven't been destroyed yet, print their
119 // results now. This happens in -disable-free mode.
120 llvm::TimerGroup::printAll(llvm::errs());
122 // Our error handler depends on the Diagnostics object, which we're
123 // potentially about to delete. Uninstall the handler now so that any
124 // later errors use the default handling behavior instead.
125 llvm::remove_fatal_error_handler();
127 // When running with -disable-free, don't do any destruction or shutdown.
128 if (Clang->getFrontendOpts().DisableFree) {
129 if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
130 llvm::PrintStatistics();
131 BuryPointer(std::move(Clang));
135 // Managed static deconstruction. Useful for making things like
136 // -time-passes usable.
137 llvm::llvm_shutdown();