Skip to content

Commit

Permalink
fix #13272
Browse files Browse the repository at this point in the history
  • Loading branch information
ludviggunne committed Jan 4, 2025
1 parent cbae103 commit ca39147
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Setting
if (func->isAttributeConstructor() || func->isAttributeDestructor() || func->type != Function::eFunction || func->isOperator())
continue;

if (func->isAttributeUnused() || func->isAttributeMaybeUnused())
continue;

if (func->isExtern())
continue;

Expand Down
6 changes: 6 additions & 0 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,12 @@ class CPPCHECKLIB Function {
bool isAttributeNodiscard() const {
return tokenDef->isAttributeNodiscard();
}
bool isAttributeUnused() const {
return tokenDef->isAttributeUnused();
}
bool isAttributeMaybeUnused() const {
return tokenDef->isAttributeMaybeUnused();
}

bool hasBody() const {
return getFlag(fHasBody);
Expand Down
11 changes: 10 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9414,11 +9414,20 @@ void Tokenizer::simplifyCPPAttribute()
if (head && head->str() == "(" && isFunctionHead(head, "{|;")) {
head->previous()->isAttributeNodiscard(true);
}
} else if (Token::findsimplematch(tok->tokAt(2), "maybe_unused", tok->link())) {
} else if ((Token::findsimplematch(tok->tokAt(2), "maybe_unused", tok->link())
&& (mSettings.standards.c >= Standards::C23 || mSettings.standards.cpp >= Standards::CPP17))
|| (Token::findsimplematch(tok->tokAt(2), "__maybe_unused__", tok->link())
&& mSettings.standards.c >= Standards::C23)) {
Token* head = skipCPPOrAlignAttribute(tok)->next();
while (isCPPAttribute(head) || isAlignAttribute(head))
head = skipCPPOrAlignAttribute(head)->next();
head->isAttributeMaybeUnused(true);
} else if (Token::findmatch(tok->tokAt(2), "gnu :: unused", tok->link()) ||
Token::findsimplematch(tok->tokAt(2), "unused", tok->link())) {
Token* head = skipCPPOrAlignAttribute(tok)->next();
while (isCPPAttribute(head) || isAlignAttribute(head))
head = skipCPPOrAlignAttribute(head)->next();
head->isAttributeUnused(true);
} else if (Token::Match(tok->previous(), ") [ [ expects|ensures|assert default|audit|axiom| : %name% <|<=|>|>= %num% ] ]")) {
const Token *vartok = tok->tokAt(4);
if (vartok->str() == ":")
Expand Down

0 comments on commit ca39147

Please sign in to comment.