Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,19 @@ static unsigned int tokDistance(const Token* tok1, const Token* tok2) { // use w
return dist;
}

// a parenthesis starting with a ptr-operator is a nested declarator, otherwise it is a parameter list
static bool isNestedDeclarator(const Token* paren)
{
if (!Token::simpleMatch(paren, "("))
return false;
const Token* tok = paren->next();
if (Token::simpleMatch(tok, "::"))
tok = tok->next();
while (Token::Match(tok, "%name% ::"))
tok = tok->tokAt(2);
return Token::Match(tok, "*|&|&&");
}

static const Token* skipConstVolatileBackwards(const Token* tok) {
while (Token::Match(tok, "const|volatile"))
tok = tok->previous();
Expand Down Expand Up @@ -3447,6 +3460,35 @@ bool Tokenizer::simplifyUsing()
tok1->deleteThis();
substitute = true;
}
} else if (fpArgList && fpQual && Token::Match(tok1->next(), "%name%") &&
Token::findsimplematch(fpQual->next(), "(", fpQual->link())) {
// nested declarator, e.g. function returning function pointer: int (*(*)(int))(float)
Token* const nameStart = tok1->next();
Token* nameEnd = nameStart;
while (Token::Match(nameEnd, "%name% :: %name%"))
nameEnd = nameEnd->tokAt(2);
if (Token::simpleMatch(nameEnd->next(), "("))
nameEnd = nameEnd->linkAt(1);
else {
while (Token::simpleMatch(nameEnd->next(), "["))
nameEnd = nameEnd->linkAt(1);
}
if (!nameEnd->next())
syntaxError(nameEnd);
TokenList::copyTokens(tok1, start, usingEnd->previous());
tok1->deleteThis();
// the name goes after the ptr-operators of the innermost nested declarator.
// after deleteThis() tok1 is the first copied token, so this is the copy of fpQual
Token* pos = tok1->tokAt(tokDistance(start, fpQual))->next();
for (;;) {
while (Token::Match(pos, "%name%|::|*|&|&&"))
pos = pos->next();
if (!isNestedDeclarator(pos))
break;
pos = pos->next();
}
Token::move(nameStart, nameEnd, pos->previous());
substitute = true;
} else if (fpArgList && fpQual && Token::Match(tok1->next(), "%name%")) {
// function pointer
const bool isFuncDecl = Token::simpleMatch(tok1->tokAt(2), "(");
Expand Down
25 changes: 25 additions & 0 deletions test/testsimplifyusing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class TestSimplifyUsing : public TestFixture {
TEST_CASE(simplifyUsing10720);
TEST_CASE(simplifyUsing13873); // function declaration
TEST_CASE(simplifyUsing14877);
TEST_CASE(simplifyUsing15315);

TEST_CASE(scopeInfo1);
TEST_CASE(scopeInfo2);
Expand Down Expand Up @@ -1813,6 +1814,30 @@ class TestSimplifyUsing : public TestFixture {
ASSERT_EQUALS(expected, tok(code));
}

void simplifyUsing15315() {
const char code[] = "using D = int(*(*)(int))(float);\n"
"void f(D d);\n"
"D g(int);\n"
"D N::h(char c) { return nullptr; }\n";
const char expected[] = "void f ( int ( * ( * d ) ( int ) ) ( float ) ) ; "
"int ( * ( * g ( int ) ) ( int ) ) ( float ) ; "
"int ( * ( * N :: h ( char c ) ) ( int ) ) ( float ) { return nullptr ; }";
ASSERT_EQUALS(expected, tok(code));
ASSERT_EQUALS("", errout_str());

ASSERT_THROW_INTERNAL(tok("using D = int(*(*)(int))(float);\nD d"), SYNTAX);

const char code2[] = "using F = int (*())(float);\n"
"F f;\n"
"using G = int (*(std::string))(float);\n"
"G g;\n";
ASSERT_EQUALS("int * f ( ) ; int * g ( std :: string ) ;", tok(code2));

const char code3[] = "using D = int (*(*)(int))(float);\n"
"D a[2];\n";
ASSERT_EQUALS("int ( * ( * a [ 2 ] ) ( int ) ) ( float ) ;", tok(code3));
}

void scopeInfo1() {
const char code[] = "struct A {\n"
" enum class Mode { UNKNOWN, ENABLED, NONE, };\n"
Expand Down
Loading