[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
Wed Sep 28 10:26:19 PDT 2022


msebor updated this revision to Diff 463607.
msebor marked an inline comment as done.
msebor added a comment.

Changes from previous version:

- Replace loop with `Expr::IgnoreImpCasts()`.
- Use a multiline comment in a test to improve readability.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D134311/new/

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
@@ -74,3 +74,23 @@
 
 _Static_assert(1 , "") // expected-error {{expected ';' after '_Static_assert'}} \
                       // ext-warning {{'_Static_assert' is a C11 extension}}
+
+static int static_var;
+_Static_assert(&static_var != 0, "");  // ext-warning {{'_Static_assert' is a C11 extension}} \
+                                       // expected-warning {{comparison of address of 'static_var' not equal to a null pointer is always true}}
+_Static_assert("" != 0, "");           // ext-warning {{'_Static_assert' is a C11 extension}}
+_Static_assert(("" != 0), "");         // ext-warning {{'_Static_assert' is a C11 extension}}
+_Static_assert(*"1", "");              // ext-warning {{'_Static_assert' is a C11 extension}}
+_Static_assert("1"[0], "");            // ext-warning {{'_Static_assert' is a C11 extension}}
+_Static_assert(1.0 != 0, "");          // ext-warning {{'_Static_assert' is a C11 extension}}
+_Static_assert(__builtin_strlen("1"), "");  // ext-warning {{'_Static_assert' is a C11 extension}}
+#ifndef __cplusplus
+// ext-warning at -9 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// ext-warning at -8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// ext-warning at -8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// ext-warning at -8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// ext-warning at -8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// ext-warning at -8 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// __builtin_strlen(literal) is considered an integer constant expression
+// and doesn't cause a pedantic warning
+#endif
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -16736,10 +16736,21 @@
       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;
+      BaseExpr = BaseExpr->IgnoreImpCasts();
+    }
+
     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.463607.patch
Type: text/x-patch
Size: 3163 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220928/bcd155d9/attachment.bin>


More information about the cfe-commits mailing list