Skip to content
Open
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
22 changes: 13 additions & 9 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ static void skipEnumBody(T *&tok)
/**
* is tok the start brace { of a class, struct, union, or enum
*/
static bool isClassStructUnionEnumStart(const Token * tok)
static const Token* isClassStructUnionEnumStart(const Token * tok)
{
if (!Token::Match(tok->previous(), "class|struct|union|enum|%name%|>|>> {"))
return false;
return nullptr;
const Token * tok2 = tok->previous();
while (tok2 && !Token::Match(tok2, "class|struct|union|enum|{|}|)|;|>|>>"))
tok2 = tok2->previous();
return Token::Match(tok2, "class|struct|union|enum");
return Token::Match(tok2, "class|struct|union|enum") ? tok2 : nullptr;
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -8783,12 +8783,16 @@ void Tokenizer::findGarbageCode() const
syntaxError(tok, "keyword '" + tok->str() + "' is not allowed in global scope");
}
for (const Token *tok = tokens(); tok; tok = tok->next()) {
if (tok->str() == "{" && isClassStructUnionEnumStart(tok)) {
for (const Token* tok2 = tok->next(); tok2 != tok->link(); tok2 = tok2->next()) {
if (tok2->str() == "{")
tok2 = tok2->link();
else if (tok2->isKeyword() && nonGlobalKeywords.count(tok2->str()) && !Token::Match(tok2->tokAt(-2), "operator %str%"))
syntaxError(tok2, "keyword '" + tok2->str() + "' is not allowed in class/struct/union/enum scope");
if (tok->str() == "{") {
if (const Token* start = isClassStructUnionEnumStart(tok)) {
if (Token::simpleMatch(start->tokAt(-1), "->"))
continue;
for (const Token* tok2 = tok->next(); tok2 != tok->link(); tok2 = tok2->next()) {
if (tok2->str() == "{")
tok2 = tok2->link();
else if (tok2->isKeyword() && nonGlobalKeywords.count(tok2->str()) && !Token::Match(tok2->tokAt(-2), "operator %str%"))
syntaxError(tok2, "keyword '" + tok2->str() + "' is not allowed in class/struct/union/enum scope");
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7683,6 +7683,12 @@ class TestTokenizer : public TestFixture {

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

ASSERT_NO_THROW(tokenizeAndStringify("struct S {};\n" // #14400
"auto f(bool b) -> struct S {\n"
" if (b) {}\n"
" return {};\n"
"};\n"));

ignore_errout();
}

Expand Down
Loading