Debian Codegraph @

src/TypeUtils.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2016-2017 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "TypeUtils.h"
8 #include "ClazyContext.h"
9 #include "HierarchyUtils.h"
10 #include "StmtBodyRange.h"
11 #include "StringUtils.h"
12 #include "Utils.h"
13
14 #include <clang/AST/ASTContext.h>
15 #include <clang/AST/DeclCXX.h>
16 #include <clang/AST/Expr.h>
17 #include <clang/AST/ExprCXX.h>
18 #include <clang/AST/Stmt.h>
19 #include <clang/AST/Type.h>
20 #include <clang/Basic/LLVM.h>
21
22 using namespace clang;
23
24 bool clazy::classifyQualType(const ClazyContext *context, clang::QualType qualType, const VarDecl *varDecl, QualTypeClassification &classif, clang::Stmt *body)
25 {
26 QualType unrefQualType = clazy::unrefQualType(qualType);
27 const Type *paramType = unrefQualType.getTypePtrOrNull();
28 if (!paramType || paramType->isIncompleteType()) {
29 return false;
30 }
31
32 if (isUndeducibleAuto(paramType)) {
33 return false;
34 }
35
36 classif.size_of_T = context->astContext->getTypeSize(unrefQualType) / 8;
37 classif.isBig = classif.size_of_T > 16;
38 CXXRecordDecl *recordDecl = paramType->getAsCXXRecordDecl();
39 CXXMethodDecl *copyCtor = recordDecl ? Utils::copyCtor(recordDecl) : nullptr;
40 classif.isNonTriviallyCopyable =
41 recordDecl && (recordDecl->hasNonTrivialCopyConstructor() || recordDecl->hasNonTrivialDestructor() || (copyCtor && copyCtor->isDeleted()));
42 classif.isReference = qualType->isLValueReferenceType();
43 classif.isConst = unrefQualType.isConstQualified();
44
45 if (qualType->isRValueReferenceType()) { // && ref, nothing to do here
46 return true;
47 }
48
49 if (classif.isConst && !classif.isReference) {
50 classif.passNonTriviallyCopyableByConstRef = classif.isNonTriviallyCopyable;
51 if (classif.isBig) {
52 classif.passBigTypeByConstRef = true;
53 }
54 } else if (classif.isConst && classif.isReference && !classif.isNonTriviallyCopyable && !classif.isBig) {
55 classif.passSmallTrivialByValue = true;
56 } else if (varDecl && !classif.isConst && !classif.isReference && (classif.isBig || classif.isNonTriviallyCopyable)) {
57 if (body
58 && (Utils::containsNonConstMemberCall(context->parentMap, body, varDecl)
59 || Utils::isPassedToFunction(StmtBodyRange(body), varDecl, /*byrefonly=*/true))) {
60 return true;
61 }
62
63 classif.passNonTriviallyCopyableByConstRef = classif.isNonTriviallyCopyable;
64 if (classif.isBig) {
65 classif.passBigTypeByConstRef = true;
66 }
67 }
68
69 return true;
70 }
71
72 bool clazy::isSmallTrivial(const ClazyContext *context, QualType qualType)
73 {
74 if (qualType.isNull()) {
75 return false;
76 }
77
78 if (qualType->isPointerType()) {
79 qualType = qualType->getPointeeType();
80 }
81
82 if (qualType->isPointerType()) { // We don't care about ** (We can change this whenever we have a use case)
83 return false;
84 }
85
86 QualType unrefQualType = clazy::unrefQualType(qualType);
87 const Type *paramType = unrefQualType.getTypePtrOrNull();
88 if (!paramType || paramType->isIncompleteType()) {
89 return false;
90 }
91
92 if (isUndeducibleAuto(paramType)) {
93 return false;
94 }
95
96 if (qualType->isRValueReferenceType()) { // && ref, nothing to do here
97 return false;
98 }
99
100 CXXRecordDecl *recordDecl = paramType->getAsCXXRecordDecl();
101 CXXMethodDecl *copyCtor = recordDecl ? Utils::copyCtor(recordDecl) : nullptr;
102 const bool hasDeletedCopyCtor = copyCtor && copyCtor->isDeleted();
103 const bool isTrivial = recordDecl && !recordDecl->hasNonTrivialCopyConstructor() && !recordDecl->hasNonTrivialDestructor() && !hasDeletedCopyCtor;
104
105 if (isTrivial) {
106 const auto typeSize = context->astContext->getTypeSize(unrefQualType) / 8;
107 const bool isSmall = typeSize <= 16;
108 return isSmall;
109 }
110
111 return false;
112 }
113
114 void clazy::heapOrStackAllocated(Expr *arg, const std::string &type, const clang::LangOptions &lo, bool &isStack, bool &isHeap)
115 {
116 isStack = false;
117 isHeap = false;
118 if (isa<CXXNewExpr>(arg)) {
119 isHeap = true;
120 return;
121 }
122
123 std::vector<DeclRefExpr *> declrefs;
124 clazy::getChilds(arg, declrefs, 3);
125
126 std::vector<DeclRefExpr *> interestingDeclRefs;
127 for (auto *declref : declrefs) {
128 const auto *t = declref->getType().getTypePtrOrNull();
129 if (!t) {
130 continue;
131 }
132
133 // Remove the '*' if it's a pointer
134 QualType qt = t->isPointerType() ? t->getPointeeType() : declref->getType();
135
136 if (type == clazy::simpleTypeName(qt, lo)) {
137 interestingDeclRefs.push_back(declref);
138 }
139 }
140
141 if (interestingDeclRefs.size() > 1) {
142 // Too complex
143 return;
144 }
145
146 if (!interestingDeclRefs.empty()) {
147 auto *declref = interestingDeclRefs[0];
148 isStack = !declref->getType().getTypePtr()->isPointerType();
149 isHeap = !isStack;
150 }
151 }
152
153 bool clazy::derivesFrom(const CXXRecordDecl *derived, const CXXRecordDecl *possibleBase, std::vector<CXXRecordDecl *> *baseClasses)
154 {
155 if (!derived || !possibleBase || derived == possibleBase) {
156 return false;
157 }
158
159 for (auto base : derived->bases()) {
160 const Type *type = base.getType().getTypePtrOrNull();
161 if (!type) {
162 continue;
163 }
164 CXXRecordDecl *baseDecl = type->getAsCXXRecordDecl();
165 baseDecl = baseDecl ? baseDecl->getCanonicalDecl() : nullptr;
166
167 if (possibleBase == baseDecl || derivesFrom(baseDecl, possibleBase, baseClasses)) {
168 if (baseClasses) {
169 baseClasses->push_back(baseDecl);
170 }
171 return true;
172 }
173 }
174
175 return false;
176 }
177
178 bool clazy::derivesFrom(const clang::CXXRecordDecl *derived, const std::string &possibleBase)
179 {
180 if (!derived || !derived->hasDefinition()) {
181 return false;
182 }
183
184 if (derived->getQualifiedNameAsString() == possibleBase) {
185 return true;
186 }
187
188 for (auto base : derived->bases()) {
189 if (derivesFrom(recordFromBaseSpecifier(base), possibleBase)) {
190 return true;
191 }
192 }
193
194 return false;
195 }
196
197 bool clazy::derivesFrom(QualType derivedQT, const std::string &possibleBase)
198 {
199 derivedQT = pointeeQualType(derivedQT);
200 const auto *const t = derivedQT.getTypePtrOrNull();
201 return t ? derivesFrom(t->getAsCXXRecordDecl(), possibleBase) : false;
202 }