Skip to content

Commit

Permalink
Fixed #11908 (SymbolDatabase: array not parsed properly 'int (**rs)[3…
Browse files Browse the repository at this point in the history
…2]') (danmar#5430)
  • Loading branch information
danmar authored Sep 10, 2023
1 parent f9b18bc commit 8771912
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2243,7 +2243,7 @@ void Variable::evaluate(const Settings* settings)
setFlag(fIsConst, true);
setFlag(fIsStatic, true);
} else if (tok->str() == "*") {
setFlag(fIsPointer, !isArray() || (isContainer && !Token::Match(tok->next(), "%name% [")) || Token::Match(tok->previous(), "( * %name% )"));
setFlag(fIsPointer, !isArray() || (isContainer && !Token::Match(tok->next(), "%name% [")) || Token::Match(tok, "* const| %name% )"));
setFlag(fIsConst, false); // Points to const, isn't necessarily const itself
} else if (tok->str() == "&") {
if (isReference())
Expand Down
36 changes: 34 additions & 2 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(test_isVariableDeclarationIdentifiesDeclarationWithMultipleIndirection);
TEST_CASE(test_isVariableDeclarationIdentifiesArray);
TEST_CASE(test_isVariableDeclarationIdentifiesPointerArray);
TEST_CASE(test_isVariableDeclarationIdentifiesOfArrayPointers);
TEST_CASE(test_isVariableDeclarationIdentifiesOfArrayPointers1);
TEST_CASE(test_isVariableDeclarationIdentifiesOfArrayPointers2);
TEST_CASE(test_isVariableDeclarationIdentifiesOfArrayPointers3);
TEST_CASE(test_isVariableDeclarationIdentifiesArrayOfFunctionPointers);
TEST_CASE(isVariableDeclarationIdentifiesTemplatedPointerVariable);
TEST_CASE(isVariableDeclarationIdentifiesTemplatedPointerToPointerVariable);
Expand Down Expand Up @@ -872,7 +874,7 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(false == v.isReference());
}

void test_isVariableDeclarationIdentifiesOfArrayPointers() {
void test_isVariableDeclarationIdentifiesOfArrayPointers1() {
reset();
GET_SYMBOL_DB("A (*a)[5];");
const bool result = db->scopeList.front().isVariableDeclaration(tokenizer.tokens(), vartok, typetok);
Expand All @@ -887,6 +889,36 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(false == v.isReference());
}

void test_isVariableDeclarationIdentifiesOfArrayPointers2() {
reset();
GET_SYMBOL_DB("A (*const a)[5];");
const bool result = db->scopeList.front().isVariableDeclaration(tokenizer.tokens(), vartok, typetok);
ASSERT_EQUALS(true, result);
ASSERT_EQUALS("a", vartok->str());
ASSERT_EQUALS("A", typetok->str());
Variable v(vartok, typetok, vartok->previous(), 0, AccessControl::Public, nullptr, nullptr, &settings1);
ASSERT(true == v.isPointer());
ASSERT(false == v.isArray());
ASSERT(true == v.isPointerToArray());
ASSERT(false == v.isPointerArray());
ASSERT(false == v.isReference());
}

void test_isVariableDeclarationIdentifiesOfArrayPointers3() {
reset();
GET_SYMBOL_DB("A (** a)[5];");
const bool result = db->scopeList.front().isVariableDeclaration(tokenizer.tokens(), vartok, typetok);
ASSERT_EQUALS(true, result);
ASSERT_EQUALS("a", vartok->str());
ASSERT_EQUALS("A", typetok->str());
Variable v(vartok, typetok, vartok->previous(), 0, AccessControl::Public, nullptr, nullptr, &settings1);
ASSERT(true == v.isPointer());
ASSERT(false == v.isArray());
ASSERT(true == v.isPointerToArray());
ASSERT(false == v.isPointerArray());
ASSERT(false == v.isReference());
}

void test_isVariableDeclarationIdentifiesArrayOfFunctionPointers() {
reset();
GET_SYMBOL_DB("int (*a[])(int) = { g };"); // #11596
Expand Down

0 comments on commit 8771912

Please sign in to comment.