| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 3 |
SPDX-FileContributor: Sérgio Martins <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "connect-not-normalized.h" |
| 9 |
#include "ClazyContext.h" |
| 10 |
#include "HierarchyUtils.h" |
| 11 |
#include "NormalizedSignatureUtils.h" |
| 12 |
#include "StringUtils.h" |
| 13 |
|
| 14 |
#include <clang/AST/Decl.h> |
| 15 |
#include <clang/AST/DeclCXX.h> |
| 16 |
#include <clang/AST/Expr.h> |
| 17 |
#include <clang/AST/ExprCXX.h> |
| 18 |
#include <clang/AST/ParentMap.h> |
| 19 |
#include <clang/AST/Stmt.h> |
| 20 |
#include <clang/Basic/LLVM.h> |
| 21 |
#include <llvm/ADT/StringRef.h> |
| 22 |
#include <llvm/Support/Casting.h> |
| 23 |
|
| 24 |
using namespace clang; |
| 25 |
|
| 26 |
void ConnectNotNormalized::VisitStmt(clang::Stmt *stmt) |
| 27 |
{ |
| 28 |
handleQ_ARG(stmt) || handleConnect(dyn_cast<CallExpr>(stmt)); |
| 29 |
} |
| 30 |
|
| 31 |
bool ConnectNotNormalized::handleQ_ARG(clang::Stmt *stmt) |
| 32 |
{ |
| 33 |
if (auto *casted = dyn_cast<CallExpr>(stmt); casted && casted->getNumArgs() == 2) { |
| 34 |
if (auto *func = casted->getDirectCallee()) { |
| 35 |
const std::string retTypeName = func->getReturnType().getAsString(lo()); |
| 36 |
if (retTypeName == "QMetaMethodArgument" || retTypeName == "QMetaMethodReturnArgument") { |
| 37 |
auto *literal = clazy::getFirstChildOfType2<StringLiteral>(casted->getArg(0)); |
| 38 |
return literal ? checkNormalizedLiteral(literal, casted) : false; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
auto *expr = dyn_cast<CXXConstructExpr>(stmt); |
| 43 |
if (!expr || expr->getNumArgs() != 2) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
CXXConstructorDecl *ctor = expr->getConstructor(); |
| 48 |
if (!ctor) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
auto name = ctor->getNameAsString(); |
| 53 |
if (name != "QArgument" && name != "QReturnArgument") { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
auto *sl = clazy::getFirstChildOfType2<clang::StringLiteral>(expr->getArg(0)); |
| 58 |
return sl && checkNormalizedLiteral(sl, expr); |
| 59 |
} |
| 60 |
bool ConnectNotNormalized::checkNormalizedLiteral(clang::StringLiteral *sl, clang::Expr *expr) |
| 61 |
{ |
| 62 |
const std::string original = sl->getString().str(); |
| 63 |
const std::string normalized = clazy::normalizedType(original.c_str()); |
| 64 |
|
| 65 |
if (original == normalized) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
emitWarning(expr, "Signature is not normalized. Use " + normalized + " instead of " + original); |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
bool ConnectNotNormalized::handleConnect(CallExpr *callExpr) |
| 74 |
{ |
| 75 |
if (!callExpr) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
FunctionDecl *func = callExpr->getDirectCallee(); |
| 80 |
if (!func || func->getNumParams() != 1 || clazy::name(func) != "qFlagLocation") { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
{ |
| 85 |
// Only warn in connect statements, not disconnect, since there there's no optimization in Qt's side |
| 86 |
auto *parentCallExpr = clazy::getFirstParentOfType<CallExpr>(m_context->parentMap, m_context->parentMap->getParent(callExpr), -1); |
| 87 |
if (!parentCallExpr) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
const FunctionDecl *parentFunc = parentCallExpr->getDirectCallee(); |
| 92 |
if (!parentFunc || clazy::name(parentFunc) != "connect") { |
| 93 |
return false; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
Expr *arg1 = callExpr->getArg(0); |
| 98 |
auto *sl = clazy::getFirstChildOfType2<clang::StringLiteral>(arg1); |
| 99 |
if (!sl) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
std::string original = sl->getString().str(); |
| 103 |
std::string normalized = clazy::normalizedSignature(original.c_str()); |
| 104 |
|
| 105 |
// discard the junk after '\0' |
| 106 |
normalized = std::string(normalized.c_str()); |
| 107 |
original = std::string(original.c_str()); |
| 108 |
|
| 109 |
if (original == normalized) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
// Remove first digit |
| 114 |
normalized.erase(0, 1); |
| 115 |
original.erase(0, 1); |
| 116 |
|
| 117 |
emitWarning(callExpr->getBeginLoc(), "Signature is not normalized. Use " + normalized + " instead of " + original); |
| 118 |
return true; |
| 119 |
} |