https://github.com/budimirarandjelovicsyrmia updated 
https://github.com/llvm/llvm-project/pull/70307

From a80047859d20f0fbb591f6c8a561468ce966f845 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia <budimir.arandjelo...@syrmia.com>
Date: Thu, 26 Oct 2023 10:39:52 +0200
Subject: [PATCH] [clang] Emit bad shift warnings

---
 clang/lib/AST/ExprConstant.cpp         |  7 +++++++
 clang/lib/Sema/SemaExpr.cpp            | 16 ++++++++++++----
 clang/test/C/drs/dr0xx.c               |  3 ++-
 clang/test/C/drs/dr2xx.c               |  4 +++-
 clang/test/Sema/builtins.c             |  6 ++++--
 clang/test/Sema/constant-builtins-2.c  | 12 ++++++++----
 clang/test/Sema/integer-overflow.c     |  2 ++
 clang/test/Sema/shift-count-negative.c |  8 ++++++++
 clang/test/Sema/shift-count-overflow.c |  6 ++++++
 clang/test/Sema/shift-negative-value.c |  9 +++++++++
 clang/test/Sema/vla-2.c                |  6 ++++--
 clang/test/SemaCXX/enum.cpp            | 15 ++++++++-------
 clang/test/SemaCXX/shift.cpp           |  2 +-
 13 files changed, 74 insertions(+), 22 deletions(-)
 create mode 100644 clang/test/Sema/shift-count-negative.c
 create mode 100644 clang/test/Sema/shift-count-overflow.c
 create mode 100644 clang/test/Sema/shift-negative-value.c

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index af1f18aa8ef24..0926bcf258f58 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -2856,6 +2856,9 @@ static bool handleIntIntBinOp(EvalInfo &Info, const 
BinaryOperator *E,
       else if (LHS.countl_zero() < SA)
         Info.CCEDiag(E, diag::note_constexpr_lshift_discards);
     }
+    if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&
+        Info.getLangOpts().CPlusPlus)
+      return false;
     Result = LHS << SA;
     return true;
   }
@@ -2879,6 +2882,10 @@ static bool handleIntIntBinOp(EvalInfo &Info, const 
BinaryOperator *E,
     if (SA != RHS)
       Info.CCEDiag(E, diag::note_constexpr_large_shift)
         << RHS << E->getType() << LHS.getBitWidth();
+
+    if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&
+        Info.getLangOpts().CPlusPlus)
+      return false;
     Result = LHS >> SA;
     return true;
   }
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 44f886bf54e3a..fd1c8284b5ff0 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11246,7 +11246,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult 
&LHS, ExprResult &RHS,
   if (Right.isNegative()) {
     S.DiagRuntimeBehavior(Loc, RHS.get(),
                           S.PDiag(diag::warn_shift_negative)
-                            << RHS.get()->getSourceRange());
+                              << RHS.get()->getSourceRange());
     return;
   }
 
@@ -11261,7 +11261,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult 
&LHS, ExprResult &RHS,
   if (Right.uge(LeftSize)) {
     S.DiagRuntimeBehavior(Loc, RHS.get(),
                           S.PDiag(diag::warn_shift_gt_typewidth)
-                            << RHS.get()->getSourceRange());
+                              << RHS.get()->getSourceRange());
     return;
   }
 
@@ -11294,7 +11294,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult 
&LHS, ExprResult &RHS,
   if (Left.isNegative()) {
     S.DiagRuntimeBehavior(Loc, LHS.get(),
                           S.PDiag(diag::warn_shift_lhs_negative)
-                            << LHS.get()->getSourceRange());
+                              << LHS.get()->getSourceRange());
     return;
   }
 
