2009-12-16 04:14:24 +08:00
// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
2008-02-03 04:20:10 +08:00
2011-02-23 10:15:19 +08:00
// PR 8876 - don't warn about trivially unreachable null derefs. Note that
// we put this here because the reachability analysis only kicks in for
// suppressing false positives when code has no errors.
# define PR8876(err_ptr) do {\
if ( err_ptr ) * ( int * ) ( err_ptr ) = 1 ; \
} while ( 0 )
# define PR8876_pos(err_ptr) do {\
if ( ! err_ptr ) * ( int * ) ( err_ptr ) = 1 ; \
} while ( 0 )
2011-03-24 05:33:21 +08:00
// Test that we don't report divide-by-zero errors in unreachable code.
// This test should be left as is, as it also tests CFG functionality.
void radar9171946 ( ) {
if ( 0 ) {
0 / ( 0 ? 1 : 0 ) ; // expected-warning {{expression result unused}}
}
}
2011-02-23 10:15:19 +08:00
int test_pr8876 ( ) {
PR8876 ( 0 ) ; // no-warning
PR8876_pos ( 0 ) ; // expected-warning{{indirection of non-volatile null pointer will be deleted, not trap}} expected-note{{consider using __builtin_trap() or qualifying pointer with 'volatile'}}
return 0 ;
}
2011-02-23 13:11:46 +08:00
// PR 8183 - Handle null pointer constants on the left-side of the '&&', and reason about
// this when determining the reachability of the null pointer dereference on the right side.
void pr8183 ( unsigned long long test )
{
( void ) ( ( ( ( void * ) 0 ) ) & & ( * ( ( unsigned long long * ) ( ( ( void * ) 0 ) ) ) = ( ( unsigned long long ) ( ( test ) ) % ( unsigned long long ) ( ( 1000000000 ) ) ) ) ) ; // no-warning
( * ( ( unsigned long long * ) ( ( ( void * ) 0 ) ) ) = ( ( unsigned long long ) ( ( test ) ) % ( unsigned long long ) ( ( 1000000000 ) ) ) ) ; // expected-warning {{indirection of non-volatile null pointer will be deleted, not trap}} expected-note {{consider using __builtin_trap() or qualifying pointer with 'volatile'}}
}
2008-02-03 04:20:10 +08:00
// PR1966
_Complex double test1 ( ) {
return __extension__ 1.0 if ;
}
_Complex double test2 ( ) {
2012-11-02 09:40:23 +08:00
return 1.0 if ; // expected-warning {{imaginary constants are a GNU extension}}
2008-02-03 04:20:10 +08:00
}
2008-07-26 02:07:19 +08:00
// rdar://6097308
void test3 ( ) {
int x ;
( __extension__ x ) = 10 ;
}
2008-08-22 02:04:13 +08:00
// rdar://6162726
void test4 ( ) {
static int var ;
var = + 5 ; // expected-warning {{use of unary operator that may be intended as compound assignment (+=)}}
var = - 5 ; // expected-warning {{use of unary operator that may be intended as compound assignment (-=)}}
2009-03-08 14:51:10 +08:00
var = + 5 ; // no warning when space between the = and +.
2008-08-22 02:04:13 +08:00
var = - 5 ;
2009-03-08 14:51:10 +08:00
var = + 5 ; // no warning when the subexpr of the unary op has no space before it.
var = - 5 ;
2009-03-09 15:11:10 +08:00
# define FIVE 5
var = - FIVE ; // no warning with macros.
var = - FIVE ;
2008-08-22 02:04:13 +08:00
}
2008-11-18 03:51:54 +08:00
// rdar://6319320
void test5 ( int * X , float * P ) {
( float * ) X = P ; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
2009-04-15 08:08:05 +08:00
# define FOO ((float*) X)
FOO = P ; // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
2008-11-18 03:51:54 +08:00
}
2008-11-22 02:27:34 +08:00
void test6 ( ) {
int X ;
X ( ) ; // expected-error {{called object type 'int' is not a function or function pointer}}
}
2008-11-23 03:57:03 +08:00
void test7 ( int * P , _Complex float Gamma ) {
P = ( P - 42 ) + Gamma * 4 ; // expected-error {{invalid operands to binary expression ('int *' and '_Complex float')}}
}
2008-12-12 13:35:08 +08:00
// rdar://6095061
int test8 ( void ) {
int i ;
2009-04-28 11:13:54 +08:00
__builtin_choose_expr ( 0 , 42 , i ) = 10 ;
2008-12-12 13:35:08 +08:00
return i ;
}
2009-01-25 04:17:12 +08:00
// PR3386
struct f { int x : 4 ; float y [ ] ; } ;
int test9 ( struct f * P ) {
2009-01-25 05:29:22 +08:00
int R ;
2013-03-19 07:37:25 +08:00
R = __alignof ( P - > x ) ; // expected-error {{invalid application of 'alignof' to bit-field}}
2009-04-28 11:13:54 +08:00
R = __alignof ( P - > y ) ; // ok.
2009-09-15 06:00:20 +08:00
R = sizeof ( P - > x ) ; // expected-error {{invalid application of 'sizeof' to bit-field}}
2009-01-25 05:29:22 +08:00
return R ;
2009-01-25 04:17:12 +08:00
}
2009-02-14 06:08:30 +08:00
// PR3562
void test10 ( int n , . . . ) {
struct S {
double a [ n ] ; // expected-error {{fields must have a constant size}}
} s ;
double x = s . a [ 0 ] ; // should not get another error here.
}
2009-02-20 07:45:49 +08:00
# define MYMAX(A,B) __extension__ ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
struct mystruct { int A ; } ;
2009-03-09 03:39:53 +08:00
void test11 ( struct mystruct P , float F ) {
2009-05-22 18:22:18 +08:00
MYMAX ( P , F ) ; // expected-error {{invalid operands to binary expression ('typeof (P)' (aka 'struct mystruct') and 'typeof (F)' (aka 'float'))}}
2009-02-20 07:45:49 +08:00
}
2009-03-09 03:39:53 +08:00
// PR3753
int test12 ( const char * X ) {
2010-04-10 04:26:53 +08:00
return X = = " foo " ; // expected-warning {{comparison against a string literal is unspecified (use strncmp instead)}}
2009-03-09 03:39:53 +08:00
}
2010-01-13 07:18:54 +08:00
int test12b ( const char * X ) {
return sizeof ( X = = " foo " ) ; // no-warning
}
2009-03-27 12:18:06 +08:00
// rdar://6719156
void test13 (
void ( ^ P ) ( ) ) { // expected-error {{blocks support disabled - compile with -fblocks}}
P ( ) ;
P = ^ ( ) { } ; // expected-error {{blocks support disabled - compile with -fblocks}}
}
2009-03-31 15:46:52 +08:00
void test14 ( ) {
2009-04-28 11:13:54 +08:00
typedef long long __m64 __attribute__ ( ( __vector_size__ ( 8 ) ) ) ;
typedef short __v4hi __attribute__ ( ( __vector_size__ ( 8 ) ) ) ;
2009-03-31 15:46:52 +08:00
2009-07-08 09:08:03 +08:00
// Ok.
2009-03-31 15:46:52 +08:00
__v4hi a ;
2009-07-08 09:08:03 +08:00
__m64 mask = ( __m64 ) ( ( __v4hi ) a > ( __v4hi ) a ) ;
2009-03-31 15:46:52 +08:00
}
2009-10-20 13:36:05 +08:00
// PR5242
typedef unsigned long * test15_t ;
test15_t test15 ( void ) {
return ( test15_t ) 0 + ( test15_t ) 0 ; // expected-error {{invalid operands to binary expression ('test15_t' (aka 'unsigned long *') and 'test15_t')}}
}
2009-12-05 13:40:13 +08:00
// rdar://7446395
void test16 ( float x ) { x = = ( ( void * ) 0 ) ; } // expected-error {{invalid operands to binary expression}}
2009-10-20 13:36:05 +08:00
2010-01-13 05:23:57 +08:00
// PR6004
void test17 ( int x ) {
x = x / 0 ; // expected-warning {{division by zero is undefined}}
x = x % 0 ; // expected-warning {{remainder by zero is undefined}}
x / = 0 ; // expected-warning {{division by zero is undefined}}
x % = 0 ; // expected-warning {{remainder by zero is undefined}}
2010-01-13 05:30:55 +08:00
x = sizeof ( x / 0 ) ; // no warning.
2010-01-13 05:23:57 +08:00
}
2012-05-11 13:16:41 +08:00
// PR6501 & PR11857
2011-07-29 08:24:42 +08:00
void test18_a ( int a ) ; // expected-note 2 {{'test18_a' declared here}}
2012-05-11 13:16:41 +08:00
void test18_b ( int ) ; // expected-note {{'test18_b' declared here}}
2012-05-15 14:21:54 +08:00
void test18_c ( int a , int b ) ; // expected-note 2 {{'test18_c' declared here}}
2012-05-11 13:16:41 +08:00
void test18_d ( int a , . . . ) ; // expected-note {{'test18_d' declared here}}
2012-05-15 14:21:54 +08:00
void test18_e ( int a , int b , . . . ) ; // expected-note {{'test18_e' declared here}}
2010-04-20 02:39:43 +08:00
void test18 ( int b ) {
2012-05-15 14:21:54 +08:00
test18_a ( b , b ) ; // expected-error {{too many arguments to function call, expected single argument 'a', have 2}}
test18_a ( ) ; // expected-error {{too few arguments to function call, single argument 'a' was not specified}}
2012-05-11 13:16:41 +08:00
test18_b ( ) ; // expected-error {{too few arguments to function call, expected 1, have 0}}
test18_c ( b ) ; // expected-error {{too few arguments to function call, expected 2, have 1}}
2012-05-15 14:21:54 +08:00
test18_c ( b , b , b ) ; // expected-error {{too many arguments to function call, expected 2, have 3}}
2012-05-11 13:16:41 +08:00
test18_d ( ) ; // expected-error {{too few arguments to function call, at least argument 'a' must be specified}}
2012-05-15 14:21:54 +08:00
test18_e ( ) ; // expected-error {{too few arguments to function call, expected at least 2, have 0}}
2010-04-20 02:39:43 +08:00
}
2010-07-07 14:14:23 +08:00
// PR7569
void test19 ( ) {
* ( int * ) 0 = 0 ; / / expected - warning { { indirection of non - volatile null pointer } } \
// expected-note {{consider using __builtin_trap}}
* ( volatile int * ) 0 = 0 ; // Ok.
2011-04-27 01:41:22 +08:00
// rdar://9269271
int x = * ( int * ) 0 ; / / expected - warning { { indirection of non - volatile null pointer } } \
// expected-note {{consider using __builtin_trap}}
int x2 = * ( volatile int * ) 0 ; // Ok.
int * p = & ( * ( int * ) 0 ) ; // Ok;
2010-07-07 14:14:23 +08:00
}
2010-07-14 03:41:32 +08:00
int test20 ( int x ) {
2011-08-16 01:50:06 +08:00
return x & & 4 ; / / expected - warning { { use of logical ' & & ' with constant operand } } \
/ / expected - note { { use ' & ' for a bitwise operation } } \
// expected-note {{remove constant to silence this warning}}
2010-07-15 08:26:43 +08:00
2010-07-24 09:10:11 +08:00
return x & & sizeof ( int ) = = 4 ; // no warning, RHS is logical op.
// no warning, this is an idiom for "true" in old C style.
return x & & ( signed char ) 1 ;
2011-05-31 13:41:42 +08:00
return x | | 0 ;
return x | | 1 ;
2011-08-16 01:50:06 +08:00
return x | | - 1 ; / / expected - warning { { use of logical ' | | ' with constant operand } } \
// expected-note {{use '|' for a bitwise operation}}
return x | | 5 ; / / expected - warning { { use of logical ' | | ' with constant operand } } \
// expected-note {{use '|' for a bitwise operation}}
2011-05-31 13:41:42 +08:00
return x & & 0 ;
return x & & 1 ;
2011-08-16 01:50:06 +08:00
return x & & - 1 ; / / expected - warning { { use of logical ' & & ' with constant operand } } \
/ / expected - note { { use ' & ' for a bitwise operation } } \
// expected-note {{remove constant to silence this warning}}
return x & & 5 ; / / expected - warning { { use of logical ' & & ' with constant operand } } \
/ / expected - note { { use ' & ' for a bitwise operation } } \
// expected-note {{remove constant to silence this warning}}
2011-05-31 13:41:42 +08:00
return x | | ( 0 ) ;
return x | | ( 1 ) ;
2011-08-16 01:50:06 +08:00
return x | | ( - 1 ) ; / / expected - warning { { use of logical ' | | ' with constant operand } } \
// expected-note {{use '|' for a bitwise operation}}
return x | | ( 5 ) ; / / expected - warning { { use of logical ' | | ' with constant operand } } \
// expected-note {{use '|' for a bitwise operation}}
2011-05-31 13:41:42 +08:00
return x & & ( 0 ) ;
return x & & ( 1 ) ;
2011-08-16 01:50:06 +08:00
return x & & ( - 1 ) ; / / expected - warning { { use of logical ' & & ' with constant operand } } \
/ / expected - note { { use ' & ' for a bitwise operation } } \
// expected-note {{remove constant to silence this warning}}
return x & & ( 5 ) ; / / expected - warning { { use of logical ' & & ' with constant operand } } \
/ / expected - note { { use ' & ' for a bitwise operation } } \
// expected-note {{remove constant to silence this warning}}
2011-05-31 13:41:42 +08:00
2010-07-14 03:41:32 +08:00
}
2010-10-12 15:14:40 +08:00
struct Test21 ; // expected-note 2 {{forward declaration}}
void test21 ( volatile struct Test21 * ptr ) {
void test21_help ( void ) ;
( test21_help ( ) , * ptr ) ; // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
( * ptr , test21_help ( ) ) ; // expected-error {{incomplete type 'struct Test21' where a complete type is required}}
}
2010-12-04 14:09:13 +08:00
// Make sure we do function/array decay.
void test22 ( ) {
if ( " help " )
( void ) 0 ;
if ( test22 )
( void ) 0 ;
}