From: Fariborz Jahanian Date: Fri, 12 Oct 2007 22:10:42 +0000 (+0000) Subject: Check and diagnose that objective-c objects may not be statically allocated. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e7f64cc250245aa6c0e1ef6da7e9b6f62d5fedde;p=clang Check and diagnose that objective-c objects may not be statically allocated. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42936 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 9d47375f50..b66c9f4860 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -556,6 +556,11 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) { } else { QualType R = GetTypeForDeclarator(D, S); assert(!R.isNull() && "GetTypeForDeclarator() returned null type"); + if (R.getTypePtr()->isObjcInterfaceType()) { + Diag(D.getIdentifierLoc(), diag::err_statically_allocated_object, + D.getIdentifier()->getName()); + InvalidDecl = true; + } VarDecl *NewVD; VarDecl::StorageClass SC; @@ -1680,6 +1685,14 @@ void Sema::ActOnFields(Scope* S, } } } + /// A field cannot be an Objective-c object + if (FDTy->isObjcInterfaceType()) { + Diag(FD->getLocation(), diag::err_statically_allocated_object, + FD->getName()); + FD->setInvalidDecl(); + EnclosingDecl->setInvalidDecl(); + continue; + } // Keep track of the number of named members. if (IdentifierInfo *II = FD->getIdentifier()) { // Detect duplicate member names. diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj index b88c381faa..75d938806b 100644 --- a/clang.xcodeproj/project.pbxproj +++ b/clang.xcodeproj/project.pbxproj @@ -742,6 +742,7 @@ 08FB7793FE84155DC02AAC07 /* Project object */ = { isa = PBXProject; buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */; + compatibilityVersion = "Xcode 2.4"; hasScannedForEncodings = 1; mainGroup = 08FB7794FE84155DC02AAC07 /* clang */; projectDirPath = ""; diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index 5018ca0c1d..061f34c054 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -275,6 +275,7 @@ public: bool isUnionType() const; bool isVectorType() const; // GCC vector type. bool isOCUVectorType() const; // OCU vector type. + bool isObjcInterfaceType() const; // includes conforming protocol type // Type Checking Functions: Check to see if this type is structurally the // specified type, ignoring typedefs, and return a pointer to the best type @@ -933,7 +934,10 @@ inline bool Type::isVectorType() const { inline bool Type::isOCUVectorType() const { return isa(CanonicalType); } - +inline bool Type::isObjcInterfaceType() const { + return isa(CanonicalType) + || isa(CanonicalType); +} } // end namespace clang #endif diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def index dec9343cbb..2a8900c025 100644 --- a/include/clang/Basic/DiagnosticKinds.def +++ b/include/clang/Basic/DiagnosticKinds.def @@ -452,6 +452,8 @@ DIAG(warn_previous_declaration, WARNING, "previous declaration is here") DIAG(err_conflicting_aliasing_type, ERROR, "conflicting types for alias %0'") +DIAG(err_statically_allocated_object, ERROR, + "statically allocated Objective-c object '%0'") //===----------------------------------------------------------------------===// // Semantic Analysis diff --git a/test/Sema/invalid-objc-decls-1.m b/test/Sema/invalid-objc-decls-1.m new file mode 100644 index 0000000000..9d88fdc237 --- /dev/null +++ b/test/Sema/invalid-objc-decls-1.m @@ -0,0 +1,29 @@ +// RUN: clang -fsyntax-only -verify %s + +@interface Super @end +Super s1; // expected-error{{statically allocated Objective-c object 's1'}} + +extern Super e1; // expected-error{{statically allocated Objective-c object 'e1'}} + +struct S { + Super s1; // expected-error{{statically allocated Objective-c object 's1'}} +}; + +@protocol P1 @end + +@interface INTF +{ + Super ivar1; // expected-error{{statically allocated Objective-c object 'ivar1'}} +} +@end + +@interface MyIntf +{ + Super ivar1; // expected-error{{statically allocated Objective-c object 'ivar1'}} +} +@end + +Super foo(Super parm1) { + Super p1; // expected-error{{statically allocated Objective-c object 'p1'}} + return p1; +}