diff options
author | Douglas Gregor <dgregor@apple.com> | 2008-10-24 15:36:09 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2008-10-24 15:36:09 +0000 |
commit | 08d918a6c32fd9489fdee8acb4e9ff8dba048722 (patch) | |
tree | 9b75f2fbb7d2209b55ed631d1f3626df20566a98 /clang/test/SemaCXX | |
parent | 196922b4a13ab9633f67376d47c93e10c873ce9d (diff) | |
download | bcm5719-llvm-08d918a6c32fd9489fdee8acb4e9ff8dba048722.tar.gz bcm5719-llvm-08d918a6c32fd9489fdee8acb4e9ff8dba048722.zip |
Semantic analysis for C++ reinterpret_cast and const_cast. Patch by Sebastian Redl.
llvm-svn: 58094
Diffstat (limited to 'clang/test/SemaCXX')
-rw-r--r-- | clang/test/SemaCXX/const-cast.cpp | 56 | ||||
-rw-r--r-- | clang/test/SemaCXX/reinterpret-cast.cpp | 72 | ||||
-rw-r--r-- | clang/test/SemaCXX/reinterpret-fn-obj-pedantic.cpp | 9 |
3 files changed, 137 insertions, 0 deletions
diff --git a/clang/test/SemaCXX/const-cast.cpp b/clang/test/SemaCXX/const-cast.cpp new file mode 100644 index 00000000000..90e8ed73789 --- /dev/null +++ b/clang/test/SemaCXX/const-cast.cpp @@ -0,0 +1,56 @@ +// RUN: clang -fsyntax-only -verify %s + +// See if aliasing can confuse this baby. +typedef char c; +typedef c *cp; +typedef cp *cpp; +typedef cpp *cppp; +typedef cppp &cpppr; +typedef const cppp &cpppcr; +typedef const char cc; +typedef cc *ccp; +typedef volatile ccp ccvp; +typedef ccvp *ccvpp; +typedef const volatile ccvpp ccvpcvp; +typedef ccvpcvp *ccvpcvpp; +typedef int iar[100]; +typedef iar &iarr; +typedef int (*f)(int); + +char ***good_const_cast_test(ccvpcvpp var) +{ + // Cast away deep consts and volatiles. + char ***var2 = const_cast<cppp>(var); + char ***const &var3 = static_cast<cpppcr>(var2); // Different bug. + // Const reference to reference. + char ***&var4 = const_cast<cpppr>(var3); + // Drop reference. Intentionally without qualifier change. + char *** var5 = const_cast<cppp>(var4); + const int ar[100] = {0}; + int (&rar)[100] = const_cast<iarr>(ar); // expected-warning {{statement was disambiguated as declaration}} expected-error {{const_cast from 'int const [100]' to 'iarr' is not allowed}} + // Array decay. Intentionally without qualifier change. + int *pi = const_cast<int*>(ar); + f fp = 0; + // Don't misidentify fn** as a function pointer. + f *fpp = const_cast<f*>(&fp); + return var4; +} + +short *bad_const_cast_test(char const *volatile *const volatile *var) +{ + // Different pointer levels. + char **var2 = const_cast<char**>(var); // expected-error {{const_cast from 'char const *volatile *const volatile *' to 'char **' is not allowed}} + // Different final type. + short ***var3 = const_cast<short***>(var); // expected-error {{const_cast from 'char const *volatile *const volatile *' to 'short ***' is not allowed}} + // Rvalue to reference. + char ***&var4 = const_cast<cpppr>(&var2); // expected-error {{const_cast from rvalue to reference type 'cpppr'}} + // Non-pointer. + char v = const_cast<char>(**var2); // expected-error {{const_cast to 'char', which is not a reference, pointer-to-object, or pointer-to-data-member}} + const int *ar[100] = {0}; + // Not even lenient g++ accepts this. + int *(*rar)[100] = const_cast<int *(*)[100]>(&ar); // expected-error {{const_cast from 'int const *(*)[100]' to 'int *(*)[100]' is not allowed}} + f fp1 = 0; + // Function pointers. + f fp2 = const_cast<f>(fp1); // expected-error {{const_cast to 'f', which is not a reference, pointer-to-object, or pointer-to-data-member}} + return **var3; +} 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<int>(i); // expected-error {{reinterpret_cast from 'int' to 'int' is not allowed}} + structure s; + (void)reinterpret_cast<structure>(s); // expected-error {{reinterpret_cast from 'struct structure' to 'struct structure' is not allowed}} + int *pi = 0; + (void)reinterpret_cast<int*>(pi); +} + +// Test conversion between pointer and integral types, as in /3 and /4. +void integral_conversion() +{ + void *vp = reinterpret_cast<void*>(testval); + long l = reinterpret_cast<long>(vp); + (void)reinterpret_cast<float*>(l); + fnptr fnp = reinterpret_cast<fnptr>(l); + (void)reinterpret_cast<char>(fnp); // expected-error {{cast from pointer to smaller type 'char' loses information}} + (void)reinterpret_cast<long>(fnp); +} + +void pointer_conversion() +{ + int *p1 = 0; + float *p2 = reinterpret_cast<float*>(p1); + structure *p3 = reinterpret_cast<structure*>(p2); + typedef int **ppint; + ppint *deep = reinterpret_cast<ppint*>(p3); + (void)reinterpret_cast<fnptr*>(deep); +} + +void constness() +{ + int ***const ipppc = 0; + // Valid: T1* -> T2 const* + int const *icp = reinterpret_cast<int const*>(ipppc); + // Invalid: T1 const* -> T2* + (void)reinterpret_cast<int*>(icp); // expected-error {{reinterpret_cast from 'int const *' to 'int *' casts away constness}} + // Invalid: T1*** -> T2 const* const** + int const *const **icpcpp = reinterpret_cast<int const* const**>(ipppc); // expected-error {{reinterpret_cast from 'int ***const' to 'int const *const **' casts away constness}} + // Valid: T1* -> T2* + int *ip = reinterpret_cast<int*>(icpcpp); + // Valid: T* -> T const* + (void)reinterpret_cast<int const*>(ip); + // Valid: T*** -> T2 const* const* const* + (void)reinterpret_cast<int const* const* const*>(ipppc); +} + +void fnptrs() +{ + typedef int (*fnptr2)(int); + fnptr fp = 0; + (void)reinterpret_cast<fnptr2>(fp); + void *vp = reinterpret_cast<void*>(fp); + (void)reinterpret_cast<fnptr>(vp); +} + +void refs() +{ + long l = 0; + char &c = reinterpret_cast<char&>(l); + // Bad: from rvalue + (void)reinterpret_cast<int&>(&c); // expected-error {{reinterpret_cast from rvalue to reference type 'int &'}} +} diff --git a/clang/test/SemaCXX/reinterpret-fn-obj-pedantic.cpp b/clang/test/SemaCXX/reinterpret-fn-obj-pedantic.cpp new file mode 100644 index 00000000000..b39d1f0a69a --- /dev/null +++ b/clang/test/SemaCXX/reinterpret-fn-obj-pedantic.cpp @@ -0,0 +1,9 @@ +// RUN: clang -fsyntax-only -verify -std=c++98 -pedantic %s + +void fnptrs() +{ + typedef void (*fnptr)(); + fnptr fp = 0; + void *vp = reinterpret_cast<void*>(fp); // expected-warning {{reinterpret_cast between pointer-to-function and pointer-to-object is an extension}} + (void)reinterpret_cast<fnptr>(vp); // expected-warning {{reinterpret_cast between pointer-to-function and pointer-to-object is an extension}} +} |