Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/DebugCheckers.cpp
Line
Count
Source (jump to first uncovered line)
1
//==- DebugCheckers.cpp - Debugging Checkers ---------------------*- C++ -*-==//
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 defines checkers that display debugging information.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
14
#include "clang/Analysis/Analyses/Dominators.h"
15
#include "clang/Analysis/Analyses/LiveVariables.h"
16
#include "clang/Analysis/CallGraph.h"
17
#include "clang/StaticAnalyzer/Core/Checker.h"
18
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
20
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
22
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
23
#include "llvm/Support/Process.h"
24
25
using namespace clang;
26
using namespace ento;
27
28
//===----------------------------------------------------------------------===//
29
// DominatorsTreeDumper
30
//===----------------------------------------------------------------------===//
31
32
namespace {
33
class DominatorsTreeDumper : public Checker<check::ASTCodeBody> {
34
public:
35
  void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
36
9
                        BugReporter &BR) const {
37
9
    if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
38
9
      CFGDomTree Dom;
39
9
      Dom.buildDominatorTree(AC->getCFG());
40
9
      Dom.dump();
41
9
    }
42
9
  }
43
};
44
}
45
46
3
void ento::registerDominatorsTreeDumper(CheckerManager &mgr) {
47
3
  mgr.registerChecker<DominatorsTreeDumper>();
48
3
}
49
50
6
bool ento::shouldRegisterDominatorsTreeDumper(const CheckerManager &mgr) {
51
6
  return true;
52
6
}
53
54
//===----------------------------------------------------------------------===//
55
// PostDominatorsTreeDumper
56
//===----------------------------------------------------------------------===//
57
58
namespace {
59
class PostDominatorsTreeDumper : public Checker<check::ASTCodeBody> {
60
public:
61
  void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
62
9
                        BugReporter &BR) const {
63
9
    if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
64
9
      CFGPostDomTree Dom;
65
9
      Dom.buildDominatorTree(AC->getCFG());
66
9
      Dom.dump();
67
9
    }
68
9
  }
69
};
70
}
71
72
3
void ento::registerPostDominatorsTreeDumper(CheckerManager &mgr) {
73
3
  mgr.registerChecker<PostDominatorsTreeDumper>();
74
3
}
75
76
6
bool ento::shouldRegisterPostDominatorsTreeDumper(const CheckerManager &mgr) {
77
6
  return true;
78
6
}
79
80
//===----------------------------------------------------------------------===//
81
// ControlDependencyTreeDumper
82
//===----------------------------------------------------------------------===//
83
84
namespace {
85
class ControlDependencyTreeDumper : public Checker<check::ASTCodeBody> {
86
public:
87
  void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
88
9
                        BugReporter &BR) const {
89
9
    if (AnalysisDeclContext *AC = mgr.getAnalysisDeclContext(D)) {
90
9
      ControlDependencyCalculator Dom(AC->getCFG());
91
9
      Dom.dump();
92
9
    }
93
9
  }
94
};
95
}
96
97
3
void ento::registerControlDependencyTreeDumper(CheckerManager &mgr) {
98
3
  mgr.registerChecker<ControlDependencyTreeDumper>();
99
3
}
100
101
6
bool ento::shouldRegisterControlDependencyTreeDumper(const CheckerManager &mgr) {
102
6
  return true;
103
6
}
104
105
//===----------------------------------------------------------------------===//
106
// LiveVariablesDumper
107
//===----------------------------------------------------------------------===//
108
109
namespace {
110
class LiveVariablesDumper : public Checker<check::ASTCodeBody> {
111
public:
112
  void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
113
2
                        BugReporter &BR) const {
114
2
    if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) {
115
2
      L->dumpBlockLiveness(mgr.getSourceManager());
116
2
    }
117
2
  }
