Fix FP constStatement with more complex expression (#3959)

This commit is contained in:
chrchr-github 2022-03-30 22:00:57 +02:00 committed by GitHub
parent 32ded1602b
commit c85e7e7d2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -1778,7 +1778,7 @@ static bool isConstStatement(const Token *tok, bool cpp)
if (Token::simpleMatch(tok->previous(), "sizeof ("))
return true;
if (isCPPCast(tok)) {
if (Token::simpleMatch(tok->astOperand1(), "dynamic_cast") && Token::simpleMatch(tok->astOperand1()->next()->link()->previous(), "& >"))
if (Token::simpleMatch(tok->astOperand1(), "dynamic_cast") && Token::simpleMatch(tok->astOperand1()->linkAt(1)->previous(), "& >"))
return false;
return isWithoutSideEffects(cpp, tok) && isConstStatement(tok->astOperand2(), cpp);
}
@ -1793,7 +1793,10 @@ static bool isConstStatement(const Token *tok, bool cpp)
const Token* lml = previousBeforeAstLeftmostLeaf(tok);
if (lml)
lml = lml->next();
return lml && !isLikelyStream(cpp, lml) && isConstStatement(tok->astOperand2(), cpp);
const Token* stream = lml;
while (stream && Token::Match(stream->astParent(), ".|[|("))
stream = stream->astParent();
return (!stream || !isLikelyStream(cpp, stream)) && isConstStatement(tok->astOperand2(), cpp);
}
}
if (Token::simpleMatch(tok, "?") && Token::simpleMatch(tok->astOperand2(), ":")) // ternary operator

View File

@ -377,6 +377,25 @@ private:
" V << a, b, c, d;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("struct S { Eigen::Vector4d V; };\n"
"struct T { int a, int b, int c, int d; };\n"
"void f(S& s, const T& t) {\n"
" s.V << t.a, t.b, t.c, t.d;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("struct S { Eigen::Vector4d V[2]; };\n"
"void f(int a, int b, int c, int d) {\n"
" S s[1];\n"
" s[0].V[1] << a, b, c, d;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" a.b[4][3].c()->d << x , y, z;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
// #8451