Fixed #9825 (redundantAssignment: Do not warn about volatile data)

This commit is contained in:
Daniel Marjamäki 2020-08-06 22:18:08 +02:00
parent 4fabd1e059
commit 5372e93964
2 changed files with 13 additions and 3 deletions

View File

@ -2223,13 +2223,15 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
return Result(Result::Type::NONE);
}
static bool hasVolatileCast(const Token *expr)
static bool hasVolatileCastOrVar(const Token *expr)
{
bool ret = false;
visitAstNodes(expr,
[&ret](const Token *tok) {
if (Token::simpleMatch(tok, "( volatile"))
ret = true;
else if (tok->variable() && tok->variable()->isVolatile())
ret = true;
return ret ? ChildrenToVisit::none : ChildrenToVisit::op1_and_op2;
});
return ret;
@ -2316,7 +2318,7 @@ bool FwdAnalysis::hasOperand(const Token *tok, const Token *lhs) const
const Token *FwdAnalysis::reassign(const Token *expr, const Token *startToken, const Token *endToken)
{
if (hasVolatileCast(expr))
if (hasVolatileCastOrVar(expr))
return nullptr;
mWhat = What::Reassign;
Result result = check(expr, startToken, endToken);
@ -2327,7 +2329,7 @@ bool FwdAnalysis::unusedValue(const Token *expr, const Token *startToken, const
{
if (isEscapedAlias(expr))
return false;
if (hasVolatileCast(expr))
if (hasVolatileCastOrVar(expr))
return false;
mWhat = What::UnusedValue;
Result result = check(expr, startToken, endToken);

View File

@ -6776,6 +6776,14 @@ private:
ASSERT_EQUALS("test.cpp:4:style:Variable '*var' is reassigned a value before the old one has been used.\n"
"test.cpp:3:note:*var is assigned\n"
"test.cpp:4:note:*var is overwritten\n", errout.str());
// Volatile variables
check("void f() {\n"
" volatile char *reg = (volatile char *)0x12345;\n"
" *reg = 12;\n"
" *reg = 34;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void redundantVarAssignment_trivial() {