-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringUtils.h
275 lines (222 loc) · 8.02 KB
/
StringUtils.h
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
This file is part of the clazy static checker.
Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Author: Sérgio Martins <sergio.martins@kdab.com>
Copyright (C) 2015 Sergio Martins <smartins@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef CLANG_LAZY_STRING_UTILS_H
#define CLANG_LAZY_STRING_UTILS_H
#include "checkmanager.h"
#include "Utils.h"
#include "HierarchyUtils.h"
#include "clazy_stl.h"
#include "clang/AST/PrettyPrinter.h"
#include <clang/Basic/LangOptions.h>
#include <clang/AST/ExprCXX.h>
#include <clang/AST/DeclCXX.h>
#include <string>
#include <vector>
namespace clang {
class LangOpts;
}
namespace StringUtils {
template <typename T>
inline std::string classNameFor(T *ctorDecl)
{
return std::string();
}
template <>
inline std::string classNameFor(clang::CXXConstructorDecl *ctorDecl)
{
return ctorDecl->getParent()->getNameAsString();
}
template <>
inline std::string classNameFor(clang::CXXMethodDecl *method)
{
return method ? method->getParent()->getNameAsString() : std::string();
}
template <>
inline std::string classNameFor(clang::CXXConstructExpr *expr)
{
return classNameFor(expr->getConstructor());
}
template <>
inline std::string classNameFor(clang::CXXOperatorCallExpr *call)
{
return call ? classNameFor(llvm::dyn_cast_or_null<clang::CXXMethodDecl>(call->getDirectCallee())) : std::string();
}
template <>
inline std::string classNameFor(clang::ParmVarDecl *param)
{
if (!param)
return {};
clang::QualType qt = param->getType();
const clang::Type *t = qt.getTypePtrOrNull();
if (!t)
return {};
const clang::CXXRecordDecl *record = t->isRecordType() ? t->getAsCXXRecordDecl()
: t->getPointeeCXXRecordDecl();
if (record)
return record->getNameAsString();
return {};
}
template <typename T>
inline bool isOfClass(T *node, const std::string &className)
{
return node && classNameFor<T>(node) == className;
}
inline bool functionIsOneOf(clang::FunctionDecl *func, const std::vector<std::string> &functionNames)
{
return func && clazy_std::contains(functionNames, func->getNameAsString());
}
inline bool classIsOneOf(clang::CXXRecordDecl *record, const std::vector<std::string> &classNames)
{
return record && clazy_std::contains(classNames, record->getNameAsString());
}
inline void printLocation(const clang::SourceManager &sm, clang::SourceLocation loc, bool newLine = true)
{
llvm::errs() << loc.printToString(sm);
if (newLine)
llvm::errs() << "\n";
}
inline void printRange(const clang::SourceManager &sm, clang::SourceRange range, bool newLine = true)
{
printLocation(sm, range.getBegin(), false);
llvm::errs() << '-';
printLocation(sm, range.getEnd(), newLine);
}
inline void printLocation(const clang::SourceManager &sm, const clang::Stmt *s, bool newLine = true)
{
if (s)
printLocation(sm, s->getLocStart(), newLine);
}
inline void printLocation(const clang::PresumedLoc &loc, bool newLine = true)
{
llvm::errs() << loc.getFilename() << ' ' << loc.getLine() << ':' << loc.getColumn();
if (newLine)
llvm::errs() << "\n";
}
inline std::string qualifiedMethodName(clang::FunctionDecl *func)
{
if (!func)
return {};
clang::CXXMethodDecl *method = clang::dyn_cast<clang::CXXMethodDecl>(func);
if (!method)
return func->getQualifiedNameAsString();
// method->getQualifiedNameAsString() returns the name with template arguments, so do a little hack here
if (!method || !method->getParent())
return "";
return method->getParent()->getNameAsString() + "::" + method->getNameAsString();
}
inline std::string qualifiedMethodName(clang::CallExpr *call)
{
return call ? qualifiedMethodName(call->getDirectCallee()) : std::string();
}
inline std::string methodName(clang::CallExpr *call)
{
if (!call)
return {};
clang::FunctionDecl *func = call->getDirectCallee();
return func ? func->getNameAsString() : std::string();
}
inline void printParents(clang::ParentMap *map, clang::Stmt *s)
{
int level = 0;
llvm::errs() << (s ? s->getStmtClassName() : nullptr) << "\n";
while (clang::Stmt *parent = HierarchyUtils::parent(map, s)) {
++level;
llvm::errs() << std::string(level, ' ') << parent->getStmtClassName() << "\n";
s = parent;
}
}
inline std::string accessString(clang::AccessSpecifier s)
{
switch (s)
{
case clang::AccessSpecifier::AS_public:
return "public";
case clang::AccessSpecifier::AS_private:
return "private";
case clang::AccessSpecifier::AS_protected:
return "protected";
case clang::AccessSpecifier::AS_none:
return {};
}
return {};
}
/**
* Returns the type of qt without CV qualifiers or references.
* "const QString &" -> "QString"
*/
inline std::string simpleTypeName(clang::QualType qt, const clang::LangOptions &lo)
{
return qt.getNonReferenceType().getUnqualifiedType().getAsString(clang::PrintingPolicy(lo));
}
inline std::string simpleTypeName(clang::ParmVarDecl *p, const clang::LangOptions &lo)
{
return p ? simpleTypeName(p->getType(), lo) : std::string();
}
inline std::string typeName(clang::QualType qt, const clang::LangOptions &lo, bool simpleName)
{
return simpleName ? simpleTypeName(qt, lo) : qt.getAsString(lo);
}
/**
* Returns the type name of the return type.
* If \a simpleName is true, any cv qualifications, ref or pointer are not taken into account, so
* const Foo & would be equal to Foo.
*/
inline std::string returnTypeName(clang::CallExpr *call, const clang::LangOptions &lo,
bool simpleName = true)
{
if (!call)
return {};
clang::FunctionDecl *func = call->getDirectCallee();
return func ? StringUtils::typeName(func->getReturnType(), lo, simpleName) : std::string();
}
inline bool hasArgumentOfType(clang::FunctionDecl *func, const std::string &typeName,
const clang::LangOptions &lo, bool simpleName = true)
{
return clazy_std::any_of(func->params(), [simpleName,lo,typeName](clang::ParmVarDecl *param) {
return StringUtils::typeName(param->getType(), lo, simpleName) == typeName;
});
}
/**
* Returns the type of an argument at index index without CV qualifiers or references.
* void foo(int a, const QString &);
* simpleArgTypeName(foo, 1, lo) would return "QString"
*/
std::string simpleArgTypeName(clang::FunctionDecl *func, unsigned int index, const clang::LangOptions &);
/**
* Returns true if any of the function's arguments if of type simpleType
* By "simple" we mean without const or &, *
*/
bool anyArgIsOfSimpleType(clang::FunctionDecl *func, const std::string &simpleType, const clang::LangOptions &);
/**
* Returns true if any of the function's arguments if of any of the types in simpleTypes
* By "simple" we mean without const or &, *
*/
bool anyArgIsOfAnySimpleType(clang::FunctionDecl *func, const std::vector<std::string> &simpleTypes, const clang::LangOptions &);
inline void dump(const clang::SourceManager &sm, clang::Stmt *s)
{
if (!s)
return;
llvm::errs() << "Start=" << s->getLocStart().printToString(sm)
<< "; end=" << s->getLocStart().printToString(sm)
<< "\n";
for (auto child : s->children())
dump(sm, child);
}
}
#endif