Clarify the code with Token::isUnaryOp and Token::isBinaryOp

This commit is contained in:
Daniel Marjamäki 2018-07-14 13:01:24 +02:00
parent c9a2071420
commit d00e522856
1 changed files with 12 additions and 21 deletions

View File

@ -1387,7 +1387,7 @@ static bool canBeConst(const Variable *var)
if (!argVar|| (!argVar->isConst() && argVar->isReference())) if (!argVar|| (!argVar->isConst() && argVar->isReference()))
return false; return false;
} }
} else if (parent->str() == "&" && !parent->astOperand2()) { } else if (parent->isUnaryOp("&")) {
// TODO: check how pointer is used // TODO: check how pointer is used
return false; return false;
} else if (parent->isConstOp()) } else if (parent->isConstOp())
@ -1491,7 +1491,7 @@ void CheckOther::checkCharVariable()
signedCharArrayIndexError(tok); signedCharArrayIndexError(tok);
if (portability && astIsUnknownSignChar(index) && index->getValueGE(0x80, mSettings)) if (portability && astIsUnknownSignChar(index) && index->getValueGE(0x80, mSettings))
unknownSignCharArrayIndexError(tok); unknownSignCharArrayIndexError(tok);
} else if (warning && Token::Match(tok, "[&|^]") && tok->astOperand2() && tok->astOperand1()) { } else if (warning && Token::Match(tok, "[&|^]") && tok->isBinaryOp()) {
bool warn = false; bool warn = false;
if (astIsSignedChar(tok->astOperand1())) { if (astIsSignedChar(tok->astOperand1())) {
const ValueFlow::Value *v1 = tok->astOperand1()->getValueLE(-1, mSettings); const ValueFlow::Value *v1 = tok->astOperand1()->getValueLE(-1, mSettings);
@ -2485,29 +2485,20 @@ void CheckOther::checkRedundantPointerOp()
if (!mSettings->isEnabled(Settings::STYLE)) if (!mSettings->isEnabled(Settings::STYLE))
return; return;
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) { for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
if (tok->str() == "&") { if (!tok->isUnaryOp("&") || !tok->astOperand1()->isUnaryOp("*"))
// bail out for logical AND operator continue;
if (tok->astOperand2())
continue;
// pointer dereference // variable
const Token *astTok = tok->astOperand1(); const Token *varTok = tok->astOperand1()->astOperand1();
if (!astTok || astTok->str() != "*") if (!varTok || varTok->isExpandedMacro())
continue; continue;
// variable const Variable *var = varTok->variable();
const Token *varTok = astTok->astOperand1(); if (!var || !var->isPointer())
if (!varTok || varTok->isExpandedMacro() || varTok->varId() == 0) continue;
continue;
const Variable *var = symbolDatabase->getVariableFromVarId(varTok->varId()); redundantPointerOpError(tok, var->name(), false);
if (!var || !var->isPointer())
continue;
redundantPointerOpError(tok, var->name(), false);
}
} }
} }