Debian Codegraph @

src/checks/manuallevel/unneeded-cast.cpp

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 #include "unneeded-cast.h"
11 #include "ClazyContext.h"
12 #include "HierarchyUtils.h"
13 #include "QtUtils.h"
14 #include "TypeUtils.h"
15 #include "Utils.h"
16
17 #include <clang/AST/DeclCXX.h>
18 #include <clang/AST/Expr.h>
19 #include <clang/AST/ExprCXX.h>
20 #include <clang/AST/OperationKinds.h>
21 #include <clang/AST/Stmt.h>
22 #include <clang/Basic/SourceLocation.h>
23 #include <llvm/Support/Casting.h>
24
25 #include <iterator>
26
27 using namespace llvm;
28 using namespace clang;
29
30 // Returns if callExpr is a call to qobject_cast()
31 inline bool is_qobject_cast(clang::Stmt *s, clang::CXXRecordDecl **castTo = nullptr, clang::CXXRecordDecl **castFrom = nullptr)
32 {
33 if (auto *callExpr = llvm::dyn_cast<clang::CallExpr>(s)) {
34 clang::FunctionDecl *func = callExpr->getDirectCallee();
35 if (!func || clazy::name(func) != "qobject_cast") {
36 return false;
37 }
38
39 if (castFrom) {
40 clang::Expr *expr = callExpr->getArg(0);
41 if (auto *implicitCast = llvm::dyn_cast<clang::ImplicitCastExpr>(expr)) {
42 if (implicitCast->getCastKind() == clang::CK_DerivedToBase) {
43 expr = implicitCast->getSubExpr();
44 }
45 }
46 clang::QualType qt = clazy::pointeeQualType(expr->getType());
47 if (!qt.isNull()) {
48 clang::CXXRecordDecl *record = qt->getAsCXXRecordDecl();
49 *castFrom = record ? record->getCanonicalDecl() : nullptr;
50 }
51 }
52
53 if (castTo) {
54 const auto *templateArgs = func->getTemplateSpecializationArgs();
55 if (templateArgs->size() == 1) {
56 const clang::TemplateArgument &arg = templateArgs->get(0);
57 clang::QualType qt = clazy::pointeeQualType(arg.getAsType());
58 if (!qt.isNull()) {
59 clang::CXXRecordDecl *record = qt->getAsCXXRecordDecl();
60 *castTo = record ? record->getCanonicalDecl() : nullptr;
61 }
62 }
63 }
64 return true;
65 }
66
67 return false;
68 }
69
70 void UnneededCast::VisitStmt(clang::Stmt *stm)
71 {
72 if (handleNamedCast(dyn_cast<CXXNamedCastExpr>(stm))) {
73 return;
74 }
75
76 handleQObjectCast(stm);
77 }
78
79 bool UnneededCast::handleNamedCast(CXXNamedCastExpr *namedCast)
80 {
81 if (!namedCast) {
82 return false;
83 }
84
85 const bool isDynamicCast = isa<CXXDynamicCastExpr>(namedCast);
86 const bool isStaticCast = isDynamicCast ? false : isa<CXXStaticCastExpr>(namedCast);
87
88 if (!isDynamicCast && !isStaticCast) {
89 return false;
90 }
91
92 if (namedCast->getBeginLoc().isMacroID()) {
93 return false;
94 }
95
96 CXXRecordDecl *castFrom = namedCast ? Utils::namedCastInnerDecl(namedCast) : nullptr;
97 if (!castFrom || !castFrom->hasDefinition() || std::distance(castFrom->bases_begin(), castFrom->bases_end()) > 1) {
98 return false;
99 }
100
101 if (isStaticCast) {
102 if (auto *implicitCast = dyn_cast<ImplicitCastExpr>(namedCast->getSubExpr())) {
103 if (implicitCast->getCastKind() == CK_NullToPointer) {
104 // static_cast<Foo*>(0) is OK, and sometimes needed
105 return false;
106 }
107 }
108
109 // static_cast to base is needed in ternary operators
110 if (clazy::getFirstParentOfType<ConditionalOperator>(m_context->parentMap, namedCast) != nullptr) {
111 return false;
112 }
113 }
114
115 if (isDynamicCast && !isOptionSet("prefer-dynamic-cast-over-qobject") && clazy::isQObject(castFrom, m_context->qtNamespace())) {
116 emitWarning(namedCast->getBeginLoc(), "Use qobject_cast rather than dynamic_cast");
117 }
118
119 CXXRecordDecl *castTo = Utils::namedCastOuterDecl(namedCast);
120 if (!castTo) {
121 return false;
122 }
123
124 return maybeWarn(namedCast, castFrom, castTo);
125 }
126
127 bool UnneededCast::handleQObjectCast(Stmt *stm)
128 {
129 CXXRecordDecl *castTo = nullptr;
130 CXXRecordDecl *castFrom = nullptr;
131
132 if (!is_qobject_cast(stm, &castTo, &castFrom)) {
133 return false;
134 }
135
136 return maybeWarn(stm, castFrom, castTo, /*isQObjectCast=*/true);
137 }
138
139 bool UnneededCast::maybeWarn(Stmt *stmt, CXXRecordDecl *castFrom, CXXRecordDecl *castTo, bool isQObjectCast)
140 {
141 castFrom = castFrom->getCanonicalDecl();
142 castTo = castTo->getCanonicalDecl();
143
144 if (castFrom == castTo) {
145 emitWarning(stmt->getBeginLoc(), "Casting to itself");
146 return true;
147 }
148 if (clazy::derivesFrom(/*child=*/castFrom, castTo)) {
149 if (isQObjectCast) {
150 const bool isTernaryOperator = clazy::getFirstParentOfType<ConditionalOperator>(m_context->parentMap, stmt) != nullptr;
151 if (isTernaryOperator) {
152 emitWarning(stmt->getBeginLoc(), "use static_cast instead of qobject_cast");
153 } else {
154 emitWarning(stmt->getBeginLoc(), "explicitly casting to base is unnecessary");
155 }
156 } else {
157 emitWarning(stmt->getBeginLoc(), "explicitly casting to base is unnecessary");
158 }
159
160 return true;
161 }
162
163 return false;
164 }