Skip to content

Commit

Permalink
Fix detection of _Static_assert() in C (#7126)
Browse files Browse the repository at this point in the history
Correct previous by honoring _Static_assert (introduced in C11) in C.
  • Loading branch information
mptre authored Dec 23, 2024
1 parent b090590 commit 8f3d0ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2575,9 +2575,19 @@ namespace {
}

static bool
isStaticAssert(const Token *tok)
isStaticAssert(const Settings &settings, const Token *tok)
{
return Token::Match(tok, "_Static_assert|static_assert (");
if (tok->isCpp() && settings.standards.cpp >= Standards::CPP11 &&
Token::simpleMatch(tok, "static_assert")) {
return true;
}

if (tok->isC() && settings.standards.c >= Standards::C11 &&
Token::simpleMatch(tok, "_Static_assert")) {
return true;
}

return false;
}

void CheckOther::checkDuplicateExpression()
Expand Down Expand Up @@ -2701,12 +2711,12 @@ void CheckOther::checkDuplicateExpression()
if (assignment)
selfAssignmentError(tok, tok->astOperand1()->expressionString());
else if (!isEnum) {
if (tok->isCpp() && mSettings->standards.cpp >= Standards::CPP11 && tok->str() == "==") {
if (tok->str() == "==") {
const Token* parent = tok->astParent();
while (parent && parent->astParent()) {
parent = parent->astParent();
}
if (parent && isStaticAssert(parent->previous())) {
if (parent && parent->previous() && isStaticAssert(*mSettings, parent->previous())) {
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6920,7 +6920,7 @@ class TestOther : public TestFixture {
check("void f() {\n"
" enum { Four = 4 };\n"
" _Static_assert(Four == 4, \"\");\n"
"}");
"}", false);
ASSERT_EQUALS("", errout_str());

check("void f() {\n"
Expand Down

0 comments on commit 8f3d0ab

Please sign in to comment.