blob: 5ff58a2ec476ed7b17fa8741435f31295762e713 [file] [log] [blame]
Richard Trieu52541612011-07-21 02:46:28 +00001// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wsign-conversion %s
Steve Naroffb6d54e52008-01-08 01:11:38 +00002void foo() {
3 *(0 ? (double *)0 : (void *)0) = 0;
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00004 // FIXME: GCC doesn't consider the following two statements to be errors.
Steve Naroffaa58f002008-01-14 16:10:57 +00005 *(0 ? (double *)0 : (void *)(int *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
6 *(0 ? (double *)0 : (void *)(double *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
Steve Naroff890d93e2008-01-30 21:50:43 +00007 *(0 ? (double *)0 : (int *)(void *)0) = 0; // expected-error {{incomplete type 'void' is not assignable}} expected-warning {{pointer type mismatch ('double *' and 'int *')}}
Steve Naroffaaffbf72008-01-14 02:53:34 +00008 *(0 ? (double *)0 : (double *)(void *)0) = 0;
9 *((void *) 0) = 0; // expected-error {{incomplete type 'void' is not assignable}}
Steve Naroffb6d54e52008-01-08 01:11:38 +000010 double *dp;
11 int *ip;
12 void *vp;
13
14 dp = vp;
15 vp = dp;
Douglas Gregord4eea832010-04-09 00:35:39 +000016 ip = dp; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
17 dp = ip; // expected-warning {{incompatible pointer types assigning to 'double *' from 'int *'}}
Steve Naroffb6d54e52008-01-08 01:11:38 +000018 dp = 0 ? (double *)0 : (void *)0;
19 vp = 0 ? (double *)0 : (void *)0;
Douglas Gregord4eea832010-04-09 00:35:39 +000020 ip = 0 ? (double *)0 : (void *)0; // expected-warning {{incompatible pointer types assigning to 'int *' from 'double *'}}
Eli Friedmanf76f5ed2008-02-10 23:14:16 +000021
22 const int *cip;
23 vp = (0 ? vp : cip); // expected-warning {{discards qualifiers}}
24 vp = (0 ? cip : vp); // expected-warning {{discards qualifiers}}
Eli Friedman4c721d32008-02-12 08:23:06 +000025
26 int i = 2;
27 int (*pf)[2];
28 int (*pv)[i];
29 pf = (i ? pf : pv);
Eli Friedmanbab96962008-02-12 08:46:17 +000030
31 enum {xxx,yyy,zzz} e, *ee;
32 short x;
33 ee = ee ? &x : ee ? &i : &e; // expected-warning {{pointer type mismatch}}
Eli Friedman4b3f9b32008-02-13 17:29:58 +000034
35 typedef void *asdf;
36 *(0 ? (asdf) 0 : &x) = 10;
John McCallb13c87f2009-11-05 09:23:39 +000037
38 unsigned long test0 = 5;
Richard Trieu52541612011-07-21 02:46:28 +000039 test0 = test0 ? (long) test0 : test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
40 test0 = test0 ? (int) test0 : test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
41 test0 = test0 ? (short) test0 : test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
42 test0 = test0 ? test0 : (long) test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
43 test0 = test0 ? test0 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
44 test0 = test0 ? test0 : (short) test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
John McCallb13c87f2009-11-05 09:23:39 +000045 test0 = test0 ? test0 : (long) 10;
46 test0 = test0 ? test0 : (int) 10;
47 test0 = test0 ? test0 : (short) 10;
48 test0 = test0 ? (long) 10 : test0;
49 test0 = test0 ? (int) 10 : test0;
50 test0 = test0 ? (short) 10 : test0;
51
Richard Trieu52541612011-07-21 02:46:28 +000052 int test1;
John McCallb13c87f2009-11-05 09:23:39 +000053 enum Enum { EVal };
54 test0 = test0 ? EVal : test0;
Richard Trieu52541612011-07-21 02:46:28 +000055 test1 = test0 ? EVal : (int) test0;
56 test0 = test0 ?
John McCall323ed742010-05-06 08:58:33 +000057 (unsigned) EVal
Richard Trieu52541612011-07-21 02:46:28 +000058 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
59
60 test0 = test0 ? EVal : test1; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
61 test0 = test0 ? test1 : EVal; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
62
Eli Friedmanae916a12012-04-05 22:30:04 +000063 const int *const_int;
64 int *nonconst_int;
65 *(test0 ? const_int : nonconst_int) = 42; // expected-error {{read-only variable is not assignable}}
66 *(test0 ? nonconst_int : const_int) = 42; // expected-error {{read-only variable is not assignable}}
67
68 // The composite type here should be "int (*)[12]", fine for the sizeof
69 int (*incomplete)[];
70 int (*complete)[12];
71 sizeof(*(test0 ? incomplete : complete)); // expected-warning {{expression result unused}}
72 sizeof(*(test0 ? complete : incomplete)); // expected-warning {{expression result unused}}
73
74 int __attribute__((address_space(2))) *adr2;
75 int __attribute__((address_space(3))) *adr3;
76 test0 ? adr2 : adr3; // expected-warning {{pointer type mismatch}} expected-warning {{expression result unused}}
77
78 // Make sure address-space mask ends up in the result type
79 (test0 ? (test0 ? adr2 : adr2) : nonconst_int); // expected-warning {{pointer type mismatch}} expected-warning {{expression result unused}}
Steve Naroffb6d54e52008-01-08 01:11:38 +000080}
81
Steve Naroffe701c0a2008-05-12 21:44:38 +000082int Postgresql() {
83 char x;
84 return ((((&x) != ((void *) 0)) ? (*(&x) = ((char) 1)) : (void) ((void *) 0)), (unsigned long) ((void *) 0)); // expected-warning {{C99 forbids conditional expressions with only one void side}}
85}
Steve Naroff91588042009-04-08 17:05:15 +000086
87#define nil ((void*) 0)
88
89extern int f1(void);
90
91int f0(int a) {
92 // GCC considers this a warning.
Douglas Gregord4eea832010-04-09 00:35:39 +000093 return a ? f1() : nil; // expected-warning {{pointer/integer type mismatch in conditional expression ('int' and 'void *')}} expected-warning {{incompatible pointer to integer conversion returning 'void *' from a function with result type 'int'}}
Steve Naroff91588042009-04-08 17:05:15 +000094}
John McCall323ed742010-05-06 08:58:33 +000095
96int f2(int x) {
97 // We can suppress this because the immediate context wants an int.
98 return (x != 0) ? 0U : x;
99}
Chandler Carruth82214a82011-02-18 23:54:50 +0000100
101#define NULL (void*)0
102
103void PR9236() {
104 struct A {int i;} A1;
105 (void)(1 ? A1 : NULL); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
106 (void)(1 ? NULL : A1); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
107 (void)(1 ? 0 : A1); // expected-error{{incompatible operand types}}
108 (void)(1 ? (void*)0 : A1); // expected-error{{incompatible operand types}}
109 (void)(1 ? A1: (void*)0); // expected-error{{incompatible operand types}}
110 (void)(1 ? A1 : (NULL)); // expected-error{{non-pointer operand type 'struct A' incompatible with NULL}}
111}
112