Skip to content

Commit

Permalink
Fix the ability to recognize return types when simplifying attributes. (
Browse files Browse the repository at this point in the history
#3637)

* Fix the ability to recognize return types when simplifying attributes.

When parsing attributes to remove them, we have to allow for
the case where the return type of the function that follows
the attribute has a namespaced C++ type, like foo::bar .
That means that :: has to be recognized as a valid token.
Fix this in simplifyAttribute, and add tests for this as well.
  • Loading branch information
clalancette authored Dec 22, 2021
1 parent 4d5ad5e commit df1d6cf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10974,8 +10974,14 @@ void Tokenizer::simplifyAttribute()
Token *functok = nullptr;
if (Token::Match(after, "%name%|*")) {
Token *ftok = after;
while (Token::Match(ftok, "%name%|* !!("))
while (Token::Match(ftok, "%name%|::|<|* !!(")) {
if (ftok->str() == "<") {
ftok = ftok->findClosingBracket();
if (!ftok)
break;
}
ftok = ftok->next();
}
if (Token::Match(ftok, "%name% ("))
functok = ftok;
} else if (Token::Match(after, "[;{=:]")) {
Expand Down
13 changes: 13 additions & 0 deletions test/testsimplifytokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ class TestSimplifyTokens : public TestFixture {
// remove calling convention __cdecl, __stdcall, ...
TEST_CASE(simplifyCallingConvention);

// remove __attribute, __attribute__
TEST_CASE(simplifyAttribute);

TEST_CASE(simplifyFunctorCall);

TEST_CASE(simplifyFunctionPointer); // ticket #5339 (simplify function pointer after comma)
Expand Down Expand Up @@ -4942,6 +4945,16 @@ class TestSimplifyTokens : public TestFixture {
ASSERT_EQUALS("enum E { CALLBACK } ;", tok("enum E { CALLBACK } ;", true, Settings::Unix32));
}

void simplifyAttribute() {
ASSERT_EQUALS("int f ( ) ;", tok("__attribute__ ((visibility(\"default\"))) int f();", true));
ASSERT_EQUALS("int f ( ) ;", tok("__attribute__((visibility(\"default\"))) int f();", true));
ASSERT_EQUALS("int f ( ) ;", tok("__attribute ((visibility(\"default\"))) int f();", true));
ASSERT_EQUALS("int f ( ) ;", tok("__attribute__ ((visibility(\"default\"))) __attribute__ ((warn_unused_result)) int f();", true));
ASSERT_EQUALS("blah :: blah f ( ) ;", tok("__attribute__ ((visibility(\"default\"))) blah::blah f();", true));
ASSERT_EQUALS("template < T > Result < T > f ( ) ;", tok("template<T> __attribute__ ((warn_unused_result)) Result<T> f();", true));
ASSERT_EQUALS("template < T , U > Result < T , U > f ( ) ;", tok("template<T, U> __attribute__ ((warn_unused_result)) Result<T, U> f();", true));
}

void simplifyFunctorCall() {
ASSERT_EQUALS("IncrementFunctor ( ) ( a ) ;", tok("IncrementFunctor()(a);", true));
}
Expand Down

0 comments on commit df1d6cf

Please sign in to comment.