118
};
119
}
120
121
1
void ento::registerLiveVariablesDumper(CheckerManager &mgr) {
122
1
  mgr.registerChecker<LiveVariablesDumper>();
123
1
}
124
125
2
bool ento::shouldRegisterLiveVariablesDumper(const CheckerManager &mgr) {
126
2
  return true;
127
2
}
128
129
//===----------------------------------------------------------------------===//
130
// LiveStatementsDumper
131
//===----------------------------------------------------------------------===//
132
133
namespace {
134
class LiveExpressionsDumper : public Checker<check::ASTCodeBody> {
135
public:
136
  void checkASTCodeBody(const Decl *D, AnalysisManager& Mgr,
137
12
                        BugReporter &BR) const {
138
12
    if (LiveVariables *L = Mgr.getAnalysis<RelaxedLiveVariables>(D))
139
12
      L->dumpExprLiveness(Mgr.getSourceManager());
140
12
  }
141
};
142
}
143
144
3
void ento::registerLiveExpressionsDumper(CheckerManager &mgr) {
145
3
  mgr.registerChecker<LiveExpressionsDumper>();
146
3
}
147
148
6
bool ento::shouldRegisterLiveExpressionsDumper(const CheckerManager &mgr) {
149
6
  return true;
150
6
}
151
152
//===----------------------------------------------------------------------===//
153
// CFGViewer
154
//===----------------------------------------------------------------------===//
155
156
namespace {
157
class CFGViewer : public Checker<check::ASTCodeBody> {
158
public:
159
  void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
160
0
                        BugReporter &BR) const {
161
0
    if (CFG *cfg = mgr.getCFG(D)) {
162
0
      cfg->viewCFG(mgr.getLangOpts());
163
0
    }
164
0
  }
165
};
166
}
167
168
0
void ento::registerCFGViewer(CheckerManager &mgr) {
169
0
  mgr.registerChecker<CFGViewer>();
170
0
}
171
172
0
bool ento::shouldRegisterCFGViewer(const CheckerManager &mgr) {
173
0
  return true;
174
0
}
175
176
//===----------------------------------------------------------------------===//
177
// CFGDumper
178
//===----------------------------------------------------------------------===//
179
180
namespace {
181
class CFGDumper : public Checker<check::ASTCodeBody> {
182
public:
183
  void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
184
804
                        BugReporter &BR) const {
185
804
    PrintingPolicy Policy(mgr.getLangOpts());
186
804
    Policy.TerseOutput = true;
187
804
    Policy.PolishForDeclaration = true;
188
804
    D->print(llvm::errs(), Policy);
189
190
804
    if (CFG *cfg = mgr.getCFG(D)) {
191
804
      cfg->dump(mgr.getLangOpts(),
192
804
                llvm::sys::Process::StandardErrHasColors());
193
804
    }
194
804
  }
195
};
196
}
197
198
40
void ento::registerCFGDumper(CheckerManager &mgr) {
199
40
  mgr.registerChecker<CFGDumper>();
200
40
}
201
202
80
bool ento::shouldRegisterCFGDumper(const CheckerManager &mgr) {
203
80
  return true;
204
80
}
205
206
//===----------------------------------------------------------------------===//
207
// CallGraphViewer
208
//===----------------------------------------------------------------------===//
209
210
namespace {
211
class CallGraphViewer : public Checker< check::ASTDecl<TranslationUnitDecl> > {
212
public:
213
  void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
214
0
                    BugReporter &BR) const {
215
0
    CallGraph CG;
216
0
    CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
217
0
    CG.viewGraph();
218
0
  }
219
};
220
}
221
222
0
void ento::registerCallGraphViewer(CheckerManager &mgr) {
223
0
  mgr.registerChecker<CallGraphViewer>();
224
0
}
225
226
0
bool ento::shouldRegisterCallGraphViewer(const CheckerManager &mgr) {
227
0
  return true;
228
0
}
229
230
//===----------------------------------------------------------------------===//
231
// CallGraphDumper
232
//===----------------------------------------------------------------------===//
233
234
namespace {
235
class CallGraphDumper : public Checker< check::ASTDecl<TranslationUnitDecl> > {
236
public:
237
  void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager& mgr,
238
3
                    BugReporter &BR) const {
239
3
    CallGraph CG;
240
3
    CG.addToCallGraph(const_cast<TranslationUnitDecl*>(TU));
241
3
    CG.dump();
242
3
  }
243
};
244
}
245
246
3
void ento::registerCallGraphDumper(CheckerManager &mgr) {
247
3
  mgr.registerChecker<CallGraphDumper>();
248
3
}
249
250
6
bool ento::shouldRegisterCallGraphDumper(const CheckerManager &mgr) {
251
6
  return true;
252
6
}
253
254
//===----------------------------------------------------------------------===//
255
// ConfigDumper
256
//===----------------------------------------------------------------------===//
257
258
namespace {
259
class ConfigDumper : public Checker< check::EndOfTranslationUnit > {
260
  typedef AnalyzerOptions::ConfigTable Table;
261
262
  static int compareEntry(const Table::MapEntryTy *const *LHS,
263
3.53k
                          const Table::MapEntryTy *const *RHS) {
264
3.53k
    return (*LHS)->getKey().compare((*RHS)->getKey());
265
3.53k
  }
266
267
public:
268
  void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
269
                                 AnalysisManager& mgr,
270
4
                                 BugReporter &BR) const {
271
4
    const Table &Config = mgr.options.Config;
272
273
4
    SmallVector<const Table::MapEntryTy *, 32> Keys;
274
4
    for (const auto &Entry : Config)
275
520
      Keys.push_back(&Entry);
276
4
    llvm::array_pod_sort(Keys.begin(), Keys.end(), compareEntry);
277
278
4
    llvm::errs() << "[config]\n";
279
524
    for (unsigned I = 0, E = Keys.size(); I != E; 
++I520
)
280
520
      llvm::errs() << Keys[I]->getKey() << " = "
281
520
                   << (Keys[I]->second.empty() ? 
"\"\""16
:
Keys[I]->second504
)
282
520
                   << '\n';
283
4
  }
284
};
285
}
286
287
4
void ento::registerConfigDumper(CheckerManager &mgr) {
288
4
  mgr.registerChecker<ConfigDumper>();
289
4
}
290
291
8
bool ento::shouldRegisterConfigDumper(const CheckerManager &mgr) {
292
8
  return true;
293
8
}
294
295
//===----------------------------------------------------------------------===//
296
// ExplodedGraph Viewer
297
//===----------------------------------------------------------------------===//
298
299
namespace {
300
class ExplodedGraphViewer : public Checker< check::EndAnalysis > {
301
public:
302
0
  ExplodedGraphViewer() {}
303
0
  void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const {
304
0
    Eng.ViewGraph(false);
305
0
  }
306
};
307
308
}
309
310
0
void ento::registerExplodedGraphViewer(CheckerManager &mgr) {
311
0
  mgr.registerChecker<ExplodedGraphViewer>();
312
0
}
313
314
0
bool ento::shouldRegisterExplodedGraphViewer(const CheckerManager &mgr) {
315
0
  return true;
316
0
}
317
318
//===----------------------------------------------------------------------===//
319
// Emits a report for every Stmt that the analyzer visits.
320
//===----------------------------------------------------------------------===//
321
322
namespace {
323
324
class ReportStmts : public Checker<check::PreStmt<Stmt>> {
325
  BugType BT_stmtLoc{this, "Statement"};
326
327
public:
328
16
  void checkPreStmt(const Stmt *S, CheckerContext &C) const {
329
16
    ExplodedNode *Node = C.generateNonFatalErrorNode();
330
16
    if (!Node)
331
0
      return;
332
333
16
    auto Report =
334
16
        std::make_unique<PathSensitiveBugReport>(BT_stmtLoc, "Statement", Node);
335
336
16
    C.emitReport(std::move(Report));
337
16
  }
338
};
339
340
} // end of anonymous namespace
341
342
2
void ento::registerReportStmts(CheckerManager &mgr) {
343
2
  mgr.registerChecker<ReportStmts>();
344
2
}
345
346
4
bool ento::shouldRegisterReportStmts(const CheckerManager &mgr) {
347
4
  return true;
348
4
}