blob: 674f2adbbc4ee838c6fd147940b67a5fca16072f [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
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000016#include "clang/Driver/Arg.h"
17#include "clang/Driver/ArgList.h"
James Molloya3c85b82012-05-01 14:57:16 +000018#include "clang/Driver/Options.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000019#include "clang/Driver/DriverDiagnostic.h"
20#include "clang/Driver/OptTable.h"
21#include "clang/Frontend/CompilerInstance.h"
22#include "clang/Frontend/CompilerInvocation.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000023#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000024#include "clang/Frontend/TextDiagnosticBuffer.h"
25#include "clang/Frontend/TextDiagnosticPrinter.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"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000028#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/ManagedStatic.h"
Chad Rosier7ea73972012-11-12 19:39:37 +000030#include "llvm/Support/Signals.h"
Evan Cheng494eb062011-08-24 18:09:14 +000031#include "llvm/Support/TargetSelect.h"
Chris Lattner09f8cc82010-03-30 05:39:52 +000032#include "llvm/Support/Timer.h"
Daniel Dunbar51cd8f02009-11-19 07:37:51 +000033#include "llvm/Support/raw_ostream.h"
Tobias Grosser1a5307c2011-11-01 01:34:59 +000034#include "llvm/LinkAllPasses.h"
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000035#include <cstdio>
36using namespace clang;
Daniel Dunbar51cd8f02009-11-19 07:37:51 +000037
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000038//===----------------------------------------------------------------------===//
39// Main driver
40//===----------------------------------------------------------------------===//
41
Daniel Dunbar2fcaa542010-05-20 17:49:16 +000042static void LLVMErrorHandler(void *UserData, const std::string &Message) {
David Blaikie9c902b52011-09-25 23:23:43 +000043 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000044
45 Diags.Report(diag::err_fe_error_backend) << Message;
46
Chad Rosier7ea73972012-11-12 19:39:37 +000047 // Run the interrupt handlers to make sure any special cleanups get done, in
48 // particular that we remove files registered with RemoveFileOnSignal.
49 llvm::sys::RunInterruptHandlers();
50
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000051 // We cannot recover from llvm errors.
52 exit(1);
53}
54
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000055int cc1_main(const char **ArgBegin, const char **ArgEnd,
56 const char *Argv0, void *MainAddr) {
Dylan Noblesmith1cd10692012-02-13 12:32:21 +000057 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000058 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
Daniel Dunbare922d9b2010-02-16 01:54:47 +000059
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000060 // Initialize targets first, so that --version shows registered targets.
61 llvm::InitializeAllTargets();
Evan Chengc391a582011-07-22 21:59:11 +000062 llvm::InitializeAllTargetMCs();
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000063 llvm::InitializeAllAsmPrinters();
Chris Lattner61955ab2010-04-05 23:33:20 +000064 llvm::InitializeAllAsmParsers();
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000065
66 // Buffer diagnostics from argument parsing so that we can output them using a
67 // well formed diagnostic object.
Douglas Gregor811db4e2012-10-23 22:26:28 +000068 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Douglas Gregor2dd19f12010-08-18 22:29:43 +000069 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
Douglas Gregor811db4e2012-10-23 22:26:28 +000070 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +000071 bool Success;
72 Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
73 ArgBegin, ArgEnd, Diags);
Daniel Dunbard6136772009-12-13 03:45:58 +000074
75 // Infer the builtin include path if unspecified.
Daniel Dunbar30b24e92010-03-23 05:09:16 +000076 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
77 Clang->getHeaderSearchOpts().ResourceDir.empty())
78 Clang->getHeaderSearchOpts().ResourceDir =
Daniel Dunbara5a166d2009-12-15 00:06:45 +000079 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
Daniel Dunbarf72bdf72009-12-11 22:20:12 +000080
Daniel Dunbar1cdf04c2010-08-12 02:53:07 +000081 // Create the actual diagnostics engine.
82 Clang->createDiagnostics(ArgEnd - ArgBegin, const_cast<char**>(ArgBegin));
83 if (!Clang->hasDiagnostics())
84 return 1;
85
86 // Set an error handler, so that any LLVM backend diagnostics go through our
87 // error handler.
88 llvm::install_fatal_error_handler(LLVMErrorHandler,
89 static_cast<void*>(&Clang->getDiagnostics()));
90
Douglas Gregor2dd19f12010-08-18 22:29:43 +000091 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
Argyrios Kyrtzidisbe6d89d2012-01-25 20:00:43 +000092 if (!Success)
93 return 1;
Daniel Dunbar1cdf04c2010-08-12 02:53:07 +000094
Daniel Dunbare9e91b02010-08-12 02:53:12 +000095 // Execute the frontend actions.
Dylan Noblesmithe99b27f2011-12-23 03:05:38 +000096 Success = ExecuteCompilerInvocation(Clang.get());
Daniel Dunbar30b24e92010-03-23 05:09:16 +000097
Chris Lattner09f8cc82010-03-30 05:39:52 +000098 // If any timers were active but haven't been destroyed yet, print their
99 // results now. This happens in -disable-free mode.
100 llvm::TimerGroup::printAll(llvm::errs());
Daniel Dunbar2be96742010-08-02 15:31:28 +0000101
Dan Gohmanb37af7d2010-08-18 21:23:17 +0000102 // Our error handler depends on the Diagnostics object, which we're
103 // potentially about to delete. Uninstall the handler now so that any
104 // later errors use the default handling behavior instead.
105 llvm::remove_fatal_error_handler();
106
Daniel Dunbar30b24e92010-03-23 05:09:16 +0000107 // When running with -disable-free, don't do any destruction or shutdown.
108 if (Clang->getFrontendOpts().DisableFree) {
Benjamin Kramer6e152152011-02-27 18:07:41 +0000109 if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
Douglas Gregor171b7802010-03-30 17:33:59 +0000110 llvm::PrintStatistics();
Daniel Dunbar30b24e92010-03-23 05:09:16 +0000111 Clang.take();
112 return !Success;
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000113 }
114
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000115 // Managed static deconstruction. Useful for making things like
116 // -time-passes usable.
117 llvm::llvm_shutdown();
118
Daniel Dunbar4f2bc552010-01-13 00:48:06 +0000119 return !Success;
Daniel Dunbarf72bdf72009-12-11 22:20:12 +0000120}