diff options
author | Patrick Beard <pcbeard@mac.com> | 2012-04-06 18:12:22 +0000 |
---|---|---|
committer | Patrick Beard <pcbeard@mac.com> | 2012-04-06 18:12:22 +0000 |
commit | acfbe9e1f22886d82c503cd0c20dc0ef3bf9f12a (patch) | |
tree | e9cefdee91276a540df86ad882a3bdf60072bdd2 /clang/lib/Sema | |
parent | 780af51505a1fedb4e3ced8e0b5832c4d4952a27 (diff) | |
download | bcm5719-llvm-acfbe9e1f22886d82c503cd0c20dc0ef3bf9f12a.tar.gz bcm5719-llvm-acfbe9e1f22886d82c503cd0c20dc0ef3bf9f12a.zip |
Added a new attribute, objc_root_class, which informs the compiler when a root class is intentionally declared.
The warning this inhibits, -Wobjc-root-class, is opt-in for now. However, all clang unit tests that would trigger
the warning have been updated to use -Wno-objc-root-class. <rdar://problem/7446698>
llvm-svn: 154187
Diffstat (limited to 'clang/lib/Sema')
-rw-r--r-- | clang/lib/Sema/SemaDeclAttr.cpp | 19 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDeclObjC.cpp | 31 |
2 files changed, 49 insertions, 1 deletions
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 081c93f8c20..77809e153f1 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -1593,6 +1593,22 @@ static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, Attr.getRange(), S.Context)); } +static void handleObjCRootClassAttr(Sema &S, Decl *D, + const AttributeList &Attr) { + if (!isa<ObjCInterfaceDecl>(D)) { + S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); + return; + } + + unsigned NumArgs = Attr.getNumArgs(); + if (NumArgs > 0) { + S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; + return; + } + + D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context)); +} + static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, const AttributeList &Attr) { if (!isa<ObjCInterfaceDecl>(D)) { @@ -3658,6 +3674,9 @@ static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, case AttributeList::AT_objc_arc_weak_reference_unavailable: handleArcWeakrefUnavailableAttr (S, D, Attr); break; + case AttributeList::AT_objc_root_class: + handleObjCRootClassAttr(S, D, Attr); + break; case AttributeList::AT_objc_requires_property_definitions: handleObjCRequiresPropertyDefsAttr (S, D, Attr); break; diff --git a/clang/lib/Sema/SemaDeclObjC.cpp b/clang/lib/Sema/SemaDeclObjC.cpp index 6be9c7ceeb8..a942d4979d7 100644 --- a/clang/lib/Sema/SemaDeclObjC.cpp +++ b/clang/lib/Sema/SemaDeclObjC.cpp @@ -24,6 +24,7 @@ #include "clang/AST/ASTMutationListener.h" #include "clang/Basic/SourceManager.h" #include "clang/Sema/DeclSpec.h" +#include "clang/Lex/Preprocessor.h" #include "llvm/ADT/DenseSet.h" using namespace clang; @@ -2371,11 +2372,39 @@ Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, AtomicPropertySetterGetterRules(IC, IDecl); DiagnoseOwningPropertyGetterSynthesis(IC); - if (LangOpts.ObjCNonFragileABI2) + bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>(); + if (IDecl->getSuperClass() == NULL) { + // This class has no superclass, so check that it has been marked with + // __attribute((objc_root_class)). + if (!HasRootClassAttr) { + SourceLocation DeclLoc(IDecl->getLocation()); + SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc)); + Diag(DeclLoc, diag::warn_objc_root_class_missing) + << IDecl->getIdentifier(); + // See if NSObject is in the current scope, and if it is, suggest + // adding " : NSObject " to the class declaration. + NamedDecl *IF = LookupSingleName(TUScope, + NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject), + DeclLoc, LookupOrdinaryName); + ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); + if (NSObjectDecl && NSObjectDecl->getDefinition()) { + Diag(SuperClassLoc, diag::note_objc_needs_superclass) + << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject "); + } else { + Diag(SuperClassLoc, diag::note_objc_needs_superclass); + } + } + } else if (HasRootClassAttr) { + // Complain that only root classes may have this attribute. + Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass); + } + + if (LangOpts.ObjCNonFragileABI2) { while (IDecl->getSuperClass()) { DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); IDecl = IDecl->getSuperClass(); } + } } SetIvarInitializers(IC); } else if (ObjCCategoryImplDecl* CatImplClass = |