-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQTTMethodParamRule.cpp
More file actions
executable file
·193 lines (172 loc) · 5.65 KB
/
QTTMethodParamRule.cpp
File metadata and controls
executable file
·193 lines (172 loc) · 5.65 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "oclint/AbstractASTVisitorRule.h"
#include "oclint/RuleSet.h"
#include "oclint/RuleConfiguration.h"
#include <iostream>
using namespace std;
using namespace clang;
using namespace oclint;
class QTTMethodParamRule : public AbstractASTVisitorRule<QTTMethodParamRule>
{
private:
int _threshold;
string _teamname;
public:
virtual const string name() const override
{
return "方法格式规范";
}
virtual int priority() const override
{
return 3;
}
virtual const string category() const override
{
return _teamname + "方法格式";
}
#ifdef DOCGEN
virtual const std::string since() const override
{
return "0.13.1";
}
virtual const std::string description() const override
{
return ""; // TODO: fill in the description of the rule.
}
virtual const std::string example() const override
{
return R"rst(
.. code-block:: cpp
void example()
{
// TODO: modify the example for this rule.
}
)rst";
}
/* fill in the file name only when it does not match the rule identifier
virtual const std::string fileName() const override
{
return "";
}
*/
/* add each threshold's key, description, and its default value to the map.
virtual const std::map<std::string, std::string> thresholds() const override
{
std::map<std::string, std::string> thresholdMapping;
return thresholdMapping;
}
*/
/* add additional document for the rule, like references, notes, etc.
virtual const std::string additionalDocument() const override
{
return "";
}
*/
/* uncomment this method when the rule is enabled to be suppressed.
virtual bool enableSuppress() const override
{
return true;
}
*/
#endif
virtual void setUp() override {
_teamname = RuleConfiguration::stringForKey("TEAM_NAME", "趣头条");
_threshold = RuleConfiguration::intForKey("METHOD_PARAM", 3);
}
virtual void tearDown() override {}
static vector<string> split(const string &str, const string &pattern)
{
char * strc = new char[strlen(str.c_str())+1];
strcpy(strc, str.c_str()); //string转换成C-string
vector<string> res;
char* temp = strtok(strc, pattern.c_str());
while(temp != NULL)
{
res.push_back(string(temp));
temp = strtok(NULL, pattern.c_str());
}
delete[] strc;
return res;
}
static string formatObjcMethodName(string name) {
replace(name.begin(),name.end(),'\r',' ');
replace(name.begin(),name.end(),'\n',' ');
replace(name.begin(),name.end(),'\t',' ');
vector<string> vec = split(name,":");
vector<string> lineVec;
string lPart,rPart;
size_t nMaxDotPos = 0,pos;
for(int i=0;i<vec.size();i++){
string str = vec.at(i);
trim(str);
if(i==0){
lPart = str;
continue;
}
string tmpStr;
pos = str.find(")");
rPart=str.substr(0,pos+1);
tmpStr = str.substr(pos+1,str.length()-pos-1);
trim(tmpStr);
pos = tmpStr.find(" ");
if(pos != string::npos){
rPart+=tmpStr.substr(0,pos+1);
lineVec.push_back(trim(lPart)+":"+trim(rPart));
nMaxDotPos = (lPart.length()>nMaxDotPos?lPart.length():nMaxDotPos);
lPart = tmpStr.substr(pos+1,tmpStr.length()-pos-1);
}
else{
rPart+=tmpStr;
lineVec.push_back(trim(lPart)+":"+trim(rPart));
}
}
if(!lineVec.size() && lPart.length()){
lineVec.push_back(trim(lPart));
}
string joinedStr;
if(lineVec.size()==1){
joinedStr = lineVec.at(0);
}
else{
for(size_t i=0;i!=lineVec.size();i++){
string line = lineVec.at(i);
pos = line.find(":");
string tmpStr(i==0?0:nMaxDotPos-pos,' ');
joinedStr+=tmpStr+line;
if(i!=lineVec.size()-1)
joinedStr+="\n";
}
}
return joinedStr;
}
static inline string <rim(std::string &s) {
s.erase(s.begin(), find_if(s.begin(), s.end(),
not1(ptr_fun<int, int>(isspace))));
return s;
}
// trim from end
static inline string &rtrim(string &s) {
s.erase(find_if(s.rbegin(), s.rend(),
not1(ptr_fun<int, int>(isspace))).base(), s.end());
return s;
}
// trim from both ends
static inline string &trim(string &s) {
return ltrim(rtrim(s));
}
bool VisitObjCMethodDecl(ObjCMethodDecl *node)
{
string name = node->getNameAsString();
if( node->param_size()>_threshold){
string methodDeclStr;
ASTContext *context = _carrier->getASTContext();
SourceLocation begin = node->getSourceRange().getBegin();
SourceLocation end = node->getSourceRange().getEnd(); methodDeclStr.assign(context->getSourceManager().getCharacterData(begin),end.getRawEncoding()-node->getSourceRange().getBegin().getRawEncoding());
string replaceName = formatObjcMethodName(methodDeclStr);
if(methodDeclStr.find(replaceName)){
addViolation(node, this, "方法参数超过"+to_string(_threshold)+"个,需要折行并冒号对齐");
}
}
return true;
}
};
static RuleSet rules(new QTTMethodParamRule());