Fix #10978 syntaxError with attribute for function pointer (#4069)

This commit is contained in:
chrchr-github 2022-05-02 15:05:48 +02:00 committed by GitHub
parent 6c54e7363d
commit 67f4fe575f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -10978,7 +10978,7 @@ void Tokenizer::simplifyAttribute()
syntaxError(tok);
Token *functok = nullptr;
if (Token::Match(after, "%name%|*")) {
if (Token::Match(after, "%name%|*|(")) {
Token *ftok = after;
while (Token::Match(ftok, "%name%|::|<|* !!(")) {
if (ftok->str() == "<") {
@ -10988,7 +10988,9 @@ void Tokenizer::simplifyAttribute()
}
ftok = ftok->next();
}
if (Token::Match(ftok, "%name% ("))
if (Token::simpleMatch(ftok, "( *"))
ftok = ftok->tokAt(2);
if (Token::Match(ftok, "%name% (|)"))
functok = ftok;
} else if (Token::Match(after, "[;{=:]")) {
Token *prev = tok->previous();

View File

@ -263,6 +263,7 @@ private:
TEST_CASE(removeattribute);
TEST_CASE(functionAttributeBefore1);
TEST_CASE(functionAttributeBefore2);
TEST_CASE(functionAttributeBefore3);
TEST_CASE(functionAttributeAfter);
TEST_CASE(functionAttributeListBefore);
TEST_CASE(functionAttributeListAfter);
@ -3653,6 +3654,22 @@ private:
ASSERT(VAS_Fail && VAS_Fail->isAttributeNoreturn());
}
void functionAttributeBefore3() { // #10978
const char code[] = "void __attribute__((__noreturn__)) (*func_notret)(void);";
const char expected[] = "void ( * func_notret ) ( ) ;";
errout.str("");
// tokenize..
Tokenizer tokenizer(&settings0, this);
std::istringstream istr(code);
ASSERT(tokenizer.tokenize(istr, "test.cpp"));
ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));
const Token* func_notret = Token::findsimplematch(tokenizer.tokens(), "func_notret");
ASSERT(func_notret && func_notret->isAttributeNoreturn());
}
void functionAttributeAfter() {
const char code[] = "void func1() __attribute__((pure)) __attribute__((nothrow)) __attribute__((const));\n"
"void func2() __attribute__((__pure__)) __attribute__((__nothrow__)) __attribute__((__const__));\n"