blob: d78a31e67a2f94f2a45cbf84814b4f0d3826d141 [file] [log] [blame]
Daniel Dunbar2fcaa542010-05-20 17:49:16 +00001//===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
Daniel Dunbar51cd8f02009-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 Dunbarf72bdf72009-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 Dunbar51cd8f02009-11-19 07:37:51 +000013//
14//===----------------------------------------------------------------------===//
15
Reid Kleckner898229a2013-06-14 17:17:23 +000016#include "llvm/Option/Arg.h"
Adrian Prantlbc068582015-07-08 01:00:30 +000017#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000018#include "clang/Driver/DriverDiagnostic.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000019#include "clang/Driver/Options.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000020#include "clang/Frontend/CompilerInstance.h"
21#include "clang/Frontend/CompilerInvocation.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000022#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000023#include "clang/Frontend/TextDiagnosticBuffer.h"
24#include "clang/Frontend/TextDiagnosticPrinter.h"
Kostya Serebryanyce2c7262013-12-27 08:11:08 +000025#include "clang/Frontend/Utils.h"
Peter Collingbourne85dd0bd2010-08-24 00:31:22 +000026#include "clang/FrontendTool/Utils.h"
Douglas Gregor171b7802010-03-30 17:33:59 +000027#include "llvm/ADT/Statistic.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000028#include "llvm/LinkAllPasses.h"
Reid Kleckner898229a2013-06-14 17:17:23 +000029#include "llvm/Option/ArgList.h"
30#include "llvm/Option/OptTable.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000031#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/ManagedStatic.h"
Chad Rosier7ea73972012-11-12 19:39:37 +000033#include "llvm/Support/Signals.h"
Evan Cheng494eb062011-08-24 18:09:14 +000034#include "llvm/Support/TargetSelect.h"
Chris Lattner09f8cc82010-03-30 05:39:52 +000035#include "llvm/Support/Timer.h"
Daniel Dunbar51cd8f02009-11-19 07:37:51 +000036#include "llvm/Support/raw_ostream.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000037#include <cstdio>
38using namespace clang;
Reid Kleckner898229a2013-06-14 17:17:23 +000039using namespace llvm::opt;
Daniel Dunbar51cd8f02009-11-19 07:37:51 +000040
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000041//===----------------------------------------------------------------------===//
42// Main driver
43//===----------------------------------------------------------------------===//
44
Chad Rosier05c71aa2013-03-27 18:28:23 +000045static void LLVMErrorHandler(void *UserData, const std::string &Message,
46 bool GenCrashDiag) {
David Blaikie9c902b52011-09-25 23:23:43 +000047 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000048
49 Diags.Report(diag::err_fe_error_backend) << Message;
50
Chad Rosier7ea73972012-11-12 19:39:37 +000051 // Run the interrupt handlers to make sure any special cleanups get done, in
52 // particular that we remove files registered with RemoveFileOnSignal.
53 llvm::sys::RunInterruptHandlers();
54
Chad Rosierad6e96d2012-11-12 21:32:24 +000055 // We cannot recover from llvm errors. When reporting a fatal error, exit
Chad Rosier05c71aa2013-03-27 18:28:23 +000056 // with status 70 to generate crash diagnostics. For BSD systems this is
57 // defined as an internal software error. Otherwise, exit with status 1.
58 exit(GenCrashDiag ? 70 : 1);
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000059}
60
Sebastian Pop17fac042014-03-14 04:04:27 +000061#ifdef LINK_POLLY_INTO_TOOLS
62namespace polly {
63void initializePollyPasses(llvm::PassRegistry &Registry);
64}
65#endif
66
Sean Silva070cd2d2014-08-15 21:38:36 +000067int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
Adrian Prantlfb2398d2015-07-17 01:19:54 +000068 std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000069 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Daniel Dunbare922d9b2010-02-16 01:54:47 +000070
Adrian Prantlfb2398d2015-07-17 01:19:54 +000071 // Register the support for object-file-wrapped Clang modules.
72 auto PCHOps = Clang->getPCHContainerOperations();
73 PCHOps->registerWriter(llvm::make_unique<ObjectFilePCHContainerWriter>());
74 PCHOps->registerReader(llvm::make_unique<ObjectFilePCHContainerReader>());
75
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000076 // Initialize targets first, so that --version shows registered targets.
77 llvm::InitializeAllTargets();
Evan Chengc391a582011-07-22 21:59:11 +000078 llvm::InitializeAllTargetMCs();
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000079 llvm::InitializeAllAsmPrinters();
Chris Lattner61955ab2010-04-05 23:33:20 +000080 llvm::InitializeAllAsmParsers();
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000081
Sebastian Pop17fac042014-03-14 04:04:27 +000082#ifdef LINK_POLLY_INTO_TOOLS
83 llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry();
84 polly::initializePollyPasses(Registry);
85#endif
86
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000087 // Buffer diagnostics from argument parsing so that we can output them using a
88 // well formed diagnostic object.
Douglas Gregor811db4e2012-10-23 22:26:28 +000089 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregor2dd19f12010-08-18 22:29:43 +000090 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
Douglas Gregor811db4e2012-10-23 22:26:28 +000091 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
Sean Silva070cd2d2014-08-15 21:38:36 +000092 bool Success = CompilerInvocation::CreateFromArgs(
93 Clang->getInvocation(), Argv.begin(), Argv.end(), Diags);
Daniel Dunbard6136772009-12-13 03:45:58 +000094
95 // Infer the builtin include path if unspecified.
Daniel Dunbar30b24e92010-03-23 05:09:16 +000096 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
97 Clang->getHeaderSearchOpts().ResourceDir.empty())
98 Clang->getHeaderSearchOpts().ResourceDir =
Daniel Dunbara5a166d2009-12-15 00:06:45 +000099 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000100
Daniel Dunbar1cdf04c2010-08-12 02:53:07 +0000101 // Create the actual diagnostics engine.
Sean Silvaf1b49e22013-01-20 01:58:28 +0000102 Clang->createDiagnostics();
Daniel Dunbar1cdf04c2010-08-12 02:53:07 +0000103 if (!Clang->hasDiagnostics())
104 return 1;
105
106 // Set an error handler, so that any LLVM backend diagnostics go through our
107 // error handler.
108 llvm::install_fatal_error_handler(LLVMErrorHandler,
109 static_cast<void*>(&Clang->getDiagnostics()));
110
Douglas Gregor2dd19f12010-08-18 22:29:43 +0000111 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
Argyrios Kyrtzidisbe6d89d2012-01-25 20:00:43 +0000112 if (!Success)
113 return 1;
Daniel Dunbar1cdf04c2010-08-12 02:53:07 +0000114
Daniel Dunbare9e91b02010-08-12 02:53:12 +0000115 // Execute the frontend actions.
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +0000116 Success = ExecuteCompilerInvocation(Clang.get());
Daniel Dunbar30b24e92010-03-23 05:09:16 +0000117
Chris Lattner09f8cc82010-03-30 05:39:52 +0000118 // If any timers were active but haven't been destroyed yet, print their
119 // results now. This happens in -disable-free mode.
120 llvm::TimerGroup::printAll(llvm::errs());
Daniel Dunbar2be96742010-08-02 15:31:28 +0000121
Dan Gohmanb37af7d2010-08-18 21:23:17 +0000122 // Our error handler depends on the Diagnostics object, which we're
123 // potentially about to delete. Uninstall the handler now so that any
124 // later errors use the default handling behavior instead.
125 llvm::remove_fatal_error_handler();
126
Daniel Dunbar30b24e92010-03-23 05:09:16 +0000127 // When running with -disable-free, don't do any destruction or shutdown.
128 if (Clang->getFrontendOpts().DisableFree) {
David Blaikie3c13a7f2014-08-29 17:02:26 +0000129 BuryPointer(std::move(Clang));
Daniel Dunbar30b24e92010-03-23 05:09:16 +0000130 return !Success;
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000131 }
132
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000133 return !Success;
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000134}