Skip to content

Commit

Permalink
[clang] handle extended integer constant expressions in _Static_asser…
Browse files Browse the repository at this point in the history
…t (PR llvm#57687)

Automated commit created by applying diff 461610

Phabricator-ID: PHID-HMBT-5xgffhiglu2gf7cydmvf
Review-ID: D134311
  • Loading branch information
unknown authored and buildkite-agent committed Sep 20, 2022
1 parent b95c574 commit 095e96a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 15 additions & 3 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16731,10 +16731,22 @@ Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc,
AssertExpr = FullAssertExpr.get();

llvm::APSInt Cond;
Expr *BaseExpr = AssertExpr;
AllowFoldKind FoldKind = NoFold;

if (!getLangOpts().CPlusPlus) {
// In C mode only allow folding and strip the implicit conversion
// to the type of the first _Static_assert argument that would
// otherwise suppress diagnostics for arguments that convert to int.
FoldKind = AllowFold;
while (auto *BaseCast = dyn_cast<ImplicitCastExpr>(BaseExpr))
BaseExpr = BaseCast->getSubExpr();
}

if (!Failed && VerifyIntegerConstantExpression(
AssertExpr, &Cond,
diag::err_static_assert_expression_is_not_constant)
.isInvalid())
BaseExpr, &Cond,
diag::err_static_assert_expression_is_not_constant,
FoldKind).isInvalid())
Failed = true;

if (!Failed && !Cond) {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Sema/static-assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

_Static_assert("foo", "string is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
#ifndef __cplusplus
// expected-error@-2 {{static assertion expression is not an integral constant expression}}
// ext-warning@-2 {{expression is not an integer constant}}
#endif

_Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
Expand Down

0 comments on commit 095e96a

Please sign in to comment.