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/Options.h"
19 #include "clang/Driver/DriverDiagnostic.h"
20 #include "clang/Driver/OptTable.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/Support/ErrorHandling.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/TargetSelect.h"
31 #include "llvm/Support/Timer.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/LinkAllPasses.h"
35 using namespace clang;
37 //===----------------------------------------------------------------------===//
39 //===----------------------------------------------------------------------===//
41 static void LLVMErrorHandler(void *UserData, const std::string &Message) {
42 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
44 Diags.Report(diag::err_fe_error_backend) << Message;
46 // We cannot recover from llvm errors.
50 int cc1_main(const char **ArgBegin, const char **ArgEnd,
51 const char *Argv0, void *MainAddr) {
52 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
53 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
55 // Initialize targets first, so that --version shows registered targets.
56 llvm::InitializeAllTargets();
57 llvm::InitializeAllTargetMCs();
58 llvm::InitializeAllAsmPrinters();
59 llvm::InitializeAllAsmParsers();
61 // Buffer diagnostics from argument parsing so that we can output them using a
62 // well formed diagnostic object.
63 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
64 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
65 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
67 Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
68 ArgBegin, ArgEnd, Diags);
70 // Infer the builtin include path if unspecified.
71 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
72 Clang->getHeaderSearchOpts().ResourceDir.empty())
73 Clang->getHeaderSearchOpts().ResourceDir =
74 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
76 // Create the actual diagnostics engine.
77 Clang->createDiagnostics(ArgEnd - ArgBegin, const_cast<char**>(ArgBegin));
78 if (!Clang->hasDiagnostics())
81 // Set an error handler, so that any LLVM backend diagnostics go through our
83 llvm::install_fatal_error_handler(LLVMErrorHandler,
84 static_cast<void*>(&Clang->getDiagnostics()));
86 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
90 // Execute the frontend actions.
91 Success = ExecuteCompilerInvocation(Clang.get());
93 // If any timers were active but haven't been destroyed yet, print their
94 // results now. This happens in -disable-free mode.
95 llvm::TimerGroup::printAll(llvm::errs());
97 // Our error handler depends on the Diagnostics object, which we're
98 // potentially about to delete. Uninstall the handler now so that any
99 // later errors use the default handling behavior instead.
100 llvm::remove_fatal_error_handler();
102 // When running with -disable-free, don't do any destruction or shutdown.
103 if (Clang->getFrontendOpts().DisableFree) {
104 if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
105 llvm::PrintStatistics();
110 // Managed static deconstruction. Useful for making things like
111 // -time-passes usable.
112 llvm::llvm_shutdown();