r184238 - When declaring an ObjC interface decl with a @compatibility_alias alias name, change the class name to the "real" one.
Argyrios Kyrtzidis
akyrtzi at gmail.com
Tue Jun 18 14:26:33 PDT 2013
Author: akirtzidis
Date: Tue Jun 18 16:26:33 2013
New Revision: 184238
URL: http://llvm.org/viewvc/llvm-project?rev=184238&view=rev
Log:
When declaring an ObjC interface decl with a @compatibility_alias alias name, change the class name to the "real" one.
If we have something like
@class NewImage;
@compatibility_alias OldImage NewImage;
@class OldImage;
the lookup for 'OldImage' will return the 'NewImage' decl ("@class NewImage").
In such a case, when creating the decl for "@class OldImage" use the real declaration name ("NewImage"),
instead of the alias one ("OldImage"), otherwise we will break IdentifierResolver and redecls-chain invariants.
Fixes crash of rdar://14112291.
Modified:
cfe/trunk/lib/Sema/SemaDeclObjC.cpp
cfe/trunk/test/PCH/objc_import.h
cfe/trunk/test/PCH/objc_import.m
Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=184238&r1=184237&r2=184238&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Tue Jun 18 16:26:33 2013
@@ -459,6 +459,23 @@ ActOnStartClassInterface(SourceLocation
// Create a declaration to describe this @interface.
ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
+
+ if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
+ // A previous decl with a different name is because of
+ // @compatibility_alias, for example:
+ // \code
+ // @class NewImage;
+ // @compatibility_alias OldImage NewImage;
+ // \endcode
+ // A lookup for 'OldImage' will return the 'NewImage' decl.
+ //
+ // In such a case use the real declaration name, instead of the alias one,
+ // otherwise we will break IdentifierResolver and redecls-chain invariants.
+ // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
+ // has been aliased.
+ ClassName = PrevIDecl->getIdentifier();
+ }
+
ObjCInterfaceDecl *IDecl
= ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
PrevIDecl, ClassLoc);
@@ -1938,9 +1955,27 @@ Sema::ActOnForwardClassDeclaration(Sourc
// Create a declaration to describe this forward declaration.
ObjCInterfaceDecl *PrevIDecl
= dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
+
+ IdentifierInfo *ClassName = IdentList[i];
+ if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
+ // A previous decl with a different name is because of
+ // @compatibility_alias, for example:
+ // \code
+ // @class NewImage;
+ // @compatibility_alias OldImage NewImage;
+ // \endcode
+ // A lookup for 'OldImage' will return the 'NewImage' decl.
+ //
+ // In such a case use the real declaration name, instead of the alias one,
+ // otherwise we will break IdentifierResolver and redecls-chain invariants.
+ // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
+ // has been aliased.
+ ClassName = PrevIDecl->getIdentifier();
+ }
+
ObjCInterfaceDecl *IDecl
= ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
- IdentList[i], PrevIDecl, IdentLocs[i]);
+ ClassName, PrevIDecl, IdentLocs[i]);
IDecl->setAtEndRange(IdentLocs[i]);
PushOnScopeChains(IDecl, TUScope);
Modified: cfe/trunk/test/PCH/objc_import.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/objc_import.h?rev=184238&r1=184237&r2=184238&view=diff
==============================================================================
--- cfe/trunk/test/PCH/objc_import.h (original)
+++ cfe/trunk/test/PCH/objc_import.h Tue Jun 18 16:26:33 2013
@@ -5,3 +5,14 @@
- (void)instMethod;
@end
+ at class NewID1;
+ at compatibility_alias OldID1 NewID1;
+ at class OldID1;
+ at class OldID1;
+
+ at class NewID2;
+ at compatibility_alias OldID2 NewID2;
+ at class OldID2;
+ at interface OldID2
+-(void)meth;
+ at end
Modified: cfe/trunk/test/PCH/objc_import.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/objc_import.m?rev=184238&r1=184237&r2=184238&view=diff
==============================================================================
--- cfe/trunk/test/PCH/objc_import.m (original)
+++ cfe/trunk/test/PCH/objc_import.m Tue Jun 18 16:26:33 2013
@@ -15,3 +15,18 @@ void func() {
xx = [TestPCH alloc];
[xx instMethod];
}
+
+// rdar://14112291
+ at class NewID1;
+void foo1(NewID1 *p);
+void bar1(OldID1 *p) {
+ foo1(p);
+}
+ at class NewID2;
+void foo2(NewID2 *p) {
+ [p meth];
+}
+void bar2(OldID2 *p) {
+ foo2(p);
+ [p meth];
+}
More information about the cfe-commits
mailing list