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