[PATCH] D142490: [Clang] Fix ClassifyImplicitMemberAccess to handle cases where the access in an unevaluated context is not within a CXXRecordDecl or CXXMethodDecl

Shafik Yaghmour via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 24 09:39:51 PST 2023


shafik created this revision.
shafik added reviewers: aaron.ballman, erichkeane.
Herald added a project: All.
shafik requested review of this revision.

`ClassifyImplicitMemberAccess` assumes that if we are not in a static context then the `DeclContext` must be a `CXXRecordDecl` or a `CXXMethodDecl`. In the case of the unevaluated context this may not be true.

This will lead to a crash because `contextClass` will remain a `nullptr`.

Fixes: https://github.com/llvm/llvm-project/issues/37792

  https://github.com/llvm/llvm-project/issues/48405


https://reviews.llvm.org/D142490

Files:
  clang/lib/Sema/SemaExprMember.cpp
  clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
  clang/test/SemaCXX/statements.cpp


Index: clang/test/SemaCXX/statements.cpp
===================================================================
--- clang/test/SemaCXX/statements.cpp
+++ clang/test/SemaCXX/statements.cpp
@@ -52,3 +52,14 @@
   int a = test7(1);
   double b = test7(2.0);
 }
+
+namespace GH48405 {
+void foo() {
+  struct S {
+    int i;
+    int j = ({i;}); // expected-error {{invalid use of non-static data member 'i'}}
+                    // expected-error at -1 {{cannot initialize a member subobject of type 'int' with an rvalue of type 'void'}}
+		    // expected-warning at -2 {{use of GNU statement expression extension}}
+  };
+}
+}
Index: clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
===================================================================
--- clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
+++ clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
@@ -1026,3 +1026,11 @@
   void *v = x(f); // expected-error {{cannot initialize a variable of type 'void *' with an rvalue of type 'int'}}
   void *w = y(f); // expected-error {{cannot initialize a variable of type 'void *' with an rvalue of type 'int'}}
 }
+
+namespace GH37792 {
+struct A { int x; };
+
+void f() {
+  [](auto t) -> decltype(decltype(t)::x) { return 0; }(A());
+}
+}
Index: clang/lib/Sema/SemaExprMember.cpp
===================================================================
--- clang/lib/Sema/SemaExprMember.cpp
+++ clang/lib/Sema/SemaExprMember.cpp
@@ -163,8 +163,11 @@
   CXXRecordDecl *contextClass;
   if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
     contextClass = MD->getParent()->getCanonicalDecl();
-  else
+  else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC))
     contextClass = cast<CXXRecordDecl>(DC);
+  else
+    return AbstractInstanceResult ? AbstractInstanceResult
+                                  : IMA_Error_StaticContext;
 
   // [class.mfct.non-static]p3:
   // ...is used in the body of a non-static member function of class X,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142490.491839.patch
Type: text/x-patch
Size: 1923 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230124/4af75440/attachment.bin>


More information about the cfe-commits mailing list