[PATCH] C++11: if decl-specifier has 'friend', do not allow access to 'this' pointer when parsing function declarator.

Ismail Pazarbasi ismail.pazarbasi at gmail.com
Mon Mar 11 14:56:44 PDT 2013


  Added FIXMEs, and replaced local variable with conditional expression instead.

Hi rsmith, rjmccall,

http://llvm-reviews.chandlerc.com/D468

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D468?vs=1250&id=1256#toc

Files:
  lib/Parse/ParseDecl.cpp
  test/CXX/expr/expr.prim/expr.prim.general/p3-0x.cpp

Index: lib/Parse/ParseDecl.cpp
===================================================================
--- lib/Parse/ParseDecl.cpp
+++ lib/Parse/ParseDecl.cpp
@@ -4833,12 +4833,14 @@
       //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
       //   and the end of the function-definition, member-declarator, or
       //   declarator.
+      // FIXME: currently, "static" case isn't handled correctly.
       bool IsCXX11MemberFunction =
         getLangOpts().CPlusPlus11 &&
-        (D.getContext() == Declarator::MemberContext ||
-         (D.getContext() == Declarator::FileContext &&
-          D.getCXXScopeSpec().isValid() &&
-          Actions.CurContext->isRecord()));
+        (D.getContext() == Declarator::MemberContext
+         ? !D.getDeclSpec().isFriendSpecified()
+         : D.getContext() == Declarator::FileContext &&
+           D.getCXXScopeSpec().isValid() &&
+           Actions.CurContext->isRecord());
       Sema::CXXThisScopeRAII ThisScope(Actions,
                                dyn_cast<CXXRecordDecl>(Actions.CurContext),
                                DS.getTypeQualifiers() |
Index: test/CXX/expr/expr.prim/expr.prim.general/p3-0x.cpp
===================================================================
--- test/CXX/expr/expr.prim/expr.prim.general/p3-0x.cpp
+++ test/CXX/expr/expr.prim/expr.prim.general/p3-0x.cpp
@@ -61,9 +61,26 @@
   }
 }
 
+namespace PR15290 {
+  template<typename T>
+  class A {
+    T v_;
+    friend int add_to_v(A &t) noexcept(noexcept(v_ + 42))
+    {
+      return t.v_ + 42;
+    }
+  };
+  void f()
+  {
+    A<int> t;
+    add_to_v(t);
+  }
+}
+
 namespace Static {
   struct X1 {
     int m;
+    // FIXME: This should be accepted.
     static auto f() -> decltype(m); // expected-error{{'this' cannot be implicitly used in a static member function declaration}}
     static auto g() -> decltype(this->m); // expected-error{{'this' cannot be used in a static member function declaration}}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D468.3.patch
Type: text/x-patch
Size: 1981 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130311/1ad0afe7/attachment.bin>


More information about the cfe-commits mailing list