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"
18a3c85b86SJames Molloy #include "clang/Driver/Options.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"
25cdf81490STed Kremenek #include "clang/Rewrite/Frontend/FrontendActions.h"
263a02247dSChandler Carruth #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
27898229abSReid Kleckner #include "llvm/Option/OptTable.h"
28898229abSReid Kleckner #include "llvm/Option/Option.h"
298aaf4995SMichael J. Spencer #include "llvm/Support/DynamicLibrary.h"
303a02247dSChandler Carruth #include "llvm/Support/ErrorHandling.h"
3185dd0bd1SPeter Collingbourne using namespace clang;
32898229abSReid Kleckner using namespace llvm::opt;
3385dd0bd1SPeter Collingbourne 
34d35e98faSArgyrios Kyrtzidis static std::unique_ptr<FrontendAction>
35d35e98faSArgyrios Kyrtzidis CreateFrontendBaseAction(CompilerInstance &CI) {
3685dd0bd1SPeter Collingbourne   using namespace clang::frontend;
37ba294d73SAlp Toker   StringRef Action("unknown");
38ba294d73SAlp Toker   (void)Action;
3985dd0bd1SPeter Collingbourne 
4085dd0bd1SPeter Collingbourne   switch (CI.getFrontendOpts().ProgramAction) {
41d35e98faSArgyrios Kyrtzidis   case ASTDeclList:            return llvm::make_unique<ASTDeclListAction>();
42d35e98faSArgyrios Kyrtzidis   case ASTDump:                return llvm::make_unique<ASTDumpAction>();
43d35e98faSArgyrios Kyrtzidis   case ASTPrint:               return llvm::make_unique<ASTPrintAction>();
44d35e98faSArgyrios Kyrtzidis   case ASTView:                return llvm::make_unique<ASTViewAction>();
45d35e98faSArgyrios Kyrtzidis   case DumpRawTokens:          return llvm::make_unique<DumpRawTokensAction>();
46d35e98faSArgyrios Kyrtzidis   case DumpTokens:             return llvm::make_unique<DumpTokensAction>();
47d35e98faSArgyrios Kyrtzidis   case EmitAssembly:           return llvm::make_unique<EmitAssemblyAction>();
48d35e98faSArgyrios Kyrtzidis   case EmitBC:                 return llvm::make_unique<EmitBCAction>();
49d35e98faSArgyrios Kyrtzidis   case EmitHTML:               return llvm::make_unique<HTMLPrintAction>();
50d35e98faSArgyrios Kyrtzidis   case EmitLLVM:               return llvm::make_unique<EmitLLVMAction>();
51d35e98faSArgyrios Kyrtzidis   case EmitLLVMOnly:           return llvm::make_unique<EmitLLVMOnlyAction>();
52d35e98faSArgyrios Kyrtzidis   case EmitCodeGenOnly:        return llvm::make_unique<EmitCodeGenOnlyAction>();
53d35e98faSArgyrios Kyrtzidis   case EmitObj:                return llvm::make_unique<EmitObjAction>();
54d35e98faSArgyrios Kyrtzidis   case FixIt:                  return llvm::make_unique<FixItAction>();
55d35e98faSArgyrios Kyrtzidis   case GenerateModule:         return llvm::make_unique<GenerateModuleAction>();
56d35e98faSArgyrios Kyrtzidis   case GeneratePCH:            return llvm::make_unique<GeneratePCHAction>();
57d35e98faSArgyrios Kyrtzidis   case GeneratePTH:            return llvm::make_unique<GeneratePTHAction>();
58d35e98faSArgyrios Kyrtzidis   case InitOnly:               return llvm::make_unique<InitOnlyAction>();
59d35e98faSArgyrios Kyrtzidis   case ParseSyntaxOnly:        return llvm::make_unique<SyntaxOnlyAction>();
60d35e98faSArgyrios Kyrtzidis   case ModuleFileInfo:         return llvm::make_unique<DumpModuleInfoAction>();
61d35e98faSArgyrios Kyrtzidis   case VerifyPCH:              return llvm::make_unique<VerifyPCHAction>();
6285dd0bd1SPeter Collingbourne 
6385dd0bd1SPeter Collingbourne   case PluginAction: {
6485dd0bd1SPeter Collingbourne     for (FrontendPluginRegistry::iterator it =
6585dd0bd1SPeter Collingbourne            FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
6685dd0bd1SPeter Collingbourne          it != ie; ++it) {
6785dd0bd1SPeter Collingbourne       if (it->getName() == CI.getFrontendOpts().ActionName) {
68b8984329SAhmed Charles         std::unique_ptr<PluginASTAction> P(it->instantiate());
6985dd0bd1SPeter Collingbourne         if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
70236bde3dSCraig Topper           return nullptr;
71d35e98faSArgyrios Kyrtzidis         return std::move(P);
7285dd0bd1SPeter Collingbourne       }
7385dd0bd1SPeter Collingbourne     }
7485dd0bd1SPeter Collingbourne 
7585dd0bd1SPeter Collingbourne     CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
7685dd0bd1SPeter Collingbourne       << CI.getFrontendOpts().ActionName;
77236bde3dSCraig Topper     return nullptr;
7885dd0bd1SPeter Collingbourne   }
7985dd0bd1SPeter Collingbourne 
80d35e98faSArgyrios Kyrtzidis   case PrintDeclContext:       return llvm::make_unique<DeclContextPrintAction>();
81d35e98faSArgyrios Kyrtzidis   case PrintPreamble:          return llvm::make_unique<PrintPreambleAction>();
82e993e4cdSDavid Blaikie   case PrintPreprocessedInput: {
830621cb2eSAlp Toker     if (CI.getPreprocessorOutputOpts().RewriteIncludes)
84d35e98faSArgyrios Kyrtzidis       return llvm::make_unique<RewriteIncludesAction>();
85d35e98faSArgyrios Kyrtzidis     return llvm::make_unique<PrintPreprocessedAction>();
86e993e4cdSDavid Blaikie   }
87e993e4cdSDavid Blaikie 
88d35e98faSArgyrios Kyrtzidis   case RewriteMacros:          return llvm::make_unique<RewriteMacrosAction>();
89d35e98faSArgyrios Kyrtzidis   case RewriteTest:            return llvm::make_unique<RewriteTestAction>();
900621cb2eSAlp Toker #ifdef CLANG_ENABLE_OBJC_REWRITER
91d35e98faSArgyrios Kyrtzidis   case RewriteObjC:            return llvm::make_unique<RewriteObjCAction>();
92d93c8c00SRoman Divacky #else
93d93c8c00SRoman Divacky   case RewriteObjC:            Action = "RewriteObjC"; break;
94d93c8c00SRoman Divacky #endif
95d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_ARCMT
96d35e98faSArgyrios Kyrtzidis   case MigrateSource:
97d35e98faSArgyrios Kyrtzidis     return llvm::make_unique<arcmt::MigrateSourceAction>();
98d93c8c00SRoman Divacky #else
99d93c8c00SRoman Divacky   case MigrateSource:          Action = "MigrateSource"; break;
100d93c8c00SRoman Divacky #endif
101d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_STATIC_ANALYZER
102d35e98faSArgyrios Kyrtzidis   case RunAnalysis:            return llvm::make_unique<ento::AnalysisAction>();
103d93c8c00SRoman Divacky #else
104d93c8c00SRoman Divacky   case RunAnalysis:            Action = "RunAnalysis"; break;
105d93c8c00SRoman Divacky #endif
106d35e98faSArgyrios Kyrtzidis   case RunPreprocessorOnly:    return llvm::make_unique<PreprocessOnlyAction>();
10785dd0bd1SPeter Collingbourne   }
108d93c8c00SRoman Divacky 
109d93c8c00SRoman Divacky #if !defined(CLANG_ENABLE_ARCMT) || !defined(CLANG_ENABLE_STATIC_ANALYZER) \
1100621cb2eSAlp Toker   || !defined(CLANG_ENABLE_OBJC_REWRITER)
111d93c8c00SRoman Divacky   CI.getDiagnostics().Report(diag::err_fe_action_not_available) << Action;
112d93c8c00SRoman Divacky   return 0;
113d93c8c00SRoman Divacky #else
114f47fa304SDavid Blaikie   llvm_unreachable("Invalid program action!");
115d93c8c00SRoman Divacky #endif
11685dd0bd1SPeter Collingbourne }
11785dd0bd1SPeter Collingbourne 
118d35e98faSArgyrios Kyrtzidis static std::unique_ptr<FrontendAction>
119d35e98faSArgyrios Kyrtzidis CreateFrontendAction(CompilerInstance &CI) {
12085dd0bd1SPeter Collingbourne   // Create the underlying action.
121d35e98faSArgyrios Kyrtzidis   std::unique_ptr<FrontendAction> Act = CreateFrontendBaseAction(CI);
12285dd0bd1SPeter Collingbourne   if (!Act)
123236bde3dSCraig Topper     return nullptr;
12485dd0bd1SPeter Collingbourne 
1253d97a9beSArgyrios Kyrtzidis   const FrontendOptions &FEOpts = CI.getFrontendOpts();
1263d97a9beSArgyrios Kyrtzidis 
1273d97a9beSArgyrios Kyrtzidis   if (FEOpts.FixAndRecompile) {
128d35e98faSArgyrios Kyrtzidis     Act = llvm::make_unique<FixItRecompile>(std::move(Act));
12924e9afffSArgyrios Kyrtzidis   }
13024e9afffSArgyrios Kyrtzidis 
131d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_ARCMT
132d4d55340SArgyrios Kyrtzidis   if (CI.getFrontendOpts().ProgramAction != frontend::MigrateSource &&
133d4d55340SArgyrios Kyrtzidis       CI.getFrontendOpts().ProgramAction != frontend::GeneratePCH) {
134b5703510SChandler Carruth     // Potentially wrap the base FE action in an ARC Migrate Tool action.
1353d97a9beSArgyrios Kyrtzidis     switch (FEOpts.ARCMTAction) {
136b5703510SChandler Carruth     case FrontendOptions::ARCMT_None:
137b5703510SChandler Carruth       break;
138b5703510SChandler Carruth     case FrontendOptions::ARCMT_Check:
139d35e98faSArgyrios Kyrtzidis       Act = llvm::make_unique<arcmt::CheckAction>(std::move(Act));
140b5703510SChandler Carruth       break;
141b5703510SChandler Carruth     case FrontendOptions::ARCMT_Modify:
142d35e98faSArgyrios Kyrtzidis       Act = llvm::make_unique<arcmt::ModifyAction>(std::move(Act));
1437fbd97f6SArgyrios Kyrtzidis       break;
1447fbd97f6SArgyrios Kyrtzidis     case FrontendOptions::ARCMT_Migrate:
145d35e98faSArgyrios Kyrtzidis       Act = llvm::make_unique<arcmt::MigrateAction>(std::move(Act),
146f7639e1bSTed Kremenek                                      FEOpts.MTMigrateDir,
1473d97a9beSArgyrios Kyrtzidis                                      FEOpts.ARCMTMigrateReportOut,
1483d97a9beSArgyrios Kyrtzidis                                      FEOpts.ARCMTMigrateEmitARCErrors);
149b5703510SChandler Carruth       break;
150b5703510SChandler Carruth     }
151b5703510SChandler Carruth 
152f7639e1bSTed Kremenek     if (FEOpts.ObjCMTAction != FrontendOptions::ObjCMT_None) {
153d35e98faSArgyrios Kyrtzidis       Act = llvm::make_unique<arcmt::ObjCMigrateAction>(std::move(Act),
154d35e98faSArgyrios Kyrtzidis                                                         FEOpts.MTMigrateDir,
155182486c9SFariborz Jahanian                                                         FEOpts.ObjCMTAction);
156f7639e1bSTed Kremenek     }
1577a2645f9SArgyrios Kyrtzidis   }
158d93c8c00SRoman Divacky #endif
159f7639e1bSTed Kremenek 
16085dd0bd1SPeter Collingbourne   // If there are any AST files to merge, create a frontend action
16185dd0bd1SPeter Collingbourne   // adaptor to perform the merge.
1623d97a9beSArgyrios Kyrtzidis   if (!FEOpts.ASTMergeFiles.empty())
163d35e98faSArgyrios Kyrtzidis     Act = llvm::make_unique<ASTMergeAction>(std::move(Act),
164d35e98faSArgyrios Kyrtzidis                                             FEOpts.ASTMergeFiles);
16585dd0bd1SPeter Collingbourne 
16685dd0bd1SPeter Collingbourne   return Act;
16785dd0bd1SPeter Collingbourne }
16885dd0bd1SPeter Collingbourne 
16985dd0bd1SPeter Collingbourne bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
17085dd0bd1SPeter Collingbourne   // Honor -help.
17185dd0bd1SPeter Collingbourne   if (Clang->getFrontendOpts().ShowHelp) {
172b8984329SAhmed Charles     std::unique_ptr<OptTable> Opts(driver::createDriverOptTable());
17385dd0bd1SPeter Collingbourne     Opts->PrintHelp(llvm::outs(), "clang -cc1",
174e70ed869SRichard Smith                     "LLVM 'Clang' Compiler: http://clang.llvm.org",
175898229abSReid Kleckner                     /*Include=*/ driver::options::CC1Option, /*Exclude=*/ 0);
176958297daSRafael Espindola     return true;
17785dd0bd1SPeter Collingbourne   }
17885dd0bd1SPeter Collingbourne 
17985dd0bd1SPeter Collingbourne   // Honor -version.
18085dd0bd1SPeter Collingbourne   //
18185dd0bd1SPeter Collingbourne   // FIXME: Use a better -version message?
18285dd0bd1SPeter Collingbourne   if (Clang->getFrontendOpts().ShowVersion) {
18385dd0bd1SPeter Collingbourne     llvm::cl::PrintVersionMessage();
184958297daSRafael Espindola     return true;
18585dd0bd1SPeter Collingbourne   }
18685dd0bd1SPeter Collingbourne 
18785dd0bd1SPeter Collingbourne   // Load any requested plugins.
18885dd0bd1SPeter Collingbourne   for (unsigned i = 0,
18985dd0bd1SPeter Collingbourne          e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
19085dd0bd1SPeter Collingbourne     const std::string &Path = Clang->getFrontendOpts().Plugins[i];
19185dd0bd1SPeter Collingbourne     std::string Error;
192*d8cd0604SNAKAMURA Takumi     if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
19385dd0bd1SPeter Collingbourne       Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
19485dd0bd1SPeter Collingbourne         << Path << Error;
19585dd0bd1SPeter Collingbourne   }
19685dd0bd1SPeter Collingbourne 
19711ce9224STobias Grosser   // Honor -mllvm.
19811ce9224STobias Grosser   //
19911ce9224STobias Grosser   // FIXME: Remove this, one day.
20011ce9224STobias Grosser   // This should happen AFTER plugins have been loaded!
20111ce9224STobias Grosser   if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
20211ce9224STobias Grosser     unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
20381f9e02eSChandler Carruth     auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
20411ce9224STobias Grosser     Args[0] = "clang (LLVM option parsing)";
20511ce9224STobias Grosser     for (unsigned i = 0; i != NumArgs; ++i)
20611ce9224STobias Grosser       Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
207236bde3dSCraig Topper     Args[NumArgs + 1] = nullptr;
20881f9e02eSChandler Carruth     llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
20911ce9224STobias Grosser   }
21011ce9224STobias Grosser 
211d93c8c00SRoman Divacky #ifdef CLANG_ENABLE_STATIC_ANALYZER
21259cce71aSJordy Rose   // Honor -analyzer-checker-help.
21359cce71aSJordy Rose   // This should happen AFTER plugins have been loaded!
21440ea0eaaSTed Kremenek   if (Clang->getAnalyzerOpts()->ShowCheckerHelp) {
21559cce71aSJordy Rose     ento::printCheckerHelp(llvm::outs(), Clang->getFrontendOpts().Plugins);
216958297daSRafael Espindola     return true;
21759cce71aSJordy Rose   }
218d93c8c00SRoman Divacky #endif
21959cce71aSJordy Rose 
22085dd0bd1SPeter Collingbourne   // If there were errors in processing arguments, don't do anything else.
221aa73d020SSean Silva   if (Clang->getDiagnostics().hasErrorOccurred())
222aa73d020SSean Silva     return false;
22385dd0bd1SPeter Collingbourne   // Create and execute the frontend action.
224b8984329SAhmed Charles   std::unique_ptr<FrontendAction> Act(CreateFrontendAction(*Clang));
225aa73d020SSean Silva   if (!Act)
226aa73d020SSean Silva     return false;
227aa73d020SSean Silva   bool Success = Clang->ExecuteAction(*Act);
2284f76f4aeSTed Kremenek   if (Clang->getFrontendOpts().DisableFree)
229a97eaa1bSDavid Blaikie     BuryPointer(std::move(Act));
23085dd0bd1SPeter Collingbourne   return Success;
23185dd0bd1SPeter Collingbourne }
232