Home | History | Annotate | Download | only in FrontendTool
      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/FrontendTool/Utils.h"
     16 #include "clang/ARCMigrate/ARCMTActions.h"
     17 #include "clang/CodeGen/CodeGenAction.h"
     18 #include "clang/Driver/Options.h"
     19 #include "clang/Frontend/CompilerInstance.h"
     20 #include "clang/Frontend/CompilerInvocation.h"
     21 #include "clang/Frontend/FrontendActions.h"
     22 #include "clang/Frontend/FrontendDiagnostic.h"
     23 #include "clang/Frontend/FrontendPluginRegistry.h"
     24 #include "clang/Rewrite/Frontend/FrontendActions.h"
     25 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
     26 #include "llvm/Option/OptTable.h"
     27 #include "llvm/Option/Option.h"
     28 #include "llvm/Support/DynamicLibrary.h"
     29 #include "llvm/Support/ErrorHandling.h"
     30 using namespace clang;
     31 using namespace llvm::opt;
     32 
     33 static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
     34   using namespace clang::frontend;
     35   StringRef Action("unknown");
     36 
     37   switch (CI.getFrontendOpts().ProgramAction) {
     38   case ASTDeclList:            return new ASTDeclListAction();
     39   case ASTDump:                return new ASTDumpAction();
     40   case ASTDumpXML:             return new ASTDumpXMLAction();
     41   case ASTPrint:               return new ASTPrintAction();
     42   case ASTView:                return new ASTViewAction();
     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 #ifdef CLANG_ENABLE_REWRITER
     48   case EmitHTML:               return new HTMLPrintAction();
     49 #else
     50   case EmitHTML:               Action = "EmitHTML"; break;
     51 #endif
     52   case EmitLLVM:               return new EmitLLVMAction();
     53   case EmitLLVMOnly:           return new EmitLLVMOnlyAction();
     54   case EmitCodeGenOnly:        return new EmitCodeGenOnlyAction();
     55   case EmitObj:                return new EmitObjAction();
     56 #ifdef CLANG_ENABLE_REWRITER
     57   case FixIt:                  return new FixItAction();
     58 #else
     59   case FixIt:                  Action = "FixIt"; break;
     60 #endif
     61   case GenerateModule:         return new GenerateModuleAction;
     62   case GeneratePCH:            return new GeneratePCHAction;
     63   case GeneratePTH:            return new GeneratePTHAction();
     64   case InitOnly:               return new InitOnlyAction();
     65   case ParseSyntaxOnly:        return new SyntaxOnlyAction();
     66   case ModuleFileInfo:         return new DumpModuleInfoAction();
     67 
     68   case PluginAction: {
     69     for (FrontendPluginRegistry::iterator it =
     70            FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
     71          it != ie; ++it) {
     72       if (it->getName() == CI.getFrontendOpts().ActionName) {
     73         OwningPtr<PluginASTAction> P(it->instantiate());
     74         if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
     75           return 0;
     76         return P.take();
     77       }
     78     }
     79 
     80     CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
     81       << CI.getFrontendOpts().ActionName;
     82     return 0;
     83   }
     84 
     85   case PrintDeclContext:       return new DeclContextPrintAction();
     86   case PrintPreamble:          return new PrintPreambleAction();
     87   case PrintPreprocessedInput: {
     88     if (CI.getPreprocessorOutputOpts().RewriteIncludes) {
     89 #ifdef CLANG_ENABLE_REWRITER
     90       return new RewriteIncludesAction();
     91 #else
     92       Action = "RewriteIncludesAction";
     93       break;
     94 #endif
     95     }
     96     return new PrintPreprocessedAction();
     97   }
     98 
     99 #ifdef CLANG_ENABLE_REWRITER
    100   case RewriteMacros:          return new RewriteMacrosAction();
    101   case RewriteObjC:            return new RewriteObjCAction();
    102   case RewriteTest:            return new RewriteTestAction();
    103 #else
    104   case RewriteMacros:          Action = "RewriteMacros"; break;
    105   case RewriteObjC:            Action = "RewriteObjC"; break;
    106   case RewriteTest:            Action = "RewriteTest"; break;
    107 #endif
    108 #ifdef CLANG_ENABLE_ARCMT
    109   case MigrateSource:          return new arcmt::MigrateSourceAction();
    110 #else
    111   case MigrateSource:          Action = "MigrateSource"; break;
    112 #endif
    113 #ifdef CLANG_ENABLE_STATIC_ANALYZER
    114   case RunAnalysis:            return new ento::AnalysisAction();
    115 #else
    116   case RunAnalysis:            Action = "RunAnalysis"; break;
    117 #endif
    118   case RunPreprocessorOnly:    return new PreprocessOnlyAction();
    119   }
    120 
    121 #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
    122   || !defined(CLANG_ENABLE_REWRITER)
    123   CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
    124   return 0;
    125 #else
    126   llvm_unreachable("Invalid program action!");
    127 #endif
    128 }
    129 
    130 static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
    131   // Create the underlying action.
    132   FrontendAction *Act = CreateFrontendBaseAction(CI);
    133   if (!Act)
    134     return 0;
    135 
    136   const FrontendOptions &FEOpts = CI.getFrontendOpts();
    137 
    138 #ifdef CLANG_ENABLE_REWRITER
    139   if (FEOpts.FixAndRecompile) {
    140     Act = new FixItRecompile(Act);
    141   }
    142 #endif
    143 
    144 #ifdef CLANG_ENABLE_ARCMT
    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   }
    162 
    163   if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
    164     Act = new arcmt::ObjCMigrateAction(Act, FEOpts.MTMigrateDir,
    165                    FEOpts.ObjCMTAction & FrontendOptions::ObjCMT_Literals,
    166                    FEOpts.ObjCMTAction & FrontendOptions::ObjCMT_Subscripting,
    167                    FEOpts.ObjCMTAction & FrontendOptions::ObjCMT_Property);
    168   }
    169 #endif
    170 
    171   // If there are any AST files to merge, create a frontend action
    172   // adaptor to perform the merge.
    173   if (!FEOpts.ASTMergeFiles.empty())
    174     Act = new ASTMergeAction(Act, FEOpts.ASTMergeFiles);
    175 
    176   return Act;
    177 }
    178 
    179 bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
    180   // Honor -help.
    181   if (Clang->getFrontendOpts().ShowHelp) {
    182     OwningPtr<OptTable> Opts(driver::createDriverOptTable());
    183     Opts->PrintHelp(llvm::outs(), "clang -cc1",
    184                     "LLVM 'Clang' Compiler: http://clang.llvm.org",
    185                     /*Include=*/ driver::options::CC1Option, /*Exclude=*/ 0);
    186     return true;
    187   }
    188 
    189   // Honor -version.
    190   //
    191   // FIXME: Use a better -version message?
    192   if (Clang->getFrontendOpts().ShowVersion) {
    193     llvm::cl::PrintVersionMessage();
    194     return true;
    195   }
    196 
    197   // Load any requested plugins.
    198   for (unsigned i = 0,
    199          e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
    200     const std::string &Path = Clang->getFrontendOpts().Plugins[i];
    201     std::string Error;
    202     if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
    203       Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
    204         << Path << Error;
    205   }
    206 
    207   // Honor -mllvm.
    208   //
    209   // FIXME: Remove this, one day.
    210   // This should happen AFTER plugins have been loaded!
    211   if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
    212     unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
    213     const char **Args = new const char*[NumArgs + 2];
    214     Args[0] = "clang (LLVM option parsing)";
    215     for (unsigned i = 0; i != NumArgs; ++i)
    216       Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
    217     Args[NumArgs + 1] = 0;
    218     llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args);
    219   }
    220 
    221 #ifdef CLANG_ENABLE_STATIC_ANALYZER
    222   // Honor -analyzer-checker-help.
    223   // This should happen AFTER plugins have been loaded!
    224   if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
    225     ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
    226     return true;
    227   }
    228 #endif
    229 
    230   // If there were errors in processing arguments, don't do anything else.
    231   if (Clang->getDiagnostics().hasErrorOccurred())
    232     return false;
    233   // Create and execute the frontend action.
    234   OwningPtr<FrontendAction> Act(CreateFrontendAction(*Clang));
    235   if (!Act)
    236     return false;
    237   bool Success = Clang->ExecuteAction(*Act);
    238   if (Clang->getFrontendOpts().DisableFree)
    239     Act.take();
    240   return Success;
    241 }
    242