Skip to content

Commit b1f30ac

Browse files
authored
small PathAnalysis cleanup (danmar#7407)
1 parent 63ff0a8 commit b1f30ac

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

lib/checkstl.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,6 @@ void CheckStl::invalidContainer()
11361136
{
11371137
logChecker("CheckStl::invalidContainer");
11381138
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
1139-
const Library& library = mSettings->library;
11401139
InvalidContainerAnalyzer analyzer;
11411140
analyzer.analyze(symbolDatabase);
11421141
for (const Scope * scope : symbolDatabase->functionScopes) {
@@ -1189,7 +1188,7 @@ void CheckStl::invalidContainer()
11891188
const ValueFlow::Value* v = nullptr;
11901189
ErrorPath errorPath;
11911190
PathAnalysis::Info info =
1192-
PathAnalysis{endToken, library}.forwardFind([&](const PathAnalysis::Info& info) {
1191+
PathAnalysis{endToken}.forwardFind([&](const PathAnalysis::Info& info) {
11931192
if (!info.tok->variable())
11941193
return false;
11951194
if (info.tok->varId() == 0)
@@ -1201,7 +1200,7 @@ void CheckStl::invalidContainer()
12011200
if (Token::Match(info.tok->astParent(), "%assign%") && astIsLHS(info.tok))
12021201
skipVarIds.insert(info.tok->varId());
12031202
if (info.tok->variable()->isReference() && !isVariableDecl(info.tok) &&
1204-
reaches(info.tok->variable()->nameToken(), tok, library, nullptr)) {
1203+
reaches(info.tok->variable()->nameToken(), tok, nullptr)) {
12051204

12061205
ErrorPath ep;
12071206
bool addressOf = false;
@@ -1211,7 +1210,7 @@ void CheckStl::invalidContainer()
12111210
// An argument always reaches
12121211
if (var->isArgument() ||
12131212
(!var->isReference() && !var->isRValueReference() && !isVariableDecl(tok) &&
1214-
reaches(var->nameToken(), tok, library, &ep))) {
1213+
reaches(var->nameToken(), tok, &ep))) {
12151214
errorPath = std::move(ep);
12161215
return true;
12171216
}
@@ -1220,7 +1219,7 @@ void CheckStl::invalidContainer()
12201219
ErrorPath ep;
12211220
const ValueFlow::Value* val = getInnerLifetime(info.tok, r.tok->varId(), &ep);
12221221
// Check the iterator is created before the change
1223-
if (val && val->tokvalue != tok && reaches(val->tokvalue, tok, library, &ep)) {
1222+
if (val && val->tokvalue != tok && reaches(val->tokvalue, tok, &ep)) {
12241223
v = val;
12251224
errorPath = std::move(ep);
12261225
return true;

lib/pathanalysis.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,25 @@ PathAnalysis::Progress PathAnalysis::forwardRange(const Token* startToken, const
166166
return Progress::Break;
167167
}
168168
// Prevent infinite recursion
169-
if (tok->next() == start)
169+
if (tok->next() == mStart)
170170
break;
171171
}
172172
return Progress::Continue;
173173
}
174174

175175
void PathAnalysis::forward(const std::function<Progress(const Info&)>& f) const
176176
{
177-
const Scope * endScope = findOuterScope(start->scope());
177+
const Scope * endScope = findOuterScope(mStart->scope());
178178
if (!endScope)
179179
return;
180180
const Token * endToken = endScope->bodyEnd;
181-
Info info{start, ErrorPath{}, true};
182-
forwardRange(start, endToken, std::move(info), f);
181+
Info info{mStart, ErrorPath{}, true};
182+
forwardRange(mStart, endToken, std::move(info), f);
183183
}
184184

185-
bool reaches(const Token * start, const Token * dest, const Library& library, ErrorPath* errorPath)
185+
bool reaches(const Token * start, const Token * dest, ErrorPath* errorPath)
186186
{
187-
PathAnalysis::Info info = PathAnalysis{start, library}.forwardFind([&](const PathAnalysis::Info& i) {
187+
PathAnalysis::Info info = PathAnalysis{start}.forwardFind([&](const PathAnalysis::Info& i) {
188188
return (i.tok == dest);
189189
});
190190
if (!info.tok)

lib/pathanalysis.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323

2424
#include <cstdint>
2525
#include <functional>
26-
#include <list>
2726
#include <utility>
2827

29-
class Library;
3028
class Scope;
3129
class Token;
3230

@@ -35,20 +33,17 @@ struct PathAnalysis {
3533
Continue,
3634
Break
3735
};
38-
PathAnalysis(const Token* start, const Library& library)
39-
: start(start), library(&library)
36+
37+
explicit PathAnalysis(const Token* start)
38+
: mStart(start)
4039
{}
41-
const Token * start;
42-
const Library * library;
4340

4441
struct Info {
4542
const Token* tok;
4643
ErrorPath errorPath;
4744
bool known;
4845
};
4946

50-
void forward(const std::function<Progress(const Info&)>& f) const;
51-
5247
Info forwardFind(std::function<bool(const Info&)> pred) const {
5348
Info result{};
5449
forward([&](const Info& info) {
@@ -61,6 +56,9 @@ struct PathAnalysis {
6156
return result;
6257
}
6358
private:
59+
const Token * mStart;
60+
61+
void forward(const std::function<Progress(const Info&)>& f) const;
6462

6563
static Progress forwardRecursive(const Token* tok, Info info, const std::function<PathAnalysis::Progress(const Info&)>& f);
6664
Progress forwardRange(const Token* startToken, const Token* endToken, Info info, const std::function<Progress(const Info&)>& f) const;
@@ -77,7 +75,7 @@ struct PathAnalysis {
7775
* @param dest The path destination
7876
* @param errorPath Adds the path traversal to the errorPath
7977
*/
80-
bool reaches(const Token * start, const Token * dest, const Library& library, ErrorPath* errorPath);
78+
bool reaches(const Token * start, const Token * dest, ErrorPath* errorPath);
8179

8280
#endif
8381

0 commit comments

Comments
 (0)