Skip to content

Commit 7939ba0

Browse files
committed
[cxx2a] P1236R1: the validity of a left shift does not depend on the
value of the LHS operand. llvm-svn: 364265
1 parent 079924b commit 7939ba0

File tree

9 files changed

+153
-18
lines changed

9 files changed

+153
-18
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2388,9 +2388,11 @@ static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS,
23882388
if (SA != RHS) {
23892389
Info.CCEDiag(E, diag::note_constexpr_large_shift)
23902390
<< RHS << E->getType() << LHS.getBitWidth();
2391-
} else if (LHS.isSigned()) {
2391+
} else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus2a) {
23922392
// C++11 [expr.shift]p2: A signed left shift must have a non-negative
23932393
// operand, and must not overflow the corresponding unsigned type.
2394+
// C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to
2395+
// E1 x 2^E2 module 2^N.
23942396
if (LHS.isNegative())
23952397
Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS;
23962398
else if (LHS.countLeadingZeros() < SA)

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3640,7 +3640,8 @@ Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
36403640

36413641
bool SanitizeBase = CGF.SanOpts.has(SanitizerKind::ShiftBase) &&
36423642
Ops.Ty->hasSignedIntegerRepresentation() &&
3643-
!CGF.getLangOpts().isSignedOverflowDefined();
3643+
!CGF.getLangOpts().isSignedOverflowDefined() &&
3644+
!CGF.getLangOpts().CPlusPlus2a;
36443645
bool SanitizeExponent = CGF.SanOpts.has(SanitizerKind::ShiftExponent);
36453646
// OpenCL 6.3j: shift values are effectively % word size of LHS.
36463647
if (CGF.getLangOpts().OpenCL)

clang/lib/Sema/SemaExpr.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9645,9 +9645,11 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
96459645
return;
96469646

96479647
// When left shifting an ICE which is signed, we can check for overflow which
9648-
// according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned
9649-
// integers have defined behavior modulo one more than the maximum value
9650-
// representable in the result type, so never warn for those.
9648+
// according to C++ standards prior to C++2a has undefined behavior
9649+
// ([expr.shift] 5.8/2). Unsigned integers have defined behavior modulo one
9650+
// more than the maximum value representable in the result type, so never
9651+
// warn for those. (FIXME: Unsigned left-shift overflow in a constant
9652+
// expression is still probably a bug.)
96519653
Expr::EvalResult LHSResult;
96529654
if (LHS.get()->isValueDependent() ||
96539655
LHSType->hasUnsignedIntegerRepresentation() ||
@@ -9656,8 +9658,9 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
96569658
llvm::APSInt Left = LHSResult.Val.getInt();
96579659

96589660
// If LHS does not have a signed type and non-negative value
9659-
// then, the behavior is undefined. Warn about it.
9660-
if (Left.isNegative() && !S.getLangOpts().isSignedOverflowDefined()) {
9661+
// then, the behavior is undefined before C++2a. Warn about it.
9662+
if (Left.isNegative() && !S.getLangOpts().isSignedOverflowDefined() &&
9663+
!S.getLangOpts().CPlusPlus2a) {
96619664
S.DiagRuntimeBehavior(Loc, LHS.get(),
96629665
S.PDiag(diag::warn_shift_lhs_negative)
96639666
<< LHS.get()->getSourceRange());

clang/lib/StaticAnalyzer/Core/BasicValueFactory.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,6 @@ BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
231231
// FIXME: This logic should probably go higher up, where we can
232232
// test these conditions symbolically.
233233

234-
if (V1.isSigned() && V1.isNegative())
235-
return nullptr;
236-
237234
if (V2.isSigned() && V2.isNegative())
238235
return nullptr;
239236

@@ -242,8 +239,13 @@ BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
242239
if (Amt >= V1.getBitWidth())
243240
return nullptr;
244241

245-
if (V1.isSigned() && Amt > V1.countLeadingZeros())
242+
if (!Ctx.getLangOpts().CPlusPlus2a) {
243+
if (V1.isSigned() && V1.isNegative())
244+
return nullptr;
245+
246+
if (V1.isSigned() && Amt > V1.countLeadingZeros())
246247
return nullptr;
248+
}
247249

248250
return &getValue( V1.operator<<( (unsigned) Amt ));
249251
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -triple x86_64-apple-darwin13 -Wno-shift-count-overflow -verify=expected,cxx17 -std=c++17 %s
2+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -triple x86_64-apple-darwin13 -Wno-shift-count-overflow -verify=expected,cxx2a -std=c++2a %s
3+
4+
int testNegativeShift(int a) {
5+
if (a == -5) {
6+
return 1 << a; // expected-warning{{The result of the left shift is undefined because the right operand is negative}}
7+
}
8+
return 0;
9+
}
10+
11+
int testNegativeLeftShift(int a) {
12+
if (a == -3) {
13+
return a << 1; // cxx17-warning{{The result of the left shift is undefined because the left operand is negative}}
14+
}
15+
return 0;
16+
}
17+
18+
int testUnrepresentableLeftShift(int a) {
19+
if (a == 8)
20+
return a << 30; // cxx17-warning{{The result of the left shift is undefined due to shifting '8' by '30', which is unrepresentable in the unsigned version of the return type 'int'}}
21+
return 0;
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -std=c++2a -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK,REGULAR
2+
// RUN: %clang_cc1 -std=c++2a -fsanitize=shift-base,shift-exponent -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK,SANITIZED
3+
4+
// CHECK-LABEL: @_Z12lsh_overflow
5+
int lsh_overflow(int a, int b) {
6+
// SANITIZED: %[[RHS_INBOUNDS:.*]] = icmp ule i32 %[[RHS:.*]], 31
7+
// SANITIZED-NEXT: br i1 %[[RHS_INBOUNDS]], label %[[VALID:.*]], label
8+
9+
// SANITIZED: call void @__ubsan_handle_shift_out_of_bounds
10+
11+
// No check for the LHS here.
12+
// SANITIZED: [[VALID]]:
13+
// SANITIZED-NEXT: shl i32 %
14+
// SANITIZED-NEXT: ret i32
15+
16+
// Just ensure there's no nsw nuw flags here.
17+
// REGULAR: shl i32 %
18+
return a << b;
19+
}

clang/test/SemaCXX/constant-expression-cxx2a.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,19 @@ namespace Union {
532532
s.n = 0;
533533
}
534534
}
535+
536+
namespace TwosComplementShifts {
537+
using uint32 = __UINT32_TYPE__;
538+
using int32 = __INT32_TYPE__;
539+
static_assert(uint32(int32(0x1234) << 16) == 0x12340000);
540+
static_assert(uint32(int32(0x1234) << 19) == 0x91a00000);
541+
static_assert(uint32(int32(0x1234) << 20) == 0x23400000); // expected-warning {{requires 34 bits}}
542+
static_assert(uint32(int32(0x1234) << 24) == 0x34000000); // expected-warning {{requires 38 bits}}
543+
static_assert(uint32(int32(-1) << 31) == 0x80000000);
544+
545+
static_assert(-1 >> 1 == -1);
546+
static_assert(-1 >> 31 == -1);
547+
static_assert(-2 >> 1 == -1);
548+
static_assert(-3 >> 1 == -2);
549+
static_assert(-4 >> 1 == -2);
550+
}

clang/test/SemaCXX/shift.cpp

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,84 @@
1-
// RUN: %clang_cc1 -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify %s
1+
// RUN: %clang_cc1 -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify=expected,cxx17 -std=c++17 %s
2+
// RUN: %clang_cc1 -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify=expected,cxx2a -std=c++2a %s
23

34
#include <limits.h>
45

56
#define WORD_BIT (sizeof(int) * CHAR_BIT)
67

7-
template <int N> void f() {
8-
(void)(N << 30); // expected-warning {{bits to represent, but 'int' only has}}
9-
(void)(30 << N); // expected-warning {{bits to represent, but 'int' only has}}
10-
}
8+
enum {
9+
X = 1 << 0,
10+
Y = 1 << 1,
11+
Z = 1 << 2
12+
};
1113

1214
void test() {
13-
f<30>(); // expected-note {{instantiation}}
15+
char c;
16+
17+
c = 0 << 0;
18+
c = 0 << 1;
19+
c = 1 << 0;
20+
c = 1 << -0;
21+
c = 1 >> -0;
22+
c = 1 << -1; // expected-warning {{shift count is negative}}
23+
c = 1 >> -1; // expected-warning {{shift count is negative}}
24+
c = 1 << (unsigned)-1; // expected-warning {{shift count >= width of type}}
25+
// expected-warning@-1 {{implicit conversion}}
26+
c = 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}}
27+
c = 1 << c;
28+
c <<= 0;
29+
c >>= 0;
30+
c <<= 1;
31+
c >>= 1;
32+
c <<= -1; // expected-warning {{shift count is negative}}
33+
c >>= -1; // expected-warning {{shift count is negative}}
34+
c <<= 999999; // expected-warning {{shift count >= width of type}}
35+
c >>= 999999; // expected-warning {{shift count >= width of type}}
36+
c <<= CHAR_BIT; // expected-warning {{shift count >= width of type}}
37+
c >>= CHAR_BIT; // expected-warning {{shift count >= width of type}}
38+
c <<= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}
39+
c >>= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}
40+
(void)((long)c << CHAR_BIT);
41+
42+
int i;
43+
i = 1 << (WORD_BIT - 2);
44+
i = 2 << (WORD_BIT - 1); // expected-warning {{bits to represent, but 'int' only has}}
45+
i = 1 << (WORD_BIT - 1); // expected-warning {{sets the sign bit of the shift expression}}
46+
i = -1 << (WORD_BIT - 1); // cxx17-warning {{shifting a negative signed value is undefined}}
47+
i = -1 << 0; // cxx17-warning {{shifting a negative signed value is undefined}}
48+
i = 0 << (WORD_BIT - 1);
49+
i = (char)1 << (WORD_BIT - 2);
50+
51+
unsigned u;
52+
u = 1U << (WORD_BIT - 1);
53+
u = 5U << (WORD_BIT - 1);
54+
55+
long long int lli;
56+
lli = INT_MIN << 2; // cxx17-warning {{shifting a negative signed value is undefined}} cxx2a-warning {{requires 34 bits to represent}}
57+
lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
58+
}
59+
60+
#define a 0
61+
#define ashift 8
62+
enum { b = (a << ashift) };
63+
64+
// Don't warn for negative shifts in code that is unreachable.
65+
void test_pr5544() {
66+
(void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning
67+
}
68+
69+
void test_shift_too_much(char x) {
70+
if (0)
71+
(void) (x >> 80); // no-warning
72+
(void) (x >> 80); // expected-warning {{shift count >= width of type}}
73+
}
74+
75+
typedef unsigned vec16 __attribute__((vector_size(16)));
76+
typedef unsigned vec8 __attribute__((vector_size(8)));
77+
78+
void vect_shift_1(vec16 *x) { *x = *x << 4; }
79+
80+
void vect_shift_2(vec16 *x, vec16 y) { *x = *x << y; }
81+
82+
void vect_shift_3(vec16 *x, vec8 y) {
83+
*x = *x << y; // expected-error {{vector operands do not have the same number of elements}}
1484
}

clang/www/cxx_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ <h2 id="cxx20">C++2a implementation status</h2>
10081008
<tr>
10091009
<td>Signed integers are two's complement</td>
10101010
<td><a href="http://wg21.link/p1236r1">P1236R1</a></td>
1011-
<td class="none" align="center">No</td>
1011+
<td class="svn" align="center">SVN</td>
10121012
</tr>
10131013
<tr>
10141014
<td><tt>char8_t</tt></td>

0 commit comments

Comments
 (0)