@@ -17130,11 +17130,19 @@ Sema::VerifyIntegerConstantExpression(Expr *E, 
llvm::APSInt *Result,
   // Circumvent ICE checking in C++11 to avoid evaluating the expression twice
   // in the non-ICE case.
   if (!getLangOpts().CPlusPlus11 && E->isIntegerConstantExpr(Context)) {
+    SmallVector<PartialDiagnosticAt, 8> Notes;
     if (Result)
-      *Result = E->EvaluateKnownConstIntCheckOverflow(Context);
+      *Result = E->EvaluateKnownConstIntCheckOverflow(Context, &Notes);
     if (!isa<ConstantExpr>(E))
       E = Result ? ConstantExpr::Create(Context, E, APValue(*Result))
                  : ConstantExpr::Create(Context, E);
+
+    if (Notes.size()) {
+      Diagnoser.diagnoseFold(*this, DiagLoc) << E->getSourceRange();
+      for (const PartialDiagnosticAt &Note : Notes)
+        Diag(Note.first, Note.second);
+    }
+
     return E;
   }
 
diff --git a/clang/test/C/drs/dr0xx.c b/clang/test/C/drs/dr0xx.c
index 36de32a93da95..252dc9329c4ca 100644
--- a/clang/test/C/drs/dr0xx.c
+++ b/clang/test/C/drs/dr0xx.c
@@ -430,7 +430,8 @@ void dr081(void) {
   /* Demonstrate that we don't crash when left shifting a signed value; that's
    * implementation defined behavior.
    */
- _Static_assert(-1 << 1 == -2, "fail"); /* Didn't shift a zero into the "sign 
bit". */
+ _Static_assert(-1 << 1 == -2, "fail"); /* expected-warning {{expression is 
not an integer constant expression; folding it to a constant is a GNU 
extension}}
+                                           expected-note {{left shift of 
negative value -1}} */
  _Static_assert(1 << 3 == 1u << 3u, "fail"); /* Shift of a positive signed 
value does sensible things. */
 }
 
diff --git a/clang/test/C/drs/dr2xx.c b/clang/test/C/drs/dr2xx.c
index 1b68b65acca6a..3d53dacc044d0 100644
--- a/clang/test/C/drs/dr2xx.c
+++ b/clang/test/C/drs/dr2xx.c
@@ -277,7 +277,9 @@ void dr258(void) {
 void dr261(void) {
   /* This is still an integer constant expression despite the overflow. */
   enum e1 {
-    ex1 = __INT_MAX__ + 1  /* expected-warning {{overflow in expression; 
result is -2'147'483'648 with type 'int'}} */
+    ex1 = __INT_MAX__ + 1  /* expected-warning {{overflow in expression; 
result is -2'147'483'648 with type 'int'}}
+                              expected-warning {{expression is not an integer 
constant expression; folding it to a constant is a GNU extension}}
+                              expected-note {{value 2147483648 is outside the 
range of representable values of type 'int'}} */
   };
 
   /* This is not an integer constant expression, because of the comma operator,
diff --git a/clang/test/Sema/builtins.c b/clang/test/Sema/builtins.c
index 4f843aeec24e6..3afab07db6196 100644
--- a/clang/test/Sema/builtins.c
+++ b/clang/test/Sema/builtins.c
@@ -171,8 +171,10 @@ void test17(void) {
 #define OPT(...) (__builtin_constant_p(__VA_ARGS__) && strlen(__VA_ARGS__) < 4)
   // FIXME: These are incorrectly treated as ICEs because strlen is treated as
   // a builtin.
-  ASSERT(OPT("abc"));
-  ASSERT(!OPT("abcd"));
+  ASSERT(OPT("abc")); /* expected-warning {{expression is not an integer 
constant expression; folding it to a constant is a GNU extension}}
+                         expected-note {{subexpression not valid in a constant 
expression}} */
+  ASSERT(!OPT("abcd")); /* expected-warning {{expression is not an integer 
constant expression; folding it to a constant is a GNU extension}}
+                           expected-note {{subexpression not valid in a 
constant expression}} */
   // In these cases, the strlen is non-constant, but the __builtin_constant_p
   // is 0: the array size is not an ICE but is foldable.
   ASSERT(!OPT(test17_c));
diff --git a/clang/test/Sema/constant-builtins-2.c 
b/clang/test/Sema/constant-builtins-2.c
index a60a1f16a4587..00767267cd6c2 100644
--- a/clang/test/Sema/constant-builtins-2.c
+++ b/clang/test/Sema/constant-builtins-2.c
@@ -265,8 +265,10 @@ char clz52[__builtin_clzg((unsigned __int128)0x1) == 
BITSIZE(__int128) - 1 ? 1 :
 char clz53[__builtin_clzg((unsigned __int128)0x1, 42) == BITSIZE(__int128) - 1 
? 1 : -1];
 char clz54[__builtin_clzg((unsigned __int128)0xf) == BITSIZE(__int128) - 4 ? 1 
: -1];
 char clz55[__builtin_clzg((unsigned __int128)0xf, 42) == BITSIZE(__int128) - 4 
? 1 : -1];
-char clz56[__builtin_clzg((unsigned __int128)(1 << (BITSIZE(__int128) - 1))) 
== 0 ? 1 : -1];
-char clz57[__builtin_clzg((unsigned __int128)(1 << (BITSIZE(__int128) - 1)), 
42) == 0 ? 1 : -1];
+char clz56[__builtin_clzg((unsigned __int128)(1 << (BITSIZE(__int128) - 1))) 
== 0 ? 1 : -1]; // expected-warning {{variable length array folded to constant 
array as an extension}}
+                                                                               
              // expected-note@-1 {{shift count 127 >= width of type 'int' (32 
bits)}}
+char clz57[__builtin_clzg((unsigned __int128)(1 << (BITSIZE(__int128) - 1)), 
42) == 0 ? 1 : -1]; // expected-warning {{variable length array folded to 
constant array as an extension}}
+                                                                               
                  // expected-note@-1 {{shift count 127 >= width of type 'int' 
