diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index fc0a73729..8951d5768 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1533,7 +1533,7 @@ void Tokenizer::simplifyTypedef() Token *tok3 = tok2->next(); // handle missing variable name - if (tok2->strAt(3) == ")" || tok2->strAt(3) == ",") + if (tok2->strAt(3) == ")" || tok2->strAt(3) == "," || tok2->strAt(3) == "(") tok2 = tok2->tokAt(2); else tok2 = tok2->tokAt(3); @@ -1544,12 +1544,15 @@ void Tokenizer::simplifyTypedef() tok2 = tok2->tokAt(2); // skip over function parameters - if (tok2->strAt(1) == "(") { - tok2 = tok2->linkAt(1); + if (tok2->str() == "(" ) + tok2 = tok2->link(); - if (tok2->strAt(1) == "const") - tok2 = tok2->next(); - } + if (tok2->strAt(1) == "(") + tok2 = tok2->linkAt(1); + + // skip over const/noexcept + while (Token::Match(tok2->next(), "const|noexcept")) + tok2 = tok2->next(); tok2->insertToken(")"); tok2 = tok2->next(); diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 0b7d78f47..30f2555cf 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -164,6 +164,7 @@ private: TEST_CASE(simplifyTypedef126); // ticket #5953 TEST_CASE(simplifyTypedef127); // ticket #8878 TEST_CASE(simplifyTypedef128); // ticket #9053 + TEST_CASE(simplifyTypedef129); TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -2544,6 +2545,63 @@ private: ASSERT_EQUALS(exp, tok(code, false)); } + void simplifyTypedef129() { + { + const char code[] = "class c {\n" + " typedef char foo[4];\n" + " foo &f ;\n" + "};"; + + const char exp [] = "class c { char ( & f ) [ 4 ] ; } ;"; + ASSERT_EQUALS(exp, tok(code, false)); + } + + { + const char code[] = "class c {\n" + " typedef char foo[4];\n" + " const foo &f;\n" + "};"; + + const char exp [] = "class c { const char ( & f ) [ 4 ] ; } ;"; + ASSERT_EQUALS(exp, tok(code, false)); + } + + { + const char code[] = "class c {\n" + " typedef char foo[4];\n" + " foo _a;\n" + " constexpr const foo &c_str() const noexcept { return _a; }\n" + "};"; + + const char exp [] = "class c { char _a [ 4 ] ; const const char ( & c_str ( ) const noexcept ) [ 4 ] { return _a ; } } ;"; + ASSERT_EQUALS(exp, tok(code, false)); + } + + { + const char code[] = "class c {\n" + " typedef char foo[4];\n" + " foo _a;\n" + " constexpr operator foo &() const noexcept { return _a; }\n" + "};"; + + const char actual [] = "class c { char _a [ 4 ] ; const operatorchar ( & ( ) const noexcept ) [ 4 ] { return _a ; } } ;"; + const char exp [] = "class c { char _a [ 4 ] ; const operator char ( & ( ) const noexcept ) [ 4 ] { return _a ; } } ;"; + TODO_ASSERT_EQUALS(exp, actual, tok(code, false)); + } + + { + const char code[] = "class c {\n" + " typedef char foo[4];\n" + " foo _a;\n" + " constexpr operator const foo &() const noexcept { return _a; }\n" + "};"; + + const char actual [] = "class c { char _a [ 4 ] ; const operatorconstchar ( & ( ) const noexcept ) [ 4 ] { return _a ; } } ;"; + const char exp [] = "class c { char _a [ 4 ] ; const operator const char ( & ( ) const noexcept ) [ 4 ] { return _a ; } } ;"; + TODO_ASSERT_EQUALS(exp, actual, tok(code, false)); + } + } + void simplifyTypedefFunction1() { { const char code[] = "typedef void (*my_func)();\n"