blob: 5b3b5ad6500bca82c103ab5c201385a0cc1903f7 [file] [log] [blame]
Daniel Dunbar41b5b172010-05-20 17:49:16 +00001//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
Daniel Dunbar217acbf2009-11-19 07:37:51 +00002//
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//
Daniel Dunbar21085772009-12-11 22:20:12 +000010// 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.
Daniel Dunbar217acbf2009-11-19 07:37:51 +000013//
14//===----------------------------------------------------------------------===//
15
Reid Klecknerb1e25a12013-06-14 17:17:23 +000016#include "llvm/Option/Arg.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000017#include "clang/Driver/DriverDiagnostic.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000018#include "clang/Driver/Options.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000019#include "clang/Frontend/CompilerInstance.h"
20#include "clang/Frontend/CompilerInvocation.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000021#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000022#include "clang/Frontend/TextDiagnosticBuffer.h"
23#include "clang/Frontend/TextDiagnosticPrinter.h"
Peter Collingbourne1b7255d2010-08-24 00:31:22 +000024#include "clang/FrontendTool/Utils.h"
Douglas Gregor95dd5582010-03-30 17:33:59 +000025#include "llvm/ADT/Statistic.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000026#include "llvm/LinkAllPasses.h"
Reid Klecknerb1e25a12013-06-14 17:17:23 +000027#include "llvm/Option/ArgList.h"
28#include "llvm/Option/OptTable.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000029#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/ManagedStatic.h"
Chad Rosierb0afe832012-11-12 19:39:37 +000031#include "llvm/Support/Signals.h"
Evan Chenga6b40452011-08-24 18:09:14 +000032#include "llvm/Support/TargetSelect.h"
Chris Lattner30bc7e82010-03-30 05:39:52 +000033#include "llvm/Support/Timer.h"
Daniel Dunbar217acbf2009-11-19 07:37:51 +000034#include "llvm/Support/raw_ostream.h"
Daniel Dunbar21085772009-12-11 22:20:12 +000035#include <cstdio>
36using namespace clang;
Reid Klecknerb1e25a12013-06-14 17:17:23 +000037using namespace llvm::opt;
Daniel Dunbar217acbf2009-11-19 07:37:51 +000038
Daniel Dunbar21085772009-12-11 22:20:12 +000039//===----------------------------------------------------------------------===//
40// Main driver
41//===----------------------------------------------------------------------===//
42
Chad Rosier90836282013-03-27 18:28:23 +000043static void LLVMErrorHandler(void *UserData, const std::string &Message,
44 bool GenCrashDiag) {
David Blaikied6471f72011-09-25 23:23:43 +000045 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbar21085772009-12-11 22:20:12 +000046
47 Diags.Report(diag::err_fe_error_backend) << Message;
48
Chad Rosierb0afe832012-11-12 19:39:37 +000049 // 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
Chad Rosier5af8de82012-11-12 21:32:24 +000053 // We cannot recover from llvm errors. When reporting a fatal error, exit
Chad Rosier90836282013-03-27 18:28:23 +000054 // 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);
Daniel Dunbar21085772009-12-11 22:20:12 +000057}
58
Daniel Dunbar21085772009-12-11 22:20:12 +000059int cc1_main(const char **ArgBegin, const char **ArgEnd,
60 const char *Argv0, void *MainAddr) {
Dylan Noblesmith1e4c01b2012-02-13 12:32:21 +000061 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000062 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Daniel Dunbar42e9f8e42010-02-16 01:54:47 +000063
Daniel Dunbar21085772009-12-11 22:20:12 +000064 // Initialize targets first, so that --version shows registered targets.
65 llvm::InitializeAllTargets();
Evan Chengd99d3e12011-07-22 21:59:11 +000066 llvm::InitializeAllTargetMCs();
Daniel Dunbar21085772009-12-11 22:20:12 +000067 llvm::InitializeAllAsmPrinters();
Chris Lattner01ae93f2010-04-05 23:33:20 +000068 llvm::InitializeAllAsmParsers();
Daniel Dunbar21085772009-12-11 22:20:12 +000069
70 // Buffer diagnostics from argument parsing so that we can output them using a
71 // well formed diagnostic object.
Douglas Gregor02c23eb2012-10-23 22:26:28 +000072 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregorbdbb0042010-08-18 22:29:43 +000073 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
Douglas Gregor02c23eb2012-10-23 22:26:28 +000074 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +000075 bool Success;
76 Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
77 ArgBegin, ArgEnd, Diags);
Daniel Dunbar1e69fe32009-12-13 03:45:58 +000078
79 // Infer the builtin include path if unspecified.
Daniel Dunbar42444fb2010-03-23 05:09:16 +000080 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
81 Clang->getHeaderSearchOpts().ResourceDir.empty())
82 Clang->getHeaderSearchOpts().ResourceDir =
Daniel Dunbar8b9adfe2009-12-15 00:06:45 +000083 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
Daniel Dunbar21085772009-12-11 22:20:12 +000084
Daniel Dunbara6bf47f2010-08-12 02:53:07 +000085 // Create the actual diagnostics engine.
Sean Silvad47afb92013-01-20 01:58:28 +000086 Clang->createDiagnostics();
Daniel Dunbara6bf47f2010-08-12 02:53:07 +000087 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
Douglas Gregorbdbb0042010-08-18 22:29:43 +000095 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
Argyrios Kyrtzidisada4fa72012-01-25 20:00:43 +000096 if (!Success)
97 return 1;
Daniel Dunbara6bf47f2010-08-12 02:53:07 +000098
Daniel Dunbar8eb2b012010-08-12 02:53:12 +000099 // Execute the frontend actions.
Dylan Noblesmith8fdb6de2011-12-23 03:05:38 +0000100 Success = ExecuteCompilerInvocation(Clang.get());
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000101
Chris Lattner30bc7e82010-03-30 05:39:52 +0000102 // 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());
Daniel Dunbarf56a4882010-08-02 15:31:28 +0000105
Dan Gohman726578c2010-08-18 21:23:17 +0000106 // 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
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000111 // When running with -disable-free, don't do any destruction or shutdown.
112 if (Clang->getFrontendOpts().DisableFree) {
Benjamin Kramer77d2c5f2011-02-27 18:07:41 +0000113 if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
Douglas Gregor95dd5582010-03-30 17:33:59 +0000114 llvm::PrintStatistics();
Daniel Dunbar42444fb2010-03-23 05:09:16 +0000115 Clang.take();
116 return !Success;
Daniel Dunbar21085772009-12-11 22:20:12 +0000117 }
118
Daniel Dunbar21085772009-12-11 22:20:12 +0000119 // Managed static deconstruction. Useful for making things like
120 // -time-passes usable.
121 llvm::llvm_shutdown();
122
Daniel Dunbar0397af22010-01-13 00:48:06 +0000123 return !Success;
Daniel Dunbar21085772009-12-11 22:20:12 +0000124}