[cfe-commits] r97002 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclObjC.cpp test/SemaObjC/duplicate-ivar-in-class-extension.m

Fariborz Jahanian fjahanian at apple.com
Tue Feb 23 15:41:11 PST 2010


Author: fjahanian
Date: Tue Feb 23 17:41:11 2010
New Revision: 97002

URL: http://llvm.org/viewvc/llvm-project?rev=97002&view=rev
Log:
More Sema check for ivars in class continuation.


Added:
    cfe/trunk/test/SemaObjC/duplicate-ivar-in-class-extension.m
Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaDeclObjC.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=97002&r1=97001&r2=97002&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Tue Feb 23 17:41:11 2010
@@ -1440,6 +1440,8 @@
   void AtomicPropertySetterGetterRules(ObjCImplDecl* IMPDecl,
                                        ObjCContainerDecl* IDecl);
 
+  void DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, ObjCInterfaceDecl *SID);
+
   /// MatchTwoMethodDeclarations - Checks if two methods' type match and returns
   /// true, or false, accordingly.
   bool MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=97002&r1=97001&r2=97002&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Feb 23 17:41:11 2010
@@ -5659,21 +5659,8 @@
       }
       // Must enforce the rule that ivars in the base classes may not be
       // duplicates.
-      if (ID->getSuperClass()) {
-        for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
-             IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
-          ObjCIvarDecl* Ivar = (*IVI);
-
-          if (IdentifierInfo *II = Ivar->getIdentifier()) {
-            ObjCIvarDecl* prevIvar =
-              ID->getSuperClass()->lookupInstanceVariable(II);
-            if (prevIvar) {
-              Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
-              Diag(prevIvar->getLocation(), diag::note_previous_declaration);
-            }
-          }
-        }
-      }
+      if (ID->getSuperClass())
+        DiagnoseDuplicateIvars(ID, ID->getSuperClass());
     } else if (ObjCImplementationDecl *IMPDecl =
                   dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
       assert(IMPDecl && "ActOnFields - missing ObjCImplementationDecl");

Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=97002&r1=97001&r2=97002&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Tue Feb 23 17:41:11 2010
@@ -1781,6 +1781,29 @@
   }
 }
 
+/// DiagnoseDuplicateIvars - 
+/// Check for duplicate ivars in the entire class at the start of 
+/// @implementation. This becomes necesssary because class extension can
+/// add ivars to a class in random order which will not be known until
+/// class's @implementation is seen.
+void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, 
+                                  ObjCInterfaceDecl *SID) {
+  for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
+       IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
+    ObjCIvarDecl* Ivar = (*IVI);
+    if (Ivar->isInvalidDecl())
+      continue;
+    if (IdentifierInfo *II = Ivar->getIdentifier()) {
+      ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
+      if (prevIvar) {
+        Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
+        Diag(prevIvar->getLocation(), diag::note_previous_declaration);
+        Ivar->setInvalidDecl();
+      }
+    }
+  }
+}
+
 // Note: For class/category implemenations, allMethods/allProperties is
 // always null.
 void Sema::ActOnAtEnd(SourceRange AtEnd,
@@ -1892,6 +1915,11 @@
     if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
       ImplMethodsVsClassMethods(IC, IDecl);
       AtomicPropertySetterGetterRules(IC, IDecl);
+      if (LangOpts.ObjCNonFragileABI2)
+        while (IDecl->getSuperClass()) {
+          DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
+          IDecl = IDecl->getSuperClass();
+        }
     }
   } else if (ObjCCategoryImplDecl* CatImplClass =
                                    dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {

Added: cfe/trunk/test/SemaObjC/duplicate-ivar-in-class-extension.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/duplicate-ivar-in-class-extension.m?rev=97002&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/duplicate-ivar-in-class-extension.m (added)
+++ cfe/trunk/test/SemaObjC/duplicate-ivar-in-class-extension.m Tue Feb 23 17:41:11 2010
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -fsyntax-only -fobjc-nonfragile-abi2 -verify %s
+
+ at interface Root @end
+
+ at interface SuperClass  : Root 
+{
+  int iSuper;	// expected-note {{previous declaration is here}}
+}
+ at end
+
+ at interface SubClass : SuperClass {
+    int ivar;	// expected-error {{duplicate member 'ivar'}}
+    int another_ivar;	// expected-error {{duplicate member 'another_ivar'}}
+    int iSuper;	// expected-error {{duplicate member 'iSuper'}}
+}
+ at end
+
+ at interface SuperClass () {
+   int ivar;	// expected-note {{previous declaration is here}}
+}
+ at end
+
+ at interface Root () {
+  int another_ivar;	// expected-note {{previous declaration is here}}
+}
+ at end
+
+ at implementation SubClass
+-(int) method {
+        return self->ivar;  // would be ambiguous if the duplicate ivar were allowed
+}
+ at end





More information about the cfe-commits mailing list