llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: geoffreygaren <details> <summary>Changes</summary> The checker never reported a raw member typed id, id<P>, Class, or Class<P>. The type test correctly classifies these as unretained, but reporting required a pointee declaration to name in the message (a C++ record or an Objective-C interface). These types have neither, so the warning was dropped. The same requirement dropped CF-typed ivars such as CFStringRef in plain Objective-C, where the CF struct is a C record rather than a C++ one. Report these members too. When there is no pointee declaration, name the member's pointer type in the message, the way typedef'd types like dispatch_queue_t are already named. Messages for members that were already reported are unchanged. Assisted by: claude --- Full diff: https://github.com/llvm/llvm-project/pull/244918.diff 4 Files Affected: - (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp (+26-27) - (modified) clang/test/Analysis/Checkers/WebKit/unretained-members-arc.mm (+13) - (added) clang/test/Analysis/Checkers/WebKit/unretained-members-objc.m (+20) - (modified) clang/test/Analysis/Checkers/WebKit/unretained-members.mm (+40) ``````````diff diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp index 8b0ac3e4f2177..2b7f836615bb8 100644 --- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp @@ -110,10 +110,7 @@ class RawPtrRefMemberChecker if (!MemberType) return; - if (auto *MemberCXXRD = MemberType->getPointeeCXXRecordDecl()) - reportBug(Member, MemberType, MemberCXXRD, RD); - else if (auto *ObjCDecl = getObjCDeclFromObjCPtr(MemberType)) - reportBug(Member, MemberType, ObjCDecl, RD); + reportBug(Member, MemberType, RD); } void visitObjCDecl(const ObjCContainerDecl *CD) const { @@ -157,10 +154,7 @@ class RawPtrRefMemberChecker IvarDeclsToIgnore.insert(Ivar); - if (auto *MemberCXXRD = IvarType->getPointeeCXXRecordDecl()) - reportBug(Ivar, IvarType, MemberCXXRD, CD); - else if (auto *ObjCDecl = getObjCDeclFromObjCPtr(IvarType)) - reportBug(Ivar, IvarType, ObjCDecl, CD); + reportBug(Ivar, IvarType, CD); } void visitObjCPropertyDecl(const ObjCContainerDecl *CD, @@ -179,10 +173,7 @@ class RawPtrRefMemberChecker if (!IsUnsafe) return; - if (auto *MemberCXXRD = PropType->getPointeeCXXRecordDecl()) - reportBug(PD, PropType, MemberCXXRD, CD); - else if (auto *ObjCDecl = getObjCDeclFromObjCPtr(PropType)) - reportBug(PD, PropType, ObjCDecl, CD); + reportBug(PD, PropType, CD); } void visitPropImpl(const ObjCContainerDecl *CD, @@ -203,10 +194,7 @@ class RawPtrRefMemberChecker if (!IsUnsafe) return; - if (auto *MemberCXXRD = PropType->getPointeeCXXRecordDecl()) - reportBug(PropDecl, PropType, MemberCXXRD, CD); - else if (auto *ObjCDecl = getObjCDeclFromObjCPtr(PropType)) - reportBug(PropDecl, PropType, ObjCDecl, CD); + reportBug(PropDecl, PropType, CD); } std::pair<bool, const Type *> @@ -263,13 +251,17 @@ class RawPtrRefMemberChecker return false; } - template <typename DeclType, typename PointeeType, typename ParentDeclType> + static const NamedDecl *getPointeeDecl(const Type *T) { + if (auto *CXXRD = T->getPointeeCXXRecordDecl()) + return CXXRD; + return getObjCDeclFromObjCPtr(T); + } + + template <typename DeclType, typename ParentDeclType> void reportBug(const DeclType *Member, const Type *MemberType, - const PointeeType *Pointee, const ParentDeclType *ClassCXXRD) const { assert(Member); assert(MemberType); - assert(Pointee); SmallString<100> Buf; llvm::raw_svector_ostream Os(Buf); @@ -290,11 +282,16 @@ class RawPtrRefMemberChecker else Os << " contains a "; if (printPointer(Os, MemberType) == PrintDeclKind::Pointer) { - auto Typedef = MemberType->getAs<TypedefType>(); - assert(Typedef); - printQuotedQualifiedName(Os, Typedef->getDecl()); + if (auto *Typedef = MemberType->getAs<TypedefType>()) + printQuotedQualifiedName(Os, Typedef->getDecl()); + else { + Os << "'"; + QualType(MemberType, 0) + .print(Os, Member->getASTContext().getPrintingPolicy()); + Os << "'"; + } } else - printQuotedQualifiedName(Os, Pointee); + printQuotedQualifiedName(Os, getPointeeDecl(MemberType)); PathDiagnosticLocation BSLoc(Member->getSourceRange().getBegin(), BR->getSourceManager()); @@ -308,10 +305,12 @@ class RawPtrRefMemberChecker enum class PrintDeclKind { Pointee, Pointer }; PrintDeclKind printPointer(llvm::raw_svector_ostream &Os, const Type *T) const { - // Retain/OS types are frequently spelled through a typedef (e.g. CFXXXRef); - // print the typedef name rather than desugaring to the pointee. - if (Model->retainTypeChecker() && !isa<ObjCObjectPointerType>(T) && - T->getAs<TypedefType>()) { + // Retain/OS types are frequently spelled through a typedef (e.g. CFXXXRef), + // and id<P> has no pointee decl; name the pointer type itself for both. + bool IsRetainTypedef = Model->retainTypeChecker() && + !isa<ObjCObjectPointerType>(T) && + T->getAs<TypedefType>(); + if (IsRetainTypedef || !getPointeeDecl(T)) { Os << Model->typeName() << " "; return PrintDeclKind::Pointer; } diff --git a/clang/test/Analysis/Checkers/WebKit/unretained-members-arc.mm b/clang/test/Analysis/Checkers/WebKit/unretained-members-arc.mm index ca70b75fc7557..ec80ecf2bcd8b 100644 --- a/clang/test/Analysis/Checkers/WebKit/unretained-members-arc.mm +++ b/clang/test/Analysis/Checkers/WebKit/unretained-members-arc.mm @@ -127,3 +127,16 @@ - (NSString *)prop_string1 { @synthesize prop_string4; @synthesize prop_string5; @end + +@protocol IdMembersARCProtocol +@end + +@interface IdMembersARC : NSObject { + id<IdMembersARCProtocol> _strong; +} +@property (nonatomic, assign) id<IdMembersARCProtocol> assignIdP; +// expected-warning@-1{{Property 'assignIdP' (of 'IdMembersARC') is a RetainPtr-capable type 'id<IdMembersARCProtocol>'}} +@end + +@implementation IdMembersARC +@end diff --git a/clang/test/Analysis/Checkers/WebKit/unretained-members-objc.m b/clang/test/Analysis/Checkers/WebKit/unretained-members-objc.m new file mode 100644 index 0000000000000..b2f19d373d2bb --- /dev/null +++ b/clang/test/Analysis/Checkers/WebKit/unretained-members-objc.m @@ -0,0 +1,20 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.NoUnretainedMemberChecker -verify %s + +typedef const struct __attribute__((objc_bridge(NSString))) __CFString *CFStringRef; + +__attribute__((objc_root_class)) +@interface NSObject +@end + +@interface PlainObjCMembers : NSObject { + CFStringRef _cf; + // expected-warning@-1{{Instance variable '_cf' (of 'PlainObjCMembers') is a RetainPtr-capable type 'CFStringRef'}} + NSObject *_ns; + // expected-warning@-1{{Instance variable '_ns' (of 'PlainObjCMembers') is a raw pointer to RetainPtr-capable type 'NSObject'}} + id _id; + // expected-warning@-1{{Instance variable '_id' (of 'PlainObjCMembers') is a RetainPtr-capable type 'id'}} +} +@end + +@implementation PlainObjCMembers +@end diff --git a/clang/test/Analysis/Checkers/WebKit/unretained-members.mm b/clang/test/Analysis/Checkers/WebKit/unretained-members.mm index 51c9b7851d96d..0bc0cef386270 100644 --- a/clang/test/Analysis/Checkers/WebKit/unretained-members.mm +++ b/clang/test/Analysis/Checkers/WebKit/unretained-members.mm @@ -196,3 +196,43 @@ - (dispatch_queue_t)dispatch { return nil; } @end + +@protocol IdMembersProtocol +@end + +namespace id_members { + struct Foo { + id<IdMembersProtocol> a; + // expected-warning@-1{{Member variable 'a' (of 'id_members::Foo') is a RetainPtr-capable type 'id<IdMembersProtocol>'}} + }; +} // namespace id_members + +@interface IdMembers : NSObject { + id _idInInterface; + // expected-warning@-1{{Instance variable '_idInInterface' (of 'IdMembers') is a RetainPtr-capable type 'id'}} + id<IdMembersProtocol> _idPInInterface; + // expected-warning@-1{{Instance variable '_idPInInterface' (of 'IdMembers') is a RetainPtr-capable type 'id<IdMembersProtocol>'}} + Class _classInInterface; + // expected-warning@-1{{Instance variable '_classInInterface' (of 'IdMembers') is a RetainPtr-capable type 'Class'}} + Class<IdMembersProtocol> _classPInInterface; + // expected-warning@-1{{Instance variable '_classPInInterface' (of 'IdMembers') is a RetainPtr-capable type 'Class<IdMembersProtocol>'}} + id<IdMembersProtocol> _delegate; + // expected-warning@-1{{Instance variable '_delegate' (of 'IdMembers') is a RetainPtr-capable type 'id<IdMembersProtocol>'}} +} +@property (nonatomic, assign) id<IdMembersProtocol> assignIdP; +// expected-warning@-1{{Property 'assignIdP' (of 'IdMembers') is a RetainPtr-capable type 'id<IdMembersProtocol>'}} +@property (nonatomic, assign) id<IdMembersProtocol> delegate; +@end + +@implementation IdMembers { + id _idInImplementation; + // expected-warning@-1{{Instance variable '_idInImplementation' (of 'IdMembers') is a RetainPtr-capable type 'id'}} + id<IdMembersProtocol> _idPInImplementation; + // expected-warning@-1{{Instance variable '_idPInImplementation' (of 'IdMembers') is a RetainPtr-capable type 'id<IdMembersProtocol>'}} + Class _classInImplementation; + // expected-warning@-1{{Instance variable '_classInImplementation' (of 'IdMembers') is a RetainPtr-capable type 'Class'}} + Class<IdMembersProtocol> _classPInImplementation; + // expected-warning@-1{{Instance variable '_classPInImplementation' (of 'IdMembers') is a RetainPtr-capable type 'Class<IdMembersProtocol>'}} +} +@synthesize delegate = _delegate; +@end `````````` </details> https://github.com/llvm/llvm-project/pull/244918 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
