Skip to content

Commit 92b4225

Browse files
Fix #11559 FN functionConst (#4795)
1 parent 674231a commit 92b4225

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,11 +1373,15 @@ void SymbolDatabase::createSymbolDatabaseEnums()
13731373

13741374
// find enumerators
13751375
for (const Token* tok = mTokenizer->list.front(); tok != mTokenizer->list.back(); tok = tok->next()) {
1376-
if (tok->tokType() != Token::eName)
1376+
const bool isVariable = (tok->tokType() == Token::eVariable && !tok->variable());
1377+
if (tok->tokType() != Token::eName && !isVariable)
13771378
continue;
13781379
const Enumerator * enumerator = findEnumerator(tok, tokensThatAreNotEnumeratorValues);
1379-
if (enumerator)
1380-
const_cast<Token *>(tok)->enumerator(enumerator);
1380+
if (enumerator) {
1381+
if (isVariable)
1382+
const_cast<Token*>(tok)->varId(0);
1383+
const_cast<Token*>(tok)->enumerator(enumerator);
1384+
}
13811385
}
13821386
}
13831387

@@ -4919,6 +4923,12 @@ const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::set<st
49194923
if (enumerator && !(enumerator->scope && enumerator->scope->enumClass))
49204924
return enumerator;
49214925

4926+
if (Token::simpleMatch(tok->astParent(), ".")) {
4927+
const Token* varTok = tok->astParent()->astOperand1();
4928+
if (varTok && varTok->variable() && varTok->variable()->type() && varTok->variable()->type()->classScope)
4929+
scope = varTok->variable()->type()->classScope;
4930+
}
4931+
49224932
for (std::vector<Scope *>::const_iterator s = scope->nestedList.cbegin(); s != scope->nestedList.cend(); ++s) {
49234933
enumerator = (*s)->findEnumerator(tokStr);
49244934

@@ -5433,6 +5443,8 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
54335443
if (rml)
54345444
valuetok = rml->previous();
54355445
}
5446+
if (vartok->isEnumerator())
5447+
valuetok = vartok;
54365448
const ValueType::MatchResult res = ValueType::matchParameter(valuetok->valueType(), var, funcarg);
54375449
if (res == ValueType::MatchResult::SAME)
54385450
++same;

test/testplatform.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class TestPlatform : public TestFixture {
182182
ASSERT_EQUALS(64, platform.long_long_bit);
183183
}
184184

185-
void valid_config_file_1() {
185+
void valid_config_file_1() const {
186186
// Valid platform configuration with all possible values specified.
187187
// Similar to the avr8 platform file.
188188
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
@@ -226,7 +226,7 @@ class TestPlatform : public TestFixture {
226226
ASSERT_EQUALS(64, platform.long_long_bit);
227227
}
228228

229-
void valid_config_file_2() {
229+
void valid_config_file_2() const {
230230
// Valid platform configuration with all possible values specified and
231231
// char_bit > 8.
232232
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
@@ -296,7 +296,7 @@ class TestPlatform : public TestFixture {
296296
TODO_ASSERT(!readPlatform(platform, xmldata));
297297
}
298298

299-
void valid_config_file_4() {
299+
void valid_config_file_4() const {
300300
// Valid platform configuration with all possible values specified and
301301
// set to 0.
302302
const char xmldata[] = "<?xml version=\"1.0\"?>\n"

test/testsymboldatabase.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ class TestSymbolDatabase : public TestFixture {
387387
TEST_CASE(enum9);
388388
TEST_CASE(enum10); // #11001
389389
TEST_CASE(enum11);
390+
TEST_CASE(enum12);
390391

391392
TEST_CASE(sizeOfType);
392393

@@ -5532,6 +5533,22 @@ class TestSymbolDatabase : public TestFixture {
55325533
ASSERT_EQUALS("", errout.str());
55335534
}
55345535

5536+
void enum12() {
5537+
GET_SYMBOL_DB_C("struct { enum E { E0 }; } t;\n"
5538+
"void f() {\n"
5539+
" if (t.E0) {}\n"
5540+
"}\n");
5541+
ASSERT(db != nullptr);
5542+
auto it = db->scopeList.begin();
5543+
std::advance(it, 2);
5544+
const Enumerator* E0 = it->findEnumerator("E0");
5545+
ASSERT(E0 && E0->value_known);
5546+
ASSERT_EQUALS(E0->value, 0);
5547+
const Token* const e = Token::findsimplematch(tokenizer.tokens(), "E0 )");
5548+
ASSERT(e && e->enumerator());
5549+
ASSERT_EQUALS(e->enumerator(), E0);
5550+
}
5551+
55355552
void sizeOfType() {
55365553
// #7615 - crash in Symboldatabase::sizeOfType()
55375554
GET_SYMBOL_DB("enum e;\n"
@@ -7011,6 +7028,20 @@ class TestSymbolDatabase : public TestFixture {
70117028
ASSERT(functok->function()->name() == "f");
70127029
ASSERT_EQUALS(3, functok->function()->tokenDef->linenr());
70137030
}
7031+
{
7032+
GET_SYMBOL_DB("struct T { enum E { E0 }; } t; \n" // #11559
7033+
"void f(const void*, T::E) {}\n"
7034+
"void f(const int&, T::E) {}\n"
7035+
"void g() {\n"
7036+
" f(nullptr, t.E0);\n"
7037+
"}\n");
7038+
ASSERT_EQUALS("", errout.str());
7039+
const Token *functok = Token::findsimplematch(tokenizer.tokens(), "f ( nullptr");
7040+
ASSERT(functok);
7041+
ASSERT(functok->function());
7042+
ASSERT(functok->function()->name() == "f");
7043+
ASSERT_EQUALS(2, functok->function()->tokenDef->linenr());
7044+
}
70147045
}
70157046

70167047
void findFunction45() {

0 commit comments

Comments
 (0)