[cfe-commits] r75054 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/CXX/dcl.decl/dcl.meaning/dcl.mptr/p3.cpp

Anders Carlsson andersca at mac.com
Wed Jul 8 14:45:58 PDT 2009


Author: andersca
Date: Wed Jul  8 16:45:58 2009
New Revision: 75054

URL: http://llvm.org/viewvc/llvm-project?rev=75054&view=rev
Log:
It's not allowed to form member pointers to members that have reference type. Add a test for this and the rest of [dcl.mptr]p3.

Added:
    cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.mptr/p3.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=75054&r1=75053&r2=75054&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Jul  8 16:45:58 2009
@@ -1432,6 +1432,9 @@
 def err_not_tag_in_scope : Error<
   "%0 does not name a tag member in the specified scope">;
 
+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 warn_value_always_zero : Warning<"%0 is always zero in this context">;
 def warn_value_always_false : Warning<"%0 is always false in this context">;
 

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=75054&r1=75053&r2=75054&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Jul  8 16:45:58 2009
@@ -4687,15 +4687,23 @@
     } else if (isa<OverloadedFunctionDecl>(dcl) ||
                isa<FunctionTemplateDecl>(dcl)) {
       return Context.OverloadTy;
-    } else if (isa<FieldDecl>(dcl)) {
+    } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) {
       // Okay: we can take the address of a field.
       // Could be a pointer to member, though, if there is an explicit
       // scope qualifier for the class.
       if (isa<QualifiedDeclRefExpr>(op)) {
         DeclContext *Ctx = dcl->getDeclContext();
-        if (Ctx && Ctx->isRecord())
+        if (Ctx && Ctx->isRecord()) {
+          if (FD->getType()->isReferenceType()) {
+            Diag(OpLoc, 
+                 diag::err_cannot_form_pointer_to_member_of_reference_type)
+              << FD->getDeclName() << FD->getType();
+            return QualType();
+          }
+          
           return Context.getMemberPointerType(op->getType(),
                 Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr());
+        }
       }
     } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) {
       // Okay: we can take the address of a function.

Added: cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.mptr/p3.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.mptr/p3.cpp?rev=75054&view=auto

==============================================================================
--- cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.mptr/p3.cpp (added)
+++ cfe/trunk/test/CXX/dcl.decl/dcl.meaning/dcl.mptr/p3.cpp Wed Jul  8 16:45:58 2009
@@ -0,0 +1,20 @@
+// RUN: clang-cc -fsyntax-only -verify %t
+class A { 
+public:
+  int& i; 
+
+  A(int& i) : i(i) { }
+  
+  static int s;
+}; 
+
+void f() {
+  int b;
+  A a(b); 
+  
+  int A::*ip = &A::s; // expected-error {{incompatible type initializing 'int *', expected 'int class A::*'}}
+  a.*&A::s = 10; // expected-error{{right hand operand to .* has non pointer-to-member type 'int *'}}
+  a.*&A::i = 10; // expected-error{{cannot form a pointer-to-member to member 'i' of reference type 'int &'}}
+
+  void A::*p = 0; // expected-error{{'p' declared as a member pointer to void}}
+}





More information about the cfe-commits mailing list