From 8bbe01724be3eadfc443e2385697cb9ba15688d6 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Thu, 1 Jul 2021 21:01:14 -0400 Subject: [PATCH] fix #10335 (Type alias remains unknown with using) --- lib/tokenize.cpp | 23 +++++++++++++++++------ test/testsimplifyusing.cpp | 8 ++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index bd6da509081..c626019d989 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2368,7 +2368,15 @@ bool Tokenizer::simplifyUsing() std::string scope1 = currentScope1->fullName; bool skip = false; // don't erase type aliases we can't parse + Token *enumOpenBrace = nullptr; for (Token* tok1 = startToken; !skip && tok1 && tok1 != endToken; tok1 = tok1->next()) { + // skip enum body + if (tok1 && tok1 == enumOpenBrace) { + tok1 = tok1->link(); + enumOpenBrace = nullptr; + continue; + } + if ((Token::Match(tok1, "{|}|namespace|class|struct|union") && tok1->strAt(-1) != "using") || Token::Match(tok1, "using namespace %name% ;|::")) { setScopeInfo(tok1, ¤tScope1); @@ -2389,12 +2397,15 @@ bool Tokenizer::simplifyUsing() continue; } - // skip enum definitions - if (tok1->str() == "enum") - skipEnumBody(&tok1); - - if (!tok1) - break; + // check for enum with body + if (tok1->str() == "enum") { + Token *defStart = tok1; + while (Token::Match(defStart, "%name%|::|:")) + defStart = defStart->next(); + if (Token::simpleMatch(defStart, "{")) + enumOpenBrace = defStart; + continue; + } // check for member function and adjust scope if (isMemberFunction(tok1)) { diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index d6e4846e0a3..ae4317836f8 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -90,6 +90,7 @@ class TestSimplifyUsing : public TestFixture { TEST_CASE(simplifyUsing10171); TEST_CASE(simplifyUsing10172); TEST_CASE(simplifyUsing10173); + TEST_CASE(simplifyUsing10335); } std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Native, bool debugwarnings = true) { @@ -1304,6 +1305,13 @@ class TestSimplifyUsing : public TestFixture { ASSERT_EQUALS(exp, tok(code, true)); } } + + void simplifyUsing10335() { + const char code[] = "using uint8_t = unsigned char;\n" + "enum E : uint8_t { E0 };"; + const char exp[] = "enum E : unsigned char { E0 } ;"; + ASSERT_EQUALS(exp, tok(code, false)); + } }; REGISTER_TEST(TestSimplifyUsing)