Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 2 | template<int I, int J> |
| 3 | struct Bitfields { |
| 4 | int simple : I; // expected-error{{bit-field 'simple' has zero width}} |
| 5 | int parens : (J); |
| 6 | }; |
| 7 | |
| 8 | void test_Bitfields(Bitfields<0, 5> *b) { |
| 9 | (void)sizeof(Bitfields<10, 5>); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 10 | (void)sizeof(Bitfields<0, 1>); // expected-note{{in instantiation of template class 'Bitfields<0, 1>' requested here}} |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 11 | } |
Douglas Gregor | df03251 | 2009-03-12 22:46:12 +0000 | [diff] [blame] | 12 | |
| 13 | template<int I, int J> |
| 14 | struct BitfieldPlus { |
| 15 | int bitfield : I + J; // expected-error{{bit-field 'bitfield' has zero width}} |
| 16 | }; |
| 17 | |
| 18 | void test_BitfieldPlus() { |
| 19 | (void)sizeof(BitfieldPlus<0, 1>); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 20 | (void)sizeof(BitfieldPlus<-5, 5>); // expected-note{{in instantiation of template class 'BitfieldPlus<-5, 5>' requested here}} |
Douglas Gregor | df03251 | 2009-03-12 22:46:12 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | template<int I, int J> |
| 24 | struct BitfieldMinus { |
| 25 | int bitfield : I - J; // expected-error{{bit-field 'bitfield' has negative width (-1)}} \ |
| 26 | // expected-error{{bit-field 'bitfield' has zero width}} |
| 27 | }; |
| 28 | |
| 29 | void test_BitfieldMinus() { |
| 30 | (void)sizeof(BitfieldMinus<5, 1>); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 31 | (void)sizeof(BitfieldMinus<0, 1>); // expected-note{{in instantiation of template class 'BitfieldMinus<0, 1>' requested here}} |
| 32 | (void)sizeof(BitfieldMinus<5, 5>); // expected-note{{in instantiation of template class 'BitfieldMinus<5, 5>' requested here}} |
Douglas Gregor | df03251 | 2009-03-12 22:46:12 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | template<int I, int J> |
| 36 | struct BitfieldDivide { |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 37 | int bitfield : I / J; // expected-error{{expression is not an integral constant expression}} \ |
Chris Lattner | cb329c5 | 2010-01-12 21:30:55 +0000 | [diff] [blame] | 38 | // expected-note{{division by zero}} |
Douglas Gregor | df03251 | 2009-03-12 22:46:12 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | void test_BitfieldDivide() { |
| 42 | (void)sizeof(BitfieldDivide<5, 1>); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 43 | (void)sizeof(BitfieldDivide<5, 0>); // expected-note{{in instantiation of template class 'BitfieldDivide<5, 0>' requested here}} |
Douglas Gregor | df03251 | 2009-03-12 22:46:12 +0000 | [diff] [blame] | 44 | } |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 45 | |
| 46 | template<typename T, T I, int J> |
| 47 | struct BitfieldDep { |
| 48 | int bitfield : I + J; |
| 49 | }; |
| 50 | |
| 51 | void test_BitfieldDep() { |
| 52 | (void)sizeof(BitfieldDep<int, 1, 5>); |
| 53 | } |
| 54 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 55 | template<int I> |
| 56 | struct BitfieldNeg { |
| 57 | int bitfield : (-I); // expected-error{{bit-field 'bitfield' has negative width (-5)}} |
| 58 | }; |
| 59 | |
| 60 | template<typename T, T I> |
| 61 | struct BitfieldNeg2 { |
| 62 | int bitfield : (-I); // expected-error{{bit-field 'bitfield' has negative width (-5)}} |
| 63 | }; |
| 64 | |
| 65 | void test_BitfieldNeg() { |
| 66 | (void)sizeof(BitfieldNeg<-5>); // okay |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 67 | (void)sizeof(BitfieldNeg<5>); // expected-note{{in instantiation of template class 'BitfieldNeg<5>' requested here}} |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 68 | (void)sizeof(BitfieldNeg2<int, -5>); // okay |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 69 | (void)sizeof(BitfieldNeg2<int, 5>); // expected-note{{in instantiation of template class 'BitfieldNeg2<int, 5>' requested here}} |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 70 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 71 | |
| 72 | template<typename T> |
| 73 | void increment(T &x) { |
| 74 | (void)++x; |
| 75 | } |
| 76 | |
| 77 | struct Incrementable { |
| 78 | Incrementable &operator++(); |
| 79 | }; |
| 80 | |
| 81 | void test_increment(Incrementable inc) { |
| 82 | increment(inc); |
| 83 | } |
| 84 | |
| 85 | template<typename T> |
| 86 | void add(const T &x) { |
| 87 | (void)(x + x); |
| 88 | } |
| 89 | |
Douglas Gregor | dc81c88 | 2010-02-05 05:15:43 +0000 | [diff] [blame] | 90 | namespace PR6237 { |
| 91 | template <typename T> |
| 92 | void f(T t) { |
| 93 | t++; |
| 94 | } |
| 95 | |
| 96 | struct B { }; |
| 97 | B operator++(B &, int); |
| 98 | |
| 99 | template void f(B); |
| 100 | } |
| 101 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 102 | struct Addable { |
| 103 | Addable operator+(const Addable&) const; |
| 104 | }; |
| 105 | |
| 106 | void test_add(Addable &a) { |
| 107 | add(a); |
| 108 | } |
Douglas Gregor | 668d6d9 | 2009-12-13 20:44:55 +0000 | [diff] [blame] | 109 | |
| 110 | struct CallOperator { |
| 111 | int &operator()(int); |
| 112 | double &operator()(double); |
| 113 | }; |
| 114 | |
| 115 | template<typename Result, typename F, typename Arg1> |
| 116 | Result test_call_operator(F f, Arg1 arg1) { |
| 117 | // PR5266: non-dependent invocations of a function call operator. |
| 118 | CallOperator call_op; |
| 119 | int &ir = call_op(17); |
| 120 | return f(arg1); |
| 121 | } |
| 122 | |
| 123 | void test_call_operator(CallOperator call_op, int i, double d) { |
| 124 | int &ir = test_call_operator<int&>(call_op, i); |
| 125 | double &dr = test_call_operator<double&>(call_op, d); |
| 126 | } |
Anders Carlsson | b2e90ac | 2010-01-24 05:50:37 +0000 | [diff] [blame] | 127 | |
| 128 | template<typename T> |
| 129 | void test_asm(T t) { |
Simon Atanasyan | d95e95e | 2012-05-22 11:03:10 +0000 | [diff] [blame] | 130 | asm ("nop" : "=r"(*t) : "r"(*t)); // expected-error {{indirection requires pointer operand ('int' invalid)}} |
Anders Carlsson | b2e90ac | 2010-01-24 05:50:37 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void test_asm() { |
| 134 | int* a; |
| 135 | test_asm(a); |
| 136 | |
| 137 | int b; |
| 138 | test_asm(b); // expected-note {{in instantiation of function template specialization 'test_asm<int>' requested here}} |
| 139 | } |
Douglas Gregor | c845aad | 2010-02-26 00:01:57 +0000 | [diff] [blame] | 140 | |
| 141 | namespace PR6424 { |
| 142 | template<int I> struct X { |
| 143 | X() { |
| 144 | int *ip = I; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}} |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | template<int> struct Y { |
| 149 | typedef X<7> X7; |
| 150 | |
| 151 | void f() { X7(); } // expected-note{{instantiation}} |
| 152 | }; |
| 153 | |
| 154 | template void Y<3>::f(); |
Douglas Gregor | 1af7451 | 2010-02-26 00:38:10 +0000 | [diff] [blame] | 155 | |
| 156 | template<int I> |
| 157 | struct X2 { |
| 158 | void *operator new(__SIZE_TYPE__) { |
| 159 | int *ip = I; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}} |
| 160 | return ip; |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | template<int> struct Y2 { |
| 165 | typedef X2<7> X; |
| 166 | void f() { |
| 167 | new X(); // expected-note{{instantiation of}} |
| 168 | } |
| 169 | }; |
Douglas Gregor | 4e8ea0b | 2011-10-18 02:43:19 +0000 | [diff] [blame] | 170 | |
Douglas Gregor | 1af7451 | 2010-02-26 00:38:10 +0000 | [diff] [blame] | 171 | template void Y2<3>::f(); |
Douglas Gregor | 4e8ea0b | 2011-10-18 02:43:19 +0000 | [diff] [blame] | 172 | |
| 173 | template<typename T> |
| 174 | void rdar10283928(int count) { |
| 175 | (void)new char[count](); |
| 176 | } |
| 177 | |
| 178 | template void rdar10283928<int>(int); |
Douglas Gregor | c845aad | 2010-02-26 00:01:57 +0000 | [diff] [blame] | 179 | } |
Eli Friedman | f45b357 | 2011-09-14 19:20:00 +0000 | [diff] [blame] | 180 | |
| 181 | namespace PR10864 { |
| 182 | template<typename T> class Vals {}; |
| 183 | template<> class Vals<int> { public: static const int i = 1; }; |
| 184 | template<> class Vals<float> { public: static const double i; }; |
| 185 | template<typename T> void test_asm_tied(T o) { |
| 186 | __asm("addl $1, %0" : "=r" (o) : "0"(Vals<T>::i)); // expected-error {{input with type 'double' matching output with type 'float'}} |
| 187 | } |
| 188 | void test_asm_tied() { |
| 189 | test_asm_tied(1); |
| 190 | test_asm_tied(1.f); // expected-note {{instantiation of}} |
| 191 | } |
| 192 | } |