[PATCH] D152195: [Clang] Fix access of friend function in local class

Elizabeth Andrews via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 6 08:33:10 PDT 2023


eandrews updated this revision to Diff 528883.
eandrews added a comment.

Thanks for the review! I added a release note


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

https://reviews.llvm.org/D152195

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Sema/SemaDecl.cpp
  clang/test/Sema/local-class-friend.cpp


Index: clang/test/Sema/local-class-friend.cpp
===================================================================
--- /dev/null
+++ clang/test/Sema/local-class-friend.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -verify -fsyntax-only %s
+// expected-no-diagnostics
+
+void foo()
+{ class c1 {
+    private:
+      int testVar;
+    public:
+      friend class c2;
+  };
+
+  class c2 {
+    void f(c1 obj) {
+      int a = obj.testVar; // Ok
+    }
+  };
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -17065,11 +17065,14 @@
       S = getTagInjectionScope(S, getLangOpts());
     } else {
       assert(TUK == TUK_Friend);
+      CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(SearchDC);
+
       // C++ [namespace.memdef]p3:
       //   If a friend declaration in a non-local class first declares a
       //   class or function, the friend class or function is a member of
       //   the innermost enclosing namespace.
-      SearchDC = SearchDC->getEnclosingNamespaceContext();
+      SearchDC = RD->isLocalClass() ? RD->isLocalClass()
+                                    : SearchDC->getEnclosingNamespaceContext();
     }
 
     // In C++, we need to do a redeclaration lookup to properly
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -521,6 +521,9 @@
   (`#62494 <https://github.com/llvm/llvm-project/issues/62494>`_)
 - Fix handling of generic lambda used as template arguments.
   (`#62611 <https://github.com/llvm/llvm-project/issues/62611>`_)
+- Fix access of a friend class declared in a local class. Clang previously
+  emitted an error when a friend of a local class tried to access it's
+  private data members.
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152195.528883.patch
Type: text/x-patch
Size: 1927 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230606/c2f56610/attachment.bin>


More information about the cfe-commits mailing list