Skip to content

Commit

Permalink
Fix FN constParameterPointer
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 5, 2024
1 parent 4da68b4 commit d1dff1b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 75 deletions.
37 changes: 30 additions & 7 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,26 @@ static const Token* getVariableChangedStart(const Variable* p)
return start;
}

namespace {
struct CompareVariables {
bool operator()(const Variable* a, const Variable* b) const {
const int fileA = a->nameToken()->fileIndex();
const int fileB = b->nameToken()->fileIndex();
if (fileA == fileB) {
const int lineA = a->nameToken()->linenr();
const int lineB = b->nameToken()->linenr();
if (lineA == lineB) {
const int columnA = a->nameToken()->column();
const int columnB = b->nameToken()->column();
return columnA < columnB;
}
return lineA < lineB;
}
return fileA < fileB;
}
};
}

void CheckOther::checkConstPointer()
{
if (!mSettings->severity.isEnabled(Severity::style) &&
Expand All @@ -1498,18 +1518,20 @@ void CheckOther::checkConstPointer()

logChecker("CheckOther::checkConstPointer"); // style

std::vector<const Variable*> pointers, nonConstPointers;
std::set<const Variable*, CompareVariables> pointers, nonConstPointers;
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
const Variable* const var = tok->variable();
if (!var)
continue;
if (!var->isLocal() && !var->isArgument())
continue;
const Token* const nameTok = var->nameToken();
// declarations of (static) pointers are (not) split up, array declarations are never split up
if (tok == nameTok && (!var->isStatic() || Token::simpleMatch(nameTok->next(), "[")) &&
!astIsRangeBasedForDecl(nameTok))
continue;
if (tok == nameTok) {
// declarations of (static) pointers are (not) split up, array declarations are never split up
if (var->isLocal() && (!var->isStatic() || Token::simpleMatch(nameTok->next(), "[")) &&
!astIsRangeBasedForDecl(nameTok))
continue;
}
const ValueType* const vt = tok->valueType();
if (!vt)
continue;
Expand All @@ -1519,7 +1541,7 @@ void CheckOther::checkConstPointer()
continue;
if (std::find(nonConstPointers.cbegin(), nonConstPointers.cend(), var) != nonConstPointers.cend())
continue;
pointers.emplace_back(var);
pointers.emplace(var);
const Token* parent = tok->astParent();
enum Deref { NONE, DEREF, MEMBER } deref = NONE;
bool hasIncDec = false;
Expand Down Expand Up @@ -1604,7 +1626,8 @@ void CheckOther::checkConstPointer()
continue;
}
}
nonConstPointers.emplace_back(var);
if (tok != nameTok)
nonConstPointers.emplace(var);
}
for (const Variable *p: pointers) {
if (p->isArgument()) {
Expand Down
Loading

0 comments on commit d1dff1b

Please sign in to comment.