]> granicus.if.org Git - clang/commitdiff
Diagnose that ivars in current and super class may not
authorFariborz Jahanian <fjahanian@apple.com>
Tue, 16 Dec 2008 01:08:35 +0000 (01:08 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Tue, 16 Dec 2008 01:08:35 +0000 (01:08 +0000)
be duplicates and a test case.

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

lib/AST/DeclObjC.cpp
lib/Sema/SemaDecl.cpp
test/SemaObjC/duplicate-ivar-check.m [new file with mode: 0644]

index 7e2a99fb26b2b66f4628774224b303e9717616c5..9e803b71e164c790fcb59e852dd7efecf9e8a75e 100644 (file)
@@ -345,7 +345,8 @@ void ObjCInterfaceDecl::CollectObjCIvars(std::vector<FieldDecl*> &Fields) {
   for (ObjCInterfaceDecl::ivar_iterator I = ivar_begin(),
        E = ivar_end(); I != E; ++I) {
     ObjCIvarDecl *IVDecl = (*I);
-    Fields.push_back(cast<FieldDecl>(IVDecl));
+    if (!IVDecl->isInvalidDecl())
+      Fields.push_back(cast<FieldDecl>(IVDecl));
   }
 }
   
@@ -390,7 +391,8 @@ void ObjCInterfaceDecl::addRecordToClass(ASTContext &Context)
   /// FIXME! Can do collection of ivars and adding to the record while
   /// doing it.
   for (unsigned int i = 0; i != RecFields.size(); i++) {
-    FieldDecl *Field =  FieldDecl::Create(Context, RD, SourceLocation(), 
+    FieldDecl *Field =  FieldDecl::Create(Context, RD, 
+                                          RecFields[i]->getLocation(), 
                                           RecFields[i]->getIdentifier(),
                                           RecFields[i]->getType(), 
                                           RecFields[i]->getBitWidth(), false, 0);
index 5e0edcd1ecf55bed51f3885c5b1090ac05fbf887..0d48c626125e21d3ca629e34bacf680f694e9a23 100644 (file)
@@ -3091,6 +3091,25 @@ void Sema::ActOnFields(Scope* S,
     if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(EnclosingDecl)) {
       ID->addInstanceVariablesToClass(ClsFields, RecFields.size(), RBrac);
       ID->addRecordToClass(Context);
+      // Must enforce the rule that ivars in the base classes may not be
+      // duplicates.
+      FieldIDs.clear();
+      RecordDecl *RD = ID->getRecordForDecl();
+      for (RecordDecl::field_iterator i = RD->field_begin(),
+           e = RD->field_end(); i != e; ++i) {
+        FieldDecl *FD = *i;
+        if (IdentifierInfo *II = FD->getIdentifier())
+          if (!FieldIDs.insert(II)) {
+            Diag(FD->getLocation(), diag::err_duplicate_member) << II;
+            FD->setInvalidDecl();
+            for (RecordDecl::field_iterator j = RD->field_begin(),
+                 e1 = RD->field_end(); j != e1; ++j)
+              if (II == (*j)->getIdentifier()) {
+                Diag((*j)->getLocation(), diag::note_previous_definition);
+                break;
+              }
+          }
+      }
     }
     else if (ObjCImplementationDecl *IMPDecl = 
                dyn_cast<ObjCImplementationDecl>(EnclosingDecl)) {
diff --git a/test/SemaObjC/duplicate-ivar-check.m b/test/SemaObjC/duplicate-ivar-check.m
new file mode 100644 (file)
index 0000000..ea42e9e
--- /dev/null
@@ -0,0 +1,20 @@
+// RUN: clang -fsyntax-only -verify %s
+
+@interface B1 {
+@public
+  double fill_B;       // expected-note {{previous definition is here}}
+}
+@end
+
+@interface B : B1 {
+@public
+  int one;     // expected-note {{previous definition is here}}
+  int one;     // expected-error {{duplicate member 'one'}}
+}
+@end
+
+@interface A : B {
+@public
+  int fill_B;  // expected-error {{duplicate member 'fill_B'}}
+}
+@end