[cfe-commits] r117943 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/Basic/DiagnosticSemaKinds.td lib/AST/Expr.cpp lib/Sema/SemaCXXCast.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaStmt.cpp test/SemaCXX/ptrtomember.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Mon Nov 1 11:49:26 PDT 2010


Author: akirtzidis
Date: Mon Nov  1 13:49:26 2010
New Revision: 117943

URL: http://llvm.org/viewvc/llvm-project?rev=117943&view=rev
Log:
Emit error when using a bound member function for something other than calling it.

Also avoids IRGen crashes due to accepting invalid code.

Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/Sema/SemaCXXCast.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/SemaCXX/ptrtomember.cpp

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=117943&r1=117942&r2=117943&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Mon Nov  1 13:49:26 2010
@@ -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,

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=117943&r1=117942&r2=117943&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Nov  1 13:49:26 2010
@@ -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<

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=117943&r1=117942&r2=117943&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Nov  1 13:49:26 2010
@@ -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

Modified: cfe/trunk/lib/Sema/SemaCXXCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCXXCast.cpp?rev=117943&r1=117942&r2=117943&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCXXCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCXXCast.cpp Mon Nov  1 13:49:26 2010
@@ -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".

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=117943&r1=117942&r2=117943&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Nov  1 13:49:26 2010
@@ -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();

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=117943&r1=117942&r2=117943&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Mon Nov  1 13:49:26 2010
@@ -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))

Modified: cfe/trunk/test/SemaCXX/ptrtomember.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/ptrtomember.cpp?rev=117943&r1=117942&r2=117943&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/ptrtomember.cpp (original)
+++ cfe/trunk/test/SemaCXX/ptrtomember.cpp Mon Nov  1 13:49:26 2010
@@ -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}}
+}





More information about the cfe-commits mailing list