Fix FP with const argument when doing a variable and cast (#1649)

This commit is contained in:
Paul Fultz II 2019-02-07 07:59:09 -06:00 committed by Daniel Marjamäki
parent bdbaaff361
commit d7c20b15e7
4 changed files with 20 additions and 3 deletions

View File

@ -1042,6 +1042,11 @@ bool isLikelyStreamRead(bool cpp, const Token *op)
return (!parent->astOperand1()->valueType() || !parent->astOperand1()->valueType()->isIntegral()); return (!parent->astOperand1()->valueType() || !parent->astOperand1()->valueType()->isIntegral());
} }
bool isCPPCast(const Token* tok)
{
return tok && Token::simpleMatch(tok->previous(), "> (") && tok->astOperand2() && tok->astOperand1() && tok->astOperand1()->str().find("_cast") != std::string::npos;
}
bool isConstVarExpression(const Token *tok) bool isConstVarExpression(const Token *tok)
{ {
if (!tok) if (!tok)
@ -1052,7 +1057,7 @@ bool isConstVarExpression(const Token *tok)
std::vector<const Token *> args = getArguments(tok); std::vector<const Token *> args = getArguments(tok);
return std::all_of(args.begin(), args.end(), &isConstVarExpression); return std::all_of(args.begin(), args.end(), &isConstVarExpression);
} }
if (Token::simpleMatch(tok->previous(), "> (") && tok->astOperand2() && tok->astOperand1()->str().find("_cast") != std::string::npos) { if (isCPPCast(tok)) {
return isConstVarExpression(tok->astOperand2()); return isConstVarExpression(tok->astOperand2());
} }
if (Token::Match(tok, "( %type%")) if (Token::Match(tok, "( %type%"))

View File

@ -160,6 +160,8 @@ const Token *findLambdaEndToken(const Token *first);
*/ */
bool isLikelyStreamRead(bool cpp, const Token *op); bool isLikelyStreamRead(bool cpp, const Token *op);
bool isCPPCast(const Token* tok);
bool isConstVarExpression(const Token *tok); bool isConstVarExpression(const Token *tok);
/** /**

View File

@ -2798,12 +2798,15 @@ void CheckOther::checkConstArgument()
continue; continue;
if (!tok->hasKnownIntValue()) if (!tok->hasKnownIntValue())
continue; continue;
if (Token::Match(tok, "%var%"))
continue;
if (Token::Match(tok, "++|--")) if (Token::Match(tok, "++|--"))
continue; continue;
if (isConstVarExpression(tok)) if (isConstVarExpression(tok))
continue; continue;
const Token * tok2 = tok;
if (isCPPCast(tok2))
tok2 = tok2->astOperand2();
if (Token::Match(tok2, "%var%"))
continue;
constArgumentError(tok, tok->astParent()->previous(), &tok->values().front()); constArgumentError(tok, tok->astParent()->previous(), &tok->values().front());
} }
} }

View File

@ -7554,6 +7554,13 @@ private:
" g(x + 1);\n" " g(x + 1);\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void g(int);\n"
"void f() {\n"
" char i = 1;\n"
" g(static_cast<int>(i));\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
}; };