185dd0bd1SPeter Collingbourne //===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
285dd0bd1SPeter Collingbourne //
385dd0bd1SPeter Collingbourne //                     The LLVM Compiler Infrastructure
485dd0bd1SPeter Collingbourne //
585dd0bd1SPeter Collingbourne // This file is distributed under the University of Illinois Open Source
685dd0bd1SPeter Collingbourne // License. See LICENSE.TXT for details.
785dd0bd1SPeter Collingbourne //
885dd0bd1SPeter Collingbourne //===----------------------------------------------------------------------===//
985dd0bd1SPeter Collingbourne //
1085dd0bd1SPeter Collingbourne // This file holds ExecuteCompilerInvocation(). It is split into its own file to
1185dd0bd1SPeter Collingbourne // minimize the impact of pulling in essentially everything else in Clang.
1285dd0bd1SPeter Collingbourne //
1385dd0bd1SPeter Collingbourne //===----------------------------------------------------------------------===//
1485dd0bd1SPeter Collingbourne 
1585dd0bd1SPeter Collingbourne #include "clang/FrontendTool/Utils.h"
16b5703510SChandler Carruth #include "clang/ARCMigrate/ARCMTActions.h"
1785dd0bd1SPeter Collingbourne #include "clang/CodeGen/CodeGenAction.h"
187f633dfeSNAKAMURA Takumi #include "clang/Config/config.h"
19a3c85b86SJames Molloy #include "clang/Driver/Options.h"
2085dd0bd1SPeter Collingbourne #include "clang/Frontend/CompilerInstance.h"
213a02247dSChandler Carruth #include "clang/Frontend/CompilerInvocation.h"
2285dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendActions.h"
2385dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendDiagnostic.h"
2485dd0bd1SPeter Collingbourne #include "clang/Frontend/FrontendPluginRegistry.h"
25ce2c726eSKostya Serebryany #include "clang/Frontend/Utils.h"
26cdf81490STed Kremenek #include "clang/Rewrite/Frontend/FrontendActions.h"
273a02247dSChandler Carruth #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
28898229abSReid Kleckner #include "llvm/Option/OptTable.h"
29898229abSReid Kleckner #include "llvm/Option/Option.h"
308aaf4995SMichael J. Spencer #include "llvm/Support/DynamicLibrary.h"
313a02247dSChandler Carruth #include "llvm/Support/ErrorHandling.h"
3285dd0bd1SPeter Collingbourne using namespace clang;
33898229abSReid Kleckner using namespace llvm::opt;
3485dd0bd1SPeter Collingbourne 
35d35e98faSArgyrios Kyrtzidis static std::unique_ptr<FrontendAction>
36d35e98faSArgyrios Kyrtzidis CreateFrontendBaseAction(CompilerInstance &CI) {
3785dd0bd1SPeter Collingbourne   using namespace clang::frontend;
38ba294d73SAlp Toker   StringRef Action("unknown");
39ba294d73SAlp Toker   (void)Action;
4085dd0bd1SPeter Collingbourne 
4185dd0bd1SPeter Collingbourne   switch (CI.getFrontendOpts().ProgramAction) {
42d35e98faSArgyrios Kyrtzidis   case ASTDeclList:            return llvm::make_unique<ASTDeclListAction>();
43d35e98faSArgyrios Kyrtzidis   case ASTDump:                return llvm::make_unique<ASTDumpAction>();
44d35e98faSArgyrios Kyrtzidis   case ASTPrint:               return llvm::make_unique<ASTPrintAction>();
45d35e98faSArgyrios Kyrtzidis   case ASTView:                return llvm::make_unique<ASTViewAction>();
46d35e98faSArgyrios Kyrtzidis   case DumpRawTokens:          return llvm::make_unique<DumpRawTokensAction>();
47d35e98faSArgyrios Kyrtzidis   case DumpTokens:             return llvm::make_unique<DumpTokensAction>();
48d35e98faSArgyrios Kyrtzidis   case EmitAssembly:           return llvm::make_unique<EmitAssemblyAction>();
49d35e98faSArgyrios Kyrtzidis   case EmitBC:                 return llvm::make_unique<EmitBCAction>();
50d35e98faSArgyrios Kyrtzidis   case EmitHTML:               return llvm::make_unique<HTMLPrintAction>();
51d35e98faSArgyrios Kyrtzidis   case EmitLLVM:               return llvm::make_unique<EmitLLVMAction>();
52d35e98faSArgyrios Kyrtzidis   case EmitLLVMOnly:           return llvm::make_unique<EmitLLVMOnlyAction>();
53d35e98faSArgyrios Kyrtzidis   case EmitCodeGenOnly:        return llvm::make_unique<EmitCodeGenOnlyAction>();
54d35e98faSArgyrios Kyrtzidis   case EmitObj:                return llvm::make_unique<EmitObjAction>();
55d35e98faSArgyrios Kyrtzidis   case FixIt:                  return llvm::make_unique<FixItAction>();
56bbcc9f04SRichard Smith   case GenerateModule:
57bbcc9f04SRichard Smith     return llvm::make_unique<GenerateModuleFromModuleMapAction>();
58bbcc9f04SRichard Smith   case GenerateModuleInterface:
59bbcc9f04SRichard Smith     return llvm::make_unique<GenerateModuleInterfaceAction>();
60d35e98faSArgyrios Kyrtzidis   case GeneratePCH:            return llvm::make_unique<GeneratePCHAction>();
61d35e98faSArgyrios Kyrtzidis   case GeneratePTH:            return llvm::make_unique<GeneratePTHAction>();
62d35e98faSArgyrios Kyrtzidis   case InitOnly:               return llvm::make_unique<InitOnlyAction>();
63d35e98faSArgyrios Kyrtzidis   case ParseSyntaxOnly:        return llvm::make_unique<SyntaxOnlyAction>();
64d35e98faSArgyrios Kyrtzidis   case ModuleFileInfo:         return llvm::make_unique<DumpModuleInfoAction>();
65d35e98faSArgyrios Kyrtzidis   case VerifyPCH:              return llvm::make_unique<VerifyPCHAction>();
6685dd0bd1SPeter Collingbourne 
6785dd0bd1SPeter Collingbourne   case PluginAction: {
6885dd0bd1SPeter Collingbourne     for (FrontendPluginRegistry::iterator it =
6985dd0bd1SPeter Collingbourne            FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
7085dd0bd1SPeter Collingbourne          it != ie; ++it) {
7185dd0bd1SPeter Collingbourne       if (it->getName() == CI.getFrontendOpts().ActionName) {
72b8984329SAhmed Charles         std::unique_ptr<PluginASTAction> P(it->instantiate());
736c78974bSJohn Brawn         if ((P->getActionType() != PluginASTAction::ReplaceAction &&
746c78974bSJohn Brawn              P->getActionType() != PluginASTAction::Cmdline) ||
756c78974bSJohn Brawn             !P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()]))
76236bde3dSCraig Topper           return nullptr;
77d35e98faSArgyrios Kyrtzidis         return std::move(P);
7885dd0bd1SPeter Collingbourne       }
7985dd0bd1SPeter Collingbourne     }
8085dd0bd1SPeter Collingbourne 
8185dd0bd1SPeter Collingbourne     CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
8285dd0bd1SPeter Collingbourne       << CI.getFrontendOpts().ActionName;
83236bde3dSCraig Topper     return nullptr;
8485dd0bd1SPeter Collingbourne   }
8585dd0bd1SPeter Collingbourne 
86d35e98faSArgyrios Kyrtzidis   case PrintDeclContext:       return llvm::make_unique<DeclContextPrintAction>();
87d35e98faSArgyrios Kyrtzidis   case PrintPreamble:          return llvm::make_unique<PrintPreambleAction>();
88e993e4cdSDavid Blaikie   case PrintPreprocessedInput: {
8986a3ef5bSRichard Smith     if (CI.getPreprocessorOutputOpts().RewriteIncludes ||
9086a3ef5bSRichard Smith         CI.getPreprocessorOutputOpts().RewriteImports)
91d35e98faSArgyrios Kyrtzidis       return llvm::make_unique<RewriteIncludesAction>();
92d35e98faSArgyrios Kyrtzidis     return llvm::make_unique<PrintPreprocessedAction>();
93e993e4cdSDavid Blaikie   }
94e993e4cdSDavid Blaikie 
95d35e98faSArgyrios Kyrtzidis   case RewriteMacros:          return llvm::make_unique<RewriteMacrosAction>();
96d35e98faSArgyrios Kyrtzidis   case RewriteTest:            return llvm::make_unique<RewriteTestAction>();
970621cb2eSAlp Toker #ifdef CLANG_ENABLE_OBJC_REWRITER
98d35e98faSArgyrios Kyrtzidis   case RewriteObjC:            return llvm::make_unique<RewriteObjCAction>();
99d93c8c00SRoman Divacky #else
100d93c8c00SRoman Divacky   case RewriteObjC:            Action = "RewriteObjC"; break;
101d93c8c00SRoman Divacky #endif
102d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_ARCMT
103d35e98faSArgyrios Kyrtzidis   case MigrateSource:
104d35e98faSArgyrios Kyrtzidis     return llvm::make_unique<arcmt::MigrateSourceAction>();
105d93c8c00SRoman Divacky #else
106d93c8c00SRoman Divacky   case MigrateSource:          Action = "MigrateSource"; break;
107d93c8c00SRoman Divacky #endif
108d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_STATIC_ANALYZER
109d35e98faSArgyrios Kyrtzidis   case RunAnalysis:            return llvm::make_unique<ento::AnalysisAction>();
110d93c8c00SRoman Divacky #else
111d93c8c00SRoman Divacky   case RunAnalysis:            Action = "RunAnalysis"; break;
112d93c8c00SRoman Divacky #endif
113d35e98faSArgyrios Kyrtzidis   case RunPreprocessorOnly:    return llvm::make_unique<PreprocessOnlyAction>();
11485dd0bd1SPeter Collingbourne   }
115d93c8c00SRoman Divacky 
116d93c8c00SRoman Divacky #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
1170621cb2eSAlp Toker   || !defined(CLANG_ENABLE_OBJC_REWRITER)
118d93c8c00SRoman Divacky   CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
119d93c8c00SRoman Divacky   return 0;
120d93c8c00SRoman Divacky #else
121f47fa304SDavid Blaikie   llvm_unreachable("Invalid program action!");
122d93c8c00SRoman Divacky #endif
12385dd0bd1SPeter Collingbourne }
12485dd0bd1SPeter Collingbourne 
125d35e98faSArgyrios Kyrtzidis static std::unique_ptr<FrontendAction>
126d35e98faSArgyrios Kyrtzidis CreateFrontendAction(CompilerInstance &CI) {
12785dd0bd1SPeter Collingbourne   // Create the underlying action.
128d35e98faSArgyrios Kyrtzidis   std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
12985dd0bd1SPeter Collingbourne   if (!Act)
130236bde3dSCraig Topper     return nullptr;
13185dd0bd1SPeter Collingbourne 
1323d97a9beSArgyrios Kyrtzidis   const FrontendOptions &FEOpts = CI.getFrontendOpts();
1333d97a9beSArgyrios Kyrtzidis 
1343d97a9beSArgyrios Kyrtzidis   if (FEOpts.FixAndRecompile) {
135d35e98faSArgyrios Kyrtzidis     Act = llvm::make_unique<FixItRecompile>(std::move(Act));
13624e9afffSArgyrios Kyrtzidis   }
13724e9afffSArgyrios Kyrtzidis 
138d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_ARCMT
139d4d55340SArgyrios Kyrtzidis   if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
140d4d55340SArgyrios Kyrtzidis       CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
141b5703510SChandler Carruth     // Potentially wrap the base FE action in an ARC Migrate Tool action.
1423d97a9beSArgyrios Kyrtzidis     switch (FEOpts.ARCMTAction) {
143b5703510SChandler Carruth     case FrontendOptions::ARCMT_None:
144b5703510SChandler Carruth       break;
145b5703510SChandler Carruth     case FrontendOptions::ARCMT_Check:
146d35e98faSArgyrios Kyrtzidis       Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
147b5703510SChandler Carruth       break;
148b5703510SChandler Carruth     case FrontendOptions::ARCMT_Modify:
149d35e98faSArgyrios Kyrtzidis       Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
1507fbd97f6SArgyrios Kyrtzidis       break;
1517fbd97f6SArgyrios Kyrtzidis     case FrontendOptions::ARCMT_Migrate:
152d35e98faSArgyrios Kyrtzidis       Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
153f7639e1bSTed Kremenek                                      FEOpts.MTMigrateDir,
1543d97a9beSArgyrios Kyrtzidis                                      FEOpts.ARCMTMigrateReportOut,
1553d97a9beSArgyrios Kyrtzidis                                      FEOpts.ARCMTMigrateEmitARCErrors);
156b5703510SChandler Carruth       break;
157b5703510SChandler Carruth     }
158b5703510SChandler Carruth 
159f7639e1bSTed Kremenek     if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
160d35e98faSArgyrios Kyrtzidis       Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
161d35e98faSArgyrios Kyrtzidis                                                         FEOpts.MTMigrateDir,
162182486c9SFariborz Jahanian                                                         FEOpts.ObjCMTAction);
163f7639e1bSTed Kremenek     }
1647a2645f9SArgyrios Kyrtzidis   }
165d93c8c00SRoman Divacky #endif
166f7639e1bSTed Kremenek 
16785dd0bd1SPeter Collingbourne   // If there are any AST files to merge, create a frontend action
16885dd0bd1SPeter Collingbourne   // adaptor to perform the merge.
1693d97a9beSArgyrios Kyrtzidis   if (!FEOpts.ASTMergeFiles.empty())
170d35e98faSArgyrios Kyrtzidis     Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
171d35e98faSArgyrios Kyrtzidis                                             FEOpts.ASTMergeFiles);
17285dd0bd1SPeter Collingbourne 
17385dd0bd1SPeter Collingbourne   return Act;
17485dd0bd1SPeter Collingbourne }
17585dd0bd1SPeter Collingbourne 
17685dd0bd1SPeter Collingbourne bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
17785dd0bd1SPeter Collingbourne   // Honor -help.
17885dd0bd1SPeter Collingbourne   if (Clang->getFrontendOpts().ShowHelp) {
1790aaa7625SDavid Blaikie     std::unique_ptr<OptTable> Opts = driver::createDriverOptTable();
18085dd0bd1SPeter Collingbourne     Opts->PrintHelp(llvm::outs(), "clang -cc1",
181e70ed869SRichard Smith                     "LLVM 'Clang' Compiler: http://clang.llvm.org",
182*5dbfa4e6SGeorge Rimar                     /*Include=*/driver::options::CC1Option,
183*5dbfa4e6SGeorge Rimar                     /*Exclude=*/0, /*ShowAllAliases=*/false);
184958297daSRafael Espindola     return true;
18585dd0bd1SPeter Collingbourne   }
18685dd0bd1SPeter Collingbourne 
18785dd0bd1SPeter Collingbourne   // Honor -version.
18885dd0bd1SPeter Collingbourne   //
18985dd0bd1SPeter Collingbourne   // FIXME: Use a better -version message?
19085dd0bd1SPeter Collingbourne   if (Clang->getFrontendOpts().ShowVersion) {
19185dd0bd1SPeter Collingbourne     llvm::cl::PrintVersionMessage();
192958297daSRafael Espindola     return true;
19385dd0bd1SPeter Collingbourne   }
19485dd0bd1SPeter Collingbourne 
19585dd0bd1SPeter Collingbourne   // Load any requested plugins.
19685dd0bd1SPeter Collingbourne   for (unsigned i = 0,
19785dd0bd1SPeter Collingbourne          e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
19885dd0bd1SPeter Collingbourne     const std::string &Path = Clang->getFrontendOpts().Plugins[i];
19985dd0bd1SPeter Collingbourne     std::string Error;
200d8cd0604SNAKAMURA Takumi     if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
20185dd0bd1SPeter Collingbourne       Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
20285dd0bd1SPeter Collingbourne         << Path << Error;
20385dd0bd1SPeter Collingbourne   }
20485dd0bd1SPeter Collingbourne 
2056c78974bSJohn Brawn   // Check if any of the loaded plugins replaces the main AST action
2066c78974bSJohn Brawn   for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
2076c78974bSJohn Brawn                                         ie = FrontendPluginRegistry::end();
2086c78974bSJohn Brawn        it != ie; ++it) {
2096c78974bSJohn Brawn     std::unique_ptr<PluginASTAction> P(it->instantiate());
2106c78974bSJohn Brawn     if (P->getActionType() == PluginASTAction::ReplaceAction) {
2116c78974bSJohn Brawn       Clang->getFrontendOpts().ProgramAction = clang::frontend::PluginAction;
2126c78974bSJohn Brawn       Clang->getFrontendOpts().ActionName = it->getName();
2136c78974bSJohn Brawn       break;
2146c78974bSJohn Brawn     }
2156c78974bSJohn Brawn   }
2166c78974bSJohn Brawn 
21711ce9224STobias Grosser   // Honor -mllvm.
21811ce9224STobias Grosser   //
21911ce9224STobias Grosser   // FIXME: Remove this, one day.
22011ce9224STobias Grosser   // This should happen AFTER plugins have been loaded!
22111ce9224STobias Grosser   if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
22211ce9224STobias Grosser     unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
22381f9e02eSChandler Carruth     auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
22411ce9224STobias Grosser     Args[0] = "clang (LLVM option parsing)";
22511ce9224STobias Grosser     for (unsigned i = 0; i != NumArgs; ++i)
22611ce9224STobias Grosser       Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
227236bde3dSCraig Topper     Args[NumArgs + 1] = nullptr;
22881f9e02eSChandler Carruth     llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
22911ce9224STobias Grosser   }
23011ce9224STobias Grosser 
231d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_STATIC_ANALYZER
23259cce71aSJordy Rose   // Honor -analyzer-checker-help.
23359cce71aSJordy Rose   // This should happen AFTER plugins have been loaded!
23440ea0eaaSTed Kremenek   if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
23559cce71aSJordy Rose     ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
236958297daSRafael Espindola     return true;
23759cce71aSJordy Rose   }
238c430990dSGabor Horvath   if (Clang->getAnalyzerOpts()->ShowEnabledCheckerList) {
239c430990dSGabor Horvath     ento::printEnabledCheckerList(llvm::outs(),
240c430990dSGabor Horvath                                   Clang->getFrontendOpts().Plugins,
241c430990dSGabor Horvath                                   *Clang->getAnalyzerOpts());
242c430990dSGabor Horvath   }
243d93c8c00SRoman Divacky #endif
24459cce71aSJordy Rose 
24585dd0bd1SPeter Collingbourne   // If there were errors in processing arguments, don't do anything else.
246aa73d020SSean Silva   if (Clang->getDiagnostics().hasErrorOccurred())
247aa73d020SSean Silva     return false;
24885dd0bd1SPeter Collingbourne   // Create and execute the frontend action.
249b8984329SAhmed Charles   std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
250aa73d020SSean Silva   if (!Act)
251aa73d020SSean Silva     return false;
252aa73d020SSean Silva   bool Success = Clang->ExecuteAction(*Act);
2534f76f4aeSTed Kremenek   if (Clang->getFrontendOpts().DisableFree)
254a97eaa1bSDavid Blaikie     BuryPointer(std::move(Act));
25585dd0bd1SPeter Collingbourne   return Success;
25685dd0bd1SPeter Collingbourne }
257