Fixed #9559 (Multiple checks to std::atomic are not redundant)

This commit is contained in:
Daniel Marjamäki 2020-04-19 17:29:40 +02:00
parent 3c56ccc33f
commit e8bbfdbfee
4 changed files with 34 additions and 1 deletions

View File

@ -1871,7 +1871,7 @@ void Variable::evaluate(const Settings* settings)
setFlag(fIsStatic, true);
else if (tok->str() == "extern")
setFlag(fIsExtern, true);
else if (tok->str() == "volatile")
else if (tok->str() == "volatile" || Token::simpleMatch(tok, "std :: atomic <"))
setFlag(fIsVolatile, true);
else if (tok->str() == "mutable")
setFlag(fIsMutable, true);

View File

@ -3928,6 +3928,8 @@ struct ValueFlowConditionHandler {
Condition cond = parse(tok);
if (!cond.vartok)
continue;
if (cond.vartok->variable() && cond.vartok->variable()->isVolatile())
continue;
if (cond.true_values.empty() || cond.false_values.empty())
continue;

View File

@ -150,6 +150,7 @@ private:
TEST_CASE(isVariablePointerToVolatilePointer);
TEST_CASE(isVariablePointerToConstVolatilePointer);
TEST_CASE(isVariableMultiplePointersAndQualifiers);
TEST_CASE(variableVolatile);
TEST_CASE(VariableValueType1);
TEST_CASE(VariableValueType2);
@ -1134,6 +1135,21 @@ private:
ASSERT(false == v.isReference());
}
void variableVolatile() {
GET_SYMBOL_DB("std::atomic<int> x;\n"
"volatile int y;\n");
const Token *x = Token::findsimplematch(tokenizer.tokens(), "x");
ASSERT(x);
ASSERT(x->variable());
ASSERT(x->variable()->isVolatile());
const Token *y = Token::findsimplematch(tokenizer.tokens(), "y");
ASSERT(y);
ASSERT(y->variable());
ASSERT(y->variable()->isVolatile());
}
void arrayMemberVar1() {
GET_SYMBOL_DB("struct Foo {\n"
" int x;\n"

View File

@ -2370,6 +2370,21 @@ private:
"}\n";
ASSERT_EQUALS(false, testValueOfXKnown(code, 6U, 0));
ASSERT_EQUALS(true, testValueOfX(code, 6U, 0));
// volatile variable
code = "void foo(const volatile int &x) {\n"
" if (x==1) {\n"
" return x;\n"
" }"
"}";
ASSERT_EQUALS(false, testValueOfXKnown(code, 3U, 1));
code = "void foo(const std::atomic<int> &x) {\n"
" if (x==2) {\n"
" return x;\n"
" }"
"}";
ASSERT_EQUALS(false, testValueOfXKnown(code, 3U, 2));
}
void valueFlowAfterConditionExpr() {