[PATCH] D126864: [clang] Introduce -fstrict-flex-arrays= for stricter handling of flexible arrays

2022-09-12 Thread Martin Sebor via Phabricator via cfe-commits
msebor added inline comments.



Comment at: clang/docs/ClangCommandLineReference.rst:2648
+
+Control which arrays are considered as flexible arrays members. 
+can be 1 (array of size 0, 1 and undefined are considered) or 2 (array of size 0

aaron.ballman wrote:
> This phrasing is problematic because it uses the term "flexible array 
> members" which is a term of art in the standard when it's really talking 
> about something else. We don't want `int trailing[1];` to be considered a 
> flexible array member in terms of the language rules.
A pedantic reading of the text would suggest that the type of `s.trailing` is 
incomplete at level 1 and `sizeof s.trailing` is invalid (requiring an error).  
Another minor issue is that the text refers to a size of the array when it 
actually means the number of elements.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126864

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D133108: [clang] Rework IsTailPaddedMemberArray into isFlexibleArrayMemberExpr

2022-09-13 Thread Martin Sebor via Phabricator via cfe-commits
msebor added inline comments.



Comment at: clang/lib/Sema/SemaChecking.cpp:15969
+  if (const MemberExpr *ME = dyn_cast(BaseExpr))
+ND = ME->getMemberDecl();
+

I'm not familiar with this code but I'm guessing that at most one of the `if` 
conditionals is true, and if it's the first one then the second `dyn_cast` will 
fail.  Should the `if` guarding this assignment then be an `else if`?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D133108

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D126864: [clang] Introduce -fstrict-flex-arrays= for stricter handling of flexible arrays

2022-06-29 Thread Martin Sebor via Phabricator via cfe-commits
msebor added a comment.

In D126864#3616524 , 
@serge-sans-paille wrote:

> GCC and Clang don't have the same behavior wrt. macros-as-abound and 
> standard-layout-requirement, see https://godbolt.org/z/3vc4TcTYz
> I'm fine with keeping the CLang behavior, but do we want to keep it only for 
> level=0, and drop it for higher level (this would look odd to me).

If you're referring to the warning, GCC needs `-O2` to issue most instances of 
`-Warray-bounds`.  The warning also treats one-element trailing arrays as 
flexible array members, so it's intentionally silent about the test case.  GCC 
should update the warning to reflect the `-fstrict-flex-arrays=N` level.  
(Macros are long expanded by the time the code reaches the middle end so they 
don't come into play, except perhaps when they come from system headers.)


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

https://reviews.llvm.org/D126864

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134311: [clang] handle extended integer constant expressions in _Static_assert (PR #57687)

2022-09-20 Thread Martin Sebor via Phabricator via cfe-commits
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  
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@-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}}
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(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) {


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@-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}}
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(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) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D133108: [clang] Rework IsTailPaddedMemberArray into isFlexibleArrayMemberExpr

2022-09-20 Thread Martin Sebor via Phabricator via cfe-commits
msebor added inline comments.



Comment at: clang/test/Sema/unbounded-array-bounds.c:101
+  char tail[1];  // addr16-note {{declared here}} addr32-note {{declared here}}
+} fam1;
+

There's a difference between the sizes of `fam1` and `fam` that makes accesses 
to the four leading elements of `fam1.tail` strictly in bounds, while no access 
to either `fam.tail` or `fam0.tail` is (`sizeof fam` is the same as `sizeof 
int` while `sizeof fam1` is equal to `sizeof (int[2])` on common targets).  It 
would be helpful to capture that difference in the tests, both for the warning 
and for `__builtin_object_size`.

There should also be a difference between accessing elements of an object of an 
initialized struct with a flexible array member (i.e., one whose size is known) 
and those of an object that's only declared but that's defined in some other 
translation unit.  Since the size of the object is determined by its 
initializer, it should be reflected in `__builtin_object_size` and accesses to 
it checked by `-Warray-bounds`.  The size of the latter object is unknown it 
must be assumed to be `PTRDIFF_MAX - sizeof (int) - 1`.  It would also be 
helpful to add tests for these cases.

As far as I can see, none of these cases seems to be handled quite right on 
trunk.  For example, the size of `s` below should be 8 but Clang evaluates 
`__builtin_object_size(&s, N)` to 4, without diagnosing any past-the-end 
accesses to `s.a`:
```
struct S {
  int n;
  char a[];
} s = { 1, { 2, 3, 4, 5 } };
```


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

https://reviews.llvm.org/D133108

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D133108: [clang] Rework IsTailPaddedMemberArray into isFlexibleArrayMemberExpr

2022-09-20 Thread Martin Sebor via Phabricator via cfe-commits
msebor added inline comments.



Comment at: clang/test/Sema/unbounded-array-bounds.c:101
+  char tail[1];  // addr16-note {{declared here}} addr32-note {{declared here}}
+} fam1;
+

msebor wrote:
> There's a difference between the sizes of `fam1` and `fam` that makes 
> accesses to the four leading elements of `fam1.tail` strictly in bounds, 
> while no access to either `fam.tail` or `fam0.tail` is (`sizeof fam` is the 
> same as `sizeof int` while `sizeof fam1` is equal to `sizeof (int[2])` on 
> common targets).  It would be helpful to capture that difference in the 
> tests, both for the warning and for `__builtin_object_size`.
> 
> There should also be a difference between accessing elements of an object of 
> an initialized struct with a flexible array member (i.e., one whose size is 
> known) and those of an object that's only declared but that's defined in some 
> other translation unit.  Since the size of the object is determined by its 
> initializer, it should be reflected in `__builtin_object_size` and accesses 
> to it checked by `-Warray-bounds`.  The size of the latter object is unknown 
> it must be assumed to be `PTRDIFF_MAX - sizeof (int) - 1`.  It would also be 
> helpful to add tests for these cases.
> 
> As far as I can see, none of these cases seems to be handled quite right on 
> trunk.  For example, the size of `s` below should be 8 but Clang evaluates 
> `__builtin_object_size(&s, N)` to 4, without diagnosing any past-the-end 
> accesses to `s.a`:
> ```
> struct S {
>   int n;
>   char a[];
> } s = { 1, { 2, 3, 4, 5 } };
> ```
I opened [[ https://github.com/llvm/llvm-project/issues/57860 | PR #57860 ]] to 
better show what I mean.


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

https://reviews.llvm.org/D133108

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134311: [clang] handle extended integer constant expressions in _Static_assert (PR #57687)

2022-09-20 Thread Martin Sebor via Phabricator via cfe-commits
msebor updated this revision to Diff 461744.
msebor added a comment.

Update to the latest diff that didn't get picked up in the first submission.


Repository:
  rG LLVM Github Monorepo

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,20 @@
 
 _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(*"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@-7 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-7 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-7 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-7 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-7 {{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
@@ -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(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) {


Index: clang/test/Sema/static-assert.c
===
--- clang/test/Sema/static-assert.c
+++ clang/test/Sema/static-assert.c
@@ -74,3 +74,20 @@
 
 _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(*"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@-7 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// ext-warning@-7 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// ext-warning@-7 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// ext-warning@-7 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// ext-warning@-7 {{expression is not an integer constant expression; folding it to a constant is a GNU exten

[PATCH] D134311: [clang] handle extended integer constant expressions in _Static_assert (PR #57687)

2022-09-28 Thread Martin Sebor via Phabricator via cfe-commits
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@-9 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-8 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-8 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-8 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-8 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-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) {


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@-9 {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
+// ext-warning@-8 {{expression is not an intege

[PATCH] D134311: [clang] handle extended integer constant expressions in _Static_assert (PR #57687)

2022-09-28 Thread Martin Sebor via Phabricator via cfe-commits
msebor added inline comments.



Comment at: clang/lib/Sema/SemaDeclCXX.cpp:16733
+  while (auto *BaseCast = dyn_cast(BaseExpr))
+BaseExpr = BaseCast->getSubExpr();
+}

aaron.ballman wrote:
> tbaeder wrote:
> > There is `Expr::ignoreParenImpCasts()` or `Expr::ImpCasts()` that should do 
> > this loop for you.
> +1, I would use `Expr::IgnoreImpCasts()` instead of this manual loop.
Sure.  Thank you both for the suggestion!


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

https://reviews.llvm.org/D134311

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D134311: [clang] handle extended integer constant expressions in _Static_assert (PR #57687)

2022-09-28 Thread Martin Sebor via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGa181de452df3: [clang] handle extended integer constant 
expressions in _Static_assert (PR… (authored by msebor).

Changed prior to commit:
  https://reviews.llvm.org/D134311?vs=463607&id=463648#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134311

Files:
  clang/docs/ReleaseNotes.rst
  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@-9 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-8 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-8 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-8 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-8 {{expression is not an integer constant expression; folding 
it to a constant is a GNU extension}}
+// ext-warning@-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) {
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -99,6 +99,9 @@
 
 Bug Fixes
 -
+- Correct ``_Static_assert`` to accept the same set of extended integer
+  constant expressions as is accpted in other contexts that accept them.
+  This fixes `Issue 57687 
`_.
 - Fixes an accepts-invalid bug in C when using a ``_Noreturn`` function
   specifier on something other than a function declaration. This fixes
   `Issue 56800 `_.


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 o

[PATCH] D134791: [clang] Unify Sema and CodeGen implementation of isFlexibleArrayMemberExpr

2022-09-30 Thread Martin Sebor via Phabricator via cfe-commits
msebor added a comment.

Thanks for the heads up!  My only general comment is that unless this change 
has no externally observable effect I'd suggest adding tests.  (Otherwise, 
noting it has no such effect would be helpful.)  When I'm not familiar with the 
code (and often even what I am) I usually look for tests to understand the 
impact of a change.




Comment at: clang/include/clang/AST/Expr.h:526
 
+  /// Check whether this array fits the idiom of a flexible array member,
+  /// depending on the value of -fstrict-flex-array.

I was a little unsure what "this array" means in the context of an expression 
but after reading the code I think the intent of the function is to "Return 
true if this expression designates an array fits the idiom of a flexible array 
member..."  If I have that right then that's what I would suggest to change the 
comment to.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134791

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D126864: [clang] Introduce -fstrict-flex-arrays for stricter handling of flexible arrays

2022-06-09 Thread Martin Sebor via Phabricator via cfe-commits
msebor added a comment.

Quite a bit more code treats one-element trailing arrays as flexible array 
members than larger arrays, and may not be able to change (GCC is one example). 
 I wonder if it would be worthwhile to make it possible for code to specify 
that mode (e.g., by adding two options, or by having the new option accept an 
argument specifying the cutoff at which a trailing array is not considered a 
flexible array member).




Comment at: clang/docs/ReleaseNotes.rst:71-75
+- Clang now supports the ``-fstrict-flex-arrays`` to only consider trailing
+  arrays with unknown size arrays as flexible arrays. This breaks compatibility
+  with some legacy code but allows for better folding of
+  ``__builtin_object_size``.
+

This sentence looks mangled.  It should probably read something like
> Clang now supports the `-fstrict-flex-arrays` option to avoid treating 
> trailing array members with a specified bound as flexible array members. The 
> option yields more accurate `__builtin_object_size` results in most cases but 
> may be overly conservative for some legacy code.

(The technical term is //flexible array member//.)

If the option also affects `__builtin_dynamic_object_size` it might be worth 
mentioning that as well, if only to highlight the latter built-in since it's 
more powerful.


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

https://reviews.llvm.org/D126864

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits