| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "qstring-ref.h" |
| 8 |
#include "ClazyContext.h" |
| 9 |
#include "FixItUtils.h" |
| 10 |
#include "HierarchyUtils.h" |
| 11 |
#include "StringUtils.h" |
| 12 |
#include "Utils.h" |
| 13 |
#include "clazy_stl.h" |
| 14 |
|
| 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/Basic/IdentifierTable.h> |
| 20 |
#include <clang/Basic/LLVM.h> |
| 21 |
#include <clang/Basic/SourceLocation.h> |
| 22 |
#include <clang/Lex/Lexer.h> |
| 23 |
#include <llvm/ADT/SmallVector.h> |
| 24 |
#include <llvm/ADT/StringRef.h> |
| 25 |
#include <llvm/Support/Casting.h> |
| 26 |
|
| 27 |
#include <vector> |
| 28 |
|
| 29 |
using namespace clang; |
| 30 |
|
| 31 |
static bool isInterestingFirstMethod(CXXMethodDecl *method) |
| 32 |
{ |
| 33 |
if (!method || clazy::name(method->getParent()) != "QString") { |
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
static const std::vector<StringRef> list{"left", "mid", "right"}; |
| 38 |
return clazy::contains(list, clazy::name(method)); |
| 39 |
} |
| 40 |
|
| 41 |
static bool isInterestingSecondMethod(CXXMethodDecl *method, const clang::LangOptions &lo) |
| 42 |
{ |
| 43 |
if (!method || clazy::name(method->getParent()) != "QString") { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
static const std::vector<StringRef> list{ |
| 48 |
"compare", "contains", "count", "startsWith", "endsWith", "indexOf", "isEmpty", "isNull", "lastIndexOf", "length", |
| 49 |
"size", "toDouble", "toFloat", "toInt", "toUInt", "toULong", "toULongLong", "toUShort", "toUcs4", |
| 50 |
}; |
| 51 |
|
| 52 |
if (!clazy::contains(list, clazy::name(method))) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
return !clazy::anyArgIsOfAnySimpleType(method, {"QRegExp", "QRegularExpression"}, lo); |
| 57 |
} |
| 58 |
|
| 59 |
static bool isMethodReceivingQStringRef(CXXMethodDecl *method) |
| 60 |
{ |
| 61 |
if (!method || clazy::name(method->getParent()) != "QString") { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
static const std::vector<StringRef> list{"append", "compare", "count", "indexOf", "endsWith", "lastIndexOf", "localAwareCompare", "startsWidth"}; |
| 66 |
|
| 67 |
if (clazy::contains(list, clazy::name(method))) { |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
if (method->getOverloadedOperator() == OO_PlusEqual) { // operator+= |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
void StringRefCandidates::VisitStmt(clang::Stmt *stmt) |
| 79 |
{ |
| 80 |
// Here we look for code like str.firstMethod().secondMethod(), where firstMethod() is for example mid() and secondMethod is for example, toInt() |
| 81 |
|
| 82 |
auto *call = dyn_cast<CallExpr>(stmt); |
| 83 |
if (!call || processCase1(dyn_cast<CXXMemberCallExpr>(call))) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
processCase2(call); |
| 88 |
} |
| 89 |
|
| 90 |
static bool containsChild(Stmt *s, Stmt *target) |
| 91 |
{ |
| 92 |
if (!s) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
if (s == target) { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
if (auto *mte = dyn_cast<MaterializeTemporaryExpr>(s)) { |
| 101 |
return containsChild(mte->getSubExpr(), target); |
| 102 |
} else if (auto *ice = dyn_cast<ImplicitCastExpr>(s)) { |
| 103 |
return containsChild(ice->getSubExpr(), target); |
| 104 |
} else if (auto *bte = dyn_cast<CXXBindTemporaryExpr>(s)) { |
| 105 |
return containsChild(bte->getSubExpr(), target); |
| 106 |
} |
| 107 |
|
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
bool StringRefCandidates::isConvertedToSomethingElse(clang::Stmt *s) const |
| 112 |
{ |
| 113 |
// While passing a QString to the QVariant ctor works fine, passing QStringRef doesn't |
| 114 |
// So let's not warn when QStrings are cast to something else. |
| 115 |
if (!s) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
auto *constr = clazy::getFirstParentOfType<CXXConstructExpr>(m_context->parentMap, s); |
| 120 |
if (!constr || constr->getNumArgs() == 0) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
if (containsChild(constr->getArg(0), s)) { |
| 125 |
CXXConstructorDecl *ctor = constr->getConstructor(); |
| 126 |
CXXRecordDecl *record = ctor ? ctor->getParent() : nullptr; |
| 127 |
return record ? record->getQualifiedNameAsString() != "QString" : false; |
| 128 |
} |
| 129 |
|
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
// Catches cases like: int i = s.mid(1, 1).toInt() |
| 134 |
bool StringRefCandidates::processCase1(CXXMemberCallExpr *memberCall) |
| 135 |
{ |
| 136 |
if (!memberCall) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
// In the AST secondMethod() is parent of firstMethod() call, and will be visited first (because at runtime firstMethod() is resolved first(). |
| 141 |
// So check for interesting second method first |
| 142 |
CXXMethodDecl *method = memberCall->getMethodDecl(); |
| 143 |
if (!isInterestingSecondMethod(method, lo())) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
std::vector<CallExpr *> callExprs = Utils::callListForChain(memberCall); |
| 148 |
if (callExprs.size() < 2) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
// The list now contains {secondMethod(), firstMethod() } |
| 153 |
auto *firstMemberCall = dyn_cast<CXXMemberCallExpr>(callExprs.at(1)); |
| 154 |
|
| 155 |
if (!firstMemberCall || !isInterestingFirstMethod(firstMemberCall->getMethodDecl())) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
if (isConvertedToSomethingElse(memberCall)) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
const std::string firstMethodName = firstMemberCall->getMethodDecl()->getNameAsString(); |
| 164 |
const std::vector<FixItHint> fixits = fixit(firstMemberCall); |
| 165 |
|
| 166 |
emitWarning(firstMemberCall->getEndLoc(), "Use " + firstMethodName + "Ref() instead", fixits); |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
// Catches cases like: s.append(s2.mid(1, 1)); |
| 171 |
bool StringRefCandidates::processCase2(CallExpr *call) |
| 172 |
{ |
| 173 |
auto *memberCall = dyn_cast<CXXMemberCallExpr>(call); |
| 174 |
auto *operatorCall = memberCall ? nullptr : dyn_cast<CXXOperatorCallExpr>(call); |
| 175 |
|
| 176 |
CXXMethodDecl *method = nullptr; |
| 177 |
if (memberCall) { |
| 178 |
method = memberCall->getMethodDecl(); |
| 179 |
} else if (operatorCall && operatorCall->getCalleeDecl()) { |
| 180 |
Decl *decl = operatorCall->getCalleeDecl(); |
| 181 |
method = dyn_cast<CXXMethodDecl>(decl); |
| 182 |
} |
| 183 |
|
| 184 |
if (!isMethodReceivingQStringRef(method)) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
Expr *firstArgument = call->getNumArgs() > 0 ? call->getArg(0) : nullptr; |
| 189 |
MaterializeTemporaryExpr *temp = firstArgument ? dyn_cast<MaterializeTemporaryExpr>(firstArgument) : nullptr; |
| 190 |
if (!temp) { |
| 191 |
Expr *secondArgument = call->getNumArgs() > 1 ? call->getArg(1) : nullptr; |
| 192 |
temp = secondArgument ? dyn_cast<MaterializeTemporaryExpr>(secondArgument) : nullptr; |
| 193 |
if (!temp) { // For the CXXOperatorCallExpr it's in the second argument |
| 194 |
return false; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
auto *innerCall = clazy::getFirstChildOfType2<CallExpr>(temp); |
| 199 |
auto *innerMemberCall = innerCall ? dyn_cast<CXXMemberCallExpr>(innerCall) : nullptr; |
| 200 |
if (!innerMemberCall) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
CXXMethodDecl *innerMethod = innerMemberCall->getMethodDecl(); |
| 205 |
if (!isInterestingFirstMethod(innerMethod)) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
const std::vector<FixItHint> fixits = fixit(innerMemberCall); |
| 210 |
|
| 211 |
emitWarning(call->getSourceRange().getBegin(), "Use " + innerMethod->getNameAsString() + "Ref() instead", fixits); |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
std::vector<FixItHint> StringRefCandidates::fixit(CXXMemberCallExpr *call) |
| 216 |
{ |
| 217 |
auto *memberExpr = clazy::getFirstChildOfType<MemberExpr>(call); |
| 218 |
if (!memberExpr) { |
| 219 |
return {}; |
| 220 |
} |
| 221 |
|
| 222 |
auto insertionLoc = Lexer::getLocForEndOfToken(memberExpr->getEndLoc(), 0, sm(), lo()); |
| 223 |
if (!insertionLoc.isValid()) { |
| 224 |
return {}; |
| 225 |
} |
| 226 |
|
| 227 |
std::vector<FixItHint> fixits; |
| 228 |
fixits.push_back(clazy::createInsertion(insertionLoc, "Ref")); |
| 229 |
return fixits; |
| 230 |
} |