Skip to content

Commit 3b93a89

Browse files
Fix #13787 Variable declaration with extra parentheses (danmar#7475)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent bbdb2b1 commit 3b93a89

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/symboldatabase.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const
19441944
return false;
19451945

19461946
// regular function?
1947-
else if (Token::Match(tok, "%name% (") && !isReservedName(tok) && tok->previous() &&
1947+
else if (Token::Match(tok, "%name% ( !!*") && !isReservedName(tok) && tok->previous() &&
19481948
(Token::Match(tok->previous(), "%name%|>|&|&&|*|::|~") || // Either a return type or scope qualifier in front of tok
19491949
outerScope->isClassOrStructOrUnion())) { // or a ctor/dtor
19501950
const Token* tok1 = tok->previous();
@@ -5130,6 +5130,8 @@ bool Scope::isVariableDeclaration(const Token* const tok, const Token*& vartok,
51305130
if (!localVarTok)
51315131
return false;
51325132

5133+
if (Token::simpleMatch(localVarTok, "( *") && Token::Match(localTypeTok->previous(), "class|struct|union|enum")) // not a function call
5134+
localVarTok = localVarTok->next();
51335135
while (Token::Match(localVarTok, "const|*|&"))
51345136
localVarTok = localVarTok->next();
51355137

test/testsymboldatabase.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ class TestSymbolDatabase : public TestFixture {
469469
TEST_CASE(isFunction1); // UNKNOWN_MACRO(a,b) { .. }
470470
TEST_CASE(isFunction2);
471471
TEST_CASE(isFunction3);
472+
TEST_CASE(isFunction4);
472473

473474
TEST_CASE(findFunction1);
474475
TEST_CASE(findFunction2); // mismatch: parameter passed by address => reference argument
@@ -6992,6 +6993,19 @@ class TestSymbolDatabase : public TestFixture {
69926993
ASSERT(ret->scope() && ret->scope()->type == ScopeType::eFunction);
69936994
}
69946995

6996+
void isFunction4() {
6997+
GET_SYMBOL_DB("struct S (*a[10]);\n" // #13787
6998+
"void f(int i, struct S* p) {\n"
6999+
" a[i] = &p[i];\n"
7000+
"}\n");
7001+
ASSERT(db != nullptr);
7002+
ASSERT_EQUALS(2, db->scopeList.size());
7003+
ASSERT_EQUALS(1, db->scopeList.front().functionList.size());
7004+
const Token* a = Token::findsimplematch(tokenizer.tokens(), "a [ i");
7005+
ASSERT(a && a->variable());
7006+
ASSERT(a->variable()->scope() && a->variable()->scope()->type == ScopeType::eGlobal);
7007+
}
7008+
69957009

69967010
void findFunction1() {
69977011
GET_SYMBOL_DB("int foo(int x);\n" /* 1 */

0 commit comments

Comments
 (0)