| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016-2017 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef CLAZY_TYPE_UTILS_H |
| 8 |
#define CLAZY_TYPE_UTILS_H |
| 9 |
|
| 10 |
#include <clang/AST/ASTContext.h> |
| 11 |
#include <clang/AST/Decl.h> |
| 12 |
#include <clang/AST/DeclCXX.h> |
| 13 |
#include <clang/AST/Expr.h> |
| 14 |
#include <clang/AST/StmtCXX.h> |
| 15 |
#include <clang/AST/Type.h> |
| 16 |
#include <llvm/Support/Casting.h> |
| 17 |
|
| 18 |
#include <string> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
namespace clang |
| 22 |
{ |
| 23 |
class CompilerInstance; |
| 24 |
class Expr; |
| 25 |
class LangOptions; |
| 26 |
class QualType; |
| 27 |
class Stmt; |
| 28 |
class VarDecl; |
| 29 |
class Type; |
| 30 |
class CXXRecordDecl; |
| 31 |
class CXXBaseSpecifier; |
| 32 |
} |
| 33 |
|
| 34 |
class ClazyContext; |
| 35 |
|
| 36 |
namespace clazy |
| 37 |
{ |
| 38 |
/** |
| 39 |
* Returns the sizeof(void*) for the platform we're compiling for, in bits. |
| 40 |
*/ |
| 41 |
inline int sizeOfPointer(const clang::ASTContext *context, const clang::QualType qt) |
| 42 |
{ |
| 43 |
if (!qt.getTypePtrOrNull()) { |
| 44 |
return -1; |
| 45 |
} |
| 46 |
// HACK: What's a better way of getting the size of a pointer ? |
| 47 |
return context->getTypeSize(context->getPointerType(qt)); |
| 48 |
} |
| 49 |
|
| 50 |
struct QualTypeClassification { |
| 51 |
bool isConst = false; |
| 52 |
bool isReference = false; |
| 53 |
bool isBig = false; |
| 54 |
bool isNonTriviallyCopyable = false; |
| 55 |
bool passBigTypeByConstRef = false; |
| 56 |
bool passNonTriviallyCopyableByConstRef = false; |
| 57 |
bool passSmallTrivialByValue = false; |
| 58 |
int size_of_T = 0; |
| 59 |
}; |
| 60 |
|
| 61 |
/** |
| 62 |
* Classifies a QualType, for example: |
| 63 |
* |
| 64 |
* This function is useful to know if a type should be passed by value or const-ref. |
| 65 |
* The optional parameter body is in order to advise non-const-ref -> value, since the body |
| 66 |
* needs to be inspected to see if we that would compile. |
| 67 |
*/ |
| 68 |
bool classifyQualType(const ClazyContext *context, |
| 69 |
clang::QualType qualType, |
| 70 |
const clang::VarDecl *varDecl, |
| 71 |
QualTypeClassification &classification, |
| 72 |
clang::Stmt *body = nullptr); |
| 73 |
|
| 74 |
/** |
| 75 |
* @brief Lighter version of classifyQualType in case you just want to know if it's small and trivially copyable&destructible |
| 76 |
* @param context The clazy context |
| 77 |
* @param qualType The QualType we're testing. |
| 78 |
* @return true if the type specified by QualType (or its pointee) are small and trivially copyable/destructible. |
| 79 |
*/ |
| 80 |
bool isSmallTrivial(const ClazyContext *context, clang::QualType qualType); |
| 81 |
|
| 82 |
/** |
| 83 |
* If qt is a reference, return it without a reference. |
| 84 |
* If qt is not a reference, return qt. |
| 85 |
* |
| 86 |
* This is useful because sometimes you have an argument like "const QString &", but qualType.isConstQualified() |
| 87 |
* returns false. Must go through qualType->getPointeeType().isConstQualified(). |
| 88 |
*/ |
| 89 |
inline clang::QualType unrefQualType(const clang::QualType qualType) |
| 90 |
{ |
| 91 |
const clang::Type *t = qualType.getTypePtrOrNull(); |
| 92 |
return (t && t->isReferenceType()) ? t->getPointeeType() : qualType; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* If qt is a pointer or ref, return it without * or &. |
| 97 |
* Otherwise return qt unchanged. |
| 98 |
*/ |
| 99 |
inline clang::QualType pointeeQualType(const clang::QualType qualType) |
| 100 |
{ |
| 101 |
// TODO: Make this recursive when we need to remove more than one level of * |
| 102 |
const clang::Type *t = qualType.getTypePtrOrNull(); |
| 103 |
return (t && (t->isReferenceType() || t->isPointerType())) ? t->getPointeeType() : qualType; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns if @p arg is stack or heap allocated. |
| 108 |
* true means it is. false means it either isn't or the situation was too complex to judge. |
| 109 |
* type is the qualified type |
| 110 |
*/ |
| 111 |
void heapOrStackAllocated(clang::Expr *arg, const std::string &type, const clang::LangOptions &lo, bool &isStack, bool &isHeap); |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns true if t is an AutoType that can't be deduced. |
| 115 |
*/ |
| 116 |
inline bool isUndeducibleAuto(const clang::Type *t) |
| 117 |
{ |
| 118 |
if (!t) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
const auto *at = llvm::dyn_cast<clang::AutoType>(t); |
| 123 |
return at && at->getDeducedType().isNull(); |
| 124 |
} |
| 125 |
|
| 126 |
inline const clang::Type *unpealAuto(clang::QualType q) |
| 127 |
{ |
| 128 |
if (q.isNull()) { |
| 129 |
return nullptr; |
| 130 |
} |
| 131 |
|
| 132 |
if (const auto *t = llvm::dyn_cast<clang::AutoType>(q.getTypePtr())) { |
| 133 |
return t->getDeducedType().getTypePtrOrNull(); |
| 134 |
} |
| 135 |
|
| 136 |
return q.getTypePtr(); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns true if childDecl is a descent from parentDecl |
| 141 |
**/ |
| 142 |
bool derivesFrom(const clang::CXXRecordDecl *derived, const clang::CXXRecordDecl *possibleBase, std::vector<clang::CXXRecordDecl *> *baseClasses = nullptr); |
| 143 |
|
| 144 |
// Overload |
| 145 |
bool derivesFrom(const clang::CXXRecordDecl *derived, const std::string &possibleBase); |
| 146 |
|
| 147 |
// Overload |
| 148 |
bool derivesFrom(clang::QualType derived, const std::string &possibleBase); |
| 149 |
|
| 150 |
/** |
| 151 |
* Returns the CXXRecordDecl represented by the CXXBaseSpecifier |
| 152 |
*/ |
| 153 |
inline clang::CXXRecordDecl *recordFromBaseSpecifier(const clang::CXXBaseSpecifier &base) |
| 154 |
{ |
| 155 |
const clang::Type *t = base.getType().getTypePtrOrNull(); |
| 156 |
return t ? t->getAsCXXRecordDecl() : nullptr; |
| 157 |
} |
| 158 |
/** |
| 159 |
* Returns true if the value is const. This is usually equivalent to qt.isConstQualified() but |
| 160 |
* takes care of the special case where qt represents a pointer. Many times you don't care if the |
| 161 |
* pointer is const or not and just care about the pointee. |
| 162 |
* |
| 163 |
* A a; => false |
| 164 |
* const A a; => true |
| 165 |
* A* a; => false |
| 166 |
* const A* a; => true |
| 167 |
* A *const a; => false |
| 168 |
*/ |
| 169 |
inline bool valueIsConst(const clang::QualType qt) |
| 170 |
{ |
| 171 |
return pointeeQualType(qt).isConstQualified(); |
| 172 |
} |
| 173 |
|
| 174 |
inline clang::CXXRecordDecl *typeAsRecord(const clang::QualType qt) |
| 175 |
{ |
| 176 |
if (qt.isNull()) { |
| 177 |
return nullptr; |
| 178 |
} |
| 179 |
|
| 180 |
return qt->getAsCXXRecordDecl(); |
| 181 |
} |
| 182 |
|
| 183 |
inline clang::CXXRecordDecl *typeAsRecord(const clang::Expr *expr) |
| 184 |
{ |
| 185 |
if (!expr) { |
| 186 |
return nullptr; |
| 187 |
} |
| 188 |
|
| 189 |
return typeAsRecord(pointeeQualType(expr->getType())); |
| 190 |
} |
| 191 |
|
| 192 |
inline clang::CXXRecordDecl *typeAsRecord(const clang::ValueDecl *value) |
| 193 |
{ |
| 194 |
if (!value) { |
| 195 |
return nullptr; |
| 196 |
} |
| 197 |
|
| 198 |
return typeAsRecord(pointeeQualType(value->getType())); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Returns the class that the typedef referred by qt is in. |
| 203 |
* |
| 204 |
* class Foo { |
| 205 |
* typedef A B; |
| 206 |
* }; |
| 207 |
* |
| 208 |
* For the above example Foo would be returned. |
| 209 |
*/ |
| 210 |
inline clang::CXXRecordDecl *parentRecordForTypedef(const clang::QualType qt) |
| 211 |
{ |
| 212 |
auto *t = qt.getTypePtrOrNull(); |
| 213 |
|
| 214 |
#if LLVM_VERSION_MAJOR < 22 |
| 215 |
if (const auto *elab = llvm::dyn_cast<clang::ElaboratedType>(t)) { |
| 216 |
t = elab->desugar().getTypePtrOrNull(); |
| 217 |
} |
| 218 |
#endif |
| 219 |
|
| 220 |
if (const auto *tdt = llvm::dyn_cast<clang::TypedefType>(t)) { |
| 221 |
clang::TypedefNameDecl *tdnd = tdt->getDecl(); |
| 222 |
return llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tdnd->getDeclContext()); |
| 223 |
} |
| 224 |
|
| 225 |
return nullptr; |
| 226 |
} |
| 227 |
|
| 228 |
// Example: const QString & |
| 229 |
inline bool isConstRef(const clang::Type *t) |
| 230 |
{ |
| 231 |
return t && t->isReferenceType() && t->getPointeeType().isConstQualified(); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
#endif |