Fix #11517 FP constVariable with dynamic_cast (#4753)

This commit is contained in:
chrchr-github 2023-02-06 22:05:07 +01:00 committed by GitHub
parent 4f915499d4
commit a666e31801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View File

@ -6684,9 +6684,13 @@ static const Token* parsedecl(const Token* type,
valuetype->sign = vt->sign;
valuetype->constness = vt->constness;
valuetype->originalTypeName = vt->originalTypeName;
const bool hasConst = Token::simpleMatch(type->previous(), "const");
while (Token::Match(type, "%name%|*|&|::") && !type->variable()) {
if (type->str() == "*")
valuetype->pointer++;
if (type->str() == "*") {
valuetype->pointer = 1;
if (hasConst)
valuetype->constness = 1;
}
if (type->str() == "const")
valuetype->constness |= (1 << valuetype->pointer);
type = type->next();

View File

@ -502,6 +502,7 @@ private:
TEST_CASE(auto16);
TEST_CASE(auto17); // #11163
TEST_CASE(auto18);
TEST_CASE(auto19);
TEST_CASE(unionWithConstructor);
@ -8811,6 +8812,42 @@ private:
TODO_ASSERT(autoTok->valueType()->reference == Reference::LValue);
}
void auto19() { // #11517
{
GET_SYMBOL_DB("void f(const std::vector<void*>& v) {\n"
" for (const auto* h : v)\n"
" if (h) {}\n"
"}\n");
ASSERT_EQUALS(3, db->variableList().size());
const Variable* h = db->variableList()[2];
ASSERT(h->isPointer());
ASSERT(!h->isConst());
const Token* varTok = Token::findsimplematch(tokenizer.tokens(), "h )");
ASSERT(varTok && varTok->valueType());
ASSERT_EQUALS(varTok->valueType()->constness, 1);
ASSERT_EQUALS(varTok->valueType()->pointer, 1);
}
{
GET_SYMBOL_DB("struct B { virtual void f() {} };\n"
"struct D : B {};\n"
"void g(const std::vector<B*>& v) {\n"
" for (auto* b : v)\n"
" if (auto d = dynamic_cast<D*>(b))\n"
" d->f();\n"
"}\n");
ASSERT_EQUALS(4, db->variableList().size());
const Variable* b = db->variableList()[2];
ASSERT(b->isPointer());
ASSERT(!b->isConst());
const Token* varTok = Token::findsimplematch(tokenizer.tokens(), "b )");
ASSERT(varTok && varTok->valueType());
ASSERT_EQUALS(varTok->valueType()->constness, 0);
ASSERT_EQUALS(varTok->valueType()->pointer, 1);
}
}
void unionWithConstructor() {
GET_SYMBOL_DB("union Fred {\n"
" Fred(int x) : i(x) { }\n"