185dd0bd1SPeter Collingbourne //===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
285dd0bd1SPeter Collingbourne //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
685dd0bd1SPeter Collingbourne //
785dd0bd1SPeter Collingbourne //===----------------------------------------------------------------------===//
885dd0bd1SPeter Collingbourne //
985dd0bd1SPeter Collingbourne // This file holds ExecuteCompilerInvocation(). It is split into its own file to
1085dd0bd1SPeter Collingbourne // minimize the impact of pulling in essentially everything else in Clang.
1185dd0bd1SPeter Collingbourne //
1285dd0bd1SPeter Collingbourne //===----------------------------------------------------------------------===//
1385dd0bd1SPeter Collingbourne 
14b5703510SChandler Carruth #include "clang/ARCMigrate/ARCMTActions.h"
1585dd0bd1SPeter Collingbourne #include "clang/CodeGen/CodeGenAction.h"
167f633dfeSNAKAMURA Takumi #include "clang/Config/config.h"
17a3c85b86SJames Molloy #include "clang/Driver/Options.h"
1889f6b26fSZixu Wang #include "clang/ExtractAPI/FrontendActions.h"
1985dd0bd1SPeter Collingbourne #include "clang/Frontend/CompilerInstance.h"
203a02247dSChandler Carruth #include "clang/Frontend/CompilerInvocation.h"
2185dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendActions.h"
2285dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendDiagnostic.h"
2385dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendPluginRegistry.h"
24ce2c726eSKostya Serebryany #include "clang/Frontend/Utils.h"
259941da41SDavid Blaikie #include "clang/FrontendTool/Utils.h"
26cdf81490STed Kremenek #include "clang/Rewrite/Frontend/FrontendActions.h"
272aac0c47SKristóf Umann #include "clang/StaticAnalyzer/Frontend/AnalyzerHelpFlags.h"
283a02247dSChandler Carruth #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
29898229abSReid Kleckner #include "llvm/Option/OptTable.h"
30898229abSReid Kleckner #include "llvm/Option/Option.h"
319941da41SDavid Blaikie #include "llvm/Support/BuryPointer.h"
328aaf4995SMichael J. Spencer #include "llvm/Support/DynamicLibrary.h"
333a02247dSChandler Carruth #include "llvm/Support/ErrorHandling.h"
3485dd0bd1SPeter Collingbourne using namespace clang;
35898229abSReid Kleckner using namespace llvm::opt;
3685dd0bd1SPeter Collingbourne 
37207e7b1fSGabor Horvath namespace clang {
38207e7b1fSGabor Horvath 
39d35e98faSArgyrios Kyrtzidis static std::unique_ptr<FrontendAction>
CreateFrontendBaseAction(CompilerInstance & CI)40d35e98faSArgyrios Kyrtzidis CreateFrontendBaseAction(CompilerInstance &CI) {
4185dd0bd1SPeter Collingbourne   using namespace clang::frontend;
42ba294d73SAlp Toker   StringRef Action("unknown");
43ba294d73SAlp Toker   (void)Action;
4485dd0bd1SPeter Collingbourne 
4585dd0bd1SPeter Collingbourne   switch (CI.getFrontendOpts().ProgramAction) {
462b3d49b6SJonas Devlieghere   case ASTDeclList:            return std::make_unique<ASTDeclListAction>();
472b3d49b6SJonas Devlieghere   case ASTDump:                return std::make_unique<ASTDumpAction>();
482b3d49b6SJonas Devlieghere   case ASTPrint:               return std::make_unique<ASTPrintAction>();
492b3d49b6SJonas Devlieghere   case ASTView:                return std::make_unique<ASTViewAction>();
5016ed8dd8SAaron Ballman   case DumpCompilerOptions:
512b3d49b6SJonas Devlieghere     return std::make_unique<DumpCompilerOptionsAction>();
522b3d49b6SJonas Devlieghere   case DumpRawTokens:          return std::make_unique<DumpRawTokensAction>();
532b3d49b6SJonas Devlieghere   case DumpTokens:             return std::make_unique<DumpTokensAction>();
542b3d49b6SJonas Devlieghere   case EmitAssembly:           return std::make_unique<EmitAssemblyAction>();
552b3d49b6SJonas Devlieghere   case EmitBC:                 return std::make_unique<EmitBCAction>();
562b3d49b6SJonas Devlieghere   case EmitHTML:               return std::make_unique<HTMLPrintAction>();
572b3d49b6SJonas Devlieghere   case EmitLLVM:               return std::make_unique<EmitLLVMAction>();
582b3d49b6SJonas Devlieghere   case EmitLLVMOnly:           return std::make_unique<EmitLLVMOnlyAction>();
592b3d49b6SJonas Devlieghere   case EmitCodeGenOnly:        return std::make_unique<EmitCodeGenOnlyAction>();
602b3d49b6SJonas Devlieghere   case EmitObj:                return std::make_unique<EmitObjAction>();
61b1d946cbSZixu Wang   case ExtractAPI:
62b1d946cbSZixu Wang     return std::make_unique<ExtractAPIAction>();
632b3d49b6SJonas Devlieghere   case FixIt:                  return std::make_unique<FixItAction>();
64bbcc9f04SRichard Smith   case GenerateModule:
652b3d49b6SJonas Devlieghere     return std::make_unique<GenerateModuleFromModuleMapAction>();
66bbcc9f04SRichard Smith   case GenerateModuleInterface:
672b3d49b6SJonas Devlieghere     return std::make_unique<GenerateModuleInterfaceAction>();
68d6509cf2SRichard Smith   case GenerateHeaderModule:
692b3d49b6SJonas Devlieghere     return std::make_unique<GenerateHeaderModuleAction>();
70*6c0e60e8SIain Sandoe   case GenerateHeaderUnit:
71*6c0e60e8SIain Sandoe     return std::make_unique<GenerateHeaderUnitAction>();
722b3d49b6SJonas Devlieghere   case GeneratePCH:            return std::make_unique<GeneratePCHAction>();
73e3033c0cSPuyan Lotfi   case GenerateInterfaceStubs:
74e3033c0cSPuyan Lotfi     return std::make_unique<GenerateInterfaceStubsAction>();
752b3d49b6SJonas Devlieghere   case InitOnly:               return std::make_unique<InitOnlyAction>();
762b3d49b6SJonas Devlieghere   case ParseSyntaxOnly:        return std::make_unique<SyntaxOnlyAction>();
772b3d49b6SJonas Devlieghere   case ModuleFileInfo:         return std::make_unique<DumpModuleInfoAction>();
782b3d49b6SJonas Devlieghere   case VerifyPCH:              return std::make_unique<VerifyPCHAction>();
792b3d49b6SJonas Devlieghere   case TemplightDump:          return std::make_unique<TemplightDumpAction>();
8085dd0bd1SPeter Collingbourne 
8185dd0bd1SPeter Collingbourne   case PluginAction: {
828b0df1c1SNathan James     for (const FrontendPluginRegistry::entry &Plugin :
838b0df1c1SNathan James          FrontendPluginRegistry::entries()) {
848b0df1c1SNathan James       if (Plugin.getName() == CI.getFrontendOpts().ActionName) {
858b0df1c1SNathan James         std::unique_ptr<PluginASTAction> P(Plugin.instantiate());
866c78974bSJohn Brawn         if ((P->getActionType() != PluginASTAction::ReplaceAction &&
87fe66433fSArthur Eubanks              P->getActionType() != PluginASTAction::CmdlineAfterMainAction) ||
88adcd0268SBenjamin Kramer             !P->ParseArgs(
89adcd0268SBenjamin Kramer                 CI,
908b0df1c1SNathan James                 CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())]))
91236bde3dSCraig Topper           return nullptr;
92d35e98faSArgyrios Kyrtzidis         return std::move(P);
9385dd0bd1SPeter Collingbourne       }
9485dd0bd1SPeter Collingbourne     }
9585dd0bd1SPeter Collingbourne 
9685dd0bd1SPeter Collingbourne     CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
9785dd0bd1SPeter Collingbourne       << CI.getFrontendOpts().ActionName;
98236bde3dSCraig Topper     return nullptr;
9985dd0bd1SPeter Collingbourne   }
10085dd0bd1SPeter Collingbourne 
1012b3d49b6SJonas Devlieghere   case PrintPreamble:          return std::make_unique<PrintPreambleAction>();
102e993e4cdSDavid Blaikie   case PrintPreprocessedInput: {
10386a3ef5bSRichard Smith     if (CI.getPreprocessorOutputOpts().RewriteIncludes ||
10486a3ef5bSRichard Smith         CI.getPreprocessorOutputOpts().RewriteImports)
1052b3d49b6SJonas Devlieghere       return std::make_unique<RewriteIncludesAction>();
1062b3d49b6SJonas Devlieghere     return std::make_unique<PrintPreprocessedAction>();
107e993e4cdSDavid Blaikie   }
108e993e4cdSDavid Blaikie 
1092b3d49b6SJonas Devlieghere   case RewriteMacros:          return std::make_unique<RewriteMacrosAction>();
1102b3d49b6SJonas Devlieghere   case RewriteTest:            return std::make_unique<RewriteTestAction>();
111d9739824SNAKAMURA Takumi #if CLANG_ENABLE_OBJC_REWRITER
1122b3d49b6SJonas Devlieghere   case RewriteObjC:            return std::make_unique<RewriteObjCAction>();
113d93c8c00SRoman Divacky #else
114d93c8c00SRoman Divacky   case RewriteObjC:            Action = "RewriteObjC"; break;
115d93c8c00SRoman Divacky #endif
116d9739824SNAKAMURA Takumi #if CLANG_ENABLE_ARCMT
117d35e98faSArgyrios Kyrtzidis   case MigrateSource:
1182b3d49b6SJonas Devlieghere     return std::make_unique<arcmt::MigrateSourceAction>();
119d93c8c00SRoman Divacky #else
120d93c8c00SRoman Divacky   case MigrateSource:          Action = "MigrateSource"; break;
121d93c8c00SRoman Divacky #endif
122d9739824SNAKAMURA Takumi #if CLANG_ENABLE_STATIC_ANALYZER
1232b3d49b6SJonas Devlieghere   case RunAnalysis:            return std::make_unique<ento::AnalysisAction>();
124d93c8c00SRoman Divacky #else
125d93c8c00SRoman Divacky   case RunAnalysis:            Action = "RunAnalysis"; break;
126d93c8c00SRoman Divacky #endif
1272b3d49b6SJonas Devlieghere   case RunPreprocessorOnly:    return std::make_unique<PreprocessOnlyAction>();
1286e2d36b6SAlex Lorenz   case PrintDependencyDirectivesSourceMinimizerOutput:
1292b3d49b6SJonas Devlieghere     return std::make_unique<PrintDependencyDirectivesSourceMinimizerAction>();
13085dd0bd1SPeter Collingbourne   }
131d93c8c00SRoman Divacky 
132d9739824SNAKAMURA Takumi #if !CLANG_ENABLE_ARCMT || !CLANG_ENABLE_STATIC_ANALYZER \
133d9739824SNAKAMURA Takumi   || !CLANG_ENABLE_OBJC_REWRITER
134d93c8c00SRoman Divacky   CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
135d93c8c00SRoman Divacky   return 0;
136d93c8c00SRoman Divacky #else
137f47fa304SDavid Blaikie   llvm_unreachable("Invalid program action!");
138d93c8c00SRoman Divacky #endif
13985dd0bd1SPeter Collingbourne }
14085dd0bd1SPeter Collingbourne 
141207e7b1fSGabor Horvath std::unique_ptr<FrontendAction>
CreateFrontendAction(CompilerInstance & CI)142d35e98faSArgyrios Kyrtzidis CreateFrontendAction(CompilerInstance &CI) {
14385dd0bd1SPeter Collingbourne   // Create the underlying action.
144d35e98faSArgyrios Kyrtzidis   std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
14585dd0bd1SPeter Collingbourne   if (!Act)
146236bde3dSCraig Topper     return nullptr;
14785dd0bd1SPeter Collingbourne 
1483d97a9beSArgyrios Kyrtzidis   const FrontendOptions &FEOpts = CI.getFrontendOpts();
1493d97a9beSArgyrios Kyrtzidis 
1503d97a9beSArgyrios Kyrtzidis   if (FEOpts.FixAndRecompile) {
1512b3d49b6SJonas Devlieghere     Act = std::make_unique<FixItRecompile>(std::move(Act));
15224e9afffSArgyrios Kyrtzidis   }
15324e9afffSArgyrios Kyrtzidis 
154d9739824SNAKAMURA Takumi #if CLANG_ENABLE_ARCMT
155d4d55340SArgyrios Kyrtzidis   if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
156d4d55340SArgyrios Kyrtzidis       CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
157b5703510SChandler Carruth     // Potentially wrap the base FE action in an ARC Migrate Tool action.
1583d97a9beSArgyrios Kyrtzidis     switch (FEOpts.ARCMTAction) {
159b5703510SChandler Carruth     case FrontendOptions::ARCMT_None:
160b5703510SChandler Carruth       break;
161b5703510SChandler Carruth     case FrontendOptions::ARCMT_Check:
1622b3d49b6SJonas Devlieghere       Act = std::make_unique<arcmt::CheckAction>(std::move(Act));
163b5703510SChandler Carruth       break;
164b5703510SChandler Carruth     case FrontendOptions::ARCMT_Modify:
1652b3d49b6SJonas Devlieghere       Act = std::make_unique<arcmt::ModifyAction>(std::move(Act));
1667fbd97f6SArgyrios Kyrtzidis       break;
1677fbd97f6SArgyrios Kyrtzidis     case FrontendOptions::ARCMT_Migrate:
1682b3d49b6SJonas Devlieghere       Act = std::make_unique<arcmt::MigrateAction>(std::move(Act),
169f7639e1bSTed Kremenek                                      FEOpts.MTMigrateDir,
1703d97a9beSArgyrios Kyrtzidis                                      FEOpts.ARCMTMigrateReportOut,
1713d97a9beSArgyrios Kyrtzidis                                      FEOpts.ARCMTMigrateEmitARCErrors);
172b5703510SChandler Carruth       break;
173b5703510SChandler Carruth     }
174b5703510SChandler Carruth 
175f7639e1bSTed Kremenek     if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
1762b3d49b6SJonas Devlieghere       Act = std::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
177d35e98faSArgyrios Kyrtzidis                                                         FEOpts.MTMigrateDir,
178182486c9SFariborz Jahanian                                                         FEOpts.ObjCMTAction);
179f7639e1bSTed Kremenek     }
1807a2645f9SArgyrios Kyrtzidis   }
181d93c8c00SRoman Divacky #endif
182f7639e1bSTed Kremenek 
18385dd0bd1SPeter Collingbourne   // If there are any AST files to merge, create a frontend action
18485dd0bd1SPeter Collingbourne   // adaptor to perform the merge.
1853d97a9beSArgyrios Kyrtzidis   if (!FEOpts.ASTMergeFiles.empty())
1862b3d49b6SJonas Devlieghere     Act = std::make_unique<ASTMergeAction>(std::move(Act),
187d35e98faSArgyrios Kyrtzidis                                             FEOpts.ASTMergeFiles);
18885dd0bd1SPeter Collingbourne 
18985dd0bd1SPeter Collingbourne   return Act;
19085dd0bd1SPeter Collingbourne }
19185dd0bd1SPeter Collingbourne 
ExecuteCompilerInvocation(CompilerInstance * Clang)192207e7b1fSGabor Horvath bool ExecuteCompilerInvocation(CompilerInstance *Clang) {
19385dd0bd1SPeter Collingbourne   // Honor -help.
19485dd0bd1SPeter Collingbourne   if (Clang->getFrontendOpts().ShowHelp) {
195f1e2d585SFangrui Song     driver::getDriverOptTable().printHelp(
19643392759SIlya Biryukov         llvm::outs(), "clang -cc1 [options] file...",
197e70ed869SRichard Smith         "LLVM 'Clang' Compiler: http://clang.llvm.org",
1985dbfa4e6SGeorge Rimar         /*Include=*/driver::options::CC1Option,
1995dbfa4e6SGeorge Rimar         /*Exclude=*/0, /*ShowAllAliases=*/false);
200958297daSRafael Espindola     return true;
20185dd0bd1SPeter Collingbourne   }
20285dd0bd1SPeter Collingbourne 
20385dd0bd1SPeter Collingbourne   // Honor -version.
20485dd0bd1SPeter Collingbourne   //
20585dd0bd1SPeter Collingbourne   // FIXME: Use a better -version message?
20685dd0bd1SPeter Collingbourne   if (Clang->getFrontendOpts().ShowVersion) {
20785dd0bd1SPeter Collingbourne     llvm::cl::PrintVersionMessage();
208958297daSRafael Espindola     return true;
20985dd0bd1SPeter Collingbourne   }
21085dd0bd1SPeter Collingbourne 
211f4f9ad0fSVassil Vassilev   Clang->LoadRequestedPlugins();
2126c78974bSJohn Brawn 
21311ce9224STobias Grosser   // Honor -mllvm.
21411ce9224STobias Grosser   //
21511ce9224STobias Grosser   // FIXME: Remove this, one day.
21611ce9224STobias Grosser   // This should happen AFTER plugins have been loaded!
21711ce9224STobias Grosser   if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
21811ce9224STobias Grosser     unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
2192b3d49b6SJonas Devlieghere     auto Args = std::make_unique<const char*[]>(NumArgs + 2);
22011ce9224STobias Grosser     Args[0] = "clang (LLVM option parsing)";
22111ce9224STobias Grosser     for (unsigned i = 0; i != NumArgs; ++i)
22211ce9224STobias Grosser       Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
223236bde3dSCraig Topper     Args[NumArgs + 1] = nullptr;
22481f9e02eSChandler Carruth     llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
22511ce9224STobias Grosser   }
22611ce9224STobias Grosser 
227d9739824SNAKAMURA Takumi #if CLANG_ENABLE_STATIC_ANALYZER
2289f7fc983SKristof Umann   // These should happen AFTER plugins have been loaded!
2299f7fc983SKristof Umann 
2309f7fc983SKristof Umann   AnalyzerOptions &AnOpts = *Clang->getAnalyzerOpts();
2312aac0c47SKristóf Umann 
2329f7fc983SKristof Umann   // Honor -analyzer-checker-help and -analyzer-checker-help-hidden.
2335bc40d9bSKristof Umann   if (AnOpts.ShowCheckerHelp || AnOpts.ShowCheckerHelpAlpha ||
2345bc40d9bSKristof Umann       AnOpts.ShowCheckerHelpDeveloper) {
2352aac0c47SKristóf Umann     ento::printCheckerHelp(llvm::outs(), *Clang);
236958297daSRafael Espindola     return true;
23759cce71aSJordy Rose   }
238f1f351c9SKristof Umann 
239e8df27d9SKristof Umann   // Honor -analyzer-checker-option-help.
240ac95c865SKristof Umann   if (AnOpts.ShowCheckerOptionList || AnOpts.ShowCheckerOptionAlphaList ||
241ac95c865SKristof Umann       AnOpts.ShowCheckerOptionDeveloperList) {
2422aac0c47SKristóf Umann     ento::printCheckerConfigList(llvm::outs(), *Clang);
243e8df27d9SKristof Umann     return true;
244e8df27d9SKristof Umann   }
245e8df27d9SKristof Umann 
246f1f351c9SKristof Umann   // Honor -analyzer-list-enabled-checkers.
2479f7fc983SKristof Umann   if (AnOpts.ShowEnabledCheckerList) {
2482aac0c47SKristóf Umann     ento::printEnabledCheckerList(llvm::outs(), *Clang);
249d977b67eSKristof Umann     return true;
250c430990dSGabor Horvath   }
251f1f351c9SKristof Umann 
252f1f351c9SKristof Umann   // Honor -analyzer-config-help.
2539f7fc983SKristof Umann   if (AnOpts.ShowConfigOptionsList) {
254f1f351c9SKristof Umann     ento::printAnalyzerConfigList(llvm::outs());
255f1f351c9SKristof Umann     return true;
256f1f351c9SKristof Umann   }
257d93c8c00SRoman Divacky #endif
25859cce71aSJordy Rose 
25985dd0bd1SPeter Collingbourne   // If there were errors in processing arguments, don't do anything else.
260aa73d020SSean Silva   if (Clang->getDiagnostics().hasErrorOccurred())
261aa73d020SSean Silva     return false;
26285dd0bd1SPeter Collingbourne   // Create and execute the frontend action.
263b8984329SAhmed Charles   std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
264aa73d020SSean Silva   if (!Act)
265aa73d020SSean Silva     return false;
266aa73d020SSean Silva   bool Success = Clang->ExecuteAction(*Act);
2674f76f4aeSTed Kremenek   if (Clang->getFrontendOpts().DisableFree)
2689941da41SDavid Blaikie     llvm::BuryPointer(std::move(Act));
26985dd0bd1SPeter Collingbourne   return Success;
27085dd0bd1SPeter Collingbourne }
271207e7b1fSGabor Horvath 
272207e7b1fSGabor Horvath } // namespace clang
273