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/ARCMigrate/ARCMTActions.h"
00017 #include "clang/CodeGen/CodeGenAction.h"
00018 #include "clang/Driver/Options.h"
00019 #include "clang/Frontend/CompilerInstance.h"
00020 #include "clang/Frontend/CompilerInvocation.h"
00021 #include "clang/Frontend/FrontendActions.h"
00022 #include "clang/Frontend/FrontendDiagnostic.h"
00023 #include "clang/Frontend/FrontendPluginRegistry.h"
00024 #include "clang/Frontend/Utils.h"
00025 #include "clang/Rewrite/Frontend/FrontendActions.h"
00026 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
00027 #include "llvm/Option/OptTable.h"
00028 #include "llvm/Option/Option.h"
00029 #include "llvm/Support/DynamicLibrary.h"
00030 #include "llvm/Support/ErrorHandling.h"
00031 using namespace clang;
00032 using namespace llvm::opt;
00033 
00034 static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
00035   using namespace clang::frontend;
00036   StringRef Action("unknown");
00037   (void)Action;
00038 
00039   switch (CI.getFrontendOpts().ProgramAction) {
00040   case ASTDeclList:            return new ASTDeclListAction();
00041   case ASTDump:                return new ASTDumpAction();
00042   case ASTPrint:               return new ASTPrintAction();
00043   case ASTView:                return new ASTViewAction();
00044   case DumpRawTokens:          return new DumpRawTokensAction();
00045   case DumpTokens:             return new DumpTokensAction();
00046   case EmitAssembly:           return new EmitAssemblyAction();
00047   case EmitBC:                 return new EmitBCAction();
00048   case EmitHTML:               return new HTMLPrintAction();
00049   case EmitLLVM:               return new EmitLLVMAction();
00050   case EmitLLVMOnly:           return new EmitLLVMOnlyAction();
00051   case EmitCodeGenOnly:        return new EmitCodeGenOnlyAction();
00052   case EmitObj:                return new EmitObjAction();
00053   case FixIt:                  return new FixItAction();
00054   case GenerateModule:         return new GenerateModuleAction;
00055   case GeneratePCH:            return new GeneratePCHAction;
00056   case GeneratePTH:            return new GeneratePTHAction();
00057   case InitOnly:               return new InitOnlyAction();
00058   case ParseSyntaxOnly:        return new SyntaxOnlyAction();
00059   case ModuleFileInfo:         return new DumpModuleInfoAction();
00060   case VerifyPCH:              return new VerifyPCHAction();
00061 
00062   case PluginAction: {
00063     for (FrontendPluginRegistry::iterator it =
00064            FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
00065          it != ie; ++it) {
00066       if (it->getName() == CI.getFrontendOpts().ActionName) {
00067         std::unique_ptr<PluginASTAction> P(it->instantiate());
00068         if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
00069           return nullptr;
00070         return P.release();
00071       }
00072     }
00073 
00074     CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
00075       << CI.getFrontendOpts().ActionName;
00076     return nullptr;
00077   }
00078 
00079   case PrintDeclContext:       return new DeclContextPrintAction();
00080   case PrintPreamble:          return new PrintPreambleAction();
00081   case PrintPreprocessedInput: {
00082     if (CI.getPreprocessorOutputOpts().RewriteIncludes)
00083       return new RewriteIncludesAction();
00084     return new PrintPreprocessedAction();
00085   }
00086 
00087   case RewriteMacros:          return new RewriteMacrosAction();
00088   case RewriteTest:            return new RewriteTestAction();
00089 #ifdef CLANG_ENABLE_OBJC_REWRITER
00090   case RewriteObjC:            return new RewriteObjCAction();
00091 #else
00092   case RewriteObjC:            Action = "RewriteObjC"; break;
00093 #endif
00094 #ifdef CLANG_ENABLE_ARCMT
00095   case MigrateSource:          return new arcmt::MigrateSourceAction();
00096 #else
00097   case MigrateSource:          Action = "MigrateSource"; break;
00098 #endif
00099 #ifdef CLANG_ENABLE_STATIC_ANALYZER
00100   case RunAnalysis:            return new ento::AnalysisAction();
00101 #else
00102   case RunAnalysis:            Action = "RunAnalysis"; break;
00103 #endif
00104   case RunPreprocessorOnly:    return new PreprocessOnlyAction();
00105   }
00106 
00107 #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
00108   || !defined(CLANG_ENABLE_OBJC_REWRITER)
00109   CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
00110   return 0;
00111 #else
00112   llvm_unreachable("Invalid program action!");
00113 #endif
00114 }
00115 
00116 static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
00117   // Create the underlying action.
00118   FrontendAction *Act = CreateFrontendBaseAction(CI);
00119   if (!Act)
00120     return nullptr;
00121 
00122   const FrontendOptions &FEOpts = CI.getFrontendOpts();
00123 
00124   if (FEOpts.FixAndRecompile) {
00125     Act = new FixItRecompile(Act);
00126   }
00127   
00128 #ifdef CLANG_ENABLE_ARCMT
00129   if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
00130       CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
00131     // Potentially wrap the base FE action in an ARC Migrate Tool action.
00132     switch (FEOpts.ARCMTAction) {
00133     case FrontendOptions::ARCMT_None:
00134       break;
00135     case FrontendOptions::ARCMT_Check:
00136       Act = new arcmt::CheckAction(Act);
00137       break;
00138     case FrontendOptions::ARCMT_Modify:
00139       Act = new arcmt::ModifyAction(Act);
00140       break;
00141     case FrontendOptions::ARCMT_Migrate:
00142       Act = new arcmt::MigrateAction(Act,
00143                                      FEOpts.MTMigrateDir,
00144                                      FEOpts.ARCMTMigrateReportOut,
00145                                      FEOpts.ARCMTMigrateEmitARCErrors);
00146       break;
00147     }
00148 
00149     if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
00150       Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir,
00151                                          FEOpts.ObjCMTAction);
00152     }
00153   }
00154 #endif
00155 
00156   // If there are any AST files to merge, create a frontend action
00157   // adaptor to perform the merge.
00158   if (!FEOpts.ASTMergeFiles.empty())
00159     Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles);
00160 
00161   return Act;
00162 }
00163 
00164 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
00165   // Honor -help.
00166   if (Clang->getFrontendOpts().ShowHelp) {
00167     std::unique_ptr<OptTable> Opts(driver::createDriverOptTable());
00168     Opts->PrintHelp(llvm::outs(), "clang -cc1",
00169                     "LLVM 'Clang' Compiler: http://clang.llvm.org",
00170                     /*Include=*/ driver::options::CC1Option, /*Exclude=*/ 0);
00171     return true;
00172   }
00173 
00174   // Honor -version.
00175   //
00176   // FIXME: Use a better -version message?
00177   if (Clang->getFrontendOpts().ShowVersion) {
00178     llvm::cl::PrintVersionMessage();
00179     return true;
00180   }
00181 
00182   // Load any requested plugins.
00183   for (unsigned i = 0,
00184          e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
00185     const std::string &Path = Clang->getFrontendOpts().Plugins[i];
00186     std::string Error;
00187     if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
00188       Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
00189         << Path << Error;
00190   }
00191 
00192   // Honor -mllvm.
00193   //
00194   // FIXME: Remove this, one day.
00195   // This should happen AFTER plugins have been loaded!
00196   if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
00197     unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
00198     auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
00199     Args[0] = "clang (LLVM option parsing)";
00200     for (unsigned i = 0; i != NumArgs; ++i)
00201       Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
00202     Args[NumArgs + 1] = nullptr;
00203     llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
00204   }
00205 
00206 #ifdef CLANG_ENABLE_STATIC_ANALYZER
00207   // Honor -analyzer-checker-help.
00208   // This should happen AFTER plugins have been loaded!
00209   if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
00210     ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
00211     return true;
00212   }
00213 #endif
00214 
00215   // If there were errors in processing arguments, don't do anything else.
00216   if (Clang->getDiagnostics().hasErrorOccurred())
00217     return false;
00218   // Create and execute the frontend action.
00219   std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
00220   if (!Act)
00221     return false;
00222   bool Success = Clang->ExecuteAction(*Act);
00223   if (Clang->getFrontendOpts().DisableFree)
00224     BuryPointer(std::move(Act));
00225   return Success;
00226 }