Home | History | Annotate | Line # | Download | only in FrontendTool
ExecuteCompilerInvocation.cpp revision 1.1.1.1
      1  1.1  joerg //===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
      2  1.1  joerg //
      3  1.1  joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4  1.1  joerg // See https://llvm.org/LICENSE.txt for license information.
      5  1.1  joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6  1.1  joerg //
      7  1.1  joerg //===----------------------------------------------------------------------===//
      8  1.1  joerg //
      9  1.1  joerg // This file holds ExecuteCompilerInvocation(). It is split into its own file to
     10  1.1  joerg // minimize the impact of pulling in essentially everything else in Clang.
     11  1.1  joerg //
     12  1.1  joerg //===----------------------------------------------------------------------===//
     13  1.1  joerg 
     14  1.1  joerg #include "clang/ARCMigrate/ARCMTActions.h"
     15  1.1  joerg #include "clang/CodeGen/CodeGenAction.h"
     16  1.1  joerg #include "clang/Config/config.h"
     17  1.1  joerg #include "clang/Driver/Options.h"
     18  1.1  joerg #include "clang/Frontend/CompilerInstance.h"
     19  1.1  joerg #include "clang/Frontend/CompilerInvocation.h"
     20  1.1  joerg #include "clang/Frontend/FrontendActions.h"
     21  1.1  joerg #include "clang/Frontend/FrontendDiagnostic.h"
     22  1.1  joerg #include "clang/Frontend/FrontendPluginRegistry.h"
     23  1.1  joerg #include "clang/Frontend/Utils.h"
     24  1.1  joerg #include "clang/FrontendTool/Utils.h"
     25  1.1  joerg #include "clang/Rewrite/Frontend/FrontendActions.h"
     26  1.1  joerg #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
     27  1.1  joerg #include "llvm/Option/OptTable.h"
     28  1.1  joerg #include "llvm/Option/Option.h"
     29  1.1  joerg #include "llvm/Support/BuryPointer.h"
     30  1.1  joerg #include "llvm/Support/DynamicLibrary.h"
     31  1.1  joerg #include "llvm/Support/ErrorHandling.h"
     32  1.1  joerg using namespace clang;
     33  1.1  joerg using namespace llvm::opt;
     34  1.1  joerg 
     35  1.1  joerg namespace clang {
     36  1.1  joerg 
     37  1.1  joerg static std::unique_ptr<FrontendAction>
     38  1.1  joerg CreateFrontendBaseAction(CompilerInstance &CI) {
     39  1.1  joerg   using namespace clang::frontend;
     40  1.1  joerg   StringRef Action("unknown");
     41  1.1  joerg   (void)Action;
     42  1.1  joerg 
     43  1.1  joerg   switch (CI.getFrontendOpts().ProgramAction) {
     44  1.1  joerg   case ASTDeclList:            return std::make_unique<ASTDeclListAction>();
     45  1.1  joerg   case ASTDump:                return std::make_unique<ASTDumpAction>();
     46  1.1  joerg   case ASTPrint:               return std::make_unique<ASTPrintAction>();
     47  1.1  joerg   case ASTView:                return std::make_unique<ASTViewAction>();
     48  1.1  joerg   case DumpCompilerOptions:
     49  1.1  joerg     return std::make_unique<DumpCompilerOptionsAction>();
     50  1.1  joerg   case DumpRawTokens:          return std::make_unique<DumpRawTokensAction>();
     51  1.1  joerg   case DumpTokens:             return std::make_unique<DumpTokensAction>();
     52  1.1  joerg   case EmitAssembly:           return std::make_unique<EmitAssemblyAction>();
     53  1.1  joerg   case EmitBC:                 return std::make_unique<EmitBCAction>();
     54  1.1  joerg   case EmitHTML:               return std::make_unique<HTMLPrintAction>();
     55  1.1  joerg   case EmitLLVM:               return std::make_unique<EmitLLVMAction>();
     56  1.1  joerg   case EmitLLVMOnly:           return std::make_unique<EmitLLVMOnlyAction>();
     57  1.1  joerg   case EmitCodeGenOnly:        return std::make_unique<EmitCodeGenOnlyAction>();
     58  1.1  joerg   case EmitObj:                return std::make_unique<EmitObjAction>();
     59  1.1  joerg   case FixIt:                  return std::make_unique<FixItAction>();
     60  1.1  joerg   case GenerateModule:
     61  1.1  joerg     return std::make_unique<GenerateModuleFromModuleMapAction>();
     62  1.1  joerg   case GenerateModuleInterface:
     63  1.1  joerg     return std::make_unique<GenerateModuleInterfaceAction>();
     64  1.1  joerg   case GenerateHeaderModule:
     65  1.1  joerg     return std::make_unique<GenerateHeaderModuleAction>();
     66  1.1  joerg   case GeneratePCH:            return std::make_unique<GeneratePCHAction>();
     67  1.1  joerg   case GenerateInterfaceIfsExpV1:
     68  1.1  joerg     return std::make_unique<GenerateInterfaceIfsExpV1Action>();
     69  1.1  joerg   case InitOnly:               return std::make_unique<InitOnlyAction>();
     70  1.1  joerg   case ParseSyntaxOnly:        return std::make_unique<SyntaxOnlyAction>();
     71  1.1  joerg   case ModuleFileInfo:         return std::make_unique<DumpModuleInfoAction>();
     72  1.1  joerg   case VerifyPCH:              return std::make_unique<VerifyPCHAction>();
     73  1.1  joerg   case TemplightDump:          return std::make_unique<TemplightDumpAction>();
     74  1.1  joerg 
     75  1.1  joerg   case PluginAction: {
     76  1.1  joerg     for (FrontendPluginRegistry::iterator it =
     77  1.1  joerg            FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
     78  1.1  joerg          it != ie; ++it) {
     79  1.1  joerg       if (it->getName() == CI.getFrontendOpts().ActionName) {
     80  1.1  joerg         std::unique_ptr<PluginASTAction> P(it->instantiate());
     81  1.1  joerg         if ((P->getActionType() != PluginASTAction::ReplaceAction &&
     82  1.1  joerg              P->getActionType() != PluginASTAction::Cmdline) ||
     83  1.1  joerg             !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
     84  1.1  joerg           return nullptr;
     85  1.1  joerg         return std::move(P);
     86  1.1  joerg       }
     87  1.1  joerg     }
     88  1.1  joerg 
     89  1.1  joerg     CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
     90  1.1  joerg       << CI.getFrontendOpts().ActionName;
     91  1.1  joerg     return nullptr;
     92  1.1  joerg   }
     93  1.1  joerg 
     94  1.1  joerg   case PrintPreamble:          return std::make_unique<PrintPreambleAction>();
     95  1.1  joerg   case PrintPreprocessedInput: {
     96  1.1  joerg     if (CI.getPreprocessorOutputOpts().RewriteIncludes ||
     97  1.1  joerg         CI.getPreprocessorOutputOpts().RewriteImports)
     98  1.1  joerg       return std::make_unique<RewriteIncludesAction>();
     99  1.1  joerg     return std::make_unique<PrintPreprocessedAction>();
    100  1.1  joerg   }
    101  1.1  joerg 
    102  1.1  joerg   case RewriteMacros:          return std::make_unique<RewriteMacrosAction>();
    103  1.1  joerg   case RewriteTest:            return std::make_unique<RewriteTestAction>();
    104  1.1  joerg #if CLANG_ENABLE_OBJC_REWRITER
    105  1.1  joerg   case RewriteObjC:            return std::make_unique<RewriteObjCAction>();
    106  1.1  joerg #else
    107  1.1  joerg   case RewriteObjC:            Action = "RewriteObjC"; break;
    108  1.1  joerg #endif
    109  1.1  joerg #if CLANG_ENABLE_ARCMT
    110  1.1  joerg   case MigrateSource:
    111  1.1  joerg     return std::make_unique<arcmt::MigrateSourceAction>();
    112  1.1  joerg #else
    113  1.1  joerg   case MigrateSource:          Action = "MigrateSource"; break;
    114  1.1  joerg #endif
    115  1.1  joerg #if CLANG_ENABLE_STATIC_ANALYZER
    116  1.1  joerg   case RunAnalysis:            return std::make_unique<ento::AnalysisAction>();
    117  1.1  joerg #else
    118  1.1  joerg   case RunAnalysis:            Action = "RunAnalysis"; break;
    119  1.1  joerg #endif
    120  1.1  joerg   case RunPreprocessorOnly:    return std::make_unique<PreprocessOnlyAction>();
    121  1.1  joerg   case PrintDependencyDirectivesSourceMinimizerOutput:
    122  1.1  joerg     return std::make_unique<PrintDependencyDirectivesSourceMinimizerAction>();
    123  1.1  joerg   }
    124  1.1  joerg 
    125  1.1  joerg #if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \
    126  1.1  joerg   || !CLANG_ENABLE_OBJC_REWRITER
    127  1.1  joerg   CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
    128  1.1  joerg   return 0;
    129  1.1  joerg #else
    130  1.1  joerg   llvm_unreachable("Invalid program action!");
    131  1.1  joerg #endif
    132  1.1  joerg }
    133  1.1  joerg 
    134  1.1  joerg std::unique_ptr<FrontendAction>
    135  1.1  joerg CreateFrontendAction(CompilerInstance &CI) {
    136  1.1  joerg   // Create the underlying action.
    137  1.1  joerg   std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
    138  1.1  joerg   if (!Act)
    139  1.1  joerg     return nullptr;
    140  1.1  joerg 
    141  1.1  joerg   const FrontendOptions &FEOpts = CI.getFrontendOpts();
    142  1.1  joerg 
    143  1.1  joerg   if (FEOpts.FixAndRecompile) {
    144  1.1  joerg     Act = std::make_unique<FixItRecompile>(std::move(Act));
    145  1.1  joerg   }
    146  1.1  joerg 
    147  1.1  joerg #if CLANG_ENABLE_ARCMT
    148  1.1  joerg   if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
    149  1.1  joerg       CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
    150  1.1  joerg     // Potentially wrap the base FE action in an ARC Migrate Tool action.
    151  1.1  joerg     switch (FEOpts.ARCMTAction) {
    152  1.1  joerg     case FrontendOptions::ARCMT_None:
    153  1.1  joerg       break;
    154  1.1  joerg     case FrontendOptions::ARCMT_Check:
    155  1.1  joerg       Act = std::make_unique<arcmt::CheckAction>(std::move(Act));
    156  1.1  joerg       break;
    157  1.1  joerg     case FrontendOptions::ARCMT_Modify:
    158  1.1  joerg       Act = std::make_unique<arcmt::ModifyAction>(std::move(Act));
    159  1.1  joerg       break;
    160  1.1  joerg     case FrontendOptions::ARCMT_Migrate:
    161  1.1  joerg       Act = std::make_unique<arcmt::MigrateAction>(std::move(Act),
    162  1.1  joerg                                      FEOpts.MTMigrateDir,
    163  1.1  joerg                                      FEOpts.ARCMTMigrateReportOut,
    164  1.1  joerg                                      FEOpts.ARCMTMigrateEmitARCErrors);
    165  1.1  joerg       break;
    166  1.1  joerg     }
    167  1.1  joerg 
    168  1.1  joerg     if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
    169  1.1  joerg       Act = std::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
    170  1.1  joerg                                                         FEOpts.MTMigrateDir,
    171  1.1  joerg                                                         FEOpts.ObjCMTAction);
    172  1.1  joerg     }
    173  1.1  joerg   }
    174  1.1  joerg #endif
    175  1.1  joerg 
    176  1.1  joerg   // If there are any AST files to merge, create a frontend action
    177  1.1  joerg   // adaptor to perform the merge.
    178  1.1  joerg   if (!FEOpts.ASTMergeFiles.empty())
    179  1.1  joerg     Act = std::make_unique<ASTMergeAction>(std::move(Act),
    180  1.1  joerg                                             FEOpts.ASTMergeFiles);
    181  1.1  joerg 
    182  1.1  joerg   return Act;
    183  1.1  joerg }
    184  1.1  joerg 
    185  1.1  joerg bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
    186  1.1  joerg   // Honor -help.
    187  1.1  joerg   if (Clang->getFrontendOpts().ShowHelp) {
    188  1.1  joerg     driver::getDriverOptTable().PrintHelp(
    189  1.1  joerg         llvm::outs(), "clang -cc1 [options] file...",
    190  1.1  joerg         "LLVM 'Clang' Compiler: http://clang.llvm.org",
    191  1.1  joerg         /*Include=*/driver::options::CC1Option,
    192  1.1  joerg         /*Exclude=*/0, /*ShowAllAliases=*/false);
    193  1.1  joerg     return true;
    194  1.1  joerg   }
    195  1.1  joerg 
    196  1.1  joerg   // Honor -version.
    197  1.1  joerg   //
    198  1.1  joerg   // FIXME: Use a better -version message?
    199  1.1  joerg   if (Clang->getFrontendOpts().ShowVersion) {
    200  1.1  joerg     llvm::cl::PrintVersionMessage();
    201  1.1  joerg     return true;
    202  1.1  joerg   }
    203  1.1  joerg 
    204  1.1  joerg   // Load any requested plugins.
    205  1.1  joerg   for (unsigned i = 0,
    206  1.1  joerg          e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
    207  1.1  joerg     const std::string &Path = Clang->getFrontendOpts().Plugins[i];
    208  1.1  joerg     std::string Error;
    209  1.1  joerg     if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
    210  1.1  joerg       Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
    211  1.1  joerg         << Path << Error;
    212  1.1  joerg   }
    213  1.1  joerg 
    214  1.1  joerg   // Check if any of the loaded plugins replaces the main AST action
    215  1.1  joerg   for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
    216  1.1  joerg                                         ie = FrontendPluginRegistry::end();
    217  1.1  joerg        it != ie; ++it) {
    218  1.1  joerg     std::unique_ptr<PluginASTAction> P(it->instantiate());
    219  1.1  joerg     if (P->getActionType() == PluginASTAction::ReplaceAction) {
    220  1.1  joerg       Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
    221  1.1  joerg       Clang->getFrontendOpts().ActionName = it->getName();
    222  1.1  joerg       break;
    223  1.1  joerg     }
    224  1.1  joerg   }
    225  1.1  joerg 
    226  1.1  joerg   // Honor -mllvm.
    227  1.1  joerg   //
    228  1.1  joerg   // FIXME: Remove this, one day.
    229  1.1  joerg   // This should happen AFTER plugins have been loaded!
    230  1.1  joerg   if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
    231  1.1  joerg     unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
    232  1.1  joerg     auto Args = std::make_unique<const char*[]>(NumArgs + 2);
    233  1.1  joerg     Args[0] = "clang (LLVM option parsing)";
    234  1.1  joerg     for (unsigned i = 0; i != NumArgs; ++i)
    235  1.1  joerg       Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
    236  1.1  joerg     Args[NumArgs + 1] = nullptr;
    237  1.1  joerg     llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
    238  1.1  joerg   }
    239  1.1  joerg 
    240  1.1  joerg #if CLANG_ENABLE_STATIC_ANALYZER
    241  1.1  joerg   // These should happen AFTER plugins have been loaded!
    242  1.1  joerg 
    243  1.1  joerg   AnalyzerOptions &AnOpts = *Clang->getAnalyzerOpts();
    244  1.1  joerg   // Honor -analyzer-checker-help and -analyzer-checker-help-hidden.
    245  1.1  joerg   if (AnOpts.ShowCheckerHelp || AnOpts.ShowCheckerHelpAlpha ||
    246  1.1  joerg       AnOpts.ShowCheckerHelpDeveloper) {
    247  1.1  joerg     ento::printCheckerHelp(llvm::outs(),
    248  1.1  joerg                            Clang->getFrontendOpts().Plugins,
    249  1.1  joerg                            AnOpts,
    250  1.1  joerg                            Clang->getDiagnostics(),
    251  1.1  joerg                            Clang->getLangOpts());
    252  1.1  joerg     return true;
    253  1.1  joerg   }
    254  1.1  joerg 
    255  1.1  joerg   // Honor -analyzer-checker-option-help.
    256  1.1  joerg   if (AnOpts.ShowCheckerOptionList || AnOpts.ShowCheckerOptionAlphaList ||
    257  1.1  joerg       AnOpts.ShowCheckerOptionDeveloperList) {
    258  1.1  joerg     ento::printCheckerConfigList(llvm::outs(),
    259  1.1  joerg                                  Clang->getFrontendOpts().Plugins,
    260  1.1  joerg                                  *Clang->getAnalyzerOpts(),
    261  1.1  joerg                                  Clang->getDiagnostics(),
    262  1.1  joerg                                  Clang->getLangOpts());
    263  1.1  joerg     return true;
    264  1.1  joerg   }
    265  1.1  joerg 
    266  1.1  joerg   // Honor -analyzer-list-enabled-checkers.
    267  1.1  joerg   if (AnOpts.ShowEnabledCheckerList) {
    268  1.1  joerg     ento::printEnabledCheckerList(llvm::outs(),
    269  1.1  joerg                                   Clang->getFrontendOpts().Plugins,
    270  1.1  joerg                                   AnOpts,
    271  1.1  joerg                                   Clang->getDiagnostics(),
    272  1.1  joerg                                   Clang->getLangOpts());
    273  1.1  joerg     return true;
    274  1.1  joerg   }
    275  1.1  joerg 
    276  1.1  joerg   // Honor -analyzer-config-help.
    277  1.1  joerg   if (AnOpts.ShowConfigOptionsList) {
    278  1.1  joerg     ento::printAnalyzerConfigList(llvm::outs());
    279  1.1  joerg     return true;
    280  1.1  joerg   }
    281  1.1  joerg #endif
    282  1.1  joerg 
    283  1.1  joerg   // If there were errors in processing arguments, don't do anything else.
    284  1.1  joerg   if (Clang->getDiagnostics().hasErrorOccurred())
    285  1.1  joerg     return false;
    286  1.1  joerg   // Create and execute the frontend action.
    287  1.1  joerg   std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
    288  1.1  joerg   if (!Act)
    289  1.1  joerg     return false;
    290  1.1  joerg   bool Success = Clang->ExecuteAction(*Act);
    291  1.1  joerg   if (Clang->getFrontendOpts().DisableFree)
    292  1.1  joerg     llvm::BuryPointer(std::move(Act));
    293  1.1  joerg   return Success;
    294  1.1  joerg }
    295  1.1  joerg 
    296  1.1  joerg } // namespace clang
    297