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