r311839 - Pass the correct object argument when a member call to an 'unrelated' class is made.

Faisal Vali via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 26 19:21:21 PDT 2017


Author: faisalv
Date: Sat Aug 26 19:21:21 2017
New Revision: 311839

URL: http://llvm.org/viewvc/llvm-project?rev=311839&view=rev
Log:
Pass the correct object argument when a member call to an 'unrelated' class is made.

Prior to this patch, clang would do the wrong thing here (see inline comments for pre-patch behavior):

  struct A {
    void bar(int) { }
    static void bar(double) { }
    
    void g(int*);
    static void g(char *);
  };


  struct B {
    void f() {
      A::bar(3);  // selects (double) ??!!
      A::g((int*)0); // Instead of no object argument, states conversion error?!!
    }
  };


The fix is as follows:  When we detect that what appears to be an implicit member function call (A::bar) is actually a call to a member of a class (A) unrelated to the type (B) that contains the member function (B::f) from which the call is being made, don't treat it (A::bar) as an Implicit Member Call Expression.

P.S. I wonder if there is an existing bug report related to this? (Surprisingly, a cursory search did not find one).


Modified:
    cfe/trunk/lib/Sema/SemaExprMember.cpp
    cfe/trunk/test/SemaCXX/member-expr.cpp

Modified: cfe/trunk/lib/Sema/SemaExprMember.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprMember.cpp?rev=311839&r1=311838&r2=311839&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprMember.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprMember.cpp Sat Aug 26 19:21:21 2017
@@ -243,7 +243,6 @@ Sema::BuildPossibleImplicitMemberExpr(co
     return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, true, S);
 
   case IMA_Mixed:
-  case IMA_Mixed_Unrelated:
   case IMA_Unresolved:
     return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, false,
                                    S);
@@ -252,6 +251,7 @@ Sema::BuildPossibleImplicitMemberExpr(co
     Diag(R.getNameLoc(), diag::warn_cxx98_compat_non_static_member_use)
       << R.getLookupNameInfo().getName();
     // Fall through.
+  case IMA_Mixed_Unrelated:
   case IMA_Static:
   case IMA_Abstract:
   case IMA_Mixed_StaticContext:

Modified: cfe/trunk/test/SemaCXX/member-expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/member-expr.cpp?rev=311839&r1=311838&r2=311839&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/member-expr.cpp (original)
+++ cfe/trunk/test/SemaCXX/member-expr.cpp Sat Aug 26 19:21:21 2017
@@ -228,3 +228,25 @@ namespace pr16676 {
         .i;  // expected-error {{member reference type 'pr16676::S *' is a pointer; did you mean to use '->'}}
   }
 }
+
+namespace unrelated_class_instance_call_should_be_illformed {
+
+
+struct A {
+  void bar(int) { }
+  static void bar(double) { }
+  
+  void g(int*);
+  static void g(char *);
+};
+
+
+struct B {
+  void f() {
+    A::bar(3);  //expected-error{{call to non-static member}}
+    A::g((int*)0); //expected-error{{call to non-static member}}
+  }
+};
+
+
+} // ns unrelated_class_mixed_static_nonstatic_call_should_be_illformed




More information about the cfe-commits mailing list