[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 10:49:46 PDT 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGc1401e9f3e7c: [Clang][Sema] Fix access of friend class in local class (authored by eandrews).
Herald added a project: clang.

Changed prior to commit:
  https://reviews.llvm.org/D152195?vs=528883&id=528938#toc

Repository:
  rG LLVM Github Monorepo

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
@@ -17066,11 +17066,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
@@ -539,6 +539,9 @@
 - Allow omitting ``typename`` in the parameter declaration of a friend
   constructor declaration.
   (`#63119 <https://github.com/llvm/llvm-project/issues/63119>`_)
+- 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.528938.patch
Type: text/x-patch
Size: 1898 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230606/fe720318/attachment-0001.bin>


More information about the cfe-commits mailing list