17330f729Sjoerg //===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file holds ExecuteCompilerInvocation(). It is split into its own file to
107330f729Sjoerg // minimize the impact of pulling in essentially everything else in Clang.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg
147330f729Sjoerg #include "clang/ARCMigrate/ARCMTActions.h"
157330f729Sjoerg #include "clang/CodeGen/CodeGenAction.h"
167330f729Sjoerg #include "clang/Config/config.h"
177330f729Sjoerg #include "clang/Driver/Options.h"
187330f729Sjoerg #include "clang/Frontend/CompilerInstance.h"
197330f729Sjoerg #include "clang/Frontend/CompilerInvocation.h"
207330f729Sjoerg #include "clang/Frontend/FrontendActions.h"
217330f729Sjoerg #include "clang/Frontend/FrontendDiagnostic.h"
227330f729Sjoerg #include "clang/Frontend/FrontendPluginRegistry.h"
237330f729Sjoerg #include "clang/Frontend/Utils.h"
247330f729Sjoerg #include "clang/FrontendTool/Utils.h"
257330f729Sjoerg #include "clang/Rewrite/Frontend/FrontendActions.h"
26*e038c9c4Sjoerg #include "clang/StaticAnalyzer/Frontend/AnalyzerHelpFlags.h"
277330f729Sjoerg #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
287330f729Sjoerg #include "llvm/Option/OptTable.h"
297330f729Sjoerg #include "llvm/Option/Option.h"
307330f729Sjoerg #include "llvm/Support/BuryPointer.h"
317330f729Sjoerg #include "llvm/Support/DynamicLibrary.h"
327330f729Sjoerg #include "llvm/Support/ErrorHandling.h"
337330f729Sjoerg using namespace clang;
347330f729Sjoerg using namespace llvm::opt;
357330f729Sjoerg
367330f729Sjoerg namespace clang {
377330f729Sjoerg
387330f729Sjoerg static std::unique_ptr<FrontendAction>
CreateFrontendBaseAction(CompilerInstance & CI)397330f729Sjoerg CreateFrontendBaseAction(CompilerInstance &CI) {
407330f729Sjoerg using namespace clang::frontend;
417330f729Sjoerg StringRef Action("unknown");
427330f729Sjoerg (void)Action;
437330f729Sjoerg
447330f729Sjoerg switch (CI.getFrontendOpts().ProgramAction) {
457330f729Sjoerg case ASTDeclList: return std::make_unique<ASTDeclListAction>();
467330f729Sjoerg case ASTDump: return std::make_unique<ASTDumpAction>();
477330f729Sjoerg case ASTPrint: return std::make_unique<ASTPrintAction>();
487330f729Sjoerg case ASTView: return std::make_unique<ASTViewAction>();
497330f729Sjoerg case DumpCompilerOptions:
507330f729Sjoerg return std::make_unique<DumpCompilerOptionsAction>();
517330f729Sjoerg case DumpRawTokens: return std::make_unique<DumpRawTokensAction>();
527330f729Sjoerg case DumpTokens: return std::make_unique<DumpTokensAction>();
537330f729Sjoerg case EmitAssembly: return std::make_unique<EmitAssemblyAction>();
547330f729Sjoerg case EmitBC: return std::make_unique<EmitBCAction>();
557330f729Sjoerg case EmitHTML: return std::make_unique<HTMLPrintAction>();
567330f729Sjoerg case EmitLLVM: return std::make_unique<EmitLLVMAction>();
577330f729Sjoerg case EmitLLVMOnly: return std::make_unique<EmitLLVMOnlyAction>();
587330f729Sjoerg case EmitCodeGenOnly: return std::make_unique<EmitCodeGenOnlyAction>();
597330f729Sjoerg case EmitObj: return std::make_unique<EmitObjAction>();
607330f729Sjoerg case FixIt: return std::make_unique<FixItAction>();
617330f729Sjoerg case GenerateModule:
627330f729Sjoerg return std::make_unique<GenerateModuleFromModuleMapAction>();
637330f729Sjoerg case GenerateModuleInterface:
647330f729Sjoerg return std::make_unique<GenerateModuleInterfaceAction>();
657330f729Sjoerg case GenerateHeaderModule:
667330f729Sjoerg return std::make_unique<GenerateHeaderModuleAction>();
677330f729Sjoerg case GeneratePCH: return std::make_unique<GeneratePCHAction>();
68*e038c9c4Sjoerg case GenerateInterfaceStubs:
69*e038c9c4Sjoerg return std::make_unique<GenerateInterfaceStubsAction>();
707330f729Sjoerg case InitOnly: return std::make_unique<InitOnlyAction>();
717330f729Sjoerg case ParseSyntaxOnly: return std::make_unique<SyntaxOnlyAction>();
727330f729Sjoerg case ModuleFileInfo: return std::make_unique<DumpModuleInfoAction>();
737330f729Sjoerg case VerifyPCH: return std::make_unique<VerifyPCHAction>();
747330f729Sjoerg case TemplightDump: return std::make_unique<TemplightDumpAction>();
757330f729Sjoerg
767330f729Sjoerg case PluginAction: {
77*e038c9c4Sjoerg for (const FrontendPluginRegistry::entry &Plugin :
78*e038c9c4Sjoerg FrontendPluginRegistry::entries()) {
79*e038c9c4Sjoerg if (Plugin.getName() == CI.getFrontendOpts().ActionName) {
80*e038c9c4Sjoerg std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
817330f729Sjoerg if ((P->getActionType() != PluginASTAction::ReplaceAction &&
827330f729Sjoerg P->getActionType() != PluginASTAction::Cmdline) ||
83*e038c9c4Sjoerg !P->ParseArgs(
84*e038c9c4Sjoerg CI,
85*e038c9c4Sjoerg CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())]))
867330f729Sjoerg return nullptr;
877330f729Sjoerg return std::move(P);
887330f729Sjoerg }
897330f729Sjoerg }
907330f729Sjoerg
917330f729Sjoerg CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
927330f729Sjoerg << CI.getFrontendOpts().ActionName;
937330f729Sjoerg return nullptr;
947330f729Sjoerg }
957330f729Sjoerg
967330f729Sjoerg case PrintPreamble: return std::make_unique<PrintPreambleAction>();
977330f729Sjoerg case PrintPreprocessedInput: {
987330f729Sjoerg if (CI.getPreprocessorOutputOpts().RewriteIncludes ||
997330f729Sjoerg CI.getPreprocessorOutputOpts().RewriteImports)
1007330f729Sjoerg return std::make_unique<RewriteIncludesAction>();
1017330f729Sjoerg return std::make_unique<PrintPreprocessedAction>();
1027330f729Sjoerg }
1037330f729Sjoerg
1047330f729Sjoerg case RewriteMacros: return std::make_unique<RewriteMacrosAction>();
1057330f729Sjoerg case RewriteTest: return std::make_unique<RewriteTestAction>();
1067330f729Sjoerg #if CLANG_ENABLE_OBJC_REWRITER
1077330f729Sjoerg case RewriteObjC: return std::make_unique<RewriteObjCAction>();
1087330f729Sjoerg #else
1097330f729Sjoerg case RewriteObjC: Action = "RewriteObjC"; break;
1107330f729Sjoerg #endif
1117330f729Sjoerg #if CLANG_ENABLE_ARCMT
1127330f729Sjoerg case MigrateSource:
1137330f729Sjoerg return std::make_unique<arcmt::MigrateSourceAction>();
1147330f729Sjoerg #else
1157330f729Sjoerg case MigrateSource: Action = "MigrateSource"; break;
1167330f729Sjoerg #endif
1177330f729Sjoerg #if CLANG_ENABLE_STATIC_ANALYZER
1187330f729Sjoerg case RunAnalysis: return std::make_unique<ento::AnalysisAction>();
1197330f729Sjoerg #else
1207330f729Sjoerg case RunAnalysis: Action = "RunAnalysis"; break;
1217330f729Sjoerg #endif
1227330f729Sjoerg case RunPreprocessorOnly: return std::make_unique<PreprocessOnlyAction>();
1237330f729Sjoerg case PrintDependencyDirectivesSourceMinimizerOutput:
1247330f729Sjoerg return std::make_unique<PrintDependencyDirectivesSourceMinimizerAction>();
1257330f729Sjoerg }
1267330f729Sjoerg
1277330f729Sjoerg #if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \
1287330f729Sjoerg || !CLANG_ENABLE_OBJC_REWRITER
1297330f729Sjoerg CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
1307330f729Sjoerg return 0;
1317330f729Sjoerg #else
1327330f729Sjoerg llvm_unreachable("Invalid program action!");
1337330f729Sjoerg #endif
1347330f729Sjoerg }
1357330f729Sjoerg
1367330f729Sjoerg std::unique_ptr<FrontendAction>
CreateFrontendAction(CompilerInstance & CI)1377330f729Sjoerg CreateFrontendAction(CompilerInstance &CI) {
1387330f729Sjoerg // Create the underlying action.
1397330f729Sjoerg std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
1407330f729Sjoerg if (!Act)
1417330f729Sjoerg return nullptr;
1427330f729Sjoerg
1437330f729Sjoerg const FrontendOptions &FEOpts = CI.getFrontendOpts();
1447330f729Sjoerg
1457330f729Sjoerg if (FEOpts.FixAndRecompile) {
1467330f729Sjoerg Act = std::make_unique<FixItRecompile>(std::move(Act));
1477330f729Sjoerg }
1487330f729Sjoerg
1497330f729Sjoerg #if CLANG_ENABLE_ARCMT
1507330f729Sjoerg if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
1517330f729Sjoerg CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
1527330f729Sjoerg // Potentially wrap the base FE action in an ARC Migrate Tool action.
1537330f729Sjoerg switch (FEOpts.ARCMTAction) {
1547330f729Sjoerg case FrontendOptions::ARCMT_None:
1557330f729Sjoerg break;
1567330f729Sjoerg case FrontendOptions::ARCMT_Check:
1577330f729Sjoerg Act = std::make_unique<arcmt::CheckAction>(std::move(Act));
1587330f729Sjoerg break;
1597330f729Sjoerg case FrontendOptions::ARCMT_Modify:
1607330f729Sjoerg Act = std::make_unique<arcmt::ModifyAction>(std::move(Act));
1617330f729Sjoerg break;
1627330f729Sjoerg case FrontendOptions::ARCMT_Migrate:
1637330f729Sjoerg Act = std::make_unique<arcmt::MigrateAction>(std::move(Act),
1647330f729Sjoerg FEOpts.MTMigrateDir,
1657330f729Sjoerg FEOpts.ARCMTMigrateReportOut,
1667330f729Sjoerg FEOpts.ARCMTMigrateEmitARCErrors);
1677330f729Sjoerg break;
1687330f729Sjoerg }
1697330f729Sjoerg
1707330f729Sjoerg if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
1717330f729Sjoerg Act = std::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
1727330f729Sjoerg FEOpts.MTMigrateDir,
1737330f729Sjoerg FEOpts.ObjCMTAction);
1747330f729Sjoerg }
1757330f729Sjoerg }
1767330f729Sjoerg #endif
1777330f729Sjoerg
1787330f729Sjoerg // If there are any AST files to merge, create a frontend action
1797330f729Sjoerg // adaptor to perform the merge.
1807330f729Sjoerg if (!FEOpts.ASTMergeFiles.empty())
1817330f729Sjoerg Act = std::make_unique<ASTMergeAction>(std::move(Act),
1827330f729Sjoerg FEOpts.ASTMergeFiles);
1837330f729Sjoerg
1847330f729Sjoerg return Act;
1857330f729Sjoerg }
1867330f729Sjoerg
ExecuteCompilerInvocation(CompilerInstance * Clang)1877330f729Sjoerg bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
1887330f729Sjoerg // Honor -help.
1897330f729Sjoerg if (Clang->getFrontendOpts().ShowHelp) {
1907330f729Sjoerg driver::getDriverOptTable().PrintHelp(
1917330f729Sjoerg llvm::outs(), "clang -cc1 [options] file...",
1927330f729Sjoerg "LLVM 'Clang' Compiler: http://clang.llvm.org",
1937330f729Sjoerg /*Include=*/driver::options::CC1Option,
1947330f729Sjoerg /*Exclude=*/0, /*ShowAllAliases=*/false);
1957330f729Sjoerg return true;
1967330f729Sjoerg }
1977330f729Sjoerg
1987330f729Sjoerg // Honor -version.
1997330f729Sjoerg //
2007330f729Sjoerg // FIXME: Use a better -version message?
2017330f729Sjoerg if (Clang->getFrontendOpts().ShowVersion) {
2027330f729Sjoerg llvm::cl::PrintVersionMessage();
2037330f729Sjoerg return true;
2047330f729Sjoerg }
2057330f729Sjoerg
2067330f729Sjoerg // Load any requested plugins.
207*e038c9c4Sjoerg for (const std::string &Path : Clang->getFrontendOpts().Plugins) {
2087330f729Sjoerg std::string Error;
2097330f729Sjoerg if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
2107330f729Sjoerg Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
2117330f729Sjoerg << Path << Error;
2127330f729Sjoerg }
2137330f729Sjoerg
2147330f729Sjoerg // Check if any of the loaded plugins replaces the main AST action
215*e038c9c4Sjoerg for (const FrontendPluginRegistry::entry &Plugin :
216*e038c9c4Sjoerg FrontendPluginRegistry::entries()) {
217*e038c9c4Sjoerg std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
2187330f729Sjoerg if (P->getActionType() == PluginASTAction::ReplaceAction) {
2197330f729Sjoerg Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
220*e038c9c4Sjoerg Clang->getFrontendOpts().ActionName = Plugin.getName().str();
2217330f729Sjoerg break;
2227330f729Sjoerg }
2237330f729Sjoerg }
2247330f729Sjoerg
2257330f729Sjoerg // Honor -mllvm.
2267330f729Sjoerg //
2277330f729Sjoerg // FIXME: Remove this, one day.
2287330f729Sjoerg // This should happen AFTER plugins have been loaded!
2297330f729Sjoerg if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
2307330f729Sjoerg unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
2317330f729Sjoerg auto Args = std::make_unique<const char*[]>(NumArgs + 2);
2327330f729Sjoerg Args[0] = "clang (LLVM option parsing)";
2337330f729Sjoerg for (unsigned i = 0; i != NumArgs; ++i)
2347330f729Sjoerg Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
2357330f729Sjoerg Args[NumArgs + 1] = nullptr;
2367330f729Sjoerg llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
2377330f729Sjoerg }
2387330f729Sjoerg
2397330f729Sjoerg #if CLANG_ENABLE_STATIC_ANALYZER
2407330f729Sjoerg // These should happen AFTER plugins have been loaded!
2417330f729Sjoerg
2427330f729Sjoerg AnalyzerOptions &AnOpts = *Clang->getAnalyzerOpts();
243*e038c9c4Sjoerg
2447330f729Sjoerg // Honor -analyzer-checker-help and -analyzer-checker-help-hidden.
2457330f729Sjoerg if (AnOpts.ShowCheckerHelp || AnOpts.ShowCheckerHelpAlpha ||
2467330f729Sjoerg AnOpts.ShowCheckerHelpDeveloper) {
247*e038c9c4Sjoerg ento::printCheckerHelp(llvm::outs(), *Clang);
2487330f729Sjoerg return true;
2497330f729Sjoerg }
2507330f729Sjoerg
2517330f729Sjoerg // Honor -analyzer-checker-option-help.
2527330f729Sjoerg if (AnOpts.ShowCheckerOptionList || AnOpts.ShowCheckerOptionAlphaList ||
2537330f729Sjoerg AnOpts.ShowCheckerOptionDeveloperList) {
254*e038c9c4Sjoerg ento::printCheckerConfigList(llvm::outs(), *Clang);
2557330f729Sjoerg return true;
2567330f729Sjoerg }
2577330f729Sjoerg
2587330f729Sjoerg // Honor -analyzer-list-enabled-checkers.
2597330f729Sjoerg if (AnOpts.ShowEnabledCheckerList) {
260*e038c9c4Sjoerg ento::printEnabledCheckerList(llvm::outs(), *Clang);
2617330f729Sjoerg return true;
2627330f729Sjoerg }
2637330f729Sjoerg
2647330f729Sjoerg // Honor -analyzer-config-help.
2657330f729Sjoerg if (AnOpts.ShowConfigOptionsList) {
2667330f729Sjoerg ento::printAnalyzerConfigList(llvm::outs());
2677330f729Sjoerg return true;
2687330f729Sjoerg }
2697330f729Sjoerg #endif
2707330f729Sjoerg
2717330f729Sjoerg // If there were errors in processing arguments, don't do anything else.
2727330f729Sjoerg if (Clang->getDiagnostics().hasErrorOccurred())
2737330f729Sjoerg return false;
2747330f729Sjoerg // Create and execute the frontend action.
2757330f729Sjoerg std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
2767330f729Sjoerg if (!Act)
2777330f729Sjoerg return false;
2787330f729Sjoerg bool Success = Clang->ExecuteAction(*Act);
2797330f729Sjoerg if (Clang->getFrontendOpts().DisableFree)
2807330f729Sjoerg llvm::BuryPointer(std::move(Act));
2817330f729Sjoerg return Success;
2827330f729Sjoerg }
2837330f729Sjoerg
2847330f729Sjoerg } // namespace clang
285