Provide a fixit when taking the address of an unqualified member function.
authorDavid Blaikie <dblaikie@gmail.com>
Thu, 11 Oct 2012 22:55:07 +0000 (22:55 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Thu, 11 Oct 2012 22:55:07 +0000 (22:55 +0000)
This only applies if the type has a name. (we could potentially do something
crazy with decltype in C++11 to qualify members of unnamed types but that
seems excessive)

It might be nice to also suggest a fixit for "&this->i", "&foo->i",
and "&foo.i" but those expressions produce 'bound' member functions that have
a different AST representation & make error recovery a little trickier. Left
as future work.

llvm-svn: 165763

clang/lib/Sema/SemaExpr.cpp
clang/test/FixIt/fixit.cpp
clang/test/FixIt/no-fixit.cpp

index 669d835..74ee870 100644 (file)
@@ -8056,8 +8056,16 @@ static QualType CheckAddressOfOperand(Sema &S, ExprResult &OrigOp,
 
     // The method was named without a qualifier.
     } else if (!DRE->getQualifier()) {
-      S.Diag(OpLoc, diag::err_unqualified_pointer_member_function)
-        << op->getSourceRange();
+      if (MD->getParent()->getName().empty())
+        S.Diag(OpLoc, diag::err_unqualified_pointer_member_function)
+          << op->getSourceRange();
+      else {
+        SmallString<32> Str;
+        StringRef Qual = (MD->getParent()->getName() + "::").toStringRef(Str);
+        S.Diag(OpLoc, diag::err_unqualified_pointer_member_function)
+          << op->getSourceRange()
+          << FixItHint::CreateInsertion(op->getSourceRange().getBegin(), Qual);
+      }
     }
 
     return S.Context.getMemberPointerType(op->getType(),
index dc0b6c8..253abd0 100644 (file)
@@ -292,3 +292,10 @@ namespace greatergreater {
     //(void)(&t<S<int>>==p);
   }
 }
+
+class foo {
+  static void test() {
+    (void)&i; // expected-error{{must explicitly qualify name of member function when taking its address}}
+  }
+  int i();
+};
index c95c867..9da2922 100644 (file)
@@ -5,3 +5,9 @@
 // CHECK-NOT: fix-it:
 
 template<template<typename> +> void func();
+
+struct {
+  void i() {
+    (void)&i;
+  }
+} x;