From 5a21851bb7943fb29268043209d1a8e4e2780396 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 14 Sep 2023 12:14:08 +0200 Subject: [PATCH] Fix #11981 internalAstError with using declaration (#5446) --- lib/tokenize.cpp | 15 ++++++++++++--- test/testsimplifyusing.cpp | 9 +++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d1c5c1d04..141034fb7 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3236,9 +3236,18 @@ bool Tokenizer::simplifyUsing() } } - // just replace simple type aliases - TokenList::copyTokens(tok1, start, usingEnd->previous()); - tok1->deleteThis(); + // Is this a "T()" expression where T is a pointer type? + if (Token::Match(tok1, "%name% ( )") && !pointers.empty()) { + Token* tok2 = tok1->linkAt(1); + tok1->deleteThis(); + TokenList::copyTokens(tok1, start, usingEnd->previous()); + tok2->insertToken("0"); + after = tok2->next(); + } + else { // just replace simple type aliases + TokenList::copyTokens(tok1, start, usingEnd->previous()); + tok1->deleteThis(); + } substitute = true; } } else { diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 02cc14aaf..33d3cb7bb 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -71,6 +71,7 @@ private: TEST_CASE(simplifyUsing26); // #11090 TEST_CASE(simplifyUsing27); TEST_CASE(simplifyUsing28); + TEST_CASE(simplifyUsing29); TEST_CASE(simplifyUsing8970); TEST_CASE(simplifyUsing8971); @@ -684,6 +685,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void simplifyUsing29() { // #11981 + const char code[] = "using T = int*;\n" + "void f(T = T()) {}\n"; + const char expected[] = "void f ( int * = ( int * ) 0 ) { }"; + ASSERT_EQUALS(expected, tok(code, cppcheck::Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS("", errout.str()); + } + void simplifyUsing8970() { const char code[] = "using V = std::vector;\n" "struct A {\n"