instantiate-expr-1.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-pc-linux-gnu
  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. void test_Bitfields(Bitfields<0, 5> *b) {
  8. (void)sizeof(Bitfields<10, 5>);
  9. (void)sizeof(Bitfields<0, 1>); // expected-note{{in instantiation of template class 'Bitfields<0, 1>' requested here}}
  10. }
  11. template<int I, int J>
  12. struct BitfieldPlus {
  13. int bitfield : I + J; // expected-error{{bit-field 'bitfield' has zero width}}
  14. };
  15. void test_BitfieldPlus() {
  16. (void)sizeof(BitfieldPlus<0, 1>);
  17. (void)sizeof(BitfieldPlus<-5, 5>); // expected-note{{in instantiation of template class 'BitfieldPlus<-5, 5>' requested here}}
  18. }
  19. template<int I, int J>
  20. struct BitfieldMinus {
  21. int bitfield : I - J; // expected-error{{bit-field 'bitfield' has negative width (-1)}} \
  22. // expected-error{{bit-field 'bitfield' has zero width}}
  23. };
  24. void test_BitfieldMinus() {
  25. (void)sizeof(BitfieldMinus<5, 1>);
  26. (void)sizeof(BitfieldMinus<0, 1>); // expected-note{{in instantiation of template class 'BitfieldMinus<0, 1>' requested here}}
  27. (void)sizeof(BitfieldMinus<5, 5>); // expected-note{{in instantiation of template class 'BitfieldMinus<5, 5>' requested here}}
  28. }
  29. template<int I, int J>
  30. struct BitfieldDivide {
  31. int bitfield : I / J; // expected-error{{expression is not an integral constant expression}} \
  32. // expected-note{{division by zero}}
  33. };
  34. void test_BitfieldDivide() {
  35. (void)sizeof(BitfieldDivide<5, 1>);
  36. (void)sizeof(BitfieldDivide<5, 0>); // expected-note{{in instantiation of template class 'BitfieldDivide<5, 0>' requested here}}
  37. }
  38. template<typename T, T I, int J>
  39. struct BitfieldDep {
  40. int bitfield : I + J;
  41. };
  42. void test_BitfieldDep() {
  43. (void)sizeof(BitfieldDep<int, 1, 5>);
  44. }
  45. template<int I>
  46. struct BitfieldNeg {
  47. int bitfield : (-I); // expected-error{{bit-field 'bitfield' has negative width (-5)}}
  48. };
  49. template<typename T, T I>
  50. struct BitfieldNeg2 {
  51. int bitfield : (-I); // expected-error{{bit-field 'bitfield' has negative width (-5)}}
  52. };
  53. void test_BitfieldNeg() {
  54. (void)sizeof(BitfieldNeg<-5>); // okay
  55. (void)sizeof(BitfieldNeg<5>); // expected-note{{in instantiation of template class 'BitfieldNeg<5>' requested here}}
  56. (void)sizeof(BitfieldNeg2<int, -5>); // okay
  57. (void)sizeof(BitfieldNeg2<int, 5>); // expected-note{{in instantiation of template class 'BitfieldNeg2<int, 5>' requested here}}
  58. }
  59. template<typename T>
  60. void increment(T &x) {
  61. (void)++x;
  62. }
  63. struct Incrementable {
  64. Incrementable &operator++();
  65. };
  66. void test_increment(Incrementable inc) {
  67. increment(inc);
  68. }
  69. template<typename T>
  70. void add(const T &x) {
  71. (void)(x + x);
  72. }
  73. namespace PR6237 {
  74. template <typename T>
  75. void f(T t) {
  76. t++;
  77. }
  78. struct B { };
  79. B operator++(B &, int);
  80. template void f(B);
  81. }
  82. struct Addable {
  83. Addable operator+(const Addable&) const;
  84. };
  85. void test_add(Addable &a) {
  86. add(a);
  87. }
  88. struct CallOperator {
  89. int &operator()(int);
  90. double &operator()(double);
  91. };
  92. template<typename Result, typename F, typename Arg1>
  93. Result test_call_operator(F f, Arg1 arg1) {
  94. // PR5266: non-dependent invocations of a function call operator.
  95. CallOperator call_op;
  96. int &ir = call_op(17);
  97. return f(arg1);
  98. }
  99. void test_call_operator(CallOperator call_op, int i, double d) {
  100. int &ir = test_call_operator<int&>(call_op, i);
  101. double &dr = test_call_operator<double&>(call_op, d);
  102. }
  103. template<typename T>
  104. void test_asm(T t) {
  105. asm ("nop" : "=r"(*t) : "r"(*t)); // expected-error {{indirection requires pointer operand ('int' invalid)}}
  106. }
  107. void test_asm() {
  108. int* a;
  109. test_asm(a);
  110. int b;
  111. test_asm(b); // expected-note {{in instantiation of function template specialization 'test_asm<int>' requested here}}
  112. }
  113. namespace PR6424 {
  114. template<int I> struct X {
  115. X() {
  116. int *ip = I; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
  117. }
  118. };
  119. template<int> struct Y {
  120. typedef X<7> X7;
  121. void f() { X7(); } // expected-note{{instantiation}}
  122. };
  123. template void Y<3>::f();
  124. template<int I>
  125. struct X2 {
  126. void *operator new(__SIZE_TYPE__) {
  127. int *ip = I; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
  128. return ip;
  129. }
  130. };
  131. template<int> struct Y2 {
  132. typedef X2<7> X;
  133. void f() {
  134. new X(); // expected-note{{instantiation of}}
  135. }
  136. };
  137. template void Y2<3>::f();
  138. template<typename T>
  139. void rdar10283928(int count) {
  140. (void)new char[count]();
  141. }
  142. template void rdar10283928<int>(int);
  143. }
  144. namespace PR10864 {
  145. template<typename T> class Vals {};
  146. template<> class Vals<int> { public: static const int i = 1; };
  147. template<> class Vals<float> { public: static const double i; };
  148. template<typename T> void test_asm_tied(T o) {
  149. __asm("addl $1, %0" : "=r" (o) : "0"(Vals<T>::i)); // expected-error {{input with type 'double' matching output with type 'float'}}
  150. }
  151. void test_asm_tied() {
  152. test_asm_tied(1);
  153. test_asm_tied(1.f); // expected-note {{instantiation of}}
  154. }
  155. }