instantiate-declref.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. namespace N {
  3. struct Outer {
  4. struct Inner {
  5. template<typename T>
  6. struct InnerTemplate {
  7. struct VeryInner {
  8. typedef T type;
  9. static enum K1 { K1Val = sizeof(T) } Kind1;
  10. static enum { K2Val = sizeof(T)*2 } Kind2;
  11. enum { K3Val = sizeof(T)*2 } Kind3;
  12. void foo() {
  13. K1 k1 = K1Val;
  14. Kind1 = K1Val;
  15. Outer::Inner::InnerTemplate<type>::VeryInner::Kind2 = K2Val;
  16. Kind3 = K3Val;
  17. }
  18. struct UeberInner {
  19. void bar() {
  20. K1 k1 = K1Val;
  21. Kind1 = K1Val;
  22. Outer::Inner::InnerTemplate<type>::VeryInner::Kind2 = K2Val;
  23. InnerTemplate t;
  24. InnerTemplate<type> t2;
  25. }
  26. };
  27. };
  28. };
  29. };
  30. };
  31. }
  32. typedef int INT;
  33. template struct N::Outer::Inner::InnerTemplate<INT>::VeryInner;
  34. template struct N::Outer::Inner::InnerTemplate<INT>::UeberInner; // expected-error{{no struct named 'UeberInner' in 'N::Outer::Inner::InnerTemplate<int>'}}
  35. namespace N2 {
  36. struct Outer2 {
  37. template<typename T, typename U = T>
  38. struct Inner {
  39. void foo() {
  40. enum { K1Val = sizeof(T) } k1;
  41. enum K2 { K2Val = sizeof(T)*2 } k2a;
  42. K2 k2b = K2Val;
  43. struct S { T x, y; } s1;
  44. struct { U x, y; } s2;
  45. s1.x = s2.x; // expected-error{{incompatible}}
  46. typedef T type;
  47. type t2 = s1.x;
  48. typedef struct { T z; } type2;
  49. type2 t3 = { s1.x };
  50. Inner i1;
  51. i1.foo();
  52. Inner<T> i2;
  53. i2.foo();
  54. }
  55. };
  56. };
  57. }
  58. template struct N2::Outer2::Inner<float>;
  59. template struct N2::Outer2::Inner<int*, float*>; // expected-note{{instantiation}}
  60. // Test dependent pointer-to-member expressions.
  61. template<typename T>
  62. struct smart_ptr {
  63. struct safe_bool {
  64. int member;
  65. };
  66. operator int safe_bool::*() const {
  67. return ptr? &safe_bool::member : 0;
  68. }
  69. T* ptr;
  70. };
  71. void test_smart_ptr(smart_ptr<int> p) {
  72. if (p) { }
  73. }
  74. // PR5517
  75. namespace test0 {
  76. template <int K> struct X {
  77. X() { extern void x(); }
  78. };
  79. void g() { X<2>(); }
  80. }
  81. // <rdar://problem/8302161>
  82. namespace test1 {
  83. template <typename T> void f(T const &t) {
  84. union { char c; T t_; };
  85. c = 'a'; // <- this shouldn't silently fail to instantiate
  86. T::foo(); // expected-error {{has no members}}
  87. }
  88. template void f(int const &); // expected-note {{requested here}}
  89. }
  90. namespace test2 {
  91. template<typename T> void f() {
  92. T::error; // expected-error {{no member}}
  93. }
  94. void g() {
  95. // This counts as an odr-use, so should trigger the instantiation of f<int>.
  96. (void)&f<int>; // expected-note {{here}}
  97. }
  98. }