]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/clang/tools/driver/cc1_main.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / tools / clang / tools / driver / cc1_main.cpp
1 //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
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"
35 #include <cstdio>
36 using namespace clang;
37
38 //===----------------------------------------------------------------------===//
39 // Main driver
40 //===----------------------------------------------------------------------===//
41
42 static void LLVMErrorHandler(void *UserData, const std::string &Message,
43                              bool GenCrashDiag) {
44   DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
45
46   Diags.Report(diag::err_fe_error_backend) << Message;
47
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();
51
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);
56 }
57
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());
62
63   // Initialize targets first, so that --version shows registered targets.
64   llvm::InitializeAllTargets();
65   llvm::InitializeAllTargetMCs();
66   llvm::InitializeAllAsmPrinters();
67   llvm::InitializeAllAsmParsers();
68
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);
74   bool Success;
75   Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
76                                                ArgBegin, ArgEnd, Diags);
77
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);
83
84   // Create the actual diagnostics engine.
85   Clang->createDiagnostics();
86   if (!Clang->hasDiagnostics())
87     return 1;
88
89   // Set an error handler, so that any LLVM backend diagnostics go through our
90   // error handler.
91   llvm::install_fatal_error_handler(LLVMErrorHandler,
92                                   static_cast<void*>(&Clang->getDiagnostics()));
93
94   DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
95   if (!Success)
96     return 1;
97
98   // Execute the frontend actions.
99   Success = ExecuteCompilerInvocation(Clang.get());
100
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());
104
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();
109
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();
114     Clang.take();
115     return !Success;
116   }
117
118   // Managed static deconstruction. Useful for making things like
119   // -time-passes usable.
120   llvm::llvm_shutdown();
121
122   return !Success;
123 }