r212160 - Prevent Clang from crashing on template code.

Richard Trieu rtrieu at google.com
Tue Jul 1 21:39:38 PDT 2014


Author: rtrieu
Date: Tue Jul  1 23:39:38 2014
New Revision: 212160

URL: http://llvm.org/viewvc/llvm-project?rev=212160&view=rev
Log:
Prevent Clang from crashing on template code.

Fixes PR20110, where Clang hits an assertion failure when it expects that the
sub-expression of a bit cast to pointer to also be a pointer, but gets a value
instead.

Differential Revision: http://reviews.llvm.org/D4280

Added:
    cfe/trunk/test/SemaCXX/PR20110.cpp
Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=212160&r1=212159&r2=212160&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Jul  1 23:39:38 2014
@@ -4613,7 +4613,6 @@ static Expr *EvalAddr(Expr *E, SmallVect
   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:
@@ -4628,6 +4627,14 @@ static Expr *EvalAddr(Expr *E, SmallVect
     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 nullptr;
+
     default:
       return nullptr;
     }

Added: cfe/trunk/test/SemaCXX/PR20110.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/PR20110.cpp?rev=212160&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/PR20110.cpp (added)
+++ cfe/trunk/test/SemaCXX/PR20110.cpp Tue Jul  1 23:39:38 2014
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+// expected-no-diagnostics
+
+// FIXME: These templates should trigger errors in C++11 mode.
+
+template <char const *p>
+class A {
+  char const *get_p() { return *p; }
+};
+template <int p>
+class B {
+  char const *get_p() { return p; }
+};
+





More information about the cfe-commits mailing list