-
Notifications
You must be signed in to change notification settings - Fork 491
/
ObjCObjectSubscriptingRule.cpp
126 lines (102 loc) · 3.58 KB
/
ObjCObjectSubscriptingRule.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "oclint/AbstractASTVisitorRule.h"
#include "oclint/RuleSet.h"
#include "oclint/util/StdUtil.h"
using namespace std;
using namespace clang;
using namespace oclint;
class ObjCObjectSubscriptingRule : public AbstractASTVisitorRule<ObjCObjectSubscriptingRule>
{
private:
bool isArrayGetSelector(Expr *expr, string &selectorString)
{
vector<string> arrayGetSelectors;
arrayGetSelectors.push_back("objectAtIndex:");
arrayGetSelectors.push_back("objectAtIndexedSubscript:");
return expr->getType().getAsString() == "NSArray *" &&
vectorContains<string>(selectorString, arrayGetSelectors);
}
bool isArraySetSelector(Expr *expr, string &selectorString)
{
vector<string> arraySetSelectors;
arraySetSelectors.push_back("replaceObjectAtIndex:withObject:");
arraySetSelectors.push_back("setObject:atIndexedSubscript:");
return expr->getType().getAsString() == "NSMutableArray *" &&
vectorContains<string>(selectorString, arraySetSelectors);
}
bool isDictionaryGetSelector(Expr *expr, string &selectorString)
{
vector<string> dictGetSelectors;
dictGetSelectors.push_back("objectForKey:");
dictGetSelectors.push_back("objectForKeyedSubscript:");
return expr->getType().getAsString() == "NSDictionary *" &&
vectorContains<string>(selectorString, dictGetSelectors);
}
bool isDictionarySetSelector(Expr *expr, string &selectorString)
{
vector<string> dictSetSelectors;
dictSetSelectors.push_back("setObject:forKey:");
dictSetSelectors.push_back("setObject:forKeyedSubscript:");
return expr->getType().getAsString() == "NSMutableDictionary *" &&
vectorContains<string>(selectorString, dictSetSelectors);
}
public:
virtual const string name() const override
{
return "use object subscripting";
}
virtual int priority() const override
{
return 3;
}
virtual const string category() const override
{
return "migration";
}
virtual unsigned int supportedLanguages() const override
{
return LANG_OBJC;
}
#ifdef DOCGEN
virtual const std::string since() const override
{
return "0.7";
}
virtual const std::string description() const override
{
return "This rule locates the places that can be migrated to the "
"new Objective-C literals with object subscripting.";
}
virtual const std::string fileName() const override
{
return "ObjCObjectSubscriptingRule.cpp";
}
virtual const std::string example() const override
{
return R"rst(
.. code-block:: objective-c
void aMethod(NSArray *a, NSDictionary *d)
{
id item = [a objectAtIndex:0];
// id item = a[0];
id item = [d objectForKey:@1];
// id item = d[@1];
}
)rst";
}
#endif
bool VisitObjCMessageExpr(ObjCMessageExpr *objCMsgExpr)
{
Expr *receiverExpr = objCMsgExpr->getInstanceReceiver();
string selectorString = objCMsgExpr->getSelector().getAsString();
if (receiverExpr && isa<ImplicitCastExpr>(receiverExpr) &&
(isArrayGetSelector(receiverExpr, selectorString) ||
isArraySetSelector(receiverExpr, selectorString) ||
isDictionaryGetSelector(receiverExpr, selectorString) ||
isDictionarySetSelector(receiverExpr, selectorString)))
{
addViolation(objCMsgExpr, this);
}
return true;
}
};
static RuleSet rules(new ObjCObjectSubscriptingRule());