From be127ba477ce959f802d167498fa742f732d6b5d Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Mon, 15 Oct 2007 19:16:57 +0000 Subject: [PATCH] Several name lookup conflict detection fixes involving objective-c names. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43000 91177308-0d34-0410-b5e6-96231b3b80d8 --- Sema/SemaDecl.cpp | 16 ++++++++++++++-- test/Sema/check-dup-objc-decls-1.m | 29 +++++++++++++++++++++++++++++ test/Sema/class-def-test-1.m | 5 +++-- 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 test/Sema/check-dup-objc-decls-1.m diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 65f6708b1b..59a2d1d547 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -945,6 +945,9 @@ Sema::DeclTy *Sema::ActOnStartClassInterface( // Chain & install the interface decl into the identifier. IDecl->setNext(ClassName->getFETokenInfo()); ClassName->setFETokenInfo(IDecl); + + // Remember that this needs to be removed when the scope is popped. + TUScope->AddDecl(IDecl); } if (SuperName) { @@ -1214,6 +1217,8 @@ Sema::DeclTy *Sema::ActOnStartClassImplementation( IDecl->setNext(ClassName->getFETokenInfo()); ClassName->setFETokenInfo(IDecl); + // Remember that this needs to be removed when the scope is popped. + TUScope->AddDecl(IDecl); } // Check that there is no duplicate implementation of this class. @@ -1397,9 +1402,16 @@ Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, llvm::SmallVector Interfaces; for (unsigned i = 0; i != NumElts; ++i) { - ObjcInterfaceDecl *IDecl = getObjCInterfaceDecl(IdentList[i]); + // Check for another declaration kind with the same name. + ScopedDecl *PrevDecl = LookupInterfaceDecl(IdentList[i]); + if (PrevDecl && !isa(PrevDecl)) { + Diag(AtClassLoc, diag::err_redefinition_different_kind, + IdentList[i]->getName()); + Diag(PrevDecl->getLocation(), diag::err_previous_definition); + } + ObjcInterfaceDecl *IDecl = dyn_cast_or_null(PrevDecl); if (!IDecl) { // Not already seen? Make a forward decl. - IDecl = new ObjcInterfaceDecl(SourceLocation(), 0, IdentList[i], true); + IDecl = new ObjcInterfaceDecl(AtClassLoc, 0, IdentList[i], true); // Chain & install the interface decl into the identifier. IDecl->setNext(IdentList[i]->getFETokenInfo()); IdentList[i]->setFETokenInfo(IDecl); diff --git a/test/Sema/check-dup-objc-decls-1.m b/test/Sema/check-dup-objc-decls-1.m new file mode 100644 index 0000000000..dd48ba4796 --- /dev/null +++ b/test/Sema/check-dup-objc-decls-1.m @@ -0,0 +1,29 @@ +// RUN: clang -fsyntax-only -verify %s + +@interface Foo // expected-error {{previous definition is here}} +@end + +float Foo; // expected-error {{redefinition of 'Foo' as different kind of symbol}} + +@class Bar; // expected-error {{previous definition is here}} + +typedef int Bar; // expected-error {{redefinition of 'Bar' as different kind of symbol}} + +@implementation FooBar // expected-warning {{cannot find interface declaration for 'FooBar'}} +@end + + +typedef int OBJECT; // expected-error {{previous definition is here}} + +@class OBJECT ; // expected-error {{redefinition of 'OBJECT' as different kind of symbol}} + + +typedef int Gorf; // expected-error {{previous definition is here}} + +@interface Gorf @end // expected-error {{redefinition of 'Gorf' as different kind of symbol}} \ + // expected-error {{previous definition is here}} + +void Gorf() // expected-error {{redefinition of 'Gorf' as different kind of symbol}} +{ + int Bar, Foo, FooBar; +} diff --git a/test/Sema/class-def-test-1.m b/test/Sema/class-def-test-1.m index a33e9652c6..91de54fbed 100644 --- a/test/Sema/class-def-test-1.m +++ b/test/Sema/class-def-test-1.m @@ -8,13 +8,14 @@ typedef int INTF; // expected-error {{previous definition is here}} @interface INTF @end // expected-error {{redefinition of 'INTF' as different kind of symbol}} -@interface OBJECT @end +@interface OBJECT @end // expected-error {{previous definition is here}} @interface INTF1 : OBJECT @end @interface INTF1 : OBJECT @end // expected-error {{duplicate interface declaration for class 'INTF1'} -typedef int OBJECT; // expected-error {{previous definition is here}} +typedef int OBJECT; // expected-error {{previous definition is here}} \ + expected-error {{redefinition of 'OBJECT' as different kind of symbol}} @interface INTF2 : OBJECT @end // expected-error {{redefinition of 'OBJECT' as different kind of symbol}} -- 2.40.0