Emit error when using a bound member function for something other than calling it.

Also avoids IRGen crashes due to accepting invalid code.

llvm-svn: 117943
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index ba4a380..3ba0667 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -396,6 +396,9 @@
   /// write barrier.
   bool isOBJCGCCandidate(ASTContext &Ctx) const;
 
+  /// \brief Returns true if this expression is a bound member function.
+  bool isBoundMemberFunction(ASTContext &Ctx) const;
+
   /// \brief Result type of CanThrow().
   enum CanThrowResult {
     CT_Cannot,
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 88e3126..2972e9b 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2641,6 +2641,8 @@
 
 def err_cannot_form_pointer_to_member_of_reference_type : Error<
   "cannot form a pointer-to-member to member %0 of reference type %1">;
+def err_invalid_use_of_bound_member_func : Error<
+  "a bound member function may only be used to call it">;
 def err_incomplete_object_call : Error<
   "incomplete type in call to object of type %0">;
 def err_incomplete_pointer_to_member_return : Error<
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 6829557..078bd7c 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -1324,6 +1324,12 @@
   }
 }
 
+bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
+  if (isTypeDependent())
+    return false;
+  return isLvalue(Ctx) == Expr::LV_MemberFunction;
+}
+
 static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
                                           Expr::CanThrowResult CT2) {
   // CanThrowResult constants are ordered so that the maximum is the correct
diff --git a/clang/lib/Sema/SemaCXXCast.cpp b/clang/lib/Sema/SemaCXXCast.cpp
index 7b1e34a..fc9ef73 100644
--- a/clang/lib/Sema/SemaCXXCast.cpp
+++ b/clang/lib/Sema/SemaCXXCast.cpp
@@ -146,6 +146,10 @@
   // FIXME: should we check this in a more fine-grained manner?
   bool TypeDependent = DestType->isDependentType() || Ex->isTypeDependent();
 
+  if (Ex->isBoundMemberFunction(Context))
+    Diag(Ex->getLocStart(), diag::err_invalid_use_of_bound_member_func)
+      << Ex->getSourceRange();
+
   switch (Kind) {
   default: assert(0 && "Unknown C++ cast!");
 
@@ -1273,6 +1277,11 @@
                          CastKind &Kind, 
                          CXXCastPath &BasePath,
                          bool FunctionalStyle) {
+  if (CastExpr->isBoundMemberFunction(Context))
+    return Diag(CastExpr->getLocStart(),
+                diag::err_invalid_use_of_bound_member_func)
+        << CastExpr->getSourceRange();
+
   // This test is outside everything else because it's the only case where
   // a non-lvalue-reference target type does not lead to decay.
   // C++ 5.2.9p4: Any expression can be explicitly converted to type "cv void".
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 6f6d371..9e28172 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -8206,6 +8206,10 @@
   DiagnoseAssignmentAsCondition(E);
 
   if (!E->isTypeDependent()) {
+    if (E->isBoundMemberFunction(Context))
+      return Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func)
+        << E->getSourceRange();
+
     DefaultFunctionArrayLvalueConversion(E);
 
     QualType T = E->getType();
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index ddee9cc..4c586e5 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -74,6 +74,12 @@
   if (!E)
     return;
 
+  if (E->isBoundMemberFunction(Context)) {
+    Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func)
+      << E->getSourceRange();
+    return;
+  }
+
   SourceLocation Loc;
   SourceRange R1, R2;
   if (!E->isUnusedResultAWarning(Loc, R1, R2, Context))
diff --git a/clang/test/SemaCXX/ptrtomember.cpp b/clang/test/SemaCXX/ptrtomember.cpp
index e84e931..f26f13f 100644
--- a/clang/test/SemaCXX/ptrtomember.cpp
+++ b/clang/test/SemaCXX/ptrtomember.cpp
@@ -16,3 +16,17 @@
 };
 
 int S2::*pf = &S2::bitfield; // expected-error {{address of bit-field requested}}
+
+struct S3 {
+  void m();
+};
+
+void f3(S3* p, void (S3::*m)()) {
+    p->*m; // expected-error {{a bound member function may only be used to call it}}
+    (void)(p->*m); // expected-error {{a bound member function may only be used to call it}}
+    (void)(void*)(p->*m); // expected-error {{a bound member function may only be used to call it}}
+    (void)reinterpret_cast<void*>(p->*m); // expected-error {{a bound member function may only be used to call it}}
+    if (p->*m) {} // expected-error {{a bound member function may only be used to call it}}
+
+    p->m; // expected-error {{a bound member function may only be used to call it}}
+}