-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSherlockObservePairing.cpp
executable file
·76 lines (64 loc) · 1.99 KB
/
SherlockObservePairing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// SherlockObservePairing.cpp
// Sherlock
//
// Created by Robbie on 2017/2/6.
//
//
#include "SherlockObservePairing.hpp"
static int notRefCount;
static int KVORefCount;
static bool isIgnoreNot;
bool SherlockObservePairing::VisitStmt(Stmt *S)
{
const ObjCMessageExpr *ME = dyn_cast_or_null<ObjCMessageExpr>(S);
if (ME) {
this->checkNotificationPairing(ME);
this->checkKVOPairing(ME);
}
return true;
}
void SherlockObservePairing::checkNotificationPairing(const ObjCMessageExpr *ME)
{
ObjCInterfaceDecl *ID = ME->getReceiverInterface();
if (ID) {
StringRef DName = ID->getName();
if (DName.equals(StringRef("NSNotificationCenter"))) {
string selStr = ME->getSelector().getAsString();
if (selStr.find("addObserver") != string::npos) {
++notRefCount;
}
if (selStr.find("removeObserver") != string::npos
&& selStr.find("name") != string::npos) {
--notRefCount;
}
if (selStr.find("removeObserver") != string::npos
&& selStr.find("name") == string::npos) {
notRefCount = 0;
isIgnoreNot = true;
}
}
}
}
void SherlockObservePairing::checkKVOPairing(const ObjCMessageExpr *ME)
{
string selStr = ME->getSelector().getAsString();
if (selStr.find("addObserver") != string::npos
&& selStr.find("forKeyPath") != string::npos
&& selStr.find("options") != string::npos
&& selStr.find("context") != string::npos) {
++KVORefCount;
}
if (selStr.find("removeObserver") != string::npos
&& selStr.find("forKeyPath") != string::npos) {
--KVORefCount;
}
}
bool SherlockObservePairing::isKVOMissPairing()
{
return KVORefCount == 0 ? false : true;
}
bool SherlockObservePairing::isNotificationMissPairing()
{
return isIgnoreNot == true ? false : notRefCount == 0 ? false : true;
}