Skip to content

Commit 29a27cb

Browse files
committed
Fix #14400 FP syntaxError for trailing return type
1 parent a8f83c1 commit 29a27cb

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

lib/tokenize.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ static void skipEnumBody(T *&tok)
103103
/**
104104
* is tok the start brace { of a class, struct, union, or enum
105105
*/
106-
static bool isClassStructUnionEnumStart(const Token * tok)
106+
static const Token* isClassStructUnionEnumStart(const Token * tok)
107107
{
108108
if (!Token::Match(tok->previous(), "class|struct|union|enum|%name%|>|>> {"))
109-
return false;
109+
return nullptr;
110110
const Token * tok2 = tok->previous();
111111
while (tok2 && !Token::Match(tok2, "class|struct|union|enum|{|}|)|;|>|>>"))
112112
tok2 = tok2->previous();
113-
return Token::Match(tok2, "class|struct|union|enum");
113+
return Token::Match(tok2, "class|struct|union|enum") ? tok2 : nullptr;
114114
}
115115

116116
//---------------------------------------------------------------------------
@@ -8783,12 +8783,16 @@ void Tokenizer::findGarbageCode() const
87838783
syntaxError(tok, "keyword '" + tok->str() + "' is not allowed in global scope");
87848784
}
87858785
for (const Token *tok = tokens(); tok; tok = tok->next()) {
8786-
if (tok->str() == "{" && isClassStructUnionEnumStart(tok)) {
8787-
for (const Token* tok2 = tok->next(); tok2 != tok->link(); tok2 = tok2->next()) {
8788-
if (tok2->str() == "{")
8789-
tok2 = tok2->link();
8790-
else if (tok2->isKeyword() && nonGlobalKeywords.count(tok2->str()) && !Token::Match(tok2->tokAt(-2), "operator %str%"))
8791-
syntaxError(tok2, "keyword '" + tok2->str() + "' is not allowed in class/struct/union/enum scope");
8786+
if (tok->str() == "{") {
8787+
if (const Token* start = isClassStructUnionEnumStart(tok)) {
8788+
if (Token::simpleMatch(start->tokAt(-1), "->"))
8789+
continue;
8790+
for (const Token* tok2 = tok->next(); tok2 != tok->link(); tok2 = tok2->next()) {
8791+
if (tok2->str() == "{")
8792+
tok2 = tok2->link();
8793+
else if (tok2->isKeyword() && nonGlobalKeywords.count(tok2->str()) && !Token::Match(tok2->tokAt(-2), "operator %str%"))
8794+
syntaxError(tok2, "keyword '" + tok2->str() + "' is not allowed in class/struct/union/enum scope");
8795+
}
87928796
}
87938797
}
87948798
}

test/testtokenize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7683,6 +7683,12 @@ class TestTokenizer : public TestFixture {
76837683

76847684
ASSERT_NO_THROW(tokenizeAndStringify("struct S { unsigned u:2, :30; };")); // #14393
76857685

7686+
ASSERT_NO_THROW(tokenizeAndStringify("struct S {};\n" // #14400
7687+
"auto f(bool b) -> struct S {\n"
7688+
" if (b) {}\n"
7689+
" return {};\n"
7690+
"};\n"));
7691+
76867692
ignore_errout();
76877693
}
76887694

0 commit comments

Comments
 (0)