[cfe-commits] r65854 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaDeclObjC.cpp test/SemaObjC/class-extension-dup-methods.m

Fariborz Jahanian fjahanian at apple.com
Mon Mar 2 11:05:09 PST 2009


Author: fjahanian
Date: Mon Mar  2 13:05:07 2009
New Revision: 65854

URL: http://llvm.org/viewvc/llvm-project?rev=65854&view=rev
Log:
Check for duplicate declaration of method of a class
in its extension.

Added:
    cfe/trunk/test/SemaObjC/class-extension-dup-methods.m
Modified:
    cfe/trunk/lib/Sema/Sema.h
    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=65854&r1=65853&r2=65854&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Mon Mar  2 13:05:07 2009
@@ -1709,6 +1709,9 @@
   void MergeProtocolPropertiesIntoClass(Decl *CDecl,
                                         DeclTy *MergeProtocols);
   
+  void DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, 
+                                        ObjCInterfaceDecl *ID);
+  
   void MergeOneProtocolPropertiesIntoClass(Decl *CDecl,
                                            ObjCProtocolDecl *PDecl);
   

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Mon Mar  2 13:05:07 2009
@@ -443,6 +443,35 @@
   }
 }
 
+/// DiagnoseClassExtensionDupMethods - Check for duplicate declartation of
+/// a class method in its extension.
+///
+void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, 
+                                            ObjCInterfaceDecl *ID) {
+  if (!ID)
+    return;  // Possibly due to previous error
+
+  llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
+  for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
+       e =  ID->meth_end(); i != e; ++i) {
+    ObjCMethodDecl *MD = *i;
+    MethodMap[MD->getSelector()] = MD;
+  }
+
+  if (MethodMap.empty())
+    return;
+  for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
+       e =  CAT->meth_end(); i != e; ++i) {
+    ObjCMethodDecl *Method = *i;
+    const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
+    if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
+      Diag(Method->getLocation(), diag::err_duplicate_method_decl)
+            << Method->getDeclName();
+      Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
+    }
+  }
+}
+
 /// ActOnForwardProtocolDeclaration - 
 Action::DeclTy *
 Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
@@ -1237,6 +1266,8 @@
 
     // Merge protocol properties into category
     MergeProtocolPropertiesIntoClass(C, C);
+    if (C->getIdentifier() == 0)
+      DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
   }
   if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
     // ProcessPropertyDecl is responsible for diagnosing conflicts with any

Added: cfe/trunk/test/SemaObjC/class-extension-dup-methods.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/class-extension-dup-methods.m?rev=65854&view=auto

==============================================================================
--- cfe/trunk/test/SemaObjC/class-extension-dup-methods.m (added)
+++ cfe/trunk/test/SemaObjC/class-extension-dup-methods.m Mon Mar  2 13:05:07 2009
@@ -0,0 +1,15 @@
+// RUN: clang -fsyntax-only -verify %s
+
+ at interface Foo
+- (int)  garf; // expected-note {{ previous declaration is here}}
+- (int) OK;
++ (int)  cgarf; // expected-note {{ previous declaration is here}}
+- (int)  InstMeth;
+ at end
+
+ at interface Foo()
+- (void)  garf; // expected-error {{duplicate declaration of method 'garf'}}
++ (void)  cgarf; // expected-error {{duplicate declaration of method 'cgarf'}}
++ (int)  InstMeth;
+- (int) OK;
+ at end





More information about the cfe-commits mailing list