| 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 "detaching-member.h" |
| 11 |
#include "ClazyContext.h" |
| 12 |
#include "HierarchyUtils.h" |
| 13 |
#include "StringUtils.h" |
| 14 |
#include "Utils.h" |
| 15 |
#include "checkbase.h" |
| 16 |
#include "clazy_stl.h" |
| 17 |
|
| 18 |
#include <clang/AST/Decl.h> |
| 19 |
#include <clang/AST/DeclCXX.h> |
| 20 |
#include <clang/AST/Expr.h> |
| 21 |
#include <clang/AST/ExprCXX.h> |
| 22 |
#include <clang/AST/Stmt.h> |
| 23 |
#include <clang/AST/Type.h> |
| 24 |
#include <clang/Basic/LLVM.h> |
| 25 |
#include <clang/Basic/OperatorKinds.h> |
| 26 |
#include <llvm/ADT/StringRef.h> |
| 27 |
#include <llvm/Support/Casting.h> |
| 28 |
|
| 29 |
#include <vector> |
| 30 |
|
| 31 |
using namespace clang; |
| 32 |
|
| 33 |
DetachingMember::DetachingMember(const std::string &name, Options options) |
| 34 |
: DetachingBase(name, options) |
| 35 |
{ |
| 36 |
m_filesToIgnore = {"qstring.h"}; |
| 37 |
} |
| 38 |
|
| 39 |
void DetachingMember::VisitStmt(clang::Stmt *stm) |
| 40 |
{ |
| 41 |
auto *callExpr = dyn_cast<CallExpr>(stm); |
| 42 |
if (!callExpr) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
auto *memberCall = dyn_cast<CXXMemberCallExpr>(callExpr); |
| 47 |
auto *operatorExpr = dyn_cast<CXXOperatorCallExpr>(callExpr); |
| 48 |
if (!memberCall && !operatorExpr) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
if (shouldIgnoreFile(stm->getBeginLoc())) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
CXXMethodDecl *method = nullptr; |
| 57 |
ValueDecl *memberDecl = nullptr; |
| 58 |
if (operatorExpr) { |
| 59 |
FunctionDecl *func = operatorExpr->getDirectCallee(); |
| 60 |
method = func ? dyn_cast<CXXMethodDecl>(func) : nullptr; |
| 61 |
if (!method || method->getOverloadedOperator() != clang::OO_Subscript) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
auto *memberExpr = clazy::getFirstParentOfType<CXXMemberCallExpr>(m_context->parentMap, operatorExpr); |
| 66 |
CXXMethodDecl *parentMemberDecl = memberExpr ? memberExpr->getMethodDecl() : nullptr; |
| 67 |
if (parentMemberDecl && !parentMemberDecl->isConst()) { |
| 68 |
// Don't warn for s.m_listOfValues[0].nonConstMethod(); |
| 69 |
// However do warn for: s.m_listOfPointers[0]->nonConstMethod(); because it compiles with .at() |
| 70 |
QualType qt = operatorExpr->getType(); |
| 71 |
const Type *t = qt.getTypePtrOrNull(); |
| 72 |
if (t && !t->isPointerType()) { |
| 73 |
return; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
memberDecl = Utils::valueDeclForOperatorCall(operatorExpr); |
| 78 |
} else { |
| 79 |
method = memberCall->getMethodDecl(); |
| 80 |
memberDecl = Utils::valueDeclForMemberCall(memberCall); |
| 81 |
} |
| 82 |
|
| 83 |
if (!method || !memberDecl || !Utils::isMemberVariable(memberDecl) || !isDetachingMethod(method, DetachingMethodWithConstCounterPart) |
| 84 |
|| method->isConst()) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
// Catch cases like m_foo[0] = .. , which is fine |
| 89 |
|
| 90 |
auto *parentUnaryOp = clazy::getFirstParentOfType<UnaryOperator>(m_context->parentMap, callExpr); |
| 91 |
if (parentUnaryOp) { |
| 92 |
// m_foo[0]++ is OK |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
auto *parentOp = clazy::getFirstParentOfType<CXXOperatorCallExpr>(m_context->parentMap, clazy::parent(m_context->parentMap, callExpr)); |
| 97 |
if (parentOp) { |
| 98 |
FunctionDecl *parentFunc = parentOp->getDirectCallee(); |
| 99 |
const std::string parentFuncName = parentFunc ? parentFunc->getNameAsString() : ""; |
| 100 |
if (clazy::startsWith(parentFuncName, "operator")) { |
| 101 |
// m_foo[0] = ... is OK |
| 102 |
return; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
auto *parentBinaryOp = clazy::getFirstParentOfType<BinaryOperator>(m_context->parentMap, callExpr); |
| 107 |
if (parentBinaryOp && parentBinaryOp->isAssignmentOp()) { |
| 108 |
// m_foo[0] += .. is OK |
| 109 |
Expr *lhs = parentBinaryOp->getLHS(); |
| 110 |
if (callExpr == lhs || clazy::isChildOf(callExpr, lhs)) { |
| 111 |
return; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
const bool returnsNonConstIterator = memberCall && clazy::endsWith(memberCall->getType().getAsString(lo()), "iterator"); |
| 116 |
if (returnsNonConstIterator) { |
| 117 |
// If we're calling begin()/end() as arguments to a function taking non-const iterators it's fine |
| 118 |
// Such as qSort(list.begin(), list.end()); |
| 119 |
auto *parentCall = clazy::getFirstParentOfType<CallExpr>(m_context->parentMap, clazy::parent(m_context->parentMap, memberCall)); |
| 120 |
FunctionDecl *parentFunc = parentCall ? parentCall->getDirectCallee() : nullptr; |
| 121 |
if (parentFunc && parentFunc->getNumParams() == parentCall->getNumArgs()) { |
| 122 |
int i = 0; |
| 123 |
for (auto *argExpr : parentCall->arguments()) { |
| 124 |
auto expr2 = dyn_cast<CXXMemberCallExpr>(argExpr); // C++17 |
| 125 |
if (!expr2) |
| 126 |
expr2 = clazy::getFirstChildOfType<CXXMemberCallExpr>(argExpr); // C++14 |
| 127 |
if (expr2) { |
| 128 |
if (expr2 == memberCall) { |
| 129 |
// Success, we found which arg |
| 130 |
ParmVarDecl *param = parentFunc->getParamDecl(i); |
| 131 |
|
| 132 |
// Check that the record declarations have the same type |
| 133 |
if (param->getType().getTypePtr()->getAsRecordDecl()->getNameAsString() |
| 134 |
== memberCall->getType().getTypePtr()->getAsRecordDecl()->getNameAsString()) { |
| 135 |
return; |
| 136 |
} |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
++i; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
std::vector<FixItHint> fixits; |
| 146 |
if (memberCall) { |
| 147 |
fixits = getFixitHints(clazy::name(method->getParent()), clazy::name(method), callExpr); |
| 148 |
} else if (auto operatorCallee = operatorExpr ? dyn_cast<CXXMethodDecl>(operatorExpr->getDirectCallee()) : nullptr) { |
| 149 |
fixits = getFixitHints(clazy::name(operatorCallee->getParent()), clazy::name(operatorCallee), callExpr); |
| 150 |
} |
| 151 |
|
| 152 |
emitWarning(stm->getBeginLoc(), "Potential detachment due to calling " + method->getQualifiedNameAsString() + "()", fixits); |
| 153 |
} |