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 "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"
36 #include <cstdio>
37 using namespace clang;
38 using namespace llvm::opt;
39 
40 //===----------------------------------------------------------------------===//
41 // Main driver
42 //===----------------------------------------------------------------------===//
43 
44 static void LLVMErrorHandler(void *UserData, const std::string &Message,
45                              bool GenCrashDiag) {
46   DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
47 
48   Diags.Report(diag::err_fe_error_backend) << Message;
49 
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();
53 
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);
58 }
59 
60 int cc1_main(const char **ArgBegin, const char **ArgEnd,
61              const char *Argv0, void *MainAddr) {
62   OwningPtr<CompilerInstance> Clang(new CompilerInstance());
63   IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
64 
65   // Initialize targets first, so that --version shows registered targets.
66   llvm::InitializeAllTargets();
67   llvm::InitializeAllTargetMCs();
68   llvm::InitializeAllAsmPrinters();
69   llvm::InitializeAllAsmParsers();
70 
71   // Buffer diagnostics from argument parsing so that we can output them using a
72   // well formed diagnostic object.
73   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
74   TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
75   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
76   bool Success;
77   Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
78                                                ArgBegin, ArgEnd, Diags);
79 
80   // Infer the builtin include path if unspecified.
81   if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
82       Clang->getHeaderSearchOpts().ResourceDir.empty())
83     Clang->getHeaderSearchOpts().ResourceDir =
84       CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
85 
86   // Create the actual diagnostics engine.
87   Clang->createDiagnostics();
88   if (!Clang->hasDiagnostics())
89     return 1;
90 
91   // Set an error handler, so that any LLVM backend diagnostics go through our
92   // error handler.
93   llvm::install_fatal_error_handler(LLVMErrorHandler,
94                                   static_cast<void*>(&Clang->getDiagnostics()));
95 
96   DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
97   if (!Success)
98     return 1;
99 
100   // Execute the frontend actions.
101   Success = ExecuteCompilerInvocation(Clang.get());
102 
103   // If any timers were active but haven't been destroyed yet, print their
104   // results now.  This happens in -disable-free mode.
105   llvm::TimerGroup::printAll(llvm::errs());
106 
107   // Our error handler depends on the Diagnostics object, which we're
108   // potentially about to delete. Uninstall the handler now so that any
109   // later errors use the default handling behavior instead.
110   llvm::remove_fatal_error_handler();
111 
112   // When running with -disable-free, don't do any destruction or shutdown.
113   if (Clang->getFrontendOpts().DisableFree) {
114     if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
115       llvm::PrintStatistics();
116     BuryPointer(Clang.take());
117     return !Success;
118   }
119 
120   // Managed static deconstruction. Useful for making things like
121   // -time-passes usable.
122   llvm::llvm_shutdown();
123 
124   return !Success;
125 }
126