Skip to content

Commit e43606c

Browse files
authored
Fix #14419 and #14444: Issues with AST/value type for enum declarations (danmar#8160)
1 parent e1a6600 commit e43606c

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6852,7 +6852,7 @@ void SymbolDatabase::setValueType(Token* tok, const Enumerator& enumerator, cons
68526852
if (valuetype.type == ValueType::Type::UNKNOWN_TYPE)
68536853
valuetype.fromLibraryType(type->expressionString(), mSettings);
68546854

6855-
if (valuetype.isIntegral()) {
6855+
if (valuetype.sign == ValueType::UNKNOWN_SIGN && valuetype.isIntegral()) {
68566856
if (type->isSigned())
68576857
valuetype.sign = ValueType::Sign::SIGNED;
68586858
else if (type->isUnsigned())

lib/tokenlist.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,13 @@ static Token * createAstAtToken(Token *tok)
15851585
if (Token::Match(tok2, "%var% [;,)]"))
15861586
return tok2;
15871587
}
1588+
if (Token::Match(tok, "enum class| %name%| :")) {
1589+
if (Token::simpleMatch(tok->next(), "class"))
1590+
tok = tok->next();
1591+
if (Token::Match(tok->next(), "%name%"))
1592+
tok = tok->next();
1593+
return tok->next();
1594+
}
15881595
if (Token *const endTok = skipMethodDeclEnding(tok)) {
15891596
Token *tok2 = tok;
15901597
do {

test/testsymboldatabase.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ class TestSymbolDatabase : public TestFixture {
464464
TEST_CASE(enum17);
465465
TEST_CASE(enum18);
466466
TEST_CASE(enum19);
467+
TEST_CASE(enum20); // #14419
467468

468469
TEST_CASE(struct1);
469470

@@ -6860,6 +6861,23 @@ class TestSymbolDatabase : public TestFixture {
68606861
}
68616862
}
68626863

6864+
void enum20() { // #14419
6865+
{
6866+
GET_SYMBOL_DB("enum class myclass : uint8_t { A = 0U };\n");
6867+
const Token *A = Token::findsimplematch(tokenizer.tokens(), "A");
6868+
ASSERT(A && A->valueType() && A->valueType()->isEnum());
6869+
ASSERT_EQUALS_ENUM(ValueType::CHAR, A->valueType()->type);
6870+
ASSERT_EQUALS_ENUM(ValueType::UNSIGNED, A->valueType()->sign);
6871+
}
6872+
{
6873+
GET_SYMBOL_DB("enum myclass : uint8_t { A = 0U };\n");
6874+
const Token *A = Token::findsimplematch(tokenizer.tokens(), "A");
6875+
ASSERT(A && A->valueType() && A->valueType()->isEnum());
6876+
ASSERT_EQUALS_ENUM(ValueType::CHAR, A->valueType()->type);
6877+
ASSERT_EQUALS_ENUM(ValueType::UNSIGNED, A->valueType()->sign);
6878+
}
6879+
}
6880+
68636881
void struct1() {
68646882
GET_SYMBOL_DB_C("struct deer {\n"
68656883
" uint16_t a;\n"

test/testtokenize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ class TestTokenizer : public TestFixture {
423423
TEST_CASE(astdesignatedinit);
424424
TEST_CASE(astrvaluedecl);
425425
TEST_CASE(astorkeyword);
426+
TEST_CASE(astenumdecl);
426427

427428
TEST_CASE(startOfExecutableScope);
428429

@@ -7389,6 +7390,11 @@ class TestTokenizer : public TestFixture {
73897390
ASSERT_EQUALS("ifsp.\"\"==sp.0==||(", testAst("void f() { if (s.p == \"\" or s.p == 0) {} }"));
73907391
}
73917392

7393+
void astenumdecl() {
7394+
ASSERT_EQUALS("A0U=", testAst("enum class myclass : unsigned char { A = 0U, };"));
7395+
ASSERT_EQUALS("A0U=", testAst("enum myclass : unsigned char { A = 0U, };"));
7396+
}
7397+
73927398
#define isStartOfExecutableScope(offset, code) isStartOfExecutableScope_(offset, code, __FILE__, __LINE__)
73937399
template<size_t size>
73947400
bool isStartOfExecutableScope_(int offset, const char (&code)[size], const char* file, int line) {

0 commit comments

Comments
 (0)