Skip to content

Commit 8f3d0ab

Browse files
authored
Fix detection of _Static_assert() in C (#7126)
Correct previous by honoring _Static_assert (introduced in C11) in C.
1 parent b090590 commit 8f3d0ab

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

lib/checkother.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,9 +2575,19 @@ namespace {
25752575
}
25762576

25772577
static bool
2578-
isStaticAssert(const Token *tok)
2578+
isStaticAssert(const Settings &settings, const Token *tok)
25792579
{
2580-
return Token::Match(tok, "_Static_assert|static_assert (");
2580+
if (tok->isCpp() && settings.standards.cpp >= Standards::CPP11 &&
2581+
Token::simpleMatch(tok, "static_assert")) {
2582+
return true;
2583+
}
2584+
2585+
if (tok->isC() && settings.standards.c >= Standards::C11 &&
2586+
Token::simpleMatch(tok, "_Static_assert")) {
2587+
return true;
2588+
}
2589+
2590+
return false;
25812591
}
25822592

25832593
void CheckOther::checkDuplicateExpression()
@@ -2701,12 +2711,12 @@ void CheckOther::checkDuplicateExpression()
27012711
if (assignment)
27022712
selfAssignmentError(tok, tok->astOperand1()->expressionString());
27032713
else if (!isEnum) {
2704-
if (tok->isCpp() && mSettings->standards.cpp >= Standards::CPP11 && tok->str() == "==") {
2714+
if (tok->str() == "==") {
27052715
const Token* parent = tok->astParent();
27062716
while (parent && parent->astParent()) {
27072717
parent = parent->astParent();
27082718
}
2709-
if (parent && isStaticAssert(parent->previous())) {
2719+
if (parent && parent->previous() && isStaticAssert(*mSettings, parent->previous())) {
27102720
continue;
27112721
}
27122722
}

test/testother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6920,7 +6920,7 @@ class TestOther : public TestFixture {
69206920
check("void f() {\n"
69216921
" enum { Four = 4 };\n"
69226922
" _Static_assert(Four == 4, \"\");\n"
6923-
"}");
6923+
"}", false);
69246924
ASSERT_EQUALS("", errout_str());
69256925

69266926
check("void f() {\n"

0 commit comments

Comments
 (0)