cxx-casting.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. char *const_cast_test(const char *var)
  3. {
  4. return const_cast<char*>(var);
  5. }
  6. struct A {
  7. virtual ~A() {}
  8. };
  9. struct B : public A {
  10. };
  11. struct B *dynamic_cast_test(struct A *a)
  12. {
  13. return dynamic_cast<struct B*>(a);
  14. }
  15. char *reinterpret_cast_test()
  16. {
  17. return reinterpret_cast<char*>(0xdeadbeef);
  18. }
  19. double static_cast_test(int i)
  20. {
  21. return static_cast<double>(i);
  22. }
  23. char postfix_expr_test()
  24. {
  25. return reinterpret_cast<char*>(0xdeadbeef)[0];
  26. }
  27. // This was being incorrectly tentatively parsed.
  28. namespace test1 {
  29. template <class T> class A {}; // expected-note 2{{here}}
  30. void foo() { A<int>(*(A<int>*)0); }
  31. }
  32. typedef char* c;
  33. typedef A* a;
  34. void test2(char x, struct B * b) {
  35. (void)const_cast<::c>(&x); // expected-error{{found '<::' after a const_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
  36. (void)dynamic_cast<::a>(b); // expected-error{{found '<::' after a dynamic_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
  37. (void)reinterpret_cast<::c>(x); // expected-error{{found '<::' after a reinterpret_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
  38. (void)static_cast<::c>(&x); // expected-error{{found '<::' after a static_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
  39. // Do not do digraph correction.
  40. (void)static_cast<: :c>(&x); //\
  41. expected-error {{expected '<' after 'static_cast'}} \
  42. expected-error {{expected expression}}\
  43. expected-error {{expected ']'}}\
  44. expected-note {{to match this '['}}
  45. (void)static_cast<: // expected-error {{expected '<' after 'static_cast'}} \
  46. expected-note {{to match this '['}}
  47. :c>(&x); // expected-error {{expected expression}} \
  48. expected-error {{expected ']'}}
  49. #define LC <:
  50. #define C :
  51. test1::A LC:B> c; // expected-error {{class template 'test1::A' requires template arguments}} expected-error 2{{}}
  52. (void)static_cast LC:c>(&x); // expected-error {{expected '<' after 'static_cast'}} expected-error 2{{}} expected-note{{}}
  53. test1::A<:C B> d; // expected-error {{class template 'test1::A' requires template arguments}} expected-error 2{{}}
  54. (void)static_cast<:C c>(&x); // expected-error {{expected '<' after 'static_cast'}} expected-error 2{{}} expected-note{{}}
  55. #define LCC <::
  56. test1::A LCC B> e; // expected-error{{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
  57. (void)static_cast LCC c>(&x); // expected-error{{found '<::' after a static_cast which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
  58. }
  59. // This note comes from "::D[:F> A5;"
  60. template <class T> class D {}; // expected-note{{template is declared here}}
  61. template <class T> void E() {};
  62. class F {};
  63. void test3() {
  64. ::D<::F> A1; // expected-error{{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
  65. D<::F> A2; // expected-error{{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
  66. ::E<::F>(); // expected-error{{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
  67. E<::F>(); // expected-error{{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
  68. ::D< ::F> A3;
  69. D< ::F> A4;
  70. ::E< ::F>();
  71. E< ::F>();
  72. // Make sure that parser doesn't expand '[:' to '< ::'
  73. ::D[:F> A5; // expected-error {{class template '::D' requires template arguments}} \
  74. // expected-error {{expected expression}} \
  75. // expected-error {{expected unqualified-id}}
  76. }
  77. // Ensure that a C-style cast doesn't turn off colon protection.
  78. void PR19748() {
  79. struct A {};
  80. int A = 0, b;
  81. int test1 = true ? (int)A : b;
  82. struct f {};
  83. extern B f(), (*p)();
  84. (true ? (B(*)())f : p)();
  85. }
  86. void PR19751(int n) {
  87. struct T { void operator++(int); };
  88. (T())++; // ok, not an ill-formed cast to function type
  89. (T())++n; // expected-error {{C-style cast from 'int' to 'T ()' is not allowed}}
  90. }
  91. // PR13619. Must be at end of file.
  92. int n = reinterpret_cast // expected-error {{expected '<'}} expected-error {{expected ';'}}