(32 bits)}}
 #endif
 int clz58 = __builtin_clzg((unsigned _BitInt(128))0); // expected-error {{not 
a compile-time constant}}
 char clz59[__builtin_clzg((unsigned _BitInt(128))0, 42) == 42 ? 1 : -1];
@@ -274,8 +276,10 @@ char clz60[__builtin_clzg((unsigned _BitInt(128))0x1) == 
BITSIZE(_BitInt(128)) -
 char clz61[__builtin_clzg((unsigned _BitInt(128))0x1, 42) == 
BITSIZE(_BitInt(128)) - 1 ? 1 : -1];
 char clz62[__builtin_clzg((unsigned _BitInt(128))0xf) == BITSIZE(_BitInt(128)) 
- 4 ? 1 : -1];
 char clz63[__builtin_clzg((unsigned _BitInt(128))0xf, 42) == 
BITSIZE(_BitInt(128)) - 4 ? 1 : -1];
-char clz64[__builtin_clzg((unsigned _BitInt(128))(1 << (BITSIZE(_BitInt(128)) 
- 1))) == 0 ? 1 : -1];
-char clz65[__builtin_clzg((unsigned _BitInt(128))(1 << (BITSIZE(_BitInt(128)) 
- 1)), 42) == 0 ? 1 : -1];
+char clz64[__builtin_clzg((unsigned _BitInt(128))(1 << (BITSIZE(_BitInt(128)) 
- 1))) == 0 ? 1 : -1]; // expected-warning {{variable length array folded to 
constant array as an extension}}
+                                                                               
                      // expected-note@-1 {{shift count 127 >= width of type 
'int' (32 bits)}}
+char clz65[__builtin_clzg((unsigned _BitInt(128))(1 << (BITSIZE(_BitInt(128)) 
- 1)), 42) == 0 ? 1 : -1]; // expected-warning {{variable length array folded 
to constant array as an extension}}
+                                                                               
                          // expected-note@-1 {{shift count 127 >= width of 
type 'int' (32 bits)}}
 
 char ctz1[__builtin_ctz(1) == 0 ? 1 : -1];
 char ctz2[__builtin_ctz(8) == 3 ? 1 : -1];
diff --git a/clang/test/Sema/integer-overflow.c 
b/clang/test/Sema/integer-overflow.c
index 220fc1bed515a..3141443c73305 100644
--- a/clang/test/Sema/integer-overflow.c
+++ b/clang/test/Sema/integer-overflow.c
@@ -174,6 +174,8 @@ void check_integer_overflows_in_function_calls(void) {
 }
 void check_integer_overflows_in_array_size(void) {
   int arr[4608 * 1024 * 1024]; // expected-warning {{overflow in expression; 
result is 536'870'912 with type 'int'}}
+                               // expected-warning@-1 {{variable length array 
folded to constant array as an extension}}
+                               // expected-note@-2 {{value 4831838208 is 
outside the range of representable values of type 'int'}}
 }
 
 struct s {
diff --git a/clang/test/Sema/shift-count-negative.c 
b/clang/test/Sema/shift-count-negative.c
new file mode 100644
index 0000000000000..97f85feed52c0
--- /dev/null
+++ b/clang/test/Sema/shift-count-negative.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -x c -fsyntax-only -verify=expected,c -pedantic %s
+// RUN: %clang_cc1 -x c++ -fsyntax-only -verify=expected,cpp %s
+
+enum shiftof {
+    X = (1<<-29) // c-warning {{expression is not an integer constant 
expression; folding it to a constant is a GNU extension}}
+                 // cpp-error@-1 {{expression is not an integral constant 
expression}}
+                 // expected-note@-2 {{negative shift count -29}}
+};
diff --git a/clang/test/Sema/shift-count-overflow.c 
b/clang/test/Sema/shift-count-overflow.c
new file mode 100644
index 0000000000000..49e058156e8fd
--- /dev/null
+++ b/clang/test/Sema/shift-count-overflow.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s
+
+enum shiftof {
+    X = (1<<32) // expected-warning {{expression is not an integer constant 
expression; folding it to a constant is a GNU extension}}
+                // expected-note@-1 {{shift count 32 >= width of type 'int'}}
+};
diff --git a/clang/test/Sema/shift-negative-value.c 
b/clang/test/Sema/shift-negative-value.c
new file mode 100644
index 0000000000000..49fdfc41e2eba
--- /dev/null
+++ b/clang/test/Sema/shift-negative-value.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -x c -fsyntax-only -verify=expected,c -pedantic %s
+// RUN: %clang_cc1 -x c++ -fsyntax-only -verify=expected,cpp 
-Wshift-negative-value %s
+// RUN: %clang_cc1 -x c++ -fsyntax-only -verify=expected,cpp -Wall %s
+
+enum shiftof {
+    X = (-1<<29) // c-warning {{expression is not an integer constant 
expression; folding it to a constant is a GNU extension}}
+                 // cpp-error@-1 {{expression is not an integral constant 
expression}}
+                 // expected-note@-2 {{left shift of negative value -1}}
+};
diff --git a/clang/test/Sema/vla-2.c b/clang/test/Sema/vla-2.c
index 316931f270607..577407e15b479 100644
--- a/clang/test/Sema/vla-2.c
+++ b/clang/test/Sema/vla-2.c
@@ -4,11 +4,13 @@
 // a different codepath when we have already emitted an error.)
 
 int PotentiallyEvaluatedSizeofWarn(int n) {
-  return (int)sizeof *(0 << 32,(int(*)[n])0); // expected-warning {{left 
operand of comma operator has no effect}} expected-warning {{shift count >= 
width of type}}
+  return (int)sizeof *(0 << 32,(int(*)[n])0); /* expected-warning {{shift 
count >= width of type}}
+                                                 expected-warning {{left 
operand of comma operator has no effect}} */
 }
 
 void PotentiallyEvaluatedTypeofWarn(int n) {
-  __typeof(*(0 << 32,(int(*)[n])0)) x; // expected-warning {{left operand of 
comma operator has no effect}} expected-warning {{shift count >= width of type}}
+  __typeof(*(0 << 32,(int(*)[n])0)) x; /* expected-warning {{shift count >= 
width of type}}
+                                          expected-warning {{left operand of 
comma operator has no effect}} */
   (void)x;
 }
 
diff --git a/clang/test/SemaCXX/enum.cpp b/clang/test/SemaCXX/enum.cpp
index c482b3c571ab4..6fadcc288a29b 100644
--- a/clang/test/SemaCXX/enum.cpp
+++ b/clang/test/SemaCXX/enum.cpp
@@ -98,12 +98,11 @@ void PR8089() {
 
 // This is accepted as a GNU extension. In C++98, there was no provision for
 // expressions with UB to be non-constant.
-enum { overflow = 123456 * 234567 };
+enum { overflow = 123456 * 234567 }; // expected-warning {{expression is not 
an integral constant expression; folding it to a constant is a GNU extension}}
+                                     // expected-note@-1 {{value 28958703552 
is outside the range of representable values of type 'int'}}
 #if __cplusplus >= 201103L
-// expected-warning@-2 {{not an integral constant expression}}
-// expected-note@-3 {{value 28958703552 is outside the range of representable 
values}}
-#else 
-// expected-warning@-5 {{overflow in expression; result is -1'106'067'520 with 
type 'int'}}
+#else
+// expected-warning@-4 {{overflow in expression; result is -1'106'067'520 with 
type 'int'}}
 #endif
 
 // FIXME: This is not consistent with the above case.
@@ -112,8 +111,10 @@ enum NoFold : int { overflow2 = 123456 * 234567 };
 // expected-error@-2 {{enumerator value is not a constant expression}}
 // expected-note@-3 {{value 28958703552 is outside the range of representable 
values}}
 #else
-// expected-warning@-5 {{overflow in expression; result is -1'106'067'520 with 
type 'int'}}
-// expected-warning@-6 {{extension}}
+// expected-warning@-5 {{enumeration types with a fixed underlying type are a 
C++11 extension}}
+// expected-warning@-6 {{overflow in expression; result is -1'106'067'520 with 
type 'int'}}
+// expected-warning@-7 {{expression is not an integral constant expression; 
folding it to a constant is a GNU extension}}
+// expected-note@-8 {{value 28958703552 is outside the range of representable 
values of type 'int'}}
 #endif
 
 // PR28903
diff --git a/clang/test/SemaCXX/shift.cpp b/clang/test/SemaCXX/shift.cpp
index 89a98791d3eba..6367e6d2f250b 100644
--- a/clang/test/SemaCXX/shift.cpp
+++ b/clang/test/SemaCXX/shift.cpp
@@ -22,7 +22,7 @@ void test() {
   c = 1 << -1; // expected-warning {{shift count is negative}}
   c = 1 >> -1; // expected-warning {{shift count is negative}}
   c = 1 << (unsigned)-1; // expected-warning {{shift count >= width of type}}
-                         // expected-warning@-1 {{implicit conversion}}
+                         // expected-warning@-1 {{implicit conversion from 
'int' to 'char' changes value from -2147483648 to 0}}
   c = 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}}
   c = 1 << c;
   c <<= 0;

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

Reply via email to