Tokenizer: fix for __attribute__ before function that returns a reference

This commit is contained in:
Daniel Marjamäki 2022-05-10 20:59:24 +02:00
parent a70d11adb6
commit 4257f9d46a
2 changed files with 19 additions and 2 deletions

View File

@ -10983,9 +10983,9 @@ void Tokenizer::simplifyAttribute()
syntaxError(tok); syntaxError(tok);
Token *functok = nullptr; Token *functok = nullptr;
if (Token::Match(after, "%name%|*|(")) { if (Token::Match(after, "%name%|*|&|(")) {
Token *ftok = after; Token *ftok = after;
while (Token::Match(ftok, "%name%|::|<|* !!(")) { while (Token::Match(ftok, "%name%|::|<|*|& !!(")) {
if (ftok->str() == "<") { if (ftok->str() == "<") {
ftok = ftok->findClosingBracket(); ftok = ftok->findClosingBracket();
if (!ftok) if (!ftok)

View File

@ -264,6 +264,7 @@ private:
TEST_CASE(functionAttributeBefore1); TEST_CASE(functionAttributeBefore1);
TEST_CASE(functionAttributeBefore2); TEST_CASE(functionAttributeBefore2);
TEST_CASE(functionAttributeBefore3); TEST_CASE(functionAttributeBefore3);
TEST_CASE(functionAttributeBefore4);
TEST_CASE(functionAttributeAfter1); TEST_CASE(functionAttributeAfter1);
TEST_CASE(functionAttributeAfter2); TEST_CASE(functionAttributeAfter2);
TEST_CASE(functionAttributeListBefore); TEST_CASE(functionAttributeListBefore);
@ -3678,6 +3679,22 @@ private:
ASSERT(func_notret && func_notret->isAttributeNoreturn()); ASSERT(func_notret && func_notret->isAttributeNoreturn());
} }
void functionAttributeBefore4() {
const char code[] = "__attribute__((const)) int& foo();";
const char expected[] = "int & foo ( ) ;";
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* foo = Token::findsimplematch(tokenizer.tokens(), "foo");
ASSERT(foo && foo->isAttributeConst());
}
void functionAttributeAfter1() { void functionAttributeAfter1() {
const char code[] = "void func1() __attribute__((pure)) __attribute__((nothrow)) __attribute__((const));\n" const char code[] = "void func1() __attribute__((pure)) __attribute__((nothrow)) __attribute__((const));\n"
"void func2() __attribute__((__pure__)) __attribute__((__nothrow__)) __attribute__((__const__));\n" "void func2() __attribute__((__pure__)) __attribute__((__nothrow__)) __attribute__((__const__));\n"