2014-01-15 17:08:07 +08:00
|
|
|
// RUN: %clang_cc1 -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify %s
|
2009-08-08 00:20:20 +08:00
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
2011-02-24 07:34:11 +08:00
|
|
|
#define WORD_BIT (sizeof(int) * CHAR_BIT)
|
|
|
|
|
2009-08-08 00:20:20 +08:00
|
|
|
enum {
|
|
|
|
X = 1 << 0,
|
|
|
|
Y = 1 << 1,
|
|
|
|
Z = 1 << 2
|
|
|
|
};
|
2007-12-13 15:28:16 +08:00
|
|
|
|
|
|
|
void test() {
|
|
|
|
char c;
|
2009-08-08 00:20:20 +08:00
|
|
|
|
|
|
|
c = 0 << 0;
|
2009-08-09 03:18:23 +08:00
|
|
|
c = 0 << 1;
|
2009-08-08 00:20:20 +08:00
|
|
|
c = 1 << 0;
|
|
|
|
c = 1 << -0;
|
|
|
|
c = 1 >> -0;
|
|
|
|
c = 1 << -1; // expected-warning {{shift count is negative}}
|
|
|
|
c = 1 >> -1; // expected-warning {{shift count is negative}}
|
[APSInt][OpenMP] Fix isNegative, etc. for unsigned types
Without this patch, APSInt inherits APInt::isNegative, which merely
checks the sign bit without regard to whether the type is actually
signed. isNonNegative and isStrictlyPositive call isNegative and so
are also affected.
This patch adjusts APSInt to override isNegative, isNonNegative, and
isStrictlyPositive with implementations that consider whether the type
is signed.
A large set of Clang OpenMP tests are affected. Without this patch,
these tests assume that `true` is not a valid argument for clauses
like `collapse`. Indeed, `true` fails APInt::isStrictlyPositive but
not APSInt::isStrictlyPositive. This patch adjusts those tests to
assume `true` should be accepted.
This patch also adds tests revealing various other similar fixes due
to APSInt::isNegative calls in Clang's ExprConstant.cpp and
SemaExpr.cpp: `++` and `--` overflow in `constexpr`, evaluated object
size based on `alloc_size`, `<<` and `>>` shift count validation, and
OpenMP array section validation.
Reviewed By: lebedev.ri, ABataev, hfinkel
Differential Revision: https://reviews.llvm.org/D59712
llvm-svn: 359012
2019-04-24 01:04:15 +08:00
|
|
|
c = 1 << (unsigned)-1; // expected-warning {{shift count >= width of type}}
|
|
|
|
// expected-warning@-1 {{implicit conversion}}
|
|
|
|
c = 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}}
|
2009-08-08 00:20:20 +08:00
|
|
|
c = 1 << c;
|
2009-08-09 03:18:23 +08:00
|
|
|
c <<= 0;
|
|
|
|
c >>= 0;
|
2009-08-08 00:20:20 +08:00
|
|
|
c <<= 1;
|
|
|
|
c >>= 1;
|
|
|
|
c <<= -1; // expected-warning {{shift count is negative}}
|
|
|
|
c >>= -1; // expected-warning {{shift count is negative}}
|
|
|
|
c <<= 999999; // expected-warning {{shift count >= width of type}}
|
|
|
|
c >>= 999999; // expected-warning {{shift count >= width of type}}
|
|
|
|
c <<= CHAR_BIT; // expected-warning {{shift count >= width of type}}
|
|
|
|
c >>= CHAR_BIT; // expected-warning {{shift count >= width of type}}
|
|
|
|
c <<= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}
|
|
|
|
c >>= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}
|
|
|
|
(void)((long)c << CHAR_BIT);
|
2011-02-24 07:34:11 +08:00
|
|
|
|
|
|
|
int i;
|
|
|
|
i = 1 << (WORD_BIT - 2);
|
2011-02-25 01:13:15 +08:00
|
|
|
i = 2 << (WORD_BIT - 1); // expected-warning {{bits to represent, but 'int' only has}}
|
2011-06-15 08:54:52 +08:00
|
|
|
i = 1 << (WORD_BIT - 1); // expected-warning {{sets the sign bit of the shift expression}}
|
2015-07-07 02:02:09 +08:00
|
|
|
i = -1 << (WORD_BIT - 1); // expected-warning {{shifting a negative signed value is undefined}}
|
|
|
|
i = -1 << 0; // expected-warning {{shifting a negative signed value is undefined}}
|
2011-02-24 07:34:11 +08:00
|
|
|
i = 0 << (WORD_BIT - 1);
|
|
|
|
i = (char)1 << (WORD_BIT - 2);
|
|
|
|
|
|
|
|
unsigned u;
|
|
|
|
u = 1U << (WORD_BIT - 1);
|
|
|
|
u = 5U << (WORD_BIT - 1);
|
|
|
|
|
|
|
|
long long int lli;
|
2015-07-07 02:02:09 +08:00
|
|
|
lli = INT_MIN << 2; // expected-warning {{shifting a negative signed value is undefined}}
|
2011-02-24 07:34:11 +08:00
|
|
|
lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
|
2007-12-13 15:28:16 +08:00
|
|
|
}
|
2009-08-09 03:18:23 +08:00
|
|
|
|
|
|
|
#define a 0
|
|
|
|
#define ashift 8
|
|
|
|
enum { b = (a << ashift) };
|
|
|
|
|
2011-03-02 02:09:31 +08:00
|
|
|
// Don't warn for negative shifts in code that is unreachable.
|
|
|
|
void test_pr5544() {
|
|
|
|
(void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning
|
|
|
|
}
|
2011-03-02 03:13:22 +08:00
|
|
|
|
|
|
|
void test_shift_too_much(char x) {
|
|
|
|
if (0)
|
|
|
|
(void) (x >> 80); // no-warning
|
|
|
|
(void) (x >> 80); // expected-warning {{shift count >= width of type}}
|
|
|
|
}
|
2016-08-12 19:22:12 +08:00
|
|
|
|
|
|
|
typedef unsigned vec16 __attribute__((vector_size(16)));
|
|
|
|
typedef unsigned vec8 __attribute__((vector_size(8)));
|
|
|
|
|
|
|
|
void vect_shift_1(vec16 *x) { *x = *x << 4; }
|
|
|
|
|
|
|
|
void vect_shift_2(vec16 *x, vec16 y) { *x = *x << y; }
|
|
|
|
|
|
|
|
void vect_shift_3(vec16 *x, vec8 y) {
|
|
|
|
*x = *x << y; // expected-error {{vector operands do not have the same number of elements}}
|
|
|
|
}
|