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