Uninitialized variables; create utility function isSizeOfEtc

This commit is contained in:
Daniel Marjamäki 2021-05-22 19:45:46 +02:00
parent 5f6b56ada2
commit 1df93f5474
1 changed files with 12 additions and 8 deletions

View File

@ -50,6 +50,10 @@ namespace {
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static bool isSizeOfEtc(const Token *tok) {
return Token::Match(tok, "sizeof|typeof|offsetof|decltype|__typeof__ (");
}
// get ast parent, skip possible address-of and casts // get ast parent, skip possible address-of and casts
static const Token *getAstParentSkipPossibleCastAndAddressOf(const Token *vartok, bool *unknown) static const Token *getAstParentSkipPossibleCastAndAddressOf(const Token *vartok, bool *unknown)
{ {
@ -580,7 +584,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
} }
// skip sizeof / offsetof // skip sizeof / offsetof
if (Token::Match(tok, "sizeof|typeof|offsetof|decltype (")) if (isSizeOfEtc(tok))
tok = tok->linkAt(1); tok = tok->linkAt(1);
// for/while.. // for/while..
@ -693,7 +697,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
return true; return true;
} }
else if (Token::Match(tok, "sizeof|typeof|offsetof|decltype (")) else if (isSizeOfEtc(tok))
tok = tok->linkAt(1); tok = tok->linkAt(1);
else if (tok->str() == "?") { else if (tok->str() == "?") {
@ -809,7 +813,7 @@ const Token *CheckUninitVar::checkExpr(const Token *tok, const Variable& var, co
{ {
if (!tok) if (!tok)
return nullptr; return nullptr;
if (Token::Match(tok->previous(), "sizeof|typeof|offsetof|decltype (")) if (isSizeOfEtc(tok->previous()))
return nullptr; return nullptr;
if (tok->astOperand1()) { if (tok->astOperand1()) {
@ -862,7 +866,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Va
return true; return true;
} }
// skip sizeof / offsetof // skip sizeof / offsetof
if (Token::Match(tok, "sizeof|typeof|offsetof|decltype (")) if (isSizeOfEtc(tok))
tok = tok->linkAt(1); tok = tok->linkAt(1);
if ((!isuninit || !membervar.empty()) && tok->str() == "&&") if ((!isuninit || !membervar.empty()) && tok->str() == "&&")
suppressErrors = true; suppressErrors = true;
@ -880,7 +884,7 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va
const Token *const end = start->link(); const Token *const end = start->link();
for (const Token *tok = start->next(); tok != end; tok = tok->next()) { for (const Token *tok = start->next(); tok != end; tok = tok->next()) {
// skip sizeof / offsetof // skip sizeof / offsetof
if (Token::Match(tok, "sizeof|typeof|offsetof|decltype (")) { if (isSizeOfEtc(tok)) {
tok = tok->linkAt(1); tok = tok->linkAt(1);
continue; continue;
} }
@ -1002,7 +1006,7 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va
varIsUsedInRhs = true; varIsUsedInRhs = true;
return ChildrenToVisit::done; return ChildrenToVisit::done;
} }
if (Token::Match(t->previous(), "sizeof|typeof|offsetof|decltype (")) if (isSizeOfEtc(t->previous()))
return ChildrenToVisit::none; return ChildrenToVisit::none;
return ChildrenToVisit::op1_and_op2; return ChildrenToVisit::op1_and_op2;
}); });
@ -1067,7 +1071,7 @@ void CheckUninitVar::checkRhs(const Token *tok, const Variable &var, Alloc alloc
if (err) if (err)
uninitvarError(tok, var.nameToken()->str(), alloc); uninitvarError(tok, var.nameToken()->str(), alloc);
break; break;
} else if (Token::Match(tok, "sizeof|typeof|offsetof|decltype (")) } else if (isSizeOfEtc(tok))
tok = tok->linkAt(1); tok = tok->linkAt(1);
} }
@ -1503,7 +1507,7 @@ void CheckUninitVar::valueFlowUninit()
if (!scope.isExecutable()) if (!scope.isExecutable())
continue; continue;
for (const Token* tok = scope.bodyStart; tok != scope.bodyEnd; tok = tok->next()) { for (const Token* tok = scope.bodyStart; tok != scope.bodyEnd; tok = tok->next()) {
if (Token::Match(tok, "sizeof|typeof|offsetof|decltype (")) { if (isSizeOfEtc(tok)) {
tok = tok->linkAt(1); tok = tok->linkAt(1);
continue; continue;
} }