Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ee31ab

Browse files
authored
[WebKit checkers] Treat function pointers with "Singleton" suffix as singleton. (llvm#158012)
1 parent 0d4a615 commit 8ee31ab

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ bool tryToFindPtrOrigin(
160160
if (Name == "__builtin___CFStringMakeConstantString" ||
161161
Name == "NSClassFromString")
162162
return callback(E, true);
163+
} else if (auto *CalleeE = call->getCallee()) {
164+
if (auto *E = dyn_cast<DeclRefExpr>(CalleeE->IgnoreParenCasts())) {
165+
if (isSingleton(E->getFoundDecl()))
166+
return callback(E, true);
167+
}
163168
}
164169

165170
// Sometimes, canonical type erroneously turns Ref<T> into T.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ bool isTrivialBuiltinFunction(const FunctionDecl *F) {
479479
Name.starts_with("os_log") || Name.starts_with("_os_log");
480480
}
481481

482-
bool isSingleton(const FunctionDecl *F) {
482+
bool isSingleton(const NamedDecl *F) {
483483
assert(F);
484484
// FIXME: check # of params == 1
485485
if (auto *MethodDecl = dyn_cast<CXXMethodDecl>(F)) {

clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class CXXMethodDecl;
2121
class CXXRecordDecl;
2222
class Decl;
2323
class FunctionDecl;
24+
class NamedDecl;
2425
class QualType;
2526
class RecordType;
2627
class Stmt;
@@ -156,7 +157,7 @@ bool isPtrConversion(const FunctionDecl *F);
156157
bool isTrivialBuiltinFunction(const FunctionDecl *F);
157158

158159
/// \returns true if \p F is a static singleton function.
159-
bool isSingleton(const FunctionDecl *F);
160+
bool isSingleton(const NamedDecl *F);
160161

161162
/// An inter-procedural analysis facility that detects functions with "trivial"
162163
/// behavior with respect to reference counting, such as simple field getters.

clang/test/Analysis/Checkers/WebKit/unretained-call-args.mm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,34 @@ void use_const_local() {
438438

439439
} // namespace const_global
440440

441+
namespace var_decl_ref_singleton {
442+
443+
static Class initSomeObject() { return nil; }
444+
static Class (*getSomeObjectClassSingleton)() = initSomeObject;
445+
446+
bool foo(NSString *obj) {
447+
return [obj isKindOfClass:getSomeObjectClassSingleton()];
448+
}
449+
450+
class Bar {
451+
public:
452+
Class someObject();
453+
static Class staticSomeObject();
454+
};
455+
typedef Class (Bar::*SomeObjectSingleton)();
456+
457+
bool bar(NSObject *obj, Bar *bar, SomeObjectSingleton someObjSingleton) {
458+
return [obj isKindOfClass:(bar->*someObjSingleton)()];
459+
// expected-warning@-1{{Call argument for parameter 'aClass' is unretained and unsafe}}
460+
}
461+
462+
bool baz(NSObject *obj) {
463+
Class (*someObjectSingleton)() = Bar::staticSomeObject;
464+
return [obj isKindOfClass:someObjectSingleton()];
465+
}
466+
467+
} // namespace var_decl_ref_singleton
468+
441469
namespace ns_retained_return_value {
442470

443471
NSString *provideNS() NS_RETURNS_RETAINED;

0 commit comments

Comments
 (0)