Coverage Report

Created: 2023-11-11 10:31

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