[PATCH] D134311: [clang] handle extended integer constant expressions in _Static_assert (PR #57687)
Martin Sebor via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 20 13:46:36 PDT 2022
msebor created this revision.
msebor added a reviewer: aaron.ballman.
Herald added a project: All.
msebor requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
The discussion in PR #57687 <https://github.com/llvm/llvm-project/issues/57687> turned up an inconsistency in where extended integer constant expressions are accepted: in most contexts Clang accepts boolean expressions involving string literals, it rejects them as the first argument to the `_Static_assert` expression. This change is my attempt to make `_Static_assert` consistent with the other contexts and with C++. I looked at how some of the other contexts are handled, hoping to follow their example, but couldn't find a simple way to do it. They all seem quite different from `_Static_assert`.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D134311
Files:
clang/lib/Sema/SemaDeclCXX.cpp
clang/test/Sema/static-assert.c
Index: clang/test/Sema/static-assert.c
===================================================================
--- clang/test/Sema/static-assert.c
+++ clang/test/Sema/static-assert.c
@@ -5,7 +5,7 @@
_Static_assert("foo", "string is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
#ifndef __cplusplus
-// expected-error at -2 {{static assertion expression is not an integral constant expression}}
+// ext-warning at -2 {{expression is not an integer constant}}
#endif
_Static_assert(1, "1 is nonzero"); // ext-warning {{'_Static_assert' is a C11 extension}}
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -16721,10 +16721,22 @@
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) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134311.461610.patch
Type: text/x-patch
Size: 1751 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220920/c99cb17c/attachment-0001.bin>
More information about the cfe-commits
mailing list