Sema: Check value dependent casts when possible
authorDavid Majnemer <david.majnemer@gmail.com>
Tue, 16 Dec 2014 00:46:30 +0000 (00:46 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Tue, 16 Dec 2014 00:46:30 +0000 (00:46 +0000)
We know that const_cast<char *>((void)Something) is ill-formed, even if
'Something' is dependent because you can't cast from void to a pointer
type.

This fixes PR21845.

llvm-svn: 224299

clang/lib/Sema/SemaCast.cpp
clang/test/SemaCXX/const-cast.cpp

index d38db87..a4c2d9b 100644 (file)
@@ -240,10 +240,8 @@ Sema::BuildCXXNamedCast(SourceLocation OpLoc, tok::TokenKind Kind,
   QualType DestType = DestTInfo->getType();
 
   // If the type is dependent, we won't do the semantic analysis now.
-  // FIXME: should we check this in a more fine-grained manner?
-  bool TypeDependent = DestType->isDependentType() ||
-                       Ex.get()->isTypeDependent() ||
-                       Ex.get()->isValueDependent();
+  bool TypeDependent =
+      DestType->isDependentType() || Ex.get()->isTypeDependent();
 
   CastOperation Op(*this, DestType, E);
   Op.OpRange = SourceRange(OpLoc, Parens.getEnd());
index cb9937c..330e186 100644 (file)
@@ -64,3 +64,6 @@ short *bad_const_cast_test(char const *volatile *const volatile *var)
   (void)const_cast<int&&>(0); // expected-error {{const_cast from rvalue to reference type 'int &&'}} expected-warning {{C++11}}
   return **var3;
 }
+
+template <typename T>
+char *PR21845() { return const_cast<char *>((void)T::x); } // expected-error {{const_cast from 'void' to 'char *' is not allowed}}