[Sema] Require a complete type for __builtin_bit_cast operands
authorErik Pilkington <erik.pilkington@gmail.com>
Mon, 12 Aug 2019 18:31:27 +0000 (18:31 +0000)
committerErik Pilkington <erik.pilkington@gmail.com>
Mon, 12 Aug 2019 18:31:27 +0000 (18:31 +0000)
Fixes llvm.org/PR42936

llvm-svn: 368600

clang/lib/Sema/SemaCast.cpp
clang/test/SemaCXX/builtin-bit-cast.cpp

index f184eda..7a1d52a 100644 (file)
@@ -2803,6 +2803,14 @@ void CastOperation::CheckBuiltinBitCast() {
     SrcExpr = Self.CreateMaterializeTemporaryExpr(SrcType, SrcExpr.get(),
                                                   /*IsLValueReference=*/false);
 
+  if (Self.RequireCompleteType(OpRange.getBegin(), DestType,
+                               diag::err_typecheck_cast_to_incomplete) ||
+      Self.RequireCompleteType(OpRange.getBegin(), SrcType,
+                               diag::err_incomplete_type)) {
+    SrcExpr = ExprError();
+    return;
+  }
+
   CharUnits DestSize = Self.Context.getTypeSizeInChars(DestType);
   CharUnits SourceSize = Self.Context.getTypeSizeInChars(SrcType);
   if (DestSize != SourceSize) {
index 3dddadc..87919d9 100644 (file)
@@ -37,3 +37,12 @@ constexpr unsigned long ul = __builtin_bit_cast(unsigned long, not_trivially_cop
 
 // expected-error@+1 {{__builtin_bit_cast destination type must be trivially copyable}}
 constexpr long us = __builtin_bit_cast(unsigned long &, 0L);
+
+namespace PR42936 {
+template <class T> struct S { int m; };
+
+extern S<int> extern_decl;
+
+int x = __builtin_bit_cast(int, extern_decl);
+S<char> y = __builtin_bit_cast(S<char>, 0);
+}