[clang] c41be8f - [Clang] Fix ClassifyImplicitMemberAccess to handle cases where the access in an unevaluated context is not within a CXXRecordDecl or CXXMethodDecl

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 14 16:38:05 PDT 2023


Author: Shafik Yaghmour
Date: 2023-03-14T16:37:58-07:00
New Revision: c41be8fc741dec82e6c628d0cad742285be293f6

URL: https://github.com/llvm/llvm-project/commit/c41be8fc741dec82e6c628d0cad742285be293f6
DIFF: https://github.com/llvm/llvm-project/commit/c41be8fc741dec82e6c628d0cad742285be293f6.diff

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

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
Fixes: https://github.com/llvm/llvm-project/issues/48405

Differential Revision: https://reviews.llvm.org/D142490

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaExprMember.cpp
    clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
    clang/test/SemaCXX/statements.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 06e4cd23db7a4..68890e287f674 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -202,6 +202,10 @@ Bug Fixes in This Version
   of `CWG2699 <https://wg21.link/CWG2699>_` being accepted by WG21.
 - Fix crash when parsing fold expression containing a delayed typo correction.
   (`#61326 <https://github.com/llvm/llvm-project/issues/61326>`_)
+- Fix crash when dealing with some member accesses outside of class or member
+  function context.
+  (`#37792 <https://github.com/llvm/llvm-project/issues/37792>`_) and
+  (`#48405 <https://github.com/llvm/llvm-project/issues/48405>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaExprMember.cpp b/clang/lib/Sema/SemaExprMember.cpp
index a3420ac6fdd2d..667a17e05f930 100644
--- a/clang/lib/Sema/SemaExprMember.cpp
+++ b/clang/lib/Sema/SemaExprMember.cpp
@@ -161,10 +161,13 @@ static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef,
   }
 
   CXXRecordDecl *contextClass;
-  if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
+  if (auto *MD = dyn_cast<CXXMethodDecl>(DC))
     contextClass = MD->getParent()->getCanonicalDecl();
-  else
+  else if (auto *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,

diff  --git a/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp b/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
index 463e077ce934c..61dfd654f6d65 100644
--- a/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
+++ b/clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
@@ -1026,3 +1026,11 @@ namespace PR46637 {
   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());
+}
+}

diff  --git a/clang/test/SemaCXX/statements.cpp b/clang/test/SemaCXX/statements.cpp
index 75ed983ba84b2..48f178dd9a8b3 100644
--- a/clang/test/SemaCXX/statements.cpp
+++ b/clang/test/SemaCXX/statements.cpp
@@ -52,3 +52,14 @@ void test8() {
   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}}
+  };
+}
+}


        


More information about the cfe-commits mailing list