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 "clang/Driver/Arg.h"
17 #include "clang/Driver/ArgList.h"
18 #include "clang/Driver/DriverDiagnostic.h"
19 #include "clang/Driver/OptTable.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/FrontendTool/Utils.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/LinkAllPasses.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/ManagedStatic.h"
31 #include "llvm/Support/Signals.h"
32 #include "llvm/Support/TargetSelect.h"
33 #include "llvm/Support/Timer.h"
34 #include "llvm/Support/raw_ostream.h"
36 using namespace clang;
38 //===----------------------------------------------------------------------===//
40 //===----------------------------------------------------------------------===//
42 static void LLVMErrorHandler(void *UserData, const std::string &Message,
44 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
46 Diags.Report(diag::err_fe_error_backend) << Message;
48 // Run the interrupt handlers to make sure any special cleanups get done, in
49 // particular that we remove files registered with RemoveFileOnSignal.
50 llvm::sys::RunInterruptHandlers();
52 // We cannot recover from llvm errors. When reporting a fatal error, exit
53 // with status 70 to generate crash diagnostics. For BSD systems this is
54 // defined as an internal software error. Otherwise, exit with status 1.
55 exit(GenCrashDiag ? 70 : 1);
58 int cc1_main(const char **ArgBegin, const char **ArgEnd,
59 const char *Argv0, void *MainAddr) {
60 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
61 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
63 // Initialize targets first, so that --version shows registered targets.
64 llvm::InitializeAllTargets();
65 llvm::InitializeAllTargetMCs();
66 llvm::InitializeAllAsmPrinters();
67 llvm::InitializeAllAsmParsers();
69 // Buffer diagnostics from argument parsing so that we can output them using a
70 // well formed diagnostic object.
71 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
72 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
73 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
75 Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
76 ArgBegin, ArgEnd, Diags);
78 // Infer the builtin include path if unspecified.
79 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
80 Clang->getHeaderSearchOpts().ResourceDir.empty())
81 Clang->getHeaderSearchOpts().ResourceDir =
82 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
84 // Create the actual diagnostics engine.
85 Clang->createDiagnostics();
86 if (!Clang->hasDiagnostics())
89 // Set an error handler, so that any LLVM backend diagnostics go through our
91 llvm::install_fatal_error_handler(LLVMErrorHandler,
92 static_cast<void*>(&Clang->getDiagnostics()));
94 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
98 // Execute the frontend actions.
99 Success = ExecuteCompilerInvocation(Clang.get());
101 // If any timers were active but haven't been destroyed yet, print their
102 // results now. This happens in -disable-free mode.
103 llvm::TimerGroup::printAll(llvm::errs());
105 // Our error handler depends on the Diagnostics object, which we're
106 // potentially about to delete. Uninstall the handler now so that any
107 // later errors use the default handling behavior instead.
108 llvm::remove_fatal_error_handler();
110 // When running with -disable-free, don't do any destruction or shutdown.
111 if (Clang->getFrontendOpts().DisableFree) {
112 if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
113 llvm::PrintStatistics();
118 // Managed static deconstruction. Useful for making things like
119 // -time-passes usable.
120 llvm::llvm_shutdown();