| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015-2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef CLAZY_QT_UTILS_H |
| 8 |
#define CLAZY_QT_UTILS_H |
| 9 |
|
| 10 |
#include "StringUtils.h" |
| 11 |
#include "Utils.h" |
| 12 |
#include "clazy_stl.h" |
| 13 |
|
| 14 |
#include <clang/AST/ASTContext.h> |
| 15 |
#include <clang/AST/Decl.h> |
| 16 |
#include <clang/AST/DeclCXX.h> |
| 17 |
#include <clang/AST/DeclTemplate.h> |
| 18 |
#include <clang/AST/Expr.h> |
| 19 |
#include <clang/AST/OperationKinds.h> |
| 20 |
#include <clang/AST/Stmt.h> |
| 21 |
#include <clang/AST/TemplateBase.h> |
| 22 |
#include <clang/AST/Type.h> |
| 23 |
#include <clang/Basic/SourceLocation.h> |
| 24 |
#include <llvm/ADT/StringRef.h> |
| 25 |
#include <llvm/Support/Casting.h> |
| 26 |
|
| 27 |
#include <string> |
| 28 |
#include <unordered_map> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
namespace clang |
| 32 |
{ |
| 33 |
class CXXRecordDecl; |
| 34 |
class CompilerInstance; |
| 35 |
class Type; |
| 36 |
class CXXMemberCallExpr; |
| 37 |
class CallExpr; |
| 38 |
class ValueDecl; |
| 39 |
class LangOptions; |
| 40 |
class QualType; |
| 41 |
class VarDecl; |
| 42 |
class SourceLocation; |
| 43 |
class FunctionDecl; |
| 44 |
class UnaryOperator; |
| 45 |
class CXXMethodDecl; |
| 46 |
class Expr; |
| 47 |
class PreprocessorOptions; |
| 48 |
class SourceManager; |
| 49 |
} |
| 50 |
|
| 51 |
struct StmtBodyRange; |
| 52 |
|
| 53 |
namespace clazy |
| 54 |
{ |
| 55 |
/** |
| 56 |
* Returns true if the class is a Qt class which can be iterated with foreach. |
| 57 |
* Which means all containers and also stuff like QAssociativeIterable. |
| 58 |
*/ |
| 59 |
bool isQtIterableClass(clang::CXXRecordDecl *record); |
| 60 |
|
| 61 |
/** |
| 62 |
* Overload. |
| 63 |
*/ |
| 64 |
bool isQtIterableClass(llvm::StringRef className); |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns true if the method is of QMetaMethod type |
| 68 |
*/ |
| 69 |
bool isQMetaMethod(clang::CallExpr *call, unsigned int argIndex, const std::string &qtNamespace); |
| 70 |
/** |
| 71 |
* Returns true if the class is a Qt class which can be iterated with foreach and also implicitly shared. |
| 72 |
*/ |
| 73 |
bool isQtCOWIterableClass(clang::CXXRecordDecl *record, const std::string &qtNamespace); |
| 74 |
|
| 75 |
/** |
| 76 |
* Overload. |
| 77 |
*/ |
| 78 |
bool isQtCOWIterableClass(const std::string &className, const std::string &qtNamespace); |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns if the iterators belongs to a COW container |
| 82 |
*/ |
| 83 |
inline bool isQtCOWIterator(clang::CXXRecordDecl *itRecord, const std::string &qtNamespace) |
| 84 |
{ |
| 85 |
if (!itRecord) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
auto *parent = llvm::dyn_cast_or_null<clang::CXXRecordDecl>(itRecord->getParent()); |
| 90 |
return parent && clazy::isQtCOWIterableClass(parent, qtNamespace); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns true if the class is a Qt class which is an associative container (QHash, QMap, QSet) |
| 95 |
*/ |
| 96 |
bool isQtAssociativeContainer(clang::CXXRecordDecl *record); |
| 97 |
|
| 98 |
/** |
| 99 |
* Overload. |
| 100 |
*/ |
| 101 |
bool isQtAssociativeContainer(llvm::StringRef className); |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns a list of Qt containers. |
| 105 |
*/ |
| 106 |
const std::vector<llvm::StringRef> &qtContainers(); |
| 107 |
|
| 108 |
/** |
| 109 |
* Returns a list of implicitly shared Qt containers. |
| 110 |
*/ |
| 111 |
const std::vector<llvm::StringRef> &qtCOWContainers(); |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns a map with the list of method names that detach each container. |
| 115 |
*/ |
| 116 |
std::unordered_map<std::string, std::vector<llvm::StringRef>> detachingMethods(); |
| 117 |
|
| 118 |
/** |
| 119 |
* Returns a map with the list of method names that detach each container, but only those methods |
| 120 |
* with const counterparts. |
| 121 |
*/ |
| 122 |
using MemberConstcounterpartPair = std::pair<llvm::StringRef, llvm::StringRef>; |
| 123 |
std::unordered_map<std::string, std::vector<MemberConstcounterpartPair>> detachingMethodsWithConstCounterParts(); |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns true if a type represents a Qt container class. |
| 127 |
*/ |
| 128 |
bool isQtContainer(clang::QualType); |
| 129 |
|
| 130 |
bool isQtContainer(const clang::CXXRecordDecl *); |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns true if -DQT_BOOTSTRAPPED was passed to the compiler |
| 134 |
*/ |
| 135 |
bool isBootstrapping(const clang::PreprocessorOptions &ppOpts); |
| 136 |
|
| 137 |
/** |
| 138 |
* Returns if decl is or derives from QObject |
| 139 |
*/ |
| 140 |
bool isQObject(const clang::CXXRecordDecl *decl, const std::string &qtNamespace); |
| 141 |
|
| 142 |
/** |
| 143 |
* Convertible means that a signal with of type source can connect to a signal/slot of type target |
| 144 |
*/ |
| 145 |
bool isConvertibleTo(const clang::Type *source, const clang::Type *target); |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns true if \a loc is in a foreach macro |
| 149 |
*/ |
| 150 |
bool isInForeach(const clang::ASTContext *context, clang::SourceLocation loc); |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns true if \a record is a java-style iterator |
| 154 |
*/ |
| 155 |
bool isJavaIterator(clang::CXXRecordDecl *record); |
| 156 |
|
| 157 |
bool isJavaIterator(clang::CXXMemberCallExpr *call); |
| 158 |
|
| 159 |
/** |
| 160 |
* Returns true if a class has a ctor that has a parameter of type paramType. |
| 161 |
* ok will be false if an error occurred, or if the record is a fwd declaration, which isn't enough |
| 162 |
* for we to find out the signature. |
| 163 |
* numCtors will have the number of constructors analyzed. |
| 164 |
*/ |
| 165 |
bool recordHasCtorWithParam(clang::CXXRecordDecl *record, const std::string ¶mType, bool &ok, int &numCtors); |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns true if recordDecl is one of the container classes that supports reserve(), such |
| 169 |
* as QList, QVector, etc. |
| 170 |
*/ |
| 171 |
bool isAReserveClass(clang::CXXRecordDecl *recordDecl, const std::string &qtNamespace); |
| 172 |
|
| 173 |
/** |
| 174 |
* Returns true if record is a shared pointer (boost, Qt or stl only). |
| 175 |
*/ |
| 176 |
bool isSharedPointer(clang::CXXRecordDecl *record, const std::string &qtNamespace); |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns the base class that inherits QObject. |
| 180 |
* Useful when the class has more than one base class and we're only interested in the QObject one. |
| 181 |
*/ |
| 182 |
clang::CXXRecordDecl *getQObjectBaseClass(clang::CXXRecordDecl *recordDecl, const std::string &qtNamespace); |
| 183 |
|
| 184 |
/** |
| 185 |
* Returns true if the function declaration is QObject::connect(). |
| 186 |
*/ |
| 187 |
bool isConnect(clang::FunctionDecl *func, const std::string &qtNamespace); |
| 188 |
/** |
| 189 |
* Returns true if the function declaration represents a QObject::connect() using the new Qt5 |
| 190 |
* (pointer to member) syntax. |
| 191 |
* |
| 192 |
* It's assumed that func represents a connect(). |
| 193 |
*/ |
| 194 |
bool connectHasPMFStyle(clang::FunctionDecl *func); |
| 195 |
|
| 196 |
/** |
| 197 |
* Returns the method referenced by a PMF-style connect for the specified connect() call. |
| 198 |
*/ |
| 199 |
clang::CXXMethodDecl *pmfFromConnect(clang::CallExpr *funcCall, int argIndex, const std::string &qtNamespace); |
| 200 |
|
| 201 |
clang::CXXMethodDecl *pmfFromExpr(clang::Expr *e, const std::string &qtNamespace); |
| 202 |
clang::CXXMethodDecl *pmfFromUnary(clang::UnaryOperator *uo); |
| 203 |
|
| 204 |
/** |
| 205 |
* Returns the varDecl for the 1st argument in a connect call |
| 206 |
*/ |
| 207 |
clang::ValueDecl *signalSenderForConnect(clang::CallExpr *call); |
| 208 |
|
| 209 |
/** |
| 210 |
* Returns the varDecl for 3rd argument in connects that are passed an explicit |
| 211 |
* receiver or context QObject. |
| 212 |
*/ |
| 213 |
clang::ValueDecl *signalReceiverForConnect(clang::CallExpr *call); |
| 214 |
|
| 215 |
/** |
| 216 |
* Returns the receiver method, in a PMF connect statement. |
| 217 |
* The method can be a slot or a signal. If it's a lambda or functor nullptr is returned |
| 218 |
*/ |
| 219 |
inline clang::CXXMethodDecl *receiverMethodForConnect(clang::CallExpr *call, const std::string &qtNamespace) |
| 220 |
{ |
| 221 |
clang::CXXMethodDecl *receiverMethod = clazy::pmfFromConnect(call, 2, qtNamespace); |
| 222 |
if (receiverMethod) { |
| 223 |
return receiverMethod; |
| 224 |
} |
| 225 |
|
| 226 |
// It's either third or fourth argument |
| 227 |
return clazy::pmfFromConnect(call, 3, qtNamespace); |
| 228 |
} |
| 229 |
|
| 230 |
inline bool isUIFile(clang::SourceLocation loc, const clang::SourceManager &sm) |
| 231 |
{ |
| 232 |
const std::string filename = Utils::filenameForLoc(loc, sm); |
| 233 |
return clazy::startsWith(filename, "ui_") && clazy::endsWith(filename, ".h"); |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
#endif |