cc1_main.cpp revision 243830
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/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"
34#include <cstdio>
35using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Main driver
39//===----------------------------------------------------------------------===//
40
41static void LLVMErrorHandler(void *UserData, const std::string &Message) {
42  DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
43
44  Diags.Report(diag::err_fe_error_backend) << Message;
45
46  // We cannot recover from llvm errors.
47  exit(1);
48}
49
50int 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());
54
55  // Initialize targets first, so that --version shows registered targets.
56  llvm::InitializeAllTargets();
57  llvm::InitializeAllTargetMCs();
58  llvm::InitializeAllAsmPrinters();
59  llvm::InitializeAllAsmParsers();
60
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);
66  bool Success;
67  Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
68                                               ArgBegin, ArgEnd, Diags);
69
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);
75
76  // Create the actual diagnostics engine.
77  Clang->createDiagnostics(ArgEnd - ArgBegin, const_cast<char**>(ArgBegin));
78  if (!Clang->hasDiagnostics())
79    return 1;
80
81  // Set an error handler, so that any LLVM backend diagnostics go through our
82  // error handler.
83  llvm::install_fatal_error_handler(LLVMErrorHandler,
84                                  static_cast<void*>(&Clang->getDiagnostics()));
85
86  DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
87  if (!Success)
88    return 1;
89
90  // Execute the frontend actions.
91  Success = ExecuteCompilerInvocation(Clang.get());
92
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());
96
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();
101
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();
106    Clang.take();
107    return !Success;
108  }
109
110  // Managed static deconstruction. Useful for making things like
111  // -time-passes usable.
112  llvm::llvm_shutdown();
113
114  return !Success;
115}
116