Coverage Report

Created: 2022-05-14 11:35

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