185dd0bd1SPeter Collingbourne //===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
285dd0bd1SPeter Collingbourne //
385dd0bd1SPeter Collingbourne //                     The LLVM Compiler Infrastructure
485dd0bd1SPeter Collingbourne //
585dd0bd1SPeter Collingbourne // This file is distributed under the University of Illinois Open Source
685dd0bd1SPeter Collingbourne // License. See LICENSE.TXT for details.
785dd0bd1SPeter Collingbourne //
885dd0bd1SPeter Collingbourne //===----------------------------------------------------------------------===//
985dd0bd1SPeter Collingbourne //
1085dd0bd1SPeter Collingbourne // This file holds ExecuteCompilerInvocation(). It is split into its own file to
1185dd0bd1SPeter Collingbourne // minimize the impact of pulling in essentially everything else in Clang.
1285dd0bd1SPeter Collingbourne //
1385dd0bd1SPeter Collingbourne //===----------------------------------------------------------------------===//
1485dd0bd1SPeter Collingbourne 
1585dd0bd1SPeter Collingbourne #include "clang/FrontendTool/Utils.h"
1685dd0bd1SPeter Collingbourne #include "clang/Checker/FrontendActions.h"
1785dd0bd1SPeter Collingbourne #include "clang/CodeGen/CodeGenAction.h"
1885dd0bd1SPeter Collingbourne #include "clang/Driver/CC1Options.h"
1985dd0bd1SPeter Collingbourne #include "clang/Driver/OptTable.h"
2085dd0bd1SPeter Collingbourne #include "clang/Frontend/CompilerInvocation.h"
2185dd0bd1SPeter Collingbourne #include "clang/Frontend/CompilerInstance.h"
2285dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendActions.h"
2385dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendDiagnostic.h"
2485dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendPluginRegistry.h"
2585dd0bd1SPeter Collingbourne #include "clang/Rewrite/FrontendActions.h"
2685dd0bd1SPeter Collingbourne #include "llvm/Support/ErrorHandling.h"
2785dd0bd1SPeter Collingbourne #include "llvm/System/DynamicLibrary.h"
2885dd0bd1SPeter Collingbourne using namespace clang;
2985dd0bd1SPeter Collingbourne 
3085dd0bd1SPeter Collingbourne static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
3185dd0bd1SPeter Collingbourne   using namespace clang::frontend;
3285dd0bd1SPeter Collingbourne 
3385dd0bd1SPeter Collingbourne   switch (CI.getFrontendOpts().ProgramAction) {
3485dd0bd1SPeter Collingbourne   default:
3585dd0bd1SPeter Collingbourne     llvm_unreachable("Invalid program action!");
3685dd0bd1SPeter Collingbourne 
3785dd0bd1SPeter Collingbourne   case ASTDump:                return new ASTDumpAction();
38*9b66c4bbSJohn McCall   case ASTDumpXML:             return new ASTDumpXMLAction();
3985dd0bd1SPeter Collingbourne   case ASTPrint:               return new ASTPrintAction();
4085dd0bd1SPeter Collingbourne   case ASTPrintXML:            return new ASTPrintXMLAction();
4185dd0bd1SPeter Collingbourne   case ASTView:                return new ASTViewAction();
4285dd0bd1SPeter Collingbourne   case BoostCon:               return new BoostConAction();
4385dd0bd1SPeter Collingbourne   case CreateModule:           return 0;
4485dd0bd1SPeter Collingbourne   case DumpRawTokens:          return new DumpRawTokensAction();
4585dd0bd1SPeter Collingbourne   case DumpTokens:             return new DumpTokensAction();
4685dd0bd1SPeter Collingbourne   case EmitAssembly:           return new EmitAssemblyAction();
4785dd0bd1SPeter Collingbourne   case EmitBC:                 return new EmitBCAction();
4885dd0bd1SPeter Collingbourne   case EmitHTML:               return new HTMLPrintAction();
4985dd0bd1SPeter Collingbourne   case EmitLLVM:               return new EmitLLVMAction();
5085dd0bd1SPeter Collingbourne   case EmitLLVMOnly:           return new EmitLLVMOnlyAction();
5185dd0bd1SPeter Collingbourne   case EmitCodeGenOnly:        return new EmitCodeGenOnlyAction();
5285dd0bd1SPeter Collingbourne   case EmitObj:                return new EmitObjAction();
5385dd0bd1SPeter Collingbourne   case FixIt:                  return new FixItAction();
5485dd0bd1SPeter Collingbourne   case GeneratePCH:            return new GeneratePCHAction();
5585dd0bd1SPeter Collingbourne   case GeneratePTH:            return new GeneratePTHAction();
5685dd0bd1SPeter Collingbourne   case InheritanceView:        return new InheritanceViewAction();
5785dd0bd1SPeter Collingbourne   case InitOnly:               return new InitOnlyAction();
5885dd0bd1SPeter Collingbourne   case ParseSyntaxOnly:        return new SyntaxOnlyAction();
5985dd0bd1SPeter Collingbourne 
6085dd0bd1SPeter Collingbourne   case PluginAction: {
6185dd0bd1SPeter Collingbourne     for (FrontendPluginRegistry::iterator it =
6285dd0bd1SPeter Collingbourne            FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
6385dd0bd1SPeter Collingbourne          it != ie; ++it) {
6485dd0bd1SPeter Collingbourne       if (it->getName() == CI.getFrontendOpts().ActionName) {
6585dd0bd1SPeter Collingbourne         llvm::OwningPtr<PluginASTAction> P(it->instantiate());
6685dd0bd1SPeter Collingbourne         if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
6785dd0bd1SPeter Collingbourne           return 0;
6885dd0bd1SPeter Collingbourne         return P.take();
6985dd0bd1SPeter Collingbourne       }
7085dd0bd1SPeter Collingbourne     }
7185dd0bd1SPeter Collingbourne 
7285dd0bd1SPeter Collingbourne     CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
7385dd0bd1SPeter Collingbourne       << CI.getFrontendOpts().ActionName;
7485dd0bd1SPeter Collingbourne     return 0;
7585dd0bd1SPeter Collingbourne   }
7685dd0bd1SPeter Collingbourne 
7785dd0bd1SPeter Collingbourne   case PrintDeclContext:       return new DeclContextPrintAction();
7885dd0bd1SPeter Collingbourne   case PrintPreamble:          return new PrintPreambleAction();
7985dd0bd1SPeter Collingbourne   case PrintPreprocessedInput: return new PrintPreprocessedAction();
8085dd0bd1SPeter Collingbourne   case RewriteMacros:          return new RewriteMacrosAction();
8185dd0bd1SPeter Collingbourne   case RewriteObjC:            return new RewriteObjCAction();
8285dd0bd1SPeter Collingbourne   case RewriteTest:            return new RewriteTestAction();
8385dd0bd1SPeter Collingbourne   case RunAnalysis:            return new AnalysisAction();
8485dd0bd1SPeter Collingbourne   case RunPreprocessorOnly:    return new PreprocessOnlyAction();
8585dd0bd1SPeter Collingbourne   }
8685dd0bd1SPeter Collingbourne }
8785dd0bd1SPeter Collingbourne 
8885dd0bd1SPeter Collingbourne static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
8985dd0bd1SPeter Collingbourne   // Create the underlying action.
9085dd0bd1SPeter Collingbourne   FrontendAction *Act = CreateFrontendBaseAction(CI);
9185dd0bd1SPeter Collingbourne   if (!Act)
9285dd0bd1SPeter Collingbourne     return 0;
9385dd0bd1SPeter Collingbourne 
9485dd0bd1SPeter Collingbourne   // If there are any AST files to merge, create a frontend action
9585dd0bd1SPeter Collingbourne   // adaptor to perform the merge.
9685dd0bd1SPeter Collingbourne   if (!CI.getFrontendOpts().ASTMergeFiles.empty())
9785dd0bd1SPeter Collingbourne     Act = new ASTMergeAction(Act, &CI.getFrontendOpts().ASTMergeFiles[0],
9885dd0bd1SPeter Collingbourne                              CI.getFrontendOpts().ASTMergeFiles.size());
9985dd0bd1SPeter Collingbourne 
10085dd0bd1SPeter Collingbourne   return Act;
10185dd0bd1SPeter Collingbourne }
10285dd0bd1SPeter Collingbourne 
10385dd0bd1SPeter Collingbourne bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
10485dd0bd1SPeter Collingbourne   // Honor -help.
10585dd0bd1SPeter Collingbourne   if (Clang->getFrontendOpts().ShowHelp) {
10685dd0bd1SPeter Collingbourne     llvm::OwningPtr<driver::OptTable> Opts(driver::createCC1OptTable());
10785dd0bd1SPeter Collingbourne     Opts->PrintHelp(llvm::outs(), "clang -cc1",
10885dd0bd1SPeter Collingbourne                     "LLVM 'Clang' Compiler: http://clang.llvm.org");
10985dd0bd1SPeter Collingbourne     return 0;
11085dd0bd1SPeter Collingbourne   }
11185dd0bd1SPeter Collingbourne 
11285dd0bd1SPeter Collingbourne   // Honor -version.
11385dd0bd1SPeter Collingbourne   //
11485dd0bd1SPeter Collingbourne   // FIXME: Use a better -version message?
11585dd0bd1SPeter Collingbourne   if (Clang->getFrontendOpts().ShowVersion) {
11685dd0bd1SPeter Collingbourne     llvm::cl::PrintVersionMessage();
11785dd0bd1SPeter Collingbourne     return 0;
11885dd0bd1SPeter Collingbourne   }
11985dd0bd1SPeter Collingbourne 
12085dd0bd1SPeter Collingbourne   // Honor -mllvm.
12185dd0bd1SPeter Collingbourne   //
12285dd0bd1SPeter Collingbourne   // FIXME: Remove this, one day.
12385dd0bd1SPeter Collingbourne   if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
12485dd0bd1SPeter Collingbourne     unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
12585dd0bd1SPeter Collingbourne     const char **Args = new const char*[NumArgs + 2];
12685dd0bd1SPeter Collingbourne     Args[0] = "clang (LLVM option parsing)";
12785dd0bd1SPeter Collingbourne     for (unsigned i = 0; i != NumArgs; ++i)
12885dd0bd1SPeter Collingbourne       Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
12985dd0bd1SPeter Collingbourne     Args[NumArgs + 1] = 0;
13085dd0bd1SPeter Collingbourne     llvm::cl::ParseCommandLineOptions(NumArgs + 1, const_cast<char **>(Args));
13185dd0bd1SPeter Collingbourne   }
13285dd0bd1SPeter Collingbourne 
13385dd0bd1SPeter Collingbourne   // Load any requested plugins.
13485dd0bd1SPeter Collingbourne   for (unsigned i = 0,
13585dd0bd1SPeter Collingbourne          e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
13685dd0bd1SPeter Collingbourne     const std::string &Path = Clang->getFrontendOpts().Plugins[i];
13785dd0bd1SPeter Collingbourne     std::string Error;
13885dd0bd1SPeter Collingbourne     if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
13985dd0bd1SPeter Collingbourne       Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
14085dd0bd1SPeter Collingbourne         << Path << Error;
14185dd0bd1SPeter Collingbourne   }
14285dd0bd1SPeter Collingbourne 
14385dd0bd1SPeter Collingbourne   // If there were errors in processing arguments, don't do anything else.
14485dd0bd1SPeter Collingbourne   bool Success = false;
1455c26cda2SArgyrios Kyrtzidis   if (!Clang->getDiagnostics().hasErrorOccurred()) {
14685dd0bd1SPeter Collingbourne     // Create and execute the frontend action.
14785dd0bd1SPeter Collingbourne     llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang));
14885dd0bd1SPeter Collingbourne     if (Act) {
14985dd0bd1SPeter Collingbourne       Success = Clang->ExecuteAction(*Act);
15085dd0bd1SPeter Collingbourne       if (Clang->getFrontendOpts().DisableFree)
15185dd0bd1SPeter Collingbourne         Act.take();
15285dd0bd1SPeter Collingbourne     }
15385dd0bd1SPeter Collingbourne   }
15485dd0bd1SPeter Collingbourne 
15585dd0bd1SPeter Collingbourne   return Success;
15685dd0bd1SPeter Collingbourne }
157