Skip to content

Commit ebfdc70

Browse files
committed
handle lists and suffixes
1 parent 56987e6 commit ebfdc70

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

‎lib/tokenize.cpp‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3456,21 +3456,35 @@ bool Tokenizer::simplifyUsing()
34563456
nameEnd = nameEnd->tokAt(2);
34573457
if (Token::simpleMatch(nameEnd->next(), "("))
34583458
nameEnd = nameEnd->linkAt(1);
3459+
else {
3460+
while (Token::simpleMatch(nameEnd->next(), "["))
3461+
nameEnd = nameEnd->linkAt(1);
3462+
}
34593463
if (!nameEnd->next())
34603464
syntaxError(nameEnd);
34613465
TokenList::copyTokens(tok1, start, usingEnd->previous());
34623466
tok1->deleteThis();
3463-
// the name goes into the innermost parenthesized declarator
3464-
Token* decl = tok1->tokAt(tokDistance(start, fpQual));
3467+
// a parenthesis starting with a ptr-operator is a nested declarator, otherwise it is a parameter list
3468+
const auto isNestedDeclarator = [](const Token* paren) {
3469+
if (!Token::simpleMatch(paren, "("))
3470+
return false;
3471+
const Token* t = paren->next();
3472+
if (Token::simpleMatch(t, "::"))
3473+
t = t->next();
3474+
while (Token::Match(t, "%name% ::"))
3475+
t = t->tokAt(2);
3476+
return Token::Match(t, "*|&|&&");
3477+
};
3478+
// the name goes after the ptr-operators of the innermost nested declarator
3479+
Token* pos = tok1->tokAt(tokDistance(start, fpQual))->next();
34653480
for (;;) {
3466-
Token* inner = decl->next();
3467-
while (inner != decl->link() && inner->str() != "(")
3468-
inner = inner->next();
3469-
if (inner == decl->link())
3481+
while (Token::Match(pos, "%name%|::|*|&|&&"))
3482+
pos = pos->next();
3483+
if (!isNestedDeclarator(pos))
34703484
break;
3471-
decl = inner;
3485+
pos = pos->next();
34723486
}
3473-
Token::move(nameStart, nameEnd, decl->link()->previous());
3487+
Token::move(nameStart, nameEnd, pos->previous());
34743488
substitute = true;
34753489
} else if (fpArgList && fpQual && Token::Match(tok1->next(), "%name%")) {
34763490
// function pointer

‎test/testsimplifyusing.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,16 @@ class TestSimplifyUsing : public TestFixture {
18261826
ASSERT_EQUALS("", errout_str());
18271827

18281828
ASSERT_THROW_INTERNAL(tok("using D = int(*(*)(int))(float);\nD d"), SYNTAX);
1829+
1830+
const char code2[] = "using F = int (*())(float);\n"
1831+
"F f;\n"
1832+
"using G = int (*(std::string))(float);\n"
1833+
"G g;\n";
1834+
ASSERT_EQUALS("int * f ( ) ; int * g ( std :: string ) ;", tok(code2));
1835+
1836+
const char code3[] = "using D = int (*(*)(int))(float);\n"
1837+
"D a[2];\n";
1838+
ASSERT_EQUALS("int ( * ( * a [ 2 ] ) ( int ) ) ( float ) ;", tok(code3));
18291839
}
18301840

18311841
void scopeInfo1() {

0 commit comments

Comments
 (0)