Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 1 | //===--- ExecuteCompilerInvocation.cpp ------------------------------------===// |
| 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 | // |
| 10 | // This file holds ExecuteCompilerInvocation(). It is split into its own file to |
| 11 | // minimize the impact of pulling in essentially everything else in Clang. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Peter Collingbourne | 1b7255d | 2010-08-24 00:31:22 +0000 | [diff] [blame] | 15 | #include "clang/FrontendTool/Utils.h" |
Argyrios Kyrtzidis | 43dee22 | 2011-02-14 18:13:31 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
Chandler Carruth | f7f8188 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 17 | #include "clang/ARCMigrate/ARCMTActions.h" |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 18 | #include "clang/CodeGen/CodeGenAction.h" |
| 19 | #include "clang/Driver/CC1Options.h" |
| 20 | #include "clang/Driver/OptTable.h" |
| 21 | #include "clang/Frontend/CompilerInvocation.h" |
| 22 | #include "clang/Frontend/CompilerInstance.h" |
| 23 | #include "clang/Frontend/FrontendActions.h" |
| 24 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 25 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 26 | #include "clang/Rewrite/FrontendActions.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 28 | #include "llvm/Support/DynamicLibrary.h" |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
| 31 | static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { |
| 32 | using namespace clang::frontend; |
| 33 | |
| 34 | switch (CI.getFrontendOpts().ProgramAction) { |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 35 | case ASTDump: return new ASTDumpAction(); |
John McCall | f351424 | 2010-11-24 11:21:45 +0000 | [diff] [blame] | 36 | case ASTDumpXML: return new ASTDumpXMLAction(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 37 | case ASTPrint: return new ASTPrintAction(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 38 | case ASTView: return new ASTViewAction(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 39 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 40 | case DumpTokens: return new DumpTokensAction(); |
| 41 | case EmitAssembly: return new EmitAssemblyAction(); |
| 42 | case EmitBC: return new EmitBCAction(); |
| 43 | case EmitHTML: return new HTMLPrintAction(); |
| 44 | case EmitLLVM: return new EmitLLVMAction(); |
| 45 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 46 | case EmitCodeGenOnly: return new EmitCodeGenOnlyAction(); |
| 47 | case EmitObj: return new EmitObjAction(); |
| 48 | case FixIt: return new FixItAction(); |
Douglas Gregor | 6649014 | 2011-11-29 22:42:06 +0000 | [diff] [blame] | 49 | case GenerateModule: return new GenerateModuleAction; |
| 50 | case GeneratePCH: return new GeneratePCHAction; |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 51 | case GeneratePTH: return new GeneratePTHAction(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 52 | case InitOnly: return new InitOnlyAction(); |
| 53 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
| 54 | |
| 55 | case PluginAction: { |
| 56 | for (FrontendPluginRegistry::iterator it = |
| 57 | FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); |
| 58 | it != ie; ++it) { |
| 59 | if (it->getName() == CI.getFrontendOpts().ActionName) { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 60 | OwningPtr<PluginASTAction> P(it->instantiate()); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 61 | if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs)) |
| 62 | return 0; |
| 63 | return P.take(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) |
| 68 | << CI.getFrontendOpts().ActionName; |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 73 | case PrintPreamble: return new PrintPreambleAction(); |
| 74 | case PrintPreprocessedInput: return new PrintPreprocessedAction(); |
| 75 | case RewriteMacros: return new RewriteMacrosAction(); |
| 76 | case RewriteObjC: return new RewriteObjCAction(); |
| 77 | case RewriteTest: return new RewriteTestAction(); |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 78 | case RunAnalysis: return new ento::AnalysisAction(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 79 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
| 80 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 81 | llvm_unreachable("Invalid program action!"); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { |
| 85 | // Create the underlying action. |
| 86 | FrontendAction *Act = CreateFrontendBaseAction(CI); |
| 87 | if (!Act) |
| 88 | return 0; |
| 89 | |
Argyrios Kyrtzidis | b3ca263 | 2012-02-04 01:36:04 +0000 | [diff] [blame] | 90 | const FrontendOptions &FEOpts = CI.getFrontendOpts(); |
| 91 | |
| 92 | if (FEOpts.FixAndRecompile) { |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 93 | Act = new FixItRecompile(Act); |
| 94 | } |
| 95 | |
Chandler Carruth | f7f8188 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 96 | // Potentially wrap the base FE action in an ARC Migrate Tool action. |
Argyrios Kyrtzidis | b3ca263 | 2012-02-04 01:36:04 +0000 | [diff] [blame] | 97 | switch (FEOpts.ARCMTAction) { |
Chandler Carruth | f7f8188 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 98 | case FrontendOptions::ARCMT_None: |
| 99 | break; |
| 100 | case FrontendOptions::ARCMT_Check: |
| 101 | Act = new arcmt::CheckAction(Act); |
| 102 | break; |
| 103 | case FrontendOptions::ARCMT_Modify: |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 104 | Act = new arcmt::ModifyAction(Act); |
| 105 | break; |
| 106 | case FrontendOptions::ARCMT_Migrate: |
Argyrios Kyrtzidis | 7ee2049 | 2011-07-19 17:20:03 +0000 | [diff] [blame] | 107 | Act = new arcmt::MigrateAction(Act, |
Argyrios Kyrtzidis | b3ca263 | 2012-02-04 01:36:04 +0000 | [diff] [blame] | 108 | FEOpts.ARCMTMigrateDir, |
| 109 | FEOpts.ARCMTMigrateReportOut, |
| 110 | FEOpts.ARCMTMigrateEmitARCErrors); |
Chandler Carruth | f7f8188 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 111 | break; |
Chandler Carruth | f7f8188 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 114 | // If there are any AST files to merge, create a frontend action |
| 115 | // adaptor to perform the merge. |
Argyrios Kyrtzidis | b3ca263 | 2012-02-04 01:36:04 +0000 | [diff] [blame] | 116 | if (!FEOpts.ASTMergeFiles.empty()) |
Argyrios Kyrtzidis | e839806 | 2012-02-04 03:26:16 +0000 | [diff] [blame] | 117 | Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 118 | |
| 119 | return Act; |
| 120 | } |
| 121 | |
| 122 | bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) { |
| 123 | // Honor -help. |
| 124 | if (Clang->getFrontendOpts().ShowHelp) { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 125 | OwningPtr<driver::OptTable> Opts(driver::createCC1OptTable()); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 126 | Opts->PrintHelp(llvm::outs(), "clang -cc1", |
| 127 | "LLVM 'Clang' Compiler: http://clang.llvm.org"); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | // Honor -version. |
| 132 | // |
| 133 | // FIXME: Use a better -version message? |
| 134 | if (Clang->getFrontendOpts().ShowVersion) { |
| 135 | llvm::cl::PrintVersionMessage(); |
| 136 | return 0; |
| 137 | } |
| 138 | |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 139 | // Load any requested plugins. |
| 140 | for (unsigned i = 0, |
| 141 | e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) { |
| 142 | const std::string &Path = Clang->getFrontendOpts().Plugins[i]; |
| 143 | std::string Error; |
| 144 | if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error)) |
| 145 | Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin) |
| 146 | << Path << Error; |
| 147 | } |
| 148 | |
Tobias Grosser | 6e0afc8 | 2011-10-10 01:23:06 +0000 | [diff] [blame] | 149 | // Honor -mllvm. |
| 150 | // |
| 151 | // FIXME: Remove this, one day. |
| 152 | // This should happen AFTER plugins have been loaded! |
| 153 | if (!Clang->getFrontendOpts().LLVMArgs.empty()) { |
| 154 | unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size(); |
| 155 | const char **Args = new const char*[NumArgs + 2]; |
| 156 | Args[0] = "clang (LLVM option parsing)"; |
| 157 | for (unsigned i = 0; i != NumArgs; ++i) |
| 158 | Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str(); |
| 159 | Args[NumArgs + 1] = 0; |
David Blaikie | 6bd17d2 | 2012-02-07 19:36:38 +0000 | [diff] [blame] | 160 | llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args); |
Tobias Grosser | 6e0afc8 | 2011-10-10 01:23:06 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 163 | // Honor -analyzer-checker-help. |
| 164 | // This should happen AFTER plugins have been loaded! |
| 165 | if (Clang->getAnalyzerOpts().ShowCheckerHelp) { |
| 166 | ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins); |
| 167 | return 0; |
| 168 | } |
| 169 | |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 170 | // If there were errors in processing arguments, don't do anything else. |
| 171 | bool Success = false; |
Argyrios Kyrtzidis | e8f0ba7 | 2010-11-19 00:19:18 +0000 | [diff] [blame] | 172 | if (!Clang->getDiagnostics().hasErrorOccurred()) { |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 173 | // Create and execute the frontend action. |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 174 | OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang)); |
Ted Kremenek | 72e3a0a | 2011-04-25 22:57:55 +0000 | [diff] [blame] | 175 | if (Act) { |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 176 | Success = Clang->ExecuteAction(*Act); |
Ted Kremenek | 72e3a0a | 2011-04-25 22:57:55 +0000 | [diff] [blame] | 177 | if (Clang->getFrontendOpts().DisableFree) |
| 178 | Act.take(); |
| 179 | } |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | return Success; |
| 183 | } |