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