-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathUtils.h
271 lines (204 loc) · 10.7 KB
/
Utils.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
/*
This file is part of the clang-lazy static checker.
Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
Author: Sérgio Martins <[email protected]>
Copyright (C) 2015 Sergio Martins <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#ifndef MOREWARNINGS_UTILS_H
#define MOREWARNINGS_UTILS_H
#include <clang/AST/DeclCXX.h>
#include <clang/AST/Expr.h>
#include <clang/AST/ExprCXX.h>
#include <clang/AST/Stmt.h>
#include <clang/AST/DeclTemplate.h>
#include <string>
#include <vector>
#include <map>
namespace clang {
class CXXNamedCastExpr;
class CXXRecordDecl;
class CXXMemberCallExpr;
class CXXConstructExpr;
class CompilerInstance;
class ClassTemplateSpecializationDecl;
class Decl;
class ParentMap;
class Stmt;
class SourceLocation;
class ExprWithCleanups;
class ValueDecl;
class ConditionalOperator;
class CXXMethodDecl;
}
namespace Utils {
/// Returns true if the class inherits from QObject
bool isQObject(clang::CXXRecordDecl *decl);
/// Returns true if fullString ends in ending
bool hasEnding(const std::string &fullString, const std::string &ending);
/// Returns true if childDecl is a descent from parentDecl
bool isChildOf(clang::CXXRecordDecl *childDecl, clang::CXXRecordDecl *parentDecl);
/// Returns true if the class has at least one constexpr ctor
bool hasConstexprCtor(clang::CXXRecordDecl *decl);
/// Returns the type we're casting *from*
clang::CXXRecordDecl * namedCastInnerDecl(clang::CXXNamedCastExpr *staticOrDynamicCast);
/// Returns the type we're casting *to*
clang::CXXRecordDecl * namedCastOuterDecl(clang::CXXNamedCastExpr *staticOrDynamicCast);
/// Returns the class declaration from a variable declaration
// So, if the var decl is "Foo f"; it returns the declaration of Foo
clang::CXXRecordDecl * recordFromVarDecl(clang::Decl *);
/// Returns the template specialization from a variable declaration
// So, if the var decl is "QList<Foo> f;", returns the template specialization QList<Foo>
clang::ClassTemplateSpecializationDecl * templateSpecializationFromVarDecl(clang::Decl *);
/// Returns true if stmt is inside a function named name
//bool statementIsInFunc(clang::ParentMap *, clang::Stmt *stmt, const std::string &name);
/// Returns true if stm is parent of a member function call named "name"
bool isParentOfMemberFunctionCall(clang::Stmt *stm, const std::string &name);
/// Returns true if all the child member function calls are const functions.
bool allChildrenMemberCallsConst(clang::Stmt *stm);
/// Returns true if at least a UnaryOperator, BinaryOperator or non-const function call is found
bool childsHaveSideEffects(clang::Stmt *stm);
/// Receives a member call, such as "list.reserve()" and returns the declaration of the variable list
// such as "QList<F> list"
clang::ValueDecl * valueDeclForMemberCall(clang::CXXMemberCallExpr *);
/// Receives an operator call, such as "list << fooo" and returns the declaration of the variable list
// such as "QList<F> list"
clang::ValueDecl * valueDeclForOperatorCall(clang::CXXOperatorCallExpr *);
/// Returns true if a decl is inside a function, instead of say a class field.
/// This returns true if "QList<int> l;" is a localvariable, instead of being a class field such
/// as struct Foo { QList<int> l; }
bool isValueDeclInFunctionContext(clang::ValueDecl *);
// Returns true of this value decl is a member variable of a class or struct
// returns null if not
clang::CXXRecordDecl* isMemberVariable(clang::ValueDecl *);
/// Recursively goes through stmt's children and returns true if it finds a "break", "continue" or a "return" stmt
/// All child statements that are on a source code line <
/// If onlyBeforThisLoc is valid, then this function will only return true if the break/return/continue happens before
bool loopCanBeInterrupted(clang::Stmt *loop, clang::CompilerInstance &ci, const clang::SourceLocation &onlyBeforeThisLoc);
// Returns true if the class derived is or descends from a class named parent
bool descendsFrom(clang::CXXRecordDecl *derived, const std::string &parentName);
std::string qualifiedNameForDeclarationOfMemberExr(clang::MemberExpr *memberExpr);
// Returns true if a body of statements contains a non const member call on object declared by varDecl
// For example:
// Foo foo; // this is the varDecl
// while (bar) { foo.setValue(); // non-const call }
bool containsNonConstMemberCall(clang::Stmt *body, const clang::VarDecl *varDecl);
// Returns true if there's an assignment to varDecl in body
bool containsAssignment(clang::Stmt *body, const clang::VarDecl *varDecl);
// Returns true if a body of statements contains a function call that takes our variable (varDecl)
// By ref or pointer
bool containsCallByRef(clang::Stmt *body, const clang::VarDecl *varDecl);
std::vector<std::string> splitString(const std::string &str, char separator);
template <typename T>
void getChilds2(clang::Stmt *stmt, std::vector<T*> &result_list, int depth = -1)
{
if (!stmt)
return;
auto cexpr = llvm::dyn_cast<T>(stmt);
if (cexpr)
result_list.push_back(cexpr);
auto it = stmt->child_begin();
auto end = stmt->child_end();
if (depth > 0 || depth == -1) {
if (depth > 0)
--depth;
for (; it != end; ++it) {
getChilds2(*it, result_list, depth);
}
}
}
// QString::fromLatin1("foo") -> true
// QString::fromLatin1("foo", 1) -> false
bool callHasDefaultArguments(clang::CallExpr *expr);
// If there's a child of type StringLiteral, returns true
// if allowEmpty is false, "" will be ignored
bool containsStringLiteral(clang::Stmt *, bool allowEmpty = true, int depth = -1);
// If depth = 0, return s
// If depth = 1, returns parent of s
// etc.
clang::Stmt* parent(clang::ParentMap *, clang::Stmt *s, uint depth = 1);
// Returns the first parent of type T, with max depth depth
template <typename T>
T* getFirstParentOfType(clang::ParentMap *pmap, clang::Stmt *s, uint depth = -1)
{
if (!s)
return nullptr;
if (auto t = clang::dyn_cast<T>(s))
return t;
if (depth == 0)
return nullptr;
--depth;
return getFirstParentOfType<T>(pmap, parent(pmap, s), depth);
}
bool isInsideOperatorCall(clang::ParentMap *map, clang::Stmt *s, const std::vector<std::string> &anyOf);
bool insideCTORCall(clang::ParentMap *map, clang::Stmt *s, const std::vector<std::string> &anyOf);
/// Goes into a statement and returns it's childs of type T
/// It only goes down 1 level of children, except if there's a ExprWithCleanups, which we unpeal
template <typename T>
void getChilds(clang::Stmt *stm, std::vector<T*> &result_list)
{
if (!stm)
return;
auto expr = clang::dyn_cast<T>(stm);
if (expr) {
result_list.push_back(expr);
return;
}
for (auto it = stm->child_begin(), e = stm->child_end(); it != e; ++it) {
if (*it == nullptr) // Can happen
continue;
auto expr = clang::dyn_cast<T>(*it);
if (expr) {
result_list.push_back(expr);
continue;
}
auto cleanups = clang::dyn_cast<clang::ExprWithCleanups>(*it);
if (cleanups) {
getChilds<T>(cleanups, result_list);
}
}
}
// returns true if the ternary operator has two string literal arguments, such as:
// foo ? "bar" : "baz"
bool ternaryOperatorIsOfStringLiteral(clang::ConditionalOperator*);
bool isAssignOperator(clang::CXXOperatorCallExpr *op, const std::string &className, const std::string &argumentType);
bool isImplicitCastTo(clang::Stmt *, const std::string &);
clang::ClassTemplateSpecializationDecl *templateDecl(clang::Decl *decl);
std::vector<clang::Stmt*> childs(clang::Stmt *parent);
clang::Stmt * getFirstChildAtDepth(clang::Stmt *parent, uint depth);
bool presumedLocationsEqual(const clang::PresumedLoc &l1, const clang::PresumedLoc &l2);
// Returns the body of a for, while or do loop
clang::Stmt *bodyFromLoop(clang::Stmt*);
// Returns the list of methods with name methodName that the class/struct record contains
std::vector<clang::CXXMethodDecl*> methodsFromString(const clang::CXXRecordDecl *record, const std::string &methodName);
// Returns the most derived class. (CXXMemberCallExpr::getRecordDecl() return the first base class with the method)
// The returned callee is the name of the variable on which the member call was made:
// o1->foo() => "o1"
// foo() => "this"
const clang::CXXRecordDecl* recordForMemberCall(clang::CXXMemberCallExpr *call, std::string &implicitCallee);
// The list of namespaces, classes, inner classes. The inner ones are at the beginning of the list
std::vector<clang::DeclContext *> contextsForDecl(clang::DeclContext *);
// Returns fully/smi-fully qualified name for a method
// but doesn't overqualify with namespaces which we're already in.
// if currentScope == nullptr will return a fully qualified name
std::string getMostNeededQualifiedName(clang::CXXMethodDecl *method, clang::DeclContext *currentScope);
// Returns true, if in a specific context, we can take the address of a method
// for example doing &ClassName::SomePrivateMember might not be possible if the member is private
// but might be possible if we're in a context which is friend of ClassName
// Or it might be protected but context is a derived class
bool canTakeAddressOf(clang::CXXMethodDecl *method, clang::DeclContext *context);
}
#endif