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 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 16 | #include "clang/Driver/Arg.h" |
| 17 | #include "clang/Driver/ArgList.h" |
| 18 | #include "clang/Driver/CC1Options.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" |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 25 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Peter Collingbourne | 1b7255d | 2010-08-24 00:31:22 +0000 | [diff] [blame] | 26 | #include "clang/FrontendTool/Utils.h" |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Statistic.h" |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include "llvm/Support/ManagedStatic.h" |
Evan Cheng | a6b4045 | 2011-08-24 18:09:14 +0000 | [diff] [blame] | 30 | #include "llvm/Support/TargetSelect.h" |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Tobias Grosser | f358c8a | 2011-11-01 01:34:59 +0000 | [diff] [blame] | 33 | #include "llvm/LinkAllPasses.h" |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 34 | #include <cstdio> |
| 35 | using namespace clang; |
Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 36 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | // Main driver |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Daniel Dunbar | 41b5b17 | 2010-05-20 17:49:16 +0000 | [diff] [blame] | 41 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 42 | DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData); |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 43 | |
| 44 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 45 | |
| 46 | // We cannot recover from llvm errors. |
| 47 | exit(1); |
| 48 | } |
| 49 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 50 | // FIXME: Define the need for this testing away. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 51 | static int cc1_test(DiagnosticsEngine &Diags, |
Daniel Dunbar | 1e69fe3 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 52 | const char **ArgBegin, const char **ArgEnd) { |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 53 | using namespace clang::driver; |
| 54 | |
Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 55 | llvm::errs() << "cc1 argv:"; |
| 56 | for (const char **i = ArgBegin; i != ArgEnd; ++i) |
| 57 | llvm::errs() << " \"" << *i << '"'; |
| 58 | llvm::errs() << "\n"; |
| 59 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 60 | // Parse the arguments. |
| 61 | OptTable *Opts = createCC1OptTable(); |
| 62 | unsigned MissingArgIndex, MissingArgCount; |
| 63 | InputArgList *Args = Opts->ParseArgs(ArgBegin, ArgEnd, |
| 64 | MissingArgIndex, MissingArgCount); |
| 65 | |
| 66 | // Check for missing argument error. |
| 67 | if (MissingArgCount) |
| 68 | Diags.Report(clang::diag::err_drv_missing_argument) |
| 69 | << Args->getArgString(MissingArgIndex) << MissingArgCount; |
| 70 | |
| 71 | // Dump the parsed arguments. |
| 72 | llvm::errs() << "cc1 parsed options:\n"; |
| 73 | for (ArgList::const_iterator it = Args->begin(), ie = Args->end(); |
| 74 | it != ie; ++it) |
| 75 | (*it)->dump(); |
| 76 | |
| 77 | // Create a compiler invocation. |
| 78 | llvm::errs() << "cc1 creating invocation.\n"; |
| 79 | CompilerInvocation Invocation; |
Dylan Noblesmith | 8fdb6de | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 80 | if (!CompilerInvocation::CreateFromArgs(Invocation, ArgBegin, ArgEnd, Diags)) |
| 81 | return 1; |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 82 | |
| 83 | // Convert the invocation back to argument strings. |
| 84 | std::vector<std::string> InvocationArgs; |
| 85 | Invocation.toArgs(InvocationArgs); |
| 86 | |
| 87 | // Dump the converted arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 88 | SmallVector<const char*, 32> Invocation2Args; |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 89 | llvm::errs() << "invocation argv :"; |
| 90 | for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) { |
| 91 | Invocation2Args.push_back(InvocationArgs[i].c_str()); |
| 92 | llvm::errs() << " \"" << InvocationArgs[i] << '"'; |
| 93 | } |
| 94 | llvm::errs() << "\n"; |
| 95 | |
| 96 | // Convert those arguments to another invocation, and check that we got the |
| 97 | // same thing. |
| 98 | CompilerInvocation Invocation2; |
Dylan Noblesmith | 8fdb6de | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 99 | if (!CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args.begin(), |
| 100 | Invocation2Args.end(), Diags)) |
| 101 | return 1; |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 102 | |
| 103 | // FIXME: Implement CompilerInvocation comparison. |
| 104 | if (true) { |
| 105 | //llvm::errs() << "warning: Invocations differ!\n"; |
| 106 | |
| 107 | std::vector<std::string> Invocation2Args; |
| 108 | Invocation2.toArgs(Invocation2Args); |
| 109 | llvm::errs() << "invocation2 argv:"; |
| 110 | for (unsigned i = 0, e = Invocation2Args.size(); i != e; ++i) |
| 111 | llvm::errs() << " \"" << Invocation2Args[i] << '"'; |
| 112 | llvm::errs() << "\n"; |
| 113 | } |
| 114 | |
Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame] | 115 | return 0; |
| 116 | } |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 117 | |
| 118 | int cc1_main(const char **ArgBegin, const char **ArgEnd, |
| 119 | const char *Argv0, void *MainAddr) { |
Daniel Dunbar | 42444fb | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 120 | llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance()); |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 121 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
Daniel Dunbar | 42e9f8e4 | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 122 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 123 | // Run clang -cc1 test. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 124 | if (ArgBegin != ArgEnd && StringRef(ArgBegin[0]) == "-cc1test") { |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 125 | DiagnosticsEngine Diags(DiagID, new TextDiagnosticPrinter(llvm::errs(), |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 126 | DiagnosticOptions())); |
Daniel Dunbar | 1e69fe3 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 127 | return cc1_test(Diags, ArgBegin + 1, ArgEnd); |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Initialize targets first, so that --version shows registered targets. |
| 131 | llvm::InitializeAllTargets(); |
Evan Cheng | d99d3e1 | 2011-07-22 21:59:11 +0000 | [diff] [blame] | 132 | llvm::InitializeAllTargetMCs(); |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 133 | llvm::InitializeAllAsmPrinters(); |
Chris Lattner | 01ae93f | 2010-04-05 23:33:20 +0000 | [diff] [blame] | 134 | llvm::InitializeAllAsmParsers(); |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 135 | |
| 136 | // Buffer diagnostics from argument parsing so that we can output them using a |
| 137 | // well formed diagnostic object. |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 138 | TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer; |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 139 | DiagnosticsEngine Diags(DiagID, DiagsBuffer); |
Dylan Noblesmith | 8fdb6de | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 140 | bool Success; |
| 141 | Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(), |
| 142 | ArgBegin, ArgEnd, Diags); |
Daniel Dunbar | 1e69fe3 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 143 | |
| 144 | // Infer the builtin include path if unspecified. |
Daniel Dunbar | 42444fb | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 145 | if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && |
| 146 | Clang->getHeaderSearchOpts().ResourceDir.empty()) |
| 147 | Clang->getHeaderSearchOpts().ResourceDir = |
Daniel Dunbar | 8b9adfe | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 148 | CompilerInvocation::GetResourcesPath(Argv0, MainAddr); |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 149 | |
Daniel Dunbar | a6bf47f | 2010-08-12 02:53:07 +0000 | [diff] [blame] | 150 | // Create the actual diagnostics engine. |
| 151 | Clang->createDiagnostics(ArgEnd - ArgBegin, const_cast<char**>(ArgBegin)); |
| 152 | if (!Clang->hasDiagnostics()) |
| 153 | return 1; |
| 154 | |
| 155 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 156 | // error handler. |
| 157 | llvm::install_fatal_error_handler(LLVMErrorHandler, |
| 158 | static_cast<void*>(&Clang->getDiagnostics())); |
| 159 | |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 160 | DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics()); |
Argyrios Kyrtzidis | ada4fa7 | 2012-01-25 20:00:43 +0000 | [diff] [blame] | 161 | if (!Success) |
| 162 | return 1; |
Daniel Dunbar | a6bf47f | 2010-08-12 02:53:07 +0000 | [diff] [blame] | 163 | |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 164 | // Execute the frontend actions. |
Dylan Noblesmith | 8fdb6de | 2011-12-23 03:05:38 +0000 | [diff] [blame] | 165 | Success = ExecuteCompilerInvocation(Clang.get()); |
Daniel Dunbar | 42444fb | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 30bc7e8 | 2010-03-30 05:39:52 +0000 | [diff] [blame] | 167 | // If any timers were active but haven't been destroyed yet, print their |
| 168 | // results now. This happens in -disable-free mode. |
| 169 | llvm::TimerGroup::printAll(llvm::errs()); |
Daniel Dunbar | f56a488 | 2010-08-02 15:31:28 +0000 | [diff] [blame] | 170 | |
Dan Gohman | 726578c | 2010-08-18 21:23:17 +0000 | [diff] [blame] | 171 | // Our error handler depends on the Diagnostics object, which we're |
| 172 | // potentially about to delete. Uninstall the handler now so that any |
| 173 | // later errors use the default handling behavior instead. |
| 174 | llvm::remove_fatal_error_handler(); |
| 175 | |
Daniel Dunbar | 42444fb | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 176 | // When running with -disable-free, don't do any destruction or shutdown. |
| 177 | if (Clang->getFrontendOpts().DisableFree) { |
Benjamin Kramer | 77d2c5f | 2011-02-27 18:07:41 +0000 | [diff] [blame] | 178 | if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats) |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 179 | llvm::PrintStatistics(); |
Daniel Dunbar | 42444fb | 2010-03-23 05:09:16 +0000 | [diff] [blame] | 180 | Clang.take(); |
| 181 | return !Success; |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 184 | // Managed static deconstruction. Useful for making things like |
| 185 | // -time-passes usable. |
| 186 | llvm::llvm_shutdown(); |
| 187 | |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 188 | return !Success; |
Daniel Dunbar | 2108577 | 2009-12-11 22:20:12 +0000 | [diff] [blame] | 189 | } |