Skip to content

Commit a666e31

Browse files
Fix #11517 FP constVariable with dynamic_cast (#4753)
1 parent 4f91549 commit a666e31

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6684,9 +6684,13 @@ static const Token* parsedecl(const Token* type,
66846684
valuetype->sign = vt->sign;
66856685
valuetype->constness = vt->constness;
66866686
valuetype->originalTypeName = vt->originalTypeName;
6687+
const bool hasConst = Token::simpleMatch(type->previous(), "const");
66876688
while (Token::Match(type, "%name%|*|&|::") && !type->variable()) {
6688-
if (type->str() == "*")
6689-
valuetype->pointer++;
6689+
if (type->str() == "*") {
6690+
valuetype->pointer = 1;
6691+
if (hasConst)
6692+
valuetype->constness = 1;
6693+
}
66906694
if (type->str() == "const")
66916695
valuetype->constness |= (1 << valuetype->pointer);
66926696
type = type->next();

test/testsymboldatabase.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ class TestSymbolDatabase : public TestFixture {
502502
TEST_CASE(auto16);
503503
TEST_CASE(auto17); // #11163
504504
TEST_CASE(auto18);
505+
TEST_CASE(auto19);
505506

506507
TEST_CASE(unionWithConstructor);
507508

@@ -8811,6 +8812,42 @@ class TestSymbolDatabase : public TestFixture {
88118812
TODO_ASSERT(autoTok->valueType()->reference == Reference::LValue);
88128813
}
88138814

8815+
void auto19() { // #11517
8816+
{
8817+
GET_SYMBOL_DB("void f(const std::vector<void*>& v) {\n"
8818+
" for (const auto* h : v)\n"
8819+
" if (h) {}\n"
8820+
"}\n");
8821+
ASSERT_EQUALS(3, db->variableList().size());
8822+
8823+
const Variable* h = db->variableList()[2];
8824+
ASSERT(h->isPointer());
8825+
ASSERT(!h->isConst());
8826+
const Token* varTok = Token::findsimplematch(tokenizer.tokens(), "h )");
8827+
ASSERT(varTok && varTok->valueType());
8828+
ASSERT_EQUALS(varTok->valueType()->constness, 1);
8829+
ASSERT_EQUALS(varTok->valueType()->pointer, 1);
8830+
}
8831+
{
8832+
GET_SYMBOL_DB("struct B { virtual void f() {} };\n"
8833+
"struct D : B {};\n"
8834+
"void g(const std::vector<B*>& v) {\n"
8835+
" for (auto* b : v)\n"
8836+
" if (auto d = dynamic_cast<D*>(b))\n"
8837+
" d->f();\n"
8838+
"}\n");
8839+
ASSERT_EQUALS(4, db->variableList().size());
8840+
8841+
const Variable* b = db->variableList()[2];
8842+
ASSERT(b->isPointer());
8843+
ASSERT(!b->isConst());
8844+
const Token* varTok = Token::findsimplematch(tokenizer.tokens(), "b )");
8845+
ASSERT(varTok && varTok->valueType());
8846+
ASSERT_EQUALS(varTok->valueType()->constness, 0);
8847+
ASSERT_EQUALS(varTok->valueType()->pointer, 1);
8848+
}
8849+
}
8850+
88148851
void unionWithConstructor() {
88158852
GET_SYMBOL_DB("union Fred {\n"
88168853
" Fred(int x) : i(x) { }\n"

0 commit comments

Comments
 (0)