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 | |
| 15 | #include "clang/Frontend/Utils.h" |
| 16 | #include "clang/Checker/FrontendActions.h" |
| 17 | #include "clang/CodeGen/CodeGenAction.h" |
| 18 | #include "clang/Driver/CC1Options.h" |
| 19 | #include "clang/Driver/OptTable.h" |
| 20 | #include "clang/Frontend/CompilerInvocation.h" |
| 21 | #include "clang/Frontend/CompilerInstance.h" |
| 22 | #include "clang/Frontend/FrontendActions.h" |
| 23 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 24 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 25 | #include "clang/Rewrite/FrontendActions.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | #include "llvm/System/DynamicLibrary.h" |
| 28 | using namespace clang; |
| 29 | |
| 30 | static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { |
| 31 | using namespace clang::frontend; |
| 32 | |
| 33 | switch (CI.getFrontendOpts().ProgramAction) { |
| 34 | default: |
| 35 | llvm_unreachable("Invalid program action!"); |
| 36 | |
| 37 | case ASTDump: return new ASTDumpAction(); |
| 38 | case ASTPrint: return new ASTPrintAction(); |
| 39 | case ASTPrintXML: return new ASTPrintXMLAction(); |
| 40 | case ASTView: return new ASTViewAction(); |
| 41 | case BoostCon: return new BoostConAction(); |
Sebastian Redl | e55fd87 | 2010-08-16 18:17:11 +0000 | [diff] [blame] | 42 | case CreateModule: return 0; |
Daniel Dunbar | 8eb2b01 | 2010-08-12 02:53:12 +0000 | [diff] [blame] | 43 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 44 | case DumpTokens: return new DumpTokensAction(); |
| 45 | case EmitAssembly: return new EmitAssemblyAction(); |
| 46 | case EmitBC: return new EmitBCAction(); |
| 47 | case EmitHTML: return new HTMLPrintAction(); |
| 48 | case EmitLLVM: return new EmitLLVMAction(); |
| 49 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 50 | case EmitCodeGenOnly: return new EmitCodeGenOnlyAction(); |
| 51 | case EmitObj: return new EmitObjAction(); |
| 52 | case FixIt: return new FixItAction(); |
| 53 | case GeneratePCH: return new GeneratePCHAction(); |
| 54 | case GeneratePTH: return new GeneratePTHAction(); |
| 55 | case InheritanceView: return new InheritanceViewAction(); |
| 56 | case InitOnly: return new InitOnlyAction(); |
| 57 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
| 58 | |
| 59 | case PluginAction: { |
| 60 | for (FrontendPluginRegistry::iterator it = |
| 61 | FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); |
| 62 | it != ie; ++it) { |
| 63 | if (it->getName() == CI.getFrontendOpts().ActionName) { |
| 64 | llvm::OwningPtr<PluginASTAction> P(it->instantiate()); |
| 65 | if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs)) |
| 66 | return 0; |
| 67 | return P.take(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) |
| 72 | << CI.getFrontendOpts().ActionName; |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 77 | case PrintPreamble: return new PrintPreambleAction(); |
| 78 | case PrintPreprocessedInput: return new PrintPreprocessedAction(); |
| 79 | case RewriteMacros: return new RewriteMacrosAction(); |
| 80 | case RewriteObjC: return new RewriteObjCAction(); |
| 81 | case RewriteTest: return new RewriteTestAction(); |
| 82 | case RunAnalysis: return new AnalysisAction(); |
| 83 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { |
| 88 | // Create the underlying action. |
| 89 | FrontendAction *Act = CreateFrontendBaseAction(CI); |
| 90 | if (!Act) |
| 91 | return 0; |
| 92 | |
| 93 | // If there are any AST files to merge, create a frontend action |
| 94 | // adaptor to perform the merge. |
| 95 | if (!CI.getFrontendOpts().ASTMergeFiles.empty()) |
| 96 | Act = new ASTMergeAction(Act, &CI.getFrontendOpts().ASTMergeFiles[0], |
| 97 | CI.getFrontendOpts().ASTMergeFiles.size()); |
| 98 | |
| 99 | return Act; |
| 100 | } |
| 101 | |
| 102 | bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) { |
| 103 | // Honor -help. |
| 104 | if (Clang->getFrontendOpts().ShowHelp) { |
| 105 | llvm::OwningPtr<driver::OptTable> Opts(driver::createCC1OptTable()); |
| 106 | Opts->PrintHelp(llvm::outs(), "clang -cc1", |
| 107 | "LLVM 'Clang' Compiler: http://clang.llvm.org"); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | // Honor -version. |
| 112 | // |
| 113 | // FIXME: Use a better -version message? |
| 114 | if (Clang->getFrontendOpts().ShowVersion) { |
| 115 | llvm::cl::PrintVersionMessage(); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | // Honor -mllvm. |
| 120 | // |
| 121 | // FIXME: Remove this, one day. |
| 122 | if (!Clang->getFrontendOpts().LLVMArgs.empty()) { |
| 123 | unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size(); |
| 124 | const char **Args = new const char*[NumArgs + 2]; |
| 125 | Args[0] = "clang (LLVM option parsing)"; |
| 126 | for (unsigned i = 0; i != NumArgs; ++i) |
| 127 | Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str(); |
| 128 | Args[NumArgs + 1] = 0; |
| 129 | llvm::cl::ParseCommandLineOptions(NumArgs + 1, const_cast<char **>(Args)); |
| 130 | } |
| 131 | |
| 132 | // Load any requested plugins. |
| 133 | for (unsigned i = 0, |
| 134 | e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) { |
| 135 | const std::string &Path = Clang->getFrontendOpts().Plugins[i]; |
| 136 | std::string Error; |
| 137 | if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error)) |
| 138 | Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin) |
| 139 | << Path << Error; |
| 140 | } |
| 141 | |
| 142 | // If there were errors in processing arguments, don't do anything else. |
| 143 | bool Success = false; |
| 144 | if (!Clang->getDiagnostics().getNumErrors()) { |
| 145 | // Create and execute the frontend action. |
| 146 | llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang)); |
| 147 | if (Act) { |
| 148 | Success = Clang->ExecuteAction(*Act); |
| 149 | if (Clang->getFrontendOpts().DisableFree) |
| 150 | Act.take(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return Success; |
| 155 | } |