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" |
Chandler Carruth | f7f8188 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 16 | #include "clang/ARCMigrate/ARCMTActions.h" |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 17 | #include "clang/CodeGen/CodeGenAction.h" |
James Molloy | bfd7a52 | 2012-05-01 14:57:16 +0000 | [diff] [blame] | 18 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/CompilerInstance.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/CompilerInvocation.h" |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/FrontendActions.h" |
| 22 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 23 | #include "clang/Frontend/FrontendPluginRegistry.h" |
Ted Kremenek | 305c613 | 2012-09-01 05:09:24 +0000 | [diff] [blame] | 24 | #include "clang/Rewrite/Frontend/FrontendActions.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/StaticAnalyzer/Frontend/FrontendActions.h" |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 26 | #include "llvm/Option/OptTable.h" |
| 27 | #include "llvm/Option/Option.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 28 | #include "llvm/Support/DynamicLibrary.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 30 | using namespace clang; |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 31 | using namespace llvm::opt; |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 32 | |
| 33 | static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { |
| 34 | using namespace clang::frontend; |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 35 | StringRef Action("unknown"); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 36 | |
| 37 | switch (CI.getFrontendOpts().ProgramAction) { |
Alexander Kornienko | 171af64 | 2012-07-31 09:37:40 +0000 | [diff] [blame] | 38 | case ASTDeclList: return new ASTDeclListAction(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 39 | case ASTDump: return new ASTDumpAction(); |
| 40 | case ASTPrint: return new ASTPrintAction(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 41 | case ASTView: return new ASTViewAction(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 42 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 43 | case DumpTokens: return new DumpTokensAction(); |
| 44 | case EmitAssembly: return new EmitAssemblyAction(); |
| 45 | case EmitBC: return new EmitBCAction(); |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 46 | #ifdef CLANG_ENABLE_REWRITER |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 47 | case EmitHTML: return new HTMLPrintAction(); |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 48 | #else |
| 49 | case EmitHTML: Action = "EmitHTML"; break; |
| 50 | #endif |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 51 | case EmitLLVM: return new EmitLLVMAction(); |
| 52 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 53 | case EmitCodeGenOnly: return new EmitCodeGenOnlyAction(); |
| 54 | case EmitObj: return new EmitObjAction(); |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 55 | #ifdef CLANG_ENABLE_REWRITER |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 56 | case FixIt: return new FixItAction(); |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 57 | #else |
| 58 | case FixIt: Action = "FixIt"; break; |
| 59 | #endif |
Douglas Gregor | 6649014 | 2011-11-29 22:42:06 +0000 | [diff] [blame] | 60 | case GenerateModule: return new GenerateModuleAction; |
| 61 | case GeneratePCH: return new GeneratePCHAction; |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 62 | case GeneratePTH: return new GeneratePTHAction(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 63 | case InitOnly: return new InitOnlyAction(); |
| 64 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
Douglas Gregor | c544ba0 | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 65 | case ModuleFileInfo: return new DumpModuleInfoAction(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 66 | |
| 67 | case PluginAction: { |
| 68 | for (FrontendPluginRegistry::iterator it = |
| 69 | FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); |
| 70 | it != ie; ++it) { |
| 71 | if (it->getName() == CI.getFrontendOpts().ActionName) { |
Dylan Noblesmith | 6f42b62 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 72 | OwningPtr<PluginASTAction> P(it->instantiate()); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 73 | if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs)) |
| 74 | return 0; |
| 75 | return P.take(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) |
| 80 | << CI.getFrontendOpts().ActionName; |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 85 | case PrintPreamble: return new PrintPreambleAction(); |
David Blaikie | f1492f9 | 2012-06-14 17:36:09 +0000 | [diff] [blame] | 86 | case PrintPreprocessedInput: { |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 87 | if (CI.getPreprocessorOutputOpts().RewriteIncludes) { |
| 88 | #ifdef CLANG_ENABLE_REWRITER |
David Blaikie | f1492f9 | 2012-06-14 17:36:09 +0000 | [diff] [blame] | 89 | return new RewriteIncludesAction(); |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 90 | #else |
| 91 | Action = "RewriteIncludesAction"; |
| 92 | break; |
| 93 | #endif |
| 94 | } |
David Blaikie | f1492f9 | 2012-06-14 17:36:09 +0000 | [diff] [blame] | 95 | return new PrintPreprocessedAction(); |
| 96 | } |
| 97 | |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 98 | #ifdef CLANG_ENABLE_REWRITER |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 99 | case RewriteMacros: return new RewriteMacrosAction(); |
| 100 | case RewriteObjC: return new RewriteObjCAction(); |
| 101 | case RewriteTest: return new RewriteTestAction(); |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 102 | #else |
| 103 | case RewriteMacros: Action = "RewriteMacros"; break; |
| 104 | case RewriteObjC: Action = "RewriteObjC"; break; |
| 105 | case RewriteTest: Action = "RewriteTest"; break; |
| 106 | #endif |
| 107 | #ifdef CLANG_ENABLE_ARCMT |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 108 | case MigrateSource: return new arcmt::MigrateSourceAction(); |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 109 | #else |
| 110 | case MigrateSource: Action = "MigrateSource"; break; |
| 111 | #endif |
| 112 | #ifdef CLANG_ENABLE_STATIC_ANALYZER |
| 113 | case RunAnalysis: return new ento::AnalysisAction(); |
| 114 | #else |
| 115 | case RunAnalysis: Action = "RunAnalysis"; break; |
| 116 | #endif |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 117 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
| 118 | } |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 119 | |
| 120 | #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \ |
| 121 | || !defined(CLANG_ENABLE_REWRITER) |
| 122 | CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action; |
| 123 | return 0; |
| 124 | #else |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 125 | llvm_unreachable("Invalid program action!"); |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 126 | #endif |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { |
| 130 | // Create the underlying action. |
| 131 | FrontendAction *Act = CreateFrontendBaseAction(CI); |
| 132 | if (!Act) |
| 133 | return 0; |
| 134 | |
Argyrios Kyrtzidis | b3ca263 | 2012-02-04 01:36:04 +0000 | [diff] [blame] | 135 | const FrontendOptions &FEOpts = CI.getFrontendOpts(); |
| 136 | |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 137 | #ifdef CLANG_ENABLE_REWRITER |
Argyrios Kyrtzidis | b3ca263 | 2012-02-04 01:36:04 +0000 | [diff] [blame] | 138 | if (FEOpts.FixAndRecompile) { |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 139 | Act = new FixItRecompile(Act); |
| 140 | } |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 141 | #endif |
Argyrios Kyrtzidis | 61d679a | 2012-01-26 02:40:48 +0000 | [diff] [blame] | 142 | |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 143 | #ifdef CLANG_ENABLE_ARCMT |
Argyrios Kyrtzidis | f1576c1 | 2013-11-13 23:38:17 +0000 | [diff] [blame] | 144 | if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource) { |
| 145 | // Potentially wrap the base FE action in an ARC Migrate Tool action. |
| 146 | switch (FEOpts.ARCMTAction) { |
| 147 | case FrontendOptions::ARCMT_None: |
| 148 | break; |
| 149 | case FrontendOptions::ARCMT_Check: |
| 150 | Act = new arcmt::CheckAction(Act); |
| 151 | break; |
| 152 | case FrontendOptions::ARCMT_Modify: |
| 153 | Act = new arcmt::ModifyAction(Act); |
| 154 | break; |
| 155 | case FrontendOptions::ARCMT_Migrate: |
| 156 | Act = new arcmt::MigrateAction(Act, |
| 157 | FEOpts.MTMigrateDir, |
| 158 | FEOpts.ARCMTMigrateReportOut, |
| 159 | FEOpts.ARCMTMigrateEmitARCErrors); |
| 160 | break; |
| 161 | } |
Chandler Carruth | f7f8188 | 2011-06-16 16:17:05 +0000 | [diff] [blame] | 162 | |
Argyrios Kyrtzidis | f1576c1 | 2013-11-13 23:38:17 +0000 | [diff] [blame] | 163 | if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) { |
| 164 | Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir, |
| 165 | FEOpts.ObjCMTAction); |
| 166 | } |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 167 | } |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 168 | #endif |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 169 | |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 170 | // If there are any AST files to merge, create a frontend action |
| 171 | // adaptor to perform the merge. |
Argyrios Kyrtzidis | b3ca263 | 2012-02-04 01:36:04 +0000 | [diff] [blame] | 172 | if (!FEOpts.ASTMergeFiles.empty()) |
Argyrios Kyrtzidis | e839806 | 2012-02-04 03:26:16 +0000 | [diff] [blame] | 173 | Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 174 | |
| 175 | return Act; |
| 176 | } |
| 177 | |
| 178 | bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) { |
| 179 | // Honor -help. |
| 180 | if (Clang->getFrontendOpts().ShowHelp) { |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 181 | OwningPtr<OptTable> Opts(driver::createDriverOptTable()); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 182 | Opts->PrintHelp(llvm::outs(), "clang -cc1", |
Richard Smith | f1eaab1 | 2012-11-09 22:36:44 +0000 | [diff] [blame] | 183 | "LLVM 'Clang' Compiler: http://clang.llvm.org", |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 184 | /*Include=*/ driver::options::CC1Option, /*Exclude=*/ 0); |
Rafael Espindola | 201d3fb | 2013-08-07 12:54:47 +0000 | [diff] [blame] | 185 | return true; |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Honor -version. |
| 189 | // |
| 190 | // FIXME: Use a better -version message? |
| 191 | if (Clang->getFrontendOpts().ShowVersion) { |
| 192 | llvm::cl::PrintVersionMessage(); |
Rafael Espindola | 201d3fb | 2013-08-07 12:54:47 +0000 | [diff] [blame] | 193 | return true; |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 196 | // Load any requested plugins. |
| 197 | for (unsigned i = 0, |
| 198 | e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) { |
| 199 | const std::string &Path = Clang->getFrontendOpts().Plugins[i]; |
| 200 | std::string Error; |
| 201 | if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error)) |
| 202 | Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin) |
| 203 | << Path << Error; |
| 204 | } |
| 205 | |
Tobias Grosser | 6e0afc8 | 2011-10-10 01:23:06 +0000 | [diff] [blame] | 206 | // Honor -mllvm. |
| 207 | // |
| 208 | // FIXME: Remove this, one day. |
| 209 | // This should happen AFTER plugins have been loaded! |
| 210 | if (!Clang->getFrontendOpts().LLVMArgs.empty()) { |
| 211 | unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size(); |
| 212 | const char **Args = new const char*[NumArgs + 2]; |
| 213 | Args[0] = "clang (LLVM option parsing)"; |
| 214 | for (unsigned i = 0; i != NumArgs; ++i) |
| 215 | Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str(); |
| 216 | Args[NumArgs + 1] = 0; |
David Blaikie | 6bd17d2 | 2012-02-07 19:36:38 +0000 | [diff] [blame] | 217 | llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args); |
Tobias Grosser | 6e0afc8 | 2011-10-10 01:23:06 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 220 | #ifdef CLANG_ENABLE_STATIC_ANALYZER |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 221 | // Honor -analyzer-checker-help. |
| 222 | // This should happen AFTER plugins have been loaded! |
Ted Kremenek | 45796b1 | 2012-08-31 04:36:05 +0000 | [diff] [blame] | 223 | if (Clang->getAnalyzerOpts()->ShowCheckerHelp) { |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 224 | ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins); |
Rafael Espindola | 201d3fb | 2013-08-07 12:54:47 +0000 | [diff] [blame] | 225 | return true; |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 226 | } |
Roman Divacky | fc2929f | 2012-12-13 16:09:42 +0000 | [diff] [blame] | 227 | #endif |
Jordy Rose | 08b8653 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 228 | |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 229 | // If there were errors in processing arguments, don't do anything else. |
Sean Silva | 8da06bb | 2013-01-06 07:49:41 +0000 | [diff] [blame] | 230 | if (Clang->getDiagnostics().hasErrorOccurred()) |
| 231 | return false; |
| 232 | // Create and execute the frontend action. |
| 233 | OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang)); |
| 234 | if (!Act) |
| 235 | return false; |
| 236 | bool Success = Clang->ExecuteAction(*Act); |
| 237 | if (Clang->getFrontendOpts().DisableFree) |
| 238 | Act.take(); |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 239 | return Success; |
| 240 | } |