Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 1 | //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===// |
Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 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 | // |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 16 | #include "llvm/Option/Arg.h" |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 17 | #include "clang/Driver/DriverDiagnostic.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 18 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/CompilerInstance.h" |
| 20 | #include "clang/Frontend/CompilerInvocation.h" |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 23 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 24 | #include "clang/Frontend/Utils.h" |
Peter Collingbourne | 1b7255d | 2010-08-24 00:31:22 +0000 | [diff] [blame] | 25 | #include "clang/FrontendTool/Utils.h" |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 27 | #include "llvm/LinkAllPasses.h" |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 28 | #include "llvm/Option/ArgList.h" |
| 29 | #include "llvm/Option/OptTable.h" |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
| 31 | #include "llvm/Support/ManagedStatic.h" |
Chad Rosier | b0afe83 | 2012-11-12 19:39:37 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Signals.h" |
Evan Cheng | a6b4045 | 2011-08-24 18:09:14 +0000 | [diff] [blame] | 33 | #include "llvm/Support/TargetSelect.h" |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 36 | #include <cstdio> |
| 37 | using namespace clang; |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 38 | using namespace llvm::opt; |
Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 39 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // Main driver |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Chad Rosier | 9083628 | 2013-03-27 18:28:23 +0000 | [diff] [blame] | 44 | static void LLVMErrorHandler(void *UserData, const std::string &Message, |
| 45 | bool GenCrashDiag) { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 46 | DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 47 | |
| 48 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 49 | |
Chad Rosier | b0afe83 | 2012-11-12 19:39:37 +0000 | [diff] [blame] | 50 | // Run the interrupt handlers to make sure any special cleanups get done, in |
| 51 | // particular that we remove files registered with RemoveFileOnSignal. |
| 52 | llvm::sys::RunInterruptHandlers(); |
| 53 | |
Chad Rosier | 5af8de8 | 2012-11-12 21:32:24 +0000 | [diff] [blame] | 54 | // We cannot recover from llvm errors. When reporting a fatal error, exit |
Chad Rosier | 9083628 | 2013-03-27 18:28:23 +0000 | [diff] [blame] | 55 | // with status 70 to generate crash diagnostics. For BSD systems this is |
| 56 | // defined as an internal software error. Otherwise, exit with status 1. |
| 57 | exit(GenCrashDiag ? 70 : 1); |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 60 | #ifdef LINK_POLLY_INTO_TOOLS |
| 61 | namespace polly { |
| 62 | void initializePollyPasses(llvm::PassRegistry &Registry); |
| 63 | } |
| 64 | #endif |
| 65 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 66 | int cc1_main(const char **ArgBegin, const char **ArgEnd, |
| 67 | const char *Argv0, void *MainAddr) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 68 | std::unique_ptr<CompilerInstance> Clang(new CompilerInstance()); |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 69 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
Daniel Dunbar | 42e9f8e4 | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 70 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 71 | // Initialize targets first, so that --version shows registered targets. |
| 72 | llvm::InitializeAllTargets(); |
Evan Cheng | d99d3e1 | 2011-07-22 21:59:11 +0000 | [diff] [blame] | 73 | llvm::InitializeAllTargetMCs(); |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 74 | llvm::InitializeAllAsmPrinters(); |
Chris Lattner | 01ae93f | 2010-04-05 23:33:20 +0000 | [diff] [blame] | 75 | llvm::InitializeAllAsmParsers(); |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 76 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 77 | #ifdef LINK_POLLY_INTO_TOOLS |
| 78 | llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); |
| 79 | polly::initializePollyPasses(Registry); |
| 80 | #endif |
| 81 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 82 | // Buffer diagnostics from argument parsing so that we can output them using a |
| 83 | // well formed diagnostic object. |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 84 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 85 | TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer; |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 86 | DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer); |
Dylan Noblesmith | 8fdb6de | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 87 | bool Success; |
| 88 | Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(), |
| 89 | ArgBegin, ArgEnd, Diags); |
Daniel Dunbar | 1e69fe3 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 90 | |
| 91 | // Infer the builtin include path if unspecified. |
Daniel Dunbar | 42444fb | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 92 | if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && |
| 93 | Clang->getHeaderSearchOpts().ResourceDir.empty()) |
| 94 | Clang->getHeaderSearchOpts().ResourceDir = |
Daniel Dunbar | 8b9adfe | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 95 | CompilerInvocation::GetResourcesPath(Argv0, MainAddr); |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | a6bf47f | 2010-08-12 02:53:07 +0000 | [diff] [blame] | 97 | // Create the actual diagnostics engine. |
Sean Silva | d47afb9 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 98 | Clang->createDiagnostics(); |
Daniel Dunbar | a6bf47f | 2010-08-12 02:53:07 +0000 | [diff] [blame] | 99 | if (!Clang->hasDiagnostics()) |
| 100 | return 1; |
| 101 | |
| 102 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 103 | // error handler. |
| 104 | llvm::install_fatal_error_handler(LLVMErrorHandler, |
| 105 | static_cast<void*>(&Clang->getDiagnostics())); |
| 106 | |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 107 | DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics()); |
Argyrios Kyrtzidis | ada4fa7 | 2012-01-25 20:00:43 +0000 | [diff] [blame] | 108 | if (!Success) |
| 109 | return 1; |
Daniel Dunbar | a6bf47f | 2010-08-12 02:53:07 +0000 | [diff] [blame] | 110 | |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 111 | // Execute the frontend actions. |
Dylan Noblesmith | 8fdb6de | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 112 | Success = ExecuteCompilerInvocation(Clang.get()); |
Daniel Dunbar | 42444fb | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 114 | // If any timers were active but haven't been destroyed yet, print their |
| 115 | // results now. This happens in -disable-free mode. |
| 116 | llvm::TimerGroup::printAll(llvm::errs()); |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 117 | |
Dan Gohman | 726578c | 2010-08-18 21:23:17 +0000 | [diff] [blame] | 118 | // Our error handler depends on the Diagnostics object, which we're |
| 119 | // potentially about to delete. Uninstall the handler now so that any |
| 120 | // later errors use the default handling behavior instead. |
| 121 | llvm::remove_fatal_error_handler(); |
| 122 | |
Daniel Dunbar | 42444fb | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 123 | // When running with -disable-free, don't do any destruction or shutdown. |
| 124 | if (Clang->getFrontendOpts().DisableFree) { |
Benjamin Kramer | 77d2c5f | 2011-02-27 18:07:41 +0000 | [diff] [blame] | 125 | if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats) |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 126 | llvm::PrintStatistics(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 127 | BuryPointer(Clang.release()); |
Daniel Dunbar | 42444fb | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 128 | return !Success; |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 131 | // Managed static deconstruction. Useful for making things like |
| 132 | // -time-passes usable. |
| 133 | llvm::llvm_shutdown(); |
| 134 | |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 135 | return !Success; |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 136 | } |