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
36 changes: 25 additions & 11 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9593,19 +9593,33 @@ void Tokenizer::simplifyCPPAttribute()
if (!head)
syntaxError(tok);

while (Token::Match(head->next(), "%name%|*|&|&&|const|static|inline|volatile"))
head = head->next();
if (Token::Match(head, "%name%") && !Token::Match(head, "auto ["))
head->isAttributeMaybeUnused(true);
else if (Token::Match(tok->previous(), "%name%") && Token::Match(tok->link(), "] [;={]")) {
tok->previous()->isAttributeMaybeUnused(true);
if (Token::simpleMatch(head, ";")) {
Token *backTok = tok;
while (backTok && !Token::Match(backTok, "%name%")) {
if (Token::Match(backTok, "]|)"))
backTok = backTok->link();
backTok = backTok->previous();
if (Token::Match(backTok, "[;{}]"))
backTok = nullptr;
}
if (backTok) {
backTok->isAttributeMaybeUnused(true);
}
} else {
if (Token::simpleMatch(head->next(), "[")) {
while (Token::Match(head->next(), "%name%|::|*|&|&&|const|static|inline|volatile"))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, this expression

                    while (Token::Match(head->next(), "%name%|::|*|&|&&|const|static|inline|volatile"))

works the same as this expression?

                    while (Token::Match(head->next(), "%name%|::|*|&|&&"))

or am I wrong?

head = head->next();
const Token *end = head->link();
for (head = head->next(); end && head != end; head = head->next()) {
if (Token::Match(head, "%name%")) {
head->isAttributeMaybeUnused(true);
if (Token::Match(head, "%name%") && !Token::Match(head, "auto ["))
head->isAttributeMaybeUnused(true);
else if (Token::Match(tok->previous(), "%name%") && Token::Match(tok->link(), "] [;={]")) {
tok->previous()->isAttributeMaybeUnused(true);
} else {
if (Token::simpleMatch(head->next(), "[")) {
head = head->next();
const Token *end = head->link();
for (head = head->next(); end && head != end; head = head->next()) {
if (Token::Match(head, "%name%")) {
head->isAttributeMaybeUnused(true);
}
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ class TestTokenizer : public TestFixture {
TEST_CASE(functionAttributeListAfter);
TEST_CASE(functionAttributeListAfter2);
TEST_CASE(cppMaybeUnusedBefore);
TEST_CASE(cppMaybeUnusedAfter);
TEST_CASE(cppMaybeUnusedAfter1);
TEST_CASE(cppMaybeUnusedAfter2);
TEST_CASE(cppMaybeUnusedStructuredBinding);

TEST_CASE(splitTemplateRightAngleBrackets);
Expand Down Expand Up @@ -4258,7 +4259,7 @@ class TestTokenizer : public TestFixture {
ASSERT(var && var->isAttributeMaybeUnused());
}

void cppMaybeUnusedAfter() {
void cppMaybeUnusedAfter1() {
const char code[] = "int var [[maybe_unused]] {};";
const char expected[] = "int var { } ;";

Expand All @@ -4271,6 +4272,19 @@ class TestTokenizer : public TestFixture {
ASSERT(var && var->isAttributeMaybeUnused());
}

void cppMaybeUnusedAfter2() {
const char code[] = "std::string var [[maybe_unused]];";
const char expected[] = "std :: string var ;";

SimpleTokenizer tokenizer(settings0, *this);
ASSERT(tokenizer.tokenize(code));

ASSERT_EQUALS(expected, tokenizer.tokens()->stringifyList(nullptr, false));

const Token *var = Token::findsimplematch(tokenizer.tokens(), "var");
ASSERT(var && var->isAttributeMaybeUnused());
}

void cppMaybeUnusedStructuredBinding() {
const char code[] = "[[maybe_unused]] auto [var1, var2] = f();";
const char expected[] = "auto [ var1 , var2 ] = f ( ) ;";
Expand Down
6 changes: 6 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6531,6 +6531,12 @@ class TestUnusedVar : public TestFixture {
" [[maybe_unused]] char b[1][2];\n"
"}");
ASSERT_EQUALS("", errout_str());

functionVariableUsage("int main() {\n"
" std::string a [[maybe_unused]];\n"
" f();\n"
"}");
ASSERT_EQUALS("", errout_str());
}

void localvarrvalue() { // ticket #13977
Expand Down
Loading