Skip to content

Commit dfc639c

Browse files
authored
Fix #14301: SymbolDatabase: c code, inner struct and outer struct has same name (danmar#7999)
1 parent 2d0326d commit dfc639c

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

lib/symboldatabase.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6571,23 +6571,6 @@ const Type* SymbolDatabase::findType(const Token *startTok, const Scope *startSc
65716571
if (startTok->str() == startScope->className && startScope->isClassOrStruct() && startTok->strAt(1) != "::")
65726572
return startScope->definedType;
65736573

6574-
if (startTok->isC()) {
6575-
const Scope* scope = startScope;
6576-
while (scope) {
6577-
if (startTok->str() == scope->className && scope->isClassOrStruct())
6578-
return scope->definedType;
6579-
const Scope* typeScope = scope->findRecordInNestedList(startTok->str(), /*isC*/ true);
6580-
if (typeScope) {
6581-
if (startTok->str() == typeScope->className && typeScope->isClassOrStruct()) {
6582-
if (const Type* type = typeScope->definedType)
6583-
return type;
6584-
}
6585-
}
6586-
scope = scope->nestedIn;
6587-
}
6588-
return nullptr;
6589-
}
6590-
65916574
const Scope* start_scope = startScope;
65926575

65936576
// absolute path - directly start in global scope

test/cfg/gtk.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ void g_new_if_test()
293293
};
294294

295295
const struct a * pNew3;
296-
// cppcheck-suppress valueFlowBailoutIncompleteVar
297296
if (pNew3 = g_new(struct a, 6)) {
298297
printf("%p", pNew3);
299298
}
@@ -306,7 +305,6 @@ void g_new0_test()
306305
int b;
307306
};
308307
// valid
309-
// cppcheck-suppress valueFlowBailoutIncompleteVar
310308
struct a * pNew1 = g_new0(struct a, 5);
311309
printf("%p", pNew1);
312310
g_free(pNew1);
@@ -325,7 +323,6 @@ void g_try_new_test()
325323
int b;
326324
};
327325
// valid
328-
// cppcheck-suppress valueFlowBailoutIncompleteVar
329326
struct a * pNew1 = g_try_new(struct a, 5);
330327
printf("%p", pNew1);
331328
g_free(pNew1);
@@ -343,7 +340,6 @@ void g_try_new0_test()
343340
int b;
344341
};
345342
// valid
346-
// cppcheck-suppress valueFlowBailoutIncompleteVar
347343
struct a * pNew1 = g_try_new0(struct a, 5);
348344
printf("%p", pNew1);
349345
g_free(pNew1);
@@ -361,7 +357,7 @@ void g_renew_test()
361357
struct a {
362358
int b;
363359
};
364-
// cppcheck-suppress [leakReturnValNotUsed,valueFlowBailoutIncompleteVar]
360+
// cppcheck-suppress leakReturnValNotUsed
365361
g_renew(struct a, NULL, 1);
366362

367363
struct a * pNew = g_new(struct a, 1);
@@ -376,7 +372,7 @@ void g_try_renew_test()
376372
struct a {
377373
int b;
378374
};
379-
// cppcheck-suppress [leakReturnValNotUsed,valueFlowBailoutIncompleteVar]
375+
// cppcheck-suppress leakReturnValNotUsed
380376
g_try_renew(struct a, NULL, 1);
381377

382378
struct a * pNew = g_try_new(struct a, 1);

test/testsymboldatabase.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ class TestSymbolDatabase : public TestFixture {
465465
TEST_CASE(enum18);
466466
TEST_CASE(enum19);
467467

468+
TEST_CASE(struct1);
469+
468470
TEST_CASE(sizeOfType);
469471

470472
TEST_CASE(isImplicitlyVirtual);
@@ -6859,6 +6861,42 @@ class TestSymbolDatabase : public TestFixture {
68596861
}
68606862
}
68616863

6864+
void struct1() {
6865+
GET_SYMBOL_DB_C("struct deer {\n"
6866+
" uint16_t a;\n"
6867+
" uint16_t b;\n"
6868+
"};\n"
6869+
"void herd ( void ) {\n"
6870+
" struct deer {\n"
6871+
" uint16_t a;\n"
6872+
" };\n"
6873+
"}");
6874+
6875+
ASSERT_EQUALS("", errout_str());
6876+
ASSERT(db);
6877+
6878+
const Token* deer = Token::findsimplematch(tokenizer.tokens(), "deer {");
6879+
ASSERT(deer);
6880+
ASSERT(deer->type());
6881+
ASSERT(deer->type()->classScope);
6882+
const Token* tok = deer->next();
6883+
ASSERT(tok->scope());
6884+
ASSERT_EQUALS_ENUM(ScopeType::eStruct, tok->scope()->type);
6885+
ASSERT_EQUALS(tok, tok->scope()->bodyStart);
6886+
ASSERT_EQUALS(tok->scope(), deer->type()->classScope);
6887+
6888+
const Token* secondDeer = Token::findsimplematch(tok, "deer {");
6889+
ASSERT(secondDeer);
6890+
ASSERT(secondDeer != deer);
6891+
ASSERT(secondDeer->type());
6892+
ASSERT(secondDeer->type()->classScope);
6893+
tok = secondDeer->next();
6894+
ASSERT(tok->scope());
6895+
ASSERT_EQUALS_ENUM(ScopeType::eStruct, tok->scope()->type);
6896+
ASSERT_EQUALS(tok, tok->scope()->bodyStart);
6897+
ASSERT_EQUALS(tok->scope(), secondDeer->type()->classScope);
6898+
}
6899+
68626900
void sizeOfType() {
68636901
// #7615 - crash in Symboldatabase::sizeOfType()
68646902
GET_SYMBOL_DB("enum e;\n"

0 commit comments

Comments
 (0)