Debian Codegraph @

src/StringUtils.h

raw
1 /*
2 SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected]
3 SPDX-FileContributor: Sérgio Martins <[email protected]>
4
5 SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9
10 #ifndef CLANG_LAZY_STRING_UTILS_H
11 #define CLANG_LAZY_STRING_UTILS_H
12
13 #include "Utils.h"
14 #include "clazy_stl.h"
15
16 #include <clang/AST/Decl.h>
17 #include <clang/AST/DeclCXX.h>
18 #include <clang/AST/DeclarationName.h>
19 #include <clang/AST/Expr.h>
20 #include <clang/AST/ExprCXX.h>
21 #include <clang/AST/PrettyPrinter.h>
22 #include <clang/AST/Stmt.h>
23 #include <clang/AST/Type.h>
24 #include <clang/Basic/LLVM.h>
25 #include <clang/Basic/LangOptions.h>
26 #include <clang/Basic/OperatorKinds.h>
27 #include <clang/Basic/SourceLocation.h>
28 #include <clang/Basic/Specifiers.h>
29 #include <llvm/ADT/ArrayRef.h>
30 #include <llvm/ADT/StringRef.h>
31 #include <llvm/Support/Casting.h>
32 #include <llvm/Support/raw_ostream.h>
33
34 #include <string>
35 #include <vector>
36
37 namespace clang
38 {
39 class LangOpts;
40 class SourceManager;
41 }
42
43 namespace clazy
44 {
45 // Returns the class name.
46 // The name will not include any templates, so "QVector::iterator" would be returned for QVector<int>::iterator
47 // Use record->getQualifiedNameAsString() if you want the templates.
48
49 inline std::string classNameFor(const clang::CXXRecordDecl *record)
50 {
51 if (!record) {
52 return {};
53 }
54
55 const std::string name = record->getNameAsString();
56
57 if (const auto *p = record->getParent()) {
58 // TODO: Also append the namespace, when needed.
59 auto parentName = classNameFor(llvm::dyn_cast<clang::CXXRecordDecl>(p));
60 if (!parentName.empty()) {
61 return parentName + "::" + name;
62 }
63 }
64
65 return name;
66 }
67
68 inline std::string classNameFor(clang::CXXConstructorDecl *ctorDecl)
69 {
70 return classNameFor(ctorDecl->getParent());
71 }
72
73 inline std::string classNameFor(clang::CXXMethodDecl *method)
74 {
75 return method ? classNameFor(method->getParent()) : std::string();
76 }
77
78 inline std::string classNameFor(clang::CXXConstructExpr *expr)
79 {
80 return classNameFor(expr->getConstructor());
81 }
82
83 inline std::string classNameFor(clang::CXXOperatorCallExpr *call)
84 {
85 return call ? classNameFor(llvm::dyn_cast_or_null<clang::CXXMethodDecl>(call->getDirectCallee())) : std::string();
86 }
87
88 inline std::string classNameFor(clang::QualType qt)
89 {
90 qt = qt.getNonReferenceType().getUnqualifiedType();
91 const clang::Type *t = qt.getTypePtrOrNull();
92 if (!t) {
93 return {};
94 }
95
96 #if LLVM_VERSION_MAJOR < 22
97 if (clang::ElaboratedType::classof(t)) {
98 return classNameFor(static_cast<const clang::ElaboratedType *>(t)->getNamedType());
99 }
100 #endif
101
102 const clang::CXXRecordDecl *record = t->isRecordType() ? t->getAsCXXRecordDecl() : t->getPointeeCXXRecordDecl();
103 return classNameFor(record);
104 }
105
106 inline std::string classNameFor(clang::ParmVarDecl *param)
107 {
108 if (!param) {
109 return {};
110 }
111
112 return classNameFor(param->getType());
113 }
114
115 inline llvm::StringRef name(const clang::NamedDecl *decl)
116 {
117 if (decl->getDeclName().isIdentifier()) {
118 return decl->getName();
119 }
120
121 return "";
122 }
123
124 inline llvm::StringRef name(const clang::CXXMethodDecl *method)
125 {
126 auto op = method->getOverloadedOperator();
127 if (op == clang::OO_Subscript) {
128 return "operator[]";
129 }
130 if (op == clang::OO_LessLess) {
131 return "operator<<";
132 }
133 if (op == clang::OO_PlusEqual) {
134 return "operator+=";
135 }
136 if (op == clang::OO_EqualEqual) {
137 return "operator==";
138 }
139 if (op == clang::OO_Equal) {
140 return "operator=";
141 }
142
143 return name(static_cast<const clang::NamedDecl *>(method));
144 }
145
146 inline llvm::StringRef name(const clang::CXXConstructorDecl *decl)
147 {
148 return name(decl->getParent());
149 }
150
151 inline llvm::StringRef name(const clang::CXXDestructorDecl *decl)
152 {
153 return name(decl->getParent());
154 }
155
156 // Returns the type name with or without namespace, depending on how it was written by the user.
157 // If the user omitted the namespace then the return won't have namespace
158 inline std::string name(clang::QualType t, const clang::LangOptions &lo, bool asWritten)
159 {
160 clang::PrintingPolicy p(lo);
161 p.SuppressScope = asWritten;
162 return t.getAsString(p);
163 }
164
165 template<typename T>
166 inline bool isOfClass(T *node, llvm::StringRef className)
167 {
168 return node && classNameFor(node) == className;
169 }
170
171 inline bool functionIsOneOf(clang::FunctionDecl *func, const std::vector<llvm::StringRef> &functionNames)
172 {
173 return func && clazy::contains(functionNames, clazy::name(func));
174 }
175
176 inline bool classIsOneOf(clang::CXXRecordDecl *record, const std::vector<llvm::StringRef> &classNames)
177 {
178 return record && clazy::contains(classNames, clazy::name(record));
179 }
180
181 inline void printLocation(const clang::SourceManager &sm, clang::SourceLocation loc, bool newLine = true)
182 {
183 llvm::errs() << loc.printToString(sm);
184 if (newLine) {
185 llvm::errs() << "\n";
186 }
187 }
188
189 inline void printRange(const clang::SourceManager &sm, clang::SourceRange range, bool newLine = true)
190 {
191 printLocation(sm, range.getBegin(), false);
192 llvm::errs() << '-';
193 printLocation(sm, range.getEnd(), newLine);
194 }
195
196 inline void printLocation(const clang::SourceManager &sm, const clang::Stmt *s, bool newLine = true)
197 {
198 if (s) {
199 printLocation(sm, s->getBeginLoc(), newLine);
200 }
201 }
202
203 inline void printLocation(const clang::PresumedLoc &loc, bool newLine = true)
204 {
205 llvm::errs() << loc.getFilename() << ' ' << loc.getLine() << ':' << loc.getColumn();
206 if (newLine) {
207 llvm::errs() << "\n";
208 }
209 }
210
211 inline std::string qualifiedMethodName(clang::FunctionDecl *func)
212 {
213 if (!func) {
214 return {};
215 }
216
217 auto *method = clang::dyn_cast<clang::CXXMethodDecl>(func);
218 if (!method) {
219 return func->getQualifiedNameAsString();
220 }
221
222 // method->getQualifiedNameAsString() returns the name with template arguments, so do a little hack here
223 if (!method->getParent()) {
224 return "";
225 }
226
227 return method->getParent()->getNameAsString() + "::" + method->getNameAsString();
228 }
229
230 inline std::string qualifiedMethodName(clang::CallExpr *call)
231 {
232 return call ? qualifiedMethodName(call->getDirectCallee()) : std::string();
233 }
234
235 inline std::string accessString(clang::AccessSpecifier s)
236 {
237 switch (s) {
238 case clang::AccessSpecifier::AS_public:
239 return "public";
240 case clang::AccessSpecifier::AS_private:
241 return "private";
242 case clang::AccessSpecifier::AS_protected:
243 return "protected";
244 case clang::AccessSpecifier::AS_none:
245 return {};
246 }
247 return {};
248 }
249
250 /**
251 * Returns the type of qt without CV qualifiers or references.
252 * "const QString &" -> "QString"
253 */
254 inline std::string simpleTypeName(clang::QualType qt, const clang::LangOptions &lo)
255 {
256 const auto *t = qt.getTypePtrOrNull();
257 if (!t) {
258 return {};
259 }
260
261 #if LLVM_VERSION_MAJOR < 22
262 if (clang::ElaboratedType::classof(t)) {
263 qt = static_cast<const clang::ElaboratedType *>(t)->getNamedType();
264 }
265 #endif
266
267 return qt.getNonReferenceType().getUnqualifiedType().getAsString(clang::PrintingPolicy(lo));
268 }
269
270 inline std::string simpleTypeName(clang::ParmVarDecl *p, const clang::LangOptions &lo)
271 {
272 return p ? simpleTypeName(p->getType(), lo) : std::string();
273 }
274
275 inline std::string typeName(clang::QualType qt, const clang::LangOptions &lo, bool simpleName)
276 {
277 return simpleName ? simpleTypeName(qt, lo) : qt.getAsString(lo);
278 }
279
280 /**
281 * Returns the type name of the return type.
282 * If \a simpleName is true, any cv qualifications, ref or pointer are not taken into account, so
283 * const Foo & would be equal to Foo.
284 */
285 inline std::string returnTypeName(clang::CallExpr *call, const clang::LangOptions &lo, bool simpleName = true)
286 {
287 if (!call) {
288 return {};
289 }
290
291 clang::FunctionDecl *func = call->getDirectCallee();
292 return func ? clazy::typeName(func->getReturnType(), lo, simpleName) : std::string();
293 }
294
295 inline bool hasArgumentOfType(clang::FunctionDecl *func, llvm::StringRef typeName, const clang::LangOptions &lo, bool simpleName = true)
296 {
297 return std::ranges::any_of(Utils::functionParameters(func), [simpleName, lo, typeName](clang::ParmVarDecl *param) {
298 return clazy::typeName(param->getType(), lo, simpleName) == typeName;
299 });
300 }
301
302 /**
303 * Returns the type of an argument at index index without CV qualifiers or references.
304 * void foo(int a, const QString &);
305 * simpleArgTypeName(foo, 1, lo) would return "QString"
306 */
307 std::string simpleArgTypeName(clang::FunctionDecl *func, unsigned int index, const clang::LangOptions &);
308
309 /**
310 * Returns true if any of the function's arguments if of type simpleType
311 * By "simple" we mean without const or &, *
312 */
313 bool anyArgIsOfSimpleType(clang::FunctionDecl *func, const std::string &simpleType, const clang::LangOptions &);
314
315 /**
316 * Returns true if any of the function's arguments if of any of the types in simpleTypes
317 * By "simple" we mean without const or &, *
318 */
319 bool anyArgIsOfAnySimpleType(clang::FunctionDecl *func, const std::vector<std::string> &simpleTypes, const clang::LangOptions &);
320
321 inline std::string qtNamespaced(const std::string &className, const std::string &qtNamespace)
322 {
323 return qtNamespace.empty() ? className : qtNamespace + "::" + className;
324 }
325
326 inline void dump(const clang::SourceManager &sm, clang::Stmt *s)
327 {
328 if (!s) {
329 return;
330 }
331
332 llvm::errs() << "Start=" << s->getBeginLoc().printToString(sm) << "; end=" << s->getBeginLoc().printToString(sm) << "\n";
333
334 for (auto *child : s->children()) {
335 dump(sm, child);
336 }
337 }
338
339 }
340
341 #endif