cxx-decl.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // RUN: %clang_cc1 -verify -fsyntax-only -triple i386-linux -pedantic-errors -fcxx-exceptions -fexceptions %s
  2. const char const *x10; // expected-error {{duplicate 'const' declaration specifier}}
  3. int x(*g); // expected-error {{use of undeclared identifier 'g'}}
  4. struct Type {
  5. int Type;
  6. };
  7. // rdar://8365458
  8. // rdar://9132143
  9. typedef char bool; // expected-error {{redeclaration of C++ built-in type 'bool'}}
  10. // PR4451 - We should recover well from the typo of '::' as ':' in a2.
  11. namespace y {
  12. struct a { };
  13. typedef int b;
  14. }
  15. y::a a1;
  16. y:a a2; // expected-error {{unexpected ':' in nested name specifier}}
  17. y::a a3 = a2;
  18. // Some valid colons:
  19. void foo() {
  20. y: // label
  21. y::a s;
  22. int a = 4;
  23. a = a ? a : a+1;
  24. }
  25. struct b : y::a {};
  26. template <typename T>
  27. class someclass {
  28. int bar() {
  29. T *P;
  30. return 1 ? P->x : P->y;
  31. }
  32. };
  33. class asm_class_test {
  34. void foo() __asm__("baz");
  35. };
  36. enum { fooenum = 1, }; // expected-error {{commas at the end of enumerator lists are a C++11 extension}}
  37. struct a {
  38. int Type : fooenum;
  39. };
  40. void test(struct Type *P) {
  41. int Type;
  42. Type = 1 ? P->Type : Type;
  43. Type = (y:b) 4; // expected-error {{unexpected ':' in nested name specifier}}
  44. Type = 1 ? (
  45. (y:b) // expected-error {{unexpected ':' in nested name specifier}}
  46. 4) : 5;
  47. }
  48. struct test4 {
  49. int x // expected-error {{expected ';' at end of declaration list}}
  50. int y;
  51. int z // expected-error {{expected ';' at end of declaration list}}
  52. };
  53. // Make sure we know these are legitimate commas and not typos for ';'.
  54. namespace Commas {
  55. struct S {
  56. static int a;
  57. int c,
  58. operator()();
  59. };
  60. int global1,
  61. __attribute__(()) global2,
  62. (global5),
  63. *global6,
  64. &global7 = global1,
  65. &&global8 = static_cast<int&&>(global1), // expected-error 2{{rvalue reference}}
  66. S::a,
  67. global9,
  68. global10 = 0,
  69. global11 == 0, // expected-error {{did you mean '='}}
  70. global12 __attribute__(()),
  71. global13(0),
  72. global14[2],
  73. global15;
  74. void g() {
  75. static int a,
  76. b __asm__("ebx"), // expected-error {{expected ';' at end of declaration}}
  77. Statics:return;
  78. }
  79. }
  80. // PR5825
  81. struct test5 {};
  82. ::new(static_cast<void*>(0)) test5; // expected-error {{expected unqualified-id}}
  83. // PR6782
  84. template<class T>
  85. class Class1;
  86. class Class2 {
  87. } // expected-error {{expected ';' after class}}
  88. typedef Class1<Class2> Type1;
  89. // rdar : // 8307865
  90. struct CodeCompleteConsumer {
  91. };
  92. void CodeCompleteConsumer::() { // expected-error {{xpected unqualified-id}}
  93. }
  94. ;
  95. // PR4111
  96. void f(sqrgl); // expected-error {{unknown type name 'sqrgl'}}
  97. // PR9903
  98. struct S {
  99. typedef void a() { }; // expected-error {{function definition declared 'typedef'}}
  100. typedef void c() try { } catch(...) { } // expected-error {{function definition declared 'typedef'}}
  101. int n, m;
  102. typedef S() : n(1), m(2) { } // expected-error {{function definition declared 'typedef'}}
  103. };
  104. namespace TestIsValidAfterTypeSpecifier {
  105. struct s {} v;
  106. namespace a {
  107. struct s operator++(struct s a)
  108. { return a; }
  109. }
  110. namespace b {
  111. // The newline after s should make no difference.
  112. struct s
  113. operator++(struct s a)
  114. { return a; }
  115. }
  116. struct X {
  117. struct s
  118. friend f();
  119. struct s
  120. virtual f();
  121. };
  122. struct s
  123. &r0 = v;
  124. struct s
  125. bitand r2 = v;
  126. }
  127. struct DIE {
  128. void foo() {}
  129. };
  130. void test (DIE die, DIE *Die, DIE INT, DIE *FLOAT) {
  131. DIE.foo(); // expected-error {{cannot use dot operator on a type}}
  132. die.foo();
  133. DIE->foo(); // expected-error {{cannot use arrow operator on a type}}
  134. Die->foo();
  135. int.foo(); // expected-error {{cannot use dot operator on a type}}
  136. INT.foo();
  137. float->foo(); // expected-error {{cannot use arrow operator on a type}}
  138. FLOAT->foo();
  139. }
  140. namespace PR15017 {
  141. template<typename T = struct X { int i; }> struct S {}; // expected-error {{'PR15017::X' cannot be defined in a type specifier}}
  142. }
  143. // Ensure we produce at least some diagnostic for attributes in C++98.
  144. [[]] struct S; // expected-error 2{{}}
  145. namespace test7 {
  146. struct Foo {
  147. void a();
  148. void b();
  149. };
  150. void Foo::
  151. // Comment!
  152. a() {}
  153. void Foo:: // expected-error {{expected unqualified-id}}
  154. // Comment!
  155. }
  156. void test8() {
  157. struct {} o;
  158. // This used to crash.
  159. (&o)->(); // expected-error{{expected unqualified-id}}
  160. }
  161. namespace PR5066 {
  162. template<typename T> struct X {};
  163. X<int N> x; // expected-error {{type-id cannot have a name}}
  164. using T = int (*T)(); // expected-error {{type-id cannot have a name}} expected-error {{C++11}}
  165. }
  166. namespace PR17255 {
  167. void foo() {
  168. typename A::template B<>; // expected-error {{use of undeclared identifier 'A'}} \
  169. // expected-error {{expected a qualified name after 'typename'}} \
  170. // expected-error {{'template' keyword outside of a template}}
  171. }
  172. }
  173. namespace PR17567 {
  174. struct Foobar { // expected-note 2{{declared here}}
  175. FooBar(); // expected-error {{missing return type for function 'FooBar'; did you mean the constructor name 'Foobar'?}}
  176. ~FooBar(); // expected-error {{expected the class name after '~' to name a destructor}}
  177. };
  178. FooBar::FooBar() {} // expected-error {{undeclared}} expected-error {{missing return type}}
  179. FooBar::~FooBar() {} // expected-error {{undeclared}} expected-error {{expected the class name}}
  180. }
  181. namespace DuplicateFriend {
  182. struct A {
  183. friend void friend f(); // expected-warning {{duplicate 'friend' declaration specifier}}
  184. friend struct B friend; // expected-warning {{duplicate 'friend' declaration specifier}}
  185. };
  186. }
  187. // PR8380
  188. extern "" // expected-error {{unknown linkage language}}
  189. test6a { ;// expected-error {{C++ requires a type specifier for all declarations}} \
  190. // expected-error {{expected ';' after top level declarator}}
  191. int test6b;