-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerlCheck.cpp
More file actions
187 lines (170 loc) · 6.49 KB
/
PerlCheck.cpp
File metadata and controls
187 lines (170 loc) · 6.49 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
#include "PerlCheck.h"
#include "PerlLiteralFunctionCheck.h"
#include "PerlUndefSetsvCheck.h"
#include "PerlUndefSetpvXCheck.h"
#include "PerlMortalFunctionCheck.h"
#include "clang-tidy/ClangTidy.h"
#include "clang-tidy/ClangTidyCheck.h"
#include "clang-tidy/ClangTidyModule.h"
#include "clang-tidy/ClangTidyModuleRegistry.h"
using namespace clang;
using namespace clang::tidy;
using namespace clang::ast_matchers;
namespace {
class PerlCheckModule : public ClangTidyModule
{
public:
void addCheckFactories(ClangTidyCheckFactories& CheckFactories) override
{
CheckFactories.registerCheckFactory(
"perl-literal-sv_setpvn",
[](llvm::StringRef Name, ClangTidyContext *Context){
return std::make_unique<PerlLiteralFunctionCheck>(
Name, Context, "sv_setpvn", "sv_setpvs",
1, 2, llvm::SmallVector{ 0, 1 });
});
CheckFactories.registerCheckFactory(
"perl-literal-sv_setpvn_mg",
[](llvm::StringRef Name, ClangTidyContext *Context){
return std::make_unique<PerlLiteralFunctionCheck>(
Name, Context, "sv_setpvn_mg", "sv_setpvs_mg",
1, 2, llvm::SmallVector{ 0, 1 });
});
CheckFactories.registerCheckFactory(
"perl-literal-newSVpvn",
[](llvm::StringRef Name, ClangTidyContext *Context){
return std::make_unique<PerlLiteralFunctionCheck>(
Name, Context, "newSVpvn", "newSVpvs",
0, 1, llvm::SmallVector{ 0 });
});
CheckFactories.registerCheckFactory(
"perl-literal-newSVpvn_flags",
[](llvm::StringRef Name, ClangTidyContext *Context){
return std::make_unique<PerlLiteralFunctionCheck>(
Name, Context, "newSVpvn_flags", "newSVpvs_flags",
0, 1, llvm::SmallVector{ 0, 2 });
});
CheckFactories.registerCheckFactory(
"perl-literal-hv_fetch",
[](llvm::StringRef Name, ClangTidyContext *Context){
return std::make_unique<PerlLiteralFunctionCheck>(
Name, Context, "hv_fetch", "hv_fetchs",
1, 2, llvm::SmallVector{ 0, 1, 3 });
});
CheckFactories.registerCheckFactory(
"perl-literal-sv_catpvn",
[](llvm::StringRef Name, ClangTidyContext *Context){
return std::make_unique<PerlLiteralFunctionCheck>(
Name, Context, "sv_catpvn", "sv_catpvs",
1, 2, llvm::SmallVector{ 0, 1 });
});
CheckFactories.registerCheckFactory(
"perl-literal-sv_catpvn_flags",
[](llvm::StringRef Name, ClangTidyContext *Context){
return std::make_unique<PerlLiteralFunctionCheck>(
Name, Context, "sv_catpvn_flags", "sv_catpvs_flags",
1, 2, llvm::SmallVector{ 0, 1, 3 });
});
CheckFactories.registerCheckFactory(
"perl-literal-sv_catpvn_nomg",
[](llvm::StringRef Name, ClangTidyContext *Context){
return std::make_unique<PerlLiteralFunctionCheck>(
Name, Context, "sv_catpvn_nomg", "sv_catpvs_nomg",
1, 2, llvm::SmallVector{ 0, 1 });
});
CheckFactories.registerCheckFactory(
"perl-literal-savepvn",
[](llvm::StringRef Name, ClangTidyContext *Context){
return std::make_unique<PerlLiteralFunctionCheck>(
Name, Context, "savepvn", "savepvs",
0, 1, llvm::SmallVector{ 0 });
});
CheckFactories.registerCheckFactory(
"perl-literal-get_cvn_flags",
[](llvm::StringRef Name, ClangTidyContext *Context){
return std::make_unique<PerlLiteralFunctionCheck>(
Name, Context, "get_cvn_flags", "get_cvs",
0, 1, llvm::SmallVector{ 0, 2 });
});
CheckFactories.registerCheck<PerlUndefSetsvCheck>("perl-undef-sv_setsv");
CheckFactories.registerCheckFactory(
"perl-undef-sv_setpv",
[](llvm::StringRef Name, ClangTidyContext *Context) {
return std::make_unique<PerlUndefSetpvXCheck>(
Name, Context, "sv_setpv", "Perl_sv_setpv");
});
CheckFactories.registerCheckFactory(
"perl-undef-sv_setpvn",
[](llvm::StringRef Name, ClangTidyContext *Context) {
return std::make_unique<PerlUndefSetpvXCheck>(
Name, Context, "sv_setpvn", "Perl_sv_setpvn");
});
CheckFactories.registerCheckFactory(
"perl-mortal-newSVpvn",
[](llvm::StringRef Name, ClangTidyContext *Context) {
return std::make_unique<PerlMortalFunctionCheck>(
Name, Context, "newSVpvn", "newSVpvn_flags",
-1, llvm::SmallVector{ 0, 1, -1 }, "SVs_TEMP"sv
);
});
#if 1
CheckFactories.registerCheckFactory(
"perl-mortal-newSVsv",
[](llvm::StringRef Name, ClangTidyContext *Context) {
return std::make_unique<PerlMortalFunctionCheck>(
Name, Context, "newSVsv", "sv_mortalcopy_flags",
-1, llvm::SmallVector{ 0, -1 }, "SV_GMAGIC|SV_NOSTEAL"sv, false
);
});
#endif
}
};
} // namespace
namespace clang::tidy {
// Register the module using this statically initialized variable.
static ClangTidyModuleRegistry::Add<::PerlCheckModule> perlCheckInit(
"perl-check", "Add checks perl source checks.");
// This anchor is used to force the linker to link in the generated object file and thus register the module.
volatile int perlCheckAnchorSource = 0;
} // namespace clang::tidy
namespace perl_check {
using perl_check::GetArgs;
std::string
MakeCall(llvm::StringRef NewName, const GetArgs &Args,
const llvm::SmallVector<int> &KeepArgs,
int InFlagsArgNum,
llvm::StringRef DefaultFlags, bool RealArgs) {
std::string result;
llvm::raw_string_ostream srepl{result};
srepl.reserveExtraSpace(80); // typically enough
srepl << NewName << "(";
srepl << Args(KeepArgs[0]);
for (auto i = std::next(KeepArgs.begin()); i != KeepArgs.end(); ++i) {
srepl << ", ";
if (*i == -1) {
if (InFlagsArgNum == -1) {
srepl << DefaultFlags;
}
else {
if (RealArgs) {
srepl << Args(*i);
}
else {
srepl << "...";
}
srepl << " | " << DefaultFlags;
}
}
else {
if (RealArgs) {
srepl << Args(*i);
}
else {
srepl << "...";
}
}
}
srepl << ")";
return result;
}
}