This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyBugReporter.cpp
155 lines (134 loc) · 5.65 KB
/
MyBugReporter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "MyBugReporter.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
using namespace clang;
using namespace ento;
void MyBugReporter::diagnosePath(BugReport *report) {
std::unique_ptr<BugReport> UniqueR(report);
if (const ExplodedNode *E = report->getErrorNode()) {
const AnalysisDeclContext *DeclCtx =
E->getLocationContext()->getAnalysisDeclContext();
// The source of autosynthesized body can be handcrafted AST or a model
// file. The locations from handcrafted ASTs have no valid source locations
// and have to be discarded. Locations from model files should be preserved
// for processing and reporting.
if (DeclCtx->isBodyAutosynthesized() &&
!DeclCtx->isBodyAutosynthesizedFromModelFile())
return;
}
bool ValidSourceLoc = report->getLocation(reporter.getSourceManager()).isValid();
assert(ValidSourceLoc);
// If we mess up in a release build, we'd still prefer to just drop the bug
// instead of trying to go on.
if (!ValidSourceLoc)
return;
for (PathDiagnosticConsumer *PD : reporter.getPathDiagnosticConsumers()) {
BugType& BT = report->getBugType();
PathDiagnostic * D = new PathDiagnostic(
report->getBugType().getCheckName(),
report->getDeclWithIssue(), report->getBugType().getName(),
report->getDescription(),
report->getShortDescription(/*Fallback=*/false), BT.getCategory(),
report->getUniqueingLocation(),
report->getUniqueingDecl());
SmallVector<BugReport *, 1> bugReports;
bugReports.push_back(report);
ArrayRef<BugReport *> tmp = bugReports;
if (!reporter.generatePathDiagnostic(*D, *PD, tmp))
return;
for (PathDiagnosticEventPiece * piece : pieces) {
D->getActivePath().push_back(std::move(piece));
for (const SourceRange &Range : report->getRanges())
D->getActivePath().back()->addRange(Range);
}
// Examine the report and see if the last piece is in a header. Reset the
// report location to the last piece in the main source file.
AnalyzerOptions& Opts = reporter.getAnalyzerOptions();
if (Opts.shouldReportIssuesInMainSourceFile() && !Opts.AnalyzeAll)
D->resetDiagnosticLocationToMainFile();
// If the path is empty, generate a single step path with the location
// of the issue.
if (D->path.empty()) {
PathDiagnosticLocation L = report->getLocation(reporter.getSourceManager());
auto piece = llvm::make_unique<PathDiagnosticEventPiece>(
L, report->getDescription());
for (const SourceRange &Range : report->getRanges())
piece->addRange(Range);
D->setEndOfPath(std::move(piece));
}
// Get the meta data.
const BugReport::ExtraTextList &Meta = report->getExtraText();
for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
e = Meta.end(); i != e; ++i) {
D->addMeta(*i);
}
diagnosed.push_back(std::make_pair(PD, D));
}
}
// just put pieces and conduct no path sensitive diagnosis
// @arg report specifies the end of anlaysis path
void MyBugReporter::diagnoseSimple(BugReport *report) {
std::unique_ptr<BugReport> UniqueR(report);
if (const ExplodedNode *E = report->getErrorNode()) {
const AnalysisDeclContext *DeclCtx =
E->getLocationContext()->getAnalysisDeclContext();
// The source of autosynthesized body can be handcrafted AST or a model
// file. The locations from handcrafted ASTs have no valid source locations
// and have to be discarded. Locations from model files should be preserved
// for processing and reporting.
if (DeclCtx->isBodyAutosynthesized() &&
!DeclCtx->isBodyAutosynthesizedFromModelFile())
return;
}
bool ValidSourceLoc = report->getLocation(reporter.getSourceManager()).isValid();
assert(ValidSourceLoc);
// If we mess up in a release build, we'd still prefer to just drop the bug
// instead of trying to go on.
if (!ValidSourceLoc)
return;
for (PathDiagnosticConsumer *PD : reporter.getPathDiagnosticConsumers()) {
BugType& BT = report->getBugType();
PathDiagnostic * D = new PathDiagnostic(
report->getBugType().getCheckName(),
report->getDeclWithIssue(), report->getBugType().getName(),
report->getDescription(),
report->getShortDescription(/*Fallback=*/false), BT.getCategory(),
report->getUniqueingLocation(),
report->getUniqueingDecl());
SmallVector<BugReport *, 1> bugReports;
bugReports.push_back(report);
ArrayRef<BugReport *> tmp = bugReports;
for (PathDiagnosticEventPiece * piece : pieces) {
D->getActivePath().push_back(std::move(piece));
for (const SourceRange &Range : report->getRanges())
D->getActivePath().back()->addRange(Range);
}
// do not call generatePathDiagnostic()
// Examine the report and see if the last piece is in a header. Reset the
// report location to the last piece in the main source file.
AnalyzerOptions& Opts = reporter.getAnalyzerOptions();
if (Opts.shouldReportIssuesInMainSourceFile() && !Opts.AnalyzeAll)
D->resetDiagnosticLocationToMainFile();
// Path is always empty here
PathDiagnosticLocation L = report->getLocation(reporter.getSourceManager());
auto piece = llvm::make_unique<PathDiagnosticEventPiece>(
L, report->getDescription());
for (const SourceRange &Range : report->getRanges())
piece->addRange(Range);
D->setEndOfPath(std::move(piece));
// Get the meta data.
const BugReport::ExtraTextList &Meta = report->getExtraText();
for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
e = Meta.end(); i != e; ++i) {
D->addMeta(*i);
}
diagnosed.push_back(std::make_pair(PD, D));
}
}
void MyBugReporter::flush() {
for (std::pair<PathDiagnosticConsumer *, PathDiagnostic *> d : diagnosed) {
std::unique_ptr<PathDiagnostic> p(d.second);
d.first->HandlePathDiagnostic(std::move(p));
}
diagnosed.clear();
}