brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · 1ebccb1 Raw
84 lines · c
1// RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify %s2 3#include <limits.h>4 5#define WORD_BIT (sizeof(int) * CHAR_BIT)6 7enum {8  X = 1 << 0,9  Y = 1 << 1,10  Z = 1 << 211};12 13void test(void) {14  char c;15 16  c = 0 << 0;17  c = 0 << 1;18  c = 1 << 0;19  c = 1 << -0;20  c = 1 >> -0;21  c = 1 << -1; // expected-warning {{shift count is negative}}22  c = 1 >> -1; // expected-warning {{shift count is negative}}23  c = 1 << (unsigned)-1; // expected-warning {{shift count >= width of type}}24                         // expected-warning@-1 {{implicit conversion}}25  c = 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}}26  c = 1 << c;27  c <<= 0;28  c >>= 0;29  c <<= 1;30  c >>= 1;31  c <<= -1; // expected-warning {{shift count is negative}}32  c >>= -1; // expected-warning {{shift count is negative}}33  c <<= 999999; // expected-warning {{shift count >= width of type}}34  c >>= 999999; // expected-warning {{shift count >= width of type}}35  c <<= CHAR_BIT; // expected-warning {{shift count >= width of type}}36  c >>= CHAR_BIT; // expected-warning {{shift count >= width of type}}37  c <<= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}38  c >>= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}39  (void)((long)c << CHAR_BIT);40 41  int i;42  i = 1 << (WORD_BIT - 2);43  i = 2 << (WORD_BIT - 1); // expected-warning {{bits to represent, but 'int' only has}}44  i = 1 << (WORD_BIT - 1); // expected-warning {{sets the sign bit of the shift expression}}45  i = -1 << (WORD_BIT - 1); // expected-warning {{shifting a negative signed value is undefined}}46  i = -1 << 0; // expected-warning {{shifting a negative signed value is undefined}}47  i = 0 << (WORD_BIT - 1);48  i = (char)1 << (WORD_BIT - 2);49 50  unsigned u;51  u = 1U << (WORD_BIT - 1);52  u = 5U << (WORD_BIT - 1);53 54  long long int lli;55  lli = INT_MIN << 2; // expected-warning {{shifting a negative signed value is undefined}}56  lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);57}58 59#define a 060#define ashift 861enum { b = (a << ashift) };62 63// Don't warn for negative shifts in code that is unreachable.64void test_pr5544(void) {65  (void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning66}67 68void test_shift_too_much(char x) {69  if (0)70    (void) (x >> 80); // no-warning71  (void) (x >> 80); // expected-warning {{shift count >= width of type}}72}73 74typedef unsigned vec16 __attribute__((vector_size(16)));75typedef unsigned vec8 __attribute__((vector_size(8)));76 77void vect_shift_1(vec16 *x) { *x = *x << 4; }78 79void vect_shift_2(vec16 *x, vec16 y) { *x = *x << y; }80 81void vect_shift_3(vec16 *x, vec8 y) {82  *x = *x << y; // expected-error {{vector operands do not have the same number of elements}}83}84