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