From 08d918a6c32fd9489fdee8acb4e9ff8dba048722 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 24 Oct 2008 15:36:09 +0000 Subject: Semantic analysis for C++ reinterpret_cast and const_cast. Patch by Sebastian Redl. llvm-svn: 58094 --- clang/test/SemaCXX/reinterpret-cast.cpp | 72 +++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 clang/test/SemaCXX/reinterpret-cast.cpp (limited to 'clang/test/SemaCXX/reinterpret-cast.cpp') diff --git a/clang/test/SemaCXX/reinterpret-cast.cpp b/clang/test/SemaCXX/reinterpret-cast.cpp new file mode 100644 index 00000000000..6172c9f8d9e --- /dev/null +++ b/clang/test/SemaCXX/reinterpret-cast.cpp @@ -0,0 +1,72 @@ +// RUN: clang -fsyntax-only -verify %s + +enum test { testval = 1 }; +struct structure { int m; }; +typedef void (*fnptr)(); + +// Test the conversion to self. +void self_conversion() +{ + // T*->T* is allowed, T->T in general not. + int i = 0; + (void)reinterpret_cast(i); // expected-error {{reinterpret_cast from 'int' to 'int' is not allowed}} + structure s; + (void)reinterpret_cast(s); // expected-error {{reinterpret_cast from 'struct structure' to 'struct structure' is not allowed}} + int *pi = 0; + (void)reinterpret_cast(pi); +} + +// Test conversion between pointer and integral types, as in /3 and /4. +void integral_conversion() +{ + void *vp = reinterpret_cast(testval); + long l = reinterpret_cast(vp); + (void)reinterpret_cast(l); + fnptr fnp = reinterpret_cast(l); + (void)reinterpret_cast(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}} + (void)reinterpret_cast(fnp); +} + +void pointer_conversion() +{ + int *p1 = 0; + float *p2 = reinterpret_cast(p1); + structure *p3 = reinterpret_cast(p2); + typedef int **ppint; + ppint *deep = reinterpret_cast(p3); + (void)reinterpret_cast(deep); +} + +void constness() +{ + int ***const ipppc = 0; + // Valid: T1* -> T2 const* + int const *icp = reinterpret_cast(ipppc); + // Invalid: T1 const* -> T2* + (void)reinterpret_cast(icp); // expected-error {{reinterpret_cast from 'int const *' to 'int *' casts away constness}} + // Invalid: T1*** -> T2 const* const** + int const *const **icpcpp = reinterpret_cast(ipppc); // expected-error {{reinterpret_cast from 'int ***const' to 'int const *const **' casts away constness}} + // Valid: T1* -> T2* + int *ip = reinterpret_cast(icpcpp); + // Valid: T* -> T const* + (void)reinterpret_cast(ip); + // Valid: T*** -> T2 const* const* const* + (void)reinterpret_cast(ipppc); +} + +void fnptrs() +{ + typedef int (*fnptr2)(int); + fnptr fp = 0; + (void)reinterpret_cast(fp); + void *vp = reinterpret_cast(fp); + (void)reinterpret_cast(vp); +} + +void refs() +{ + long l = 0; + char &c = reinterpret_cast(l); + // Bad: from rvalue + (void)reinterpret_cast(&c); // expected-error {{reinterpret_cast from rvalue to reference type 'int &'}} +} -- cgit v1.2.3