[cfe-commits] r96850 - in /cfe/trunk: include/clang/AST/DeclObjC.h lib/AST/ASTContext.cpp lib/AST/DeclObjC.cpp lib/Sema/SemaDeclObjC.cpp test/SemaObjC/ivar-in-class-extension.m
Fariborz Jahanian
fjahanian at apple.com
Mon Feb 22 17:26:30 PST 2010
Author: fjahanian
Date: Mon Feb 22 19:26:30 2010
New Revision: 96850
URL: http://llvm.org/viewvc/llvm-project?rev=96850&view=rev
Log:
More support for ivars in class extension.
Added:
cfe/trunk/test/SemaObjC/ivar-in-class-extension.m
Modified:
cfe/trunk/include/clang/AST/DeclObjC.h
cfe/trunk/lib/AST/ASTContext.cpp
cfe/trunk/lib/AST/DeclObjC.cpp
cfe/trunk/lib/Sema/SemaDeclObjC.cpp
Modified: cfe/trunk/include/clang/AST/DeclObjC.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclObjC.h?rev=96850&r1=96849&r2=96850&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclObjC.h (original)
+++ cfe/trunk/include/clang/AST/DeclObjC.h Mon Feb 22 19:26:30 2010
@@ -527,6 +527,8 @@
CategoryList = category;
}
+ ObjCCategoryDecl* getClassExtension() const;
+
/// isSuperClassOf - Return true if this class is the specified class or is a
/// super class of the specified interface class.
bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
@@ -949,6 +951,20 @@
bool IsClassExtension() const { return getIdentifier() == 0; }
+ typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
+ ivar_iterator ivar_begin() const {
+ return ivar_iterator(decls_begin());
+ }
+ ivar_iterator ivar_end() const {
+ return ivar_iterator(decls_end());
+ }
+ unsigned ivar_size() const {
+ return std::distance(ivar_begin(), ivar_end());
+ }
+ bool ivar_empty() const {
+ return ivar_begin() == ivar_end();
+ }
+
SourceLocation getAtLoc() const { return AtLoc; }
void setAtLoc(SourceLocation At) { AtLoc = At; }
Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=96850&r1=96849&r2=96850&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Mon Feb 22 19:26:30 2010
@@ -900,6 +900,14 @@
///
void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
+ // Find ivars declared in class extension.
+ if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
+ for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
+ E = CDecl->ivar_end(); I != E; ++I) {
+ Ivars.push_back(*I);
+ }
+ }
+
for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
E = OI->prop_end(); I != E; ++I) {
if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=96850&r1=96849&r2=96850&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Mon Feb 22 19:26:30 2010
@@ -202,6 +202,17 @@
setProtocolList(ProtocolRefs.data(), NumProtoRefs, ProtocolLocs.data(), C);
}
+/// getClassExtension - Find class extension of the given class.
+// FIXME. can speed it up, if need be.
+ObjCCategoryDecl* ObjCInterfaceDecl::getClassExtension() const {
+ const ObjCInterfaceDecl* ClassDecl = this;
+ for (ObjCCategoryDecl *CDecl = ClassDecl->getCategoryList(); CDecl;
+ CDecl = CDecl->getNextClassCategory())
+ if (CDecl->IsClassExtension())
+ return CDecl;
+ return 0;
+}
+
ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
ObjCInterfaceDecl *&clsDeclared) {
ObjCInterfaceDecl* ClassDecl = this;
@@ -210,6 +221,12 @@
clsDeclared = ClassDecl;
return I;
}
+ if (const ObjCCategoryDecl *CDecl = ClassDecl->getClassExtension())
+ if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
+ clsDeclared = ClassDecl;
+ return I;
+ }
+
ClassDecl = ClassDecl->getSuperClass();
}
return NULL;
Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=96850&r1=96849&r2=96850&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Mon Feb 22 19:26:30 2010
@@ -599,13 +599,9 @@
SourceLocation EndProtoLoc) {
ObjCCategoryDecl *CDecl = 0;
ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
- if (!CategoryName) {
+ if (!CategoryName)
// Class extensions require a special treatment. Use an existing one.
- for (CDecl = IDecl->getCategoryList(); CDecl;
- CDecl = CDecl->getNextClassCategory())
- if (CDecl->IsClassExtension())
- break;
- }
+ CDecl = IDecl->getClassExtension();
if (!CDecl) {
CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, ClassLoc,
CategoryLoc, CategoryName);
Added: cfe/trunk/test/SemaObjC/ivar-in-class-extension.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/ivar-in-class-extension.m?rev=96850&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/ivar-in-class-extension.m (added)
+++ cfe/trunk/test/SemaObjC/ivar-in-class-extension.m Mon Feb 22 19:26:30 2010
@@ -0,0 +1,42 @@
+// RUN: %clang_cc1 -fsyntax-only -fobjc-nonfragile-abi2 -verify %s
+
+ at interface SomeClass @end
+
+int fn1(SomeClass *obj) {
+ obj->privateIvar = 1; // expected-error {{'SomeClass' does not have a member named 'privateIvar}}
+ return obj->publicIvar; // expected-error {{'SomeClass' does not have a member named 'publicIvar'}}
+}
+
+ at interface SomeClass () {
+// @private by default
+ int privateIvar;
+ at public
+ int publicIvar;
+}
+ at end
+
+int fn2(SomeClass *obj) {
+ obj->publicIvar = 1;
+ return obj->publicIvar // ok
+ + obj->privateIvar; // expected-error {{instance variable 'privateIvar' is private}}
+}
+
+ at implementation SomeClass
+
+int fn3(SomeClass *obj) {
+ obj->privateIvar = 2;
+ return obj->publicIvar // ok
+ + obj->privateIvar; // ok
+ }
+ at end
+
+ at interface SomeClass (Category)
+ { // expected-error {{ivar may be placed in a class extension}}
+ int categoryIvar;
+ }
+ at end
+
+ at interface SomeClass (Category1)
+ { // expected-error {{ivar may be placed in a class extension}}
+ }
+ at end
More information about the cfe-commits
mailing list