]> granicus.if.org Git - clang/commitdiff
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@165763 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 669d8356f5b9aaea85d5ac5e5a22a2891671f464..74ee87001247672ce2a2e39fa3024ac33eb68f50 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 dc0b6c8f2a1e624b9724de8998e49515ba54272f..253abd0f4e8bc095aac89776dc092601a599e319 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 c95c8670d6d216e793609674db3066fa4532ea2c..9da29229f04191830e43569ae8992137282ea1ed 100644 (file)
@@ -5,3 +5,9 @@
 // CHECK-NOT: fix-it:
 
 template<template<typename> +> void func();
+
+struct {
+  void i() {
+    (void)&i;
+  }
+} x;