[PATCH] D61027: Fix crash on switch conditions of non-integer types in templates

Elizabeth Andrews via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 13 08:54:32 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL368706: Fix crash on switch conditions of non-integer types in templates (authored by eandrews, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D61027?vs=203854&id=214847#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D61027/new/

https://reviews.llvm.org/D61027

Files:
  cfe/trunk/lib/AST/Expr.cpp
  cfe/trunk/lib/Sema/SemaChecking.cpp
  cfe/trunk/test/SemaTemplate/dependent-names.cpp
  cfe/trunk/test/SemaTemplate/enum-argument.cpp
  cfe/trunk/test/SemaTemplate/member-access-expr.cpp
  cfe/trunk/test/SemaTemplate/non-integral-switch-cond.cpp


Index: cfe/trunk/test/SemaTemplate/enum-argument.cpp
===================================================================
--- cfe/trunk/test/SemaTemplate/enum-argument.cpp
+++ cfe/trunk/test/SemaTemplate/enum-argument.cpp
@@ -1,5 +1,4 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
-// expected-no-diagnostics
 
 enum Enum { val = 1 };
 template <Enum v> struct C {
@@ -31,7 +30,7 @@
     unsigned long long bitfield : e0;
 
     void f(int j) {
-      bitfield + j;
+      bitfield + j; // expected-warning {{expression result unused}}
     }
   };
 }
Index: cfe/trunk/test/SemaTemplate/member-access-expr.cpp
===================================================================
--- cfe/trunk/test/SemaTemplate/member-access-expr.cpp
+++ cfe/trunk/test/SemaTemplate/member-access-expr.cpp
@@ -156,7 +156,7 @@
     void get(B **ptr) {
       // It's okay if at some point we figure out how to diagnose this
       // at instantiation time.
-      *ptr = field;
+      *ptr = field; // expected-error {{assigning to 'test6::B *' from incompatible type 'test6::A *}}
     }
   };
 }
Index: cfe/trunk/test/SemaTemplate/non-integral-switch-cond.cpp
===================================================================
--- cfe/trunk/test/SemaTemplate/non-integral-switch-cond.cpp
+++ cfe/trunk/test/SemaTemplate/non-integral-switch-cond.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+struct NOT_AN_INTEGRAL_TYPE {};
+
+template <typename T>
+struct foo {
+  NOT_AN_INTEGRAL_TYPE Bad;
+  void run() {
+    switch (Bad) { // expected-error {{statement requires expression of integer type ('NOT_AN_INTEGRAL_TYPE' invalid)}}
+    case 0:
+      break;
+    }
+  }
+};
Index: cfe/trunk/test/SemaTemplate/dependent-names.cpp
===================================================================
--- cfe/trunk/test/SemaTemplate/dependent-names.cpp
+++ cfe/trunk/test/SemaTemplate/dependent-names.cpp
@@ -273,9 +273,6 @@
       }
       int e[10];
     };
-    void g() {
-      S<int>().f(); // expected-note {{here}}
-    }
   }
 
   namespace A2 {
Index: cfe/trunk/lib/AST/Expr.cpp
===================================================================
--- cfe/trunk/lib/AST/Expr.cpp
+++ cfe/trunk/lib/AST/Expr.cpp
@@ -1669,6 +1669,15 @@
   MemberExpr *E = new (Mem) MemberExpr(Base, IsArrow, OperatorLoc, MemberDecl,
                                        NameInfo, T, VK, OK, NOUR);
 
+  if (FieldDecl *Field = dyn_cast<FieldDecl>(MemberDecl)) {
+    DeclContext *DC = MemberDecl->getDeclContext();
+    // dyn_cast_or_null is used to handle objC variables which do not
+    // have a declaration context.
+    CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC);
+    if (RD && RD->isDependentContext() && RD->isCurrentInstantiation(DC))
+      E->setTypeDependent(T->isDependentType());
+  }
+
   if (HasQualOrFound) {
     // FIXME: Wrong. We should be looking at the member declaration we found.
     if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
Index: cfe/trunk/lib/Sema/SemaChecking.cpp
===================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp
+++ cfe/trunk/lib/Sema/SemaChecking.cpp
@@ -14288,6 +14288,8 @@
   bool AnyIsPacked = false;
   do {
     QualType BaseType = ME->getBase()->getType();
+    if (BaseType->isDependentType())
+      return;
     if (ME->isArrow())
       BaseType = BaseType->getPointeeType();
     RecordDecl *RD = BaseType->getAs<RecordType>()->getDecl();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61027.214847.patch
Type: text/x-patch
Size: 3483 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190813/405219fb/attachment.bin>


More information about the cfe-commits mailing list