Skip to content

Commit 5a21851

Browse files
Fix #11981 internalAstError with using declaration (danmar#5446)
1 parent 775af5e commit 5a21851

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

lib/tokenize.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3236,9 +3236,18 @@ bool Tokenizer::simplifyUsing()
32363236
}
32373237
}
32383238

3239-
// just replace simple type aliases
3240-
TokenList::copyTokens(tok1, start, usingEnd->previous());
3241-
tok1->deleteThis();
3239+
// Is this a "T()" expression where T is a pointer type?
3240+
if (Token::Match(tok1, "%name% ( )") && !pointers.empty()) {
3241+
Token* tok2 = tok1->linkAt(1);
3242+
tok1->deleteThis();
3243+
TokenList::copyTokens(tok1, start, usingEnd->previous());
3244+
tok2->insertToken("0");
3245+
after = tok2->next();
3246+
}
3247+
else { // just replace simple type aliases
3248+
TokenList::copyTokens(tok1, start, usingEnd->previous());
3249+
tok1->deleteThis();
3250+
}
32423251
substitute = true;
32433252
}
32443253
} else {

test/testsimplifyusing.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class TestSimplifyUsing : public TestFixture {
7171
TEST_CASE(simplifyUsing26); // #11090
7272
TEST_CASE(simplifyUsing27);
7373
TEST_CASE(simplifyUsing28);
74+
TEST_CASE(simplifyUsing29);
7475

7576
TEST_CASE(simplifyUsing8970);
7677
TEST_CASE(simplifyUsing8971);
@@ -684,6 +685,14 @@ class TestSimplifyUsing : public TestFixture {
684685
ASSERT_EQUALS("", errout.str());
685686
}
686687

688+
void simplifyUsing29() { // #11981
689+
const char code[] = "using T = int*;\n"
690+
"void f(T = T()) {}\n";
691+
const char expected[] = "void f ( int * = ( int * ) 0 ) { }";
692+
ASSERT_EQUALS(expected, tok(code, cppcheck::Platform::Type::Native, /*debugwarnings*/ true));
693+
ASSERT_EQUALS("", errout.str());
694+
}
695+
687696
void simplifyUsing8970() {
688697
const char code[] = "using V = std::vector<int>;\n"
689698
"struct A {\n"

0 commit comments

Comments
 (0)