Coverage Report

Created: 2019-07-24 05:18

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