Skip to content

Commit

Permalink
Unified checks for unevaluated context (danmar#5362)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Aug 23, 2023
1 parent 499f566 commit 8cd6194
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 27 deletions.
6 changes: 3 additions & 3 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,7 @@ static bool functionModifiesArguments(const Function* f)

bool isConstFunctionCall(const Token* ftok, const Library& library)
{
if (isSizeOfEtc(ftok))
if (isUnevaluated(ftok))
return true;
if (!Token::Match(ftok, "%name% ("))
return false;
Expand Down Expand Up @@ -3382,7 +3382,7 @@ bool isGlobalData(const Token *expr, bool cpp)
return globalData || !var;
}

bool isSizeOfEtc(const Token *tok)
bool isUnevaluated(const Token *tok)
{
return Token::Match(tok, "sizeof|typeof|offsetof|decltype|__typeof__ (");
return Token::Match(tok, "alignof|_Alignof|_alignof|__alignof|__alignof__|decltype|offsetof|sizeof|typeid|typeof|__typeof__ (");
}
2 changes: 1 addition & 1 deletion lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,6 @@ CPPCHECKLIB bool isNullOperand(const Token *expr);

bool isGlobalData(const Token *expr, bool cpp);

bool isSizeOfEtc(const Token *tok);
bool isUnevaluated(const Token *tok);

#endif // astutilsH
11 changes: 3 additions & 8 deletions lib/checknullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown) const
return isPointerDeRef(tok, unknown, mSettings);
}

static bool isUnevaluatedOperator(const Token* tok)
{
return Token::Match(tok, "sizeof|decltype|typeid|typeof|alignof|_Alignof|_alignof|__alignof|__alignof__ (");
}

bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown, const Settings *settings)
{
unknown = false;
Expand Down Expand Up @@ -192,7 +187,7 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown, const Set

// Dereferencing pointer..
const Token* grandParent = parent->astParent();
if (parent->isUnaryOp("*") && !(grandParent && isUnevaluatedOperator(grandParent->previous()))) {
if (parent->isUnaryOp("*") && !(grandParent && isUnevaluated(grandParent->previous()))) {
// declaration of function pointer
if (tok->variable() && tok->variable()->nameToken() == tok)
return false;
Expand Down Expand Up @@ -290,7 +285,7 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
const bool printInconclusive = (mSettings->certainty.isEnabled(Certainty::inconclusive));

for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
if (isUnevaluatedOperator(tok)) {
if (isUnevaluated(tok)) {
tok = tok->next()->link();
continue;
}
Expand Down Expand Up @@ -349,7 +344,7 @@ void CheckNullPointer::nullConstantDereference()
tok = scope->function->token; // Check initialization list

for (; tok != scope->bodyEnd; tok = tok->next()) {
if (isUnevaluatedOperator(tok))
if (isUnevaluated(tok))
tok = tok->next()->link();

else if (Token::simpleMatch(tok, "* 0")) {
Expand Down
16 changes: 8 additions & 8 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
}

// skip sizeof / offsetof
if (isSizeOfEtc(tok))
if (isUnevaluated(tok))
tok = tok->linkAt(1);

// for/while..
Expand Down Expand Up @@ -724,7 +724,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
return true;
}

if (isSizeOfEtc(tok))
if (isUnevaluated(tok))
tok = tok->linkAt(1);

else if (tok->str() == "?") {
Expand Down Expand Up @@ -838,7 +838,7 @@ const Token* CheckUninitVar::checkExpr(const Token* tok, const Variable& var, co
{
if (!tok)
return nullptr;
if (isSizeOfEtc(tok->previous()))
if (isUnevaluated(tok->previous()))
return nullptr;

if (tok->astOperand1()) {
Expand Down Expand Up @@ -891,7 +891,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Va
return true;
}
// skip sizeof / offsetof
if (isSizeOfEtc(tok))
if (isUnevaluated(tok))
tok = tok->linkAt(1);
if ((!isuninit || !membervar.empty()) && tok->str() == "&&")
suppressErrors = true;
Expand All @@ -909,7 +909,7 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va
const Token *const end = start->link();
for (const Token *tok = start->next(); tok != end; tok = tok->next()) {
// skip sizeof / offsetof
if (isSizeOfEtc(tok)) {
if (isUnevaluated(tok)) {
tok = tok->linkAt(1);
continue;
}
Expand Down Expand Up @@ -1031,7 +1031,7 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va
varIsUsedInRhs = true;
return ChildrenToVisit::done;
}
if (isSizeOfEtc(t->previous()))
if (isUnevaluated(t->previous()))
return ChildrenToVisit::none;
return ChildrenToVisit::op1_and_op2;
});
Expand Down Expand Up @@ -1096,7 +1096,7 @@ void CheckUninitVar::checkRhs(const Token *tok, const Variable &var, Alloc alloc
if (err)
uninitvarError(tok, var.nameToken()->str(), alloc);
break;
} else if (isSizeOfEtc(tok))
} else if (isUnevaluated(tok))
tok = tok->linkAt(1);

}
Expand Down Expand Up @@ -1599,7 +1599,7 @@ void CheckUninitVar::valueFlowUninit()
// check every executable scope
for (const Scope* scope : symbolDatabase->functionScopes) {
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (isSizeOfEtc(tok)) {
if (isUnevaluated(tok)) {
tok = tok->linkAt(1);
continue;
}
Expand Down
8 changes: 2 additions & 6 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ struct ForwardTraversal {
// Traverse the parameters of the function before escaping
traverseRecursive(tok->next()->astOperand2(), f, traverseUnknown);
return Break(Analyzer::Terminate::Escape);
} else if (isUnevaluated(tok)) {
} else if (isUnevaluated(tok->previous())) {
if (out)
*out = tok->link();
return Progress::Skip;
Expand Down Expand Up @@ -825,10 +825,6 @@ struct ForwardTraversal {
return Progress::Continue;
}

static bool isUnevaluated(const Token* tok) {
return Token::Match(tok->previous(), "sizeof|decltype (");
}

static bool isFunctionCall(const Token* tok)
{
if (!Token::simpleMatch(tok, "("))
Expand All @@ -839,7 +835,7 @@ struct ForwardTraversal {
return false;
if (Token::simpleMatch(tok->link(), ") {"))
return false;
if (isUnevaluated(tok))
if (isUnevaluated(tok->previous()))
return false;
return Token::Match(tok->previous(), "%name%|)|]|>");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/reverseanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ struct ReverseTraversal {
static Token* isUnevaluated(Token* tok) {
if (Token::Match(tok, ")|>") && tok->link()) {
Token* start = tok->link();
if (Token::Match(start->previous(), "sizeof|decltype ("))
if (::isUnevaluated(start->previous()))
return start->previous();
if (Token::simpleMatch(start, "<"))
return start;
Expand Down

0 comments on commit 8cd6194

Please sign in to comment.