[PATCH] Fix for PR20110: Don't assume that the subexpression of a bit cast has pointer type if the bit cast has pointer type

Richard Trieu rtrieu at google.com
Tue Jun 24 15:13:52 PDT 2014


When a bit cast expression has pointer type, don't assume that the subexpression also has pointer type.  It is possible that the subexpression is a value type instead.  This patch checks the type of the subexpression and calls the proper function.  Prevents the assertion failure reported in PR20110.

http://llvm.org/bugs/show_bug.cgi?id=20110

http://reviews.llvm.org/D4280

Files:
  lib/Sema/SemaChecking.cpp
  test/SemaCXX/PR20110.cpp

Index: test/SemaCXX/PR20110.cpp
===================================================================
--- test/SemaCXX/PR20110.cpp
+++ test/SemaCXX/PR20110.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// expected-no-diagnostics
+
+template <char const *p>
+class A {
+  char const *get_p() { return *p; }
+};
+template <int p>
+class B {
+  char const *get_p() { return p; }
+};
+
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -4592,7 +4592,6 @@
   case Stmt::CXXReinterpretCastExprClass: {
     Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
     switch (cast<CastExpr>(E)->getCastKind()) {
-    case CK_BitCast:
     case CK_LValueToRValue:
     case CK_NoOp:
     case CK_BaseToDerived:
@@ -4607,6 +4606,14 @@
     case CK_ArrayToPointerDecay:
       return EvalVal(SubExpr, refVars, ParentDecl);
 
+    case CK_BitCast:
+      if (SubExpr->getType()->isAnyPointerType() ||
+          SubExpr->getType()->isBlockPointerType() ||
+          SubExpr->getType()->isObjCQualifiedIdType())
+        return EvalAddr(SubExpr, refVars, ParentDecl);
+      else
+        return EvalVal(SubExpr, refVars, ParentDecl);
+
     default:
       return nullptr;
     }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4280.10806.patch
Type: text/x-patch
Size: 1353 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140624/3ad49975/attachment.bin>


More information about the cfe-commits mailing list