Skip to content

Commit ca50dee

Browse files
committed
[alpha.webkit.RawPtrRefMemberChecker] The checker doesn't warn Objective-C types in ivars.
This PR fixes the bug that we weren't generating warnings when a raw poiner is used to point to a NS type in Objective-C ivars. Also fix the bug that we weren't suppressing this warning in system headers.
1 parent fe6bced commit ca50dee

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefMemberChecker.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ class RawPtrRefMemberChecker
111111
}
112112

113113
void visitObjCDecl(const ObjCContainerDecl *CD) const {
114+
if (BR->getSourceManager().isInSystemHeader(CD->getLocation()))
115+
return;
114116
if (auto *ID = dyn_cast<ObjCInterfaceDecl>(CD)) {
115117
for (auto *Ivar : ID->ivars())
116118
visitIvarDecl(CD, Ivar);
@@ -133,6 +135,14 @@ class RawPtrRefMemberChecker
133135
std::optional<bool> IsCompatible = isPtrCompatible(QT, IvarCXXRD);
134136
if (IsCompatible && *IsCompatible)
135137
reportBug(Ivar, IvarType, IvarCXXRD, CD);
138+
} else {
139+
std::optional<bool> IsCompatible = isPtrCompatible(QT, nullptr);
140+
auto *PointeeType = IvarType->getPointeeType().getTypePtrOrNull();
141+
if (IsCompatible && *IsCompatible) {
142+
auto *Desugared = PointeeType->getUnqualifiedDesugaredType();
143+
if (auto *ObjCType = dyn_cast_or_null<ObjCInterfaceType>(Desugared))
144+
reportBug(Ivar, IvarType, ObjCType->getDecl(), CD);
145+
}
136146
}
137147
}
138148

clang/test/Analysis/Checkers/WebKit/mock-system-header.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,14 @@ enum os_log_type_t : uint8_t {
2929
typedef struct os_log_s *os_log_t;
3030
os_log_t os_log_create(const char *subsystem, const char *category);
3131
void os_log_msg(os_log_t oslog, os_log_type_t type, const char *msg, ...);
32+
33+
typedef const struct __attribute__((objc_bridge(NSString))) __CFString * CFStringRef;
34+
35+
#ifdef __OBJC__
36+
@class NSString;
37+
@interface SystemObject : NSObject {
38+
NSString *ns_string;
39+
CFStringRef cf_string;
40+
}
41+
@end
42+
#endif

clang/test/Analysis/Checkers/WebKit/unretained-members.mm

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.NoUnretainedMemberChecker -verify %s
22

33
#include "objc-mock-types.h"
4-
4+
#include "mock-system-header.h"
5+
#if 0
56
namespace members {
67

78
struct Foo {
@@ -58,3 +59,12 @@ void forceTmplToInstantiate(FooTmpl<SomeObj, CFMutableArrayRef>) {}
5859

5960
void forceTmplToInstantiate(RefPtr<SomeObj>) {}
6061
}
62+
#endif
63+
64+
@interface AnotherObject : NSObject {
65+
NSString *ns_string;
66+
// expected-warning@-1{{Instance variable 'ns_string' in 'AnotherObject' is a raw pointer to retainable type 'NSString'; member variables must be a RetainPtr}}
67+
CFStringRef cf_string;
68+
// expected-warning@-1{{Instance variable 'cf_string' in 'AnotherObject' is a retainable type 'CFStringRef'; member variables must be a RetainPtr}}
69+
}
70+
@end

0 commit comments

Comments
 (0)