clang API Documentation

ExecuteCompilerInvocation.cpp
Go to the documentation of this file.
00001 //===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file holds ExecuteCompilerInvocation(). It is split into its own file to
00011 // minimize the impact of pulling in essentially everything else in Clang.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "clang/FrontendTool/Utils.h"
00016 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
00017 #include "clang/ARCMigrate/ARCMTActions.h"
00018 #include "clang/CodeGen/CodeGenAction.h"
00019 #include "clang/Driver/Options.h"
00020 #include "clang/Driver/OptTable.h"
00021 #include "clang/Frontend/CompilerInvocation.h"
00022 #include "clang/Frontend/CompilerInstance.h"
00023 #include "clang/Frontend/FrontendActions.h"
00024 #include "clang/Frontend/FrontendDiagnostic.h"
00025 #include "clang/Frontend/FrontendPluginRegistry.h"
00026 #include "clang/Rewrite/FrontendActions.h"
00027 #include "llvm/Support/ErrorHandling.h"
00028 #include "llvm/Support/DynamicLibrary.h"
00029 using namespace clang;
00030 
00031 static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
00032   using namespace clang::frontend;
00033 
00034   switch (CI.getFrontendOpts().ProgramAction) {
00035   case ASTDump:                return new ASTDumpAction();
00036   case ASTDumpXML:             return new ASTDumpXMLAction();
00037   case ASTPrint:               return new ASTPrintAction();
00038   case ASTView:                return new ASTViewAction();
00039   case DumpRawTokens:          return new DumpRawTokensAction();
00040   case DumpTokens:             return new DumpTokensAction();
00041   case EmitAssembly:           return new EmitAssemblyAction();
00042   case EmitBC:                 return new EmitBCAction();
00043   case EmitHTML:               return new HTMLPrintAction();
00044   case EmitLLVM:               return new EmitLLVMAction();
00045   case EmitLLVMOnly:           return new EmitLLVMOnlyAction();
00046   case EmitCodeGenOnly:        return new EmitCodeGenOnlyAction();
00047   case EmitObj:                return new EmitObjAction();
00048   case FixIt:                  return new FixItAction();
00049   case GenerateModule:         return new GenerateModuleAction;
00050   case GeneratePCH:            return new GeneratePCHAction;
00051   case GeneratePTH:            return new GeneratePTHAction();
00052   case InitOnly:               return new InitOnlyAction();
00053   case ParseSyntaxOnly:        return new SyntaxOnlyAction();
00054 
00055   case PluginAction: {
00056     for (FrontendPluginRegistry::iterator it =
00057            FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
00058          it != ie; ++it) {
00059       if (it->getName() == CI.getFrontendOpts().ActionName) {
00060         OwningPtr<PluginASTAction> P(it->instantiate());
00061         if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
00062           return 0;
00063         return P.take();
00064       }
00065     }
00066 
00067     CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
00068       << CI.getFrontendOpts().ActionName;
00069     return 0;
00070   }
00071 
00072   case PrintDeclContext:       return new DeclContextPrintAction();
00073   case PrintPreamble:          return new PrintPreambleAction();
00074   case PrintPreprocessedInput: return new PrintPreprocessedAction();
00075   case RewriteMacros:          return new RewriteMacrosAction();
00076   case RewriteObjC:            return new RewriteObjCAction();
00077   case RewriteTest:            return new RewriteTestAction();
00078   case RunAnalysis:            return new ento::AnalysisAction();
00079   case MigrateSource:          return new arcmt::MigrateSourceAction();
00080   case RunPreprocessorOnly:    return new PreprocessOnlyAction();
00081   }
00082   llvm_unreachable("Invalid program action!");
00083 }
00084 
00085 static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
00086   // Create the underlying action.
00087   FrontendAction *Act = CreateFrontendBaseAction(CI);
00088   if (!Act)
00089     return 0;
00090 
00091   const FrontendOptions &FEOpts = CI.getFrontendOpts();
00092 
00093   if (FEOpts.FixAndRecompile) {
00094     Act = new FixItRecompile(Act);
00095   }
00096   
00097   // Potentially wrap the base FE action in an ARC Migrate Tool action.
00098   switch (FEOpts.ARCMTAction) {
00099   case FrontendOptions::ARCMT_None:
00100     break;
00101   case FrontendOptions::ARCMT_Check:
00102     Act = new arcmt::CheckAction(Act);
00103     break;
00104   case FrontendOptions::ARCMT_Modify:
00105     Act = new arcmt::ModifyAction(Act);
00106     break;
00107   case FrontendOptions::ARCMT_Migrate:
00108     Act = new arcmt::MigrateAction(Act,
00109                                    FEOpts.MTMigrateDir,
00110                                    FEOpts.ARCMTMigrateReportOut,
00111                                    FEOpts.ARCMTMigrateEmitARCErrors);
00112     break;
00113   }
00114 
00115   if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
00116     Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir,
00117                    FEOpts.ObjCMTAction & ~FrontendOptions::ObjCMT_Literals,
00118                    FEOpts.ObjCMTAction & ~FrontendOptions::ObjCMT_Subscripting);
00119   }
00120 
00121   // If there are any AST files to merge, create a frontend action
00122   // adaptor to perform the merge.
00123   if (!FEOpts.ASTMergeFiles.empty())
00124     Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles);
00125 
00126   return Act;
00127 }
00128 
00129 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
00130   // Honor -help.
00131   if (Clang->getFrontendOpts().ShowHelp) {
00132     OwningPtr<driver::OptTable> Opts(driver::createDriverOptTable());
00133     Opts->PrintHelp(llvm::outs(), "clang -cc1",
00134                     "LLVM 'Clang' Compiler: http://clang.llvm.org");
00135     return 0;
00136   }
00137 
00138   // Honor -version.
00139   //
00140   // FIXME: Use a better -version message?
00141   if (Clang->getFrontendOpts().ShowVersion) {
00142     llvm::cl::PrintVersionMessage();
00143     return 0;
00144   }
00145 
00146   // Load any requested plugins.
00147   for (unsigned i = 0,
00148          e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
00149     const std::string &Path = Clang->getFrontendOpts().Plugins[i];
00150     std::string Error;
00151     if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
00152       Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
00153         << Path << Error;
00154   }
00155 
00156   // Honor -mllvm.
00157   //
00158   // FIXME: Remove this, one day.
00159   // This should happen AFTER plugins have been loaded!
00160   if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
00161     unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
00162     const char **Args = new const char*[NumArgs + 2];
00163     Args[0] = "clang (LLVM option parsing)";
00164     for (unsigned i = 0; i != NumArgs; ++i)
00165       Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
00166     Args[NumArgs + 1] = 0;
00167     llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
00168   }
00169 
00170   // Honor -analyzer-checker-help.
00171   // This should happen AFTER plugins have been loaded!
00172   if (Clang->getAnalyzerOpts().ShowCheckerHelp) {
00173     ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
00174     return 0;
00175   }
00176 
00177   // If there were errors in processing arguments, don't do anything else.
00178   bool Success = false;
00179   if (!Clang->getDiagnostics().hasErrorOccurred()) {
00180     // Create and execute the frontend action.
00181     OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang));
00182     if (Act) {
00183       Success = Clang->ExecuteAction(*Act);
00184       if (Clang->getFrontendOpts().DisableFree)
00185         Act.take();
00186     }
00187   }
00188 
00189   return Success;
00190 }