| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "const-signal-or-slot.h" |
| 8 |
#include "AccessSpecifierManager.h" |
| 9 |
#include "ClazyContext.h" |
| 10 |
#include "QtUtils.h" |
| 11 |
#include "TypeUtils.h" |
| 12 |
|
| 13 |
#include <clang/AST/DeclCXX.h> |
| 14 |
#include <clang/AST/Expr.h> |
| 15 |
#include <clang/AST/Stmt.h> |
| 16 |
#include <clang/AST/Type.h> |
| 17 |
#include <clang/Basic/LLVM.h> |
| 18 |
#include <llvm/Support/Casting.h> |
| 19 |
|
| 20 |
using namespace clang; |
| 21 |
|
| 22 |
void ConstSignalOrSlot::VisitStmt(clang::Stmt *stmt) |
| 23 |
{ |
| 24 |
auto *call = dyn_cast<CallExpr>(stmt); |
| 25 |
const AccessSpecifierManager *accessSpecifierManager = m_context->accessSpecifierManager; |
| 26 |
if (!call || !accessSpecifierManager) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
FunctionDecl *func = call->getDirectCallee(); |
| 31 |
if (!clazy::isConnect(func, m_context->qtNamespace()) || !clazy::connectHasPMFStyle(func)) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
CXXMethodDecl *slot = clazy::receiverMethodForConnect(call, m_context->qtNamespace()); |
| 36 |
if (!slot || !slot->isConst() || slot->getReturnType()->isVoidType()) { // const and returning void must do something, so not a getter |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
QtAccessSpecifierType specifierType = accessSpecifierManager->qtAccessSpecifierType(slot); |
| 41 |
if (specifierType == QtAccessSpecifier_Slot || specifierType == QtAccessSpecifier_Signal) { |
| 42 |
return; // For stuff explicitly marked as slots or signals we use VisitDecl |
| 43 |
} |
| 44 |
|
| 45 |
// Here the user is connecting to a const method, which isn't marked as slot or signal and returns non-void |
| 46 |
// Looks like a getter! |
| 47 |
|
| 48 |
emitWarning(stmt, slot->getQualifiedNameAsString() + " is not a slot, and is possibly a getter"); |
| 49 |
} |
| 50 |
|
| 51 |
void ConstSignalOrSlot::VisitDecl(Decl *decl) |
| 52 |
{ |
| 53 |
auto *method = dyn_cast<CXXMethodDecl>(decl); |
| 54 |
if (!method || !method->isConst()) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
const AccessSpecifierManager *a = m_context->accessSpecifierManager; |
| 59 |
if (!a) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
if (method->isThisDeclarationADefinition() && !method->hasInlineBody()) { // Don't warn twice |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
const CXXRecordDecl *record = method->getParent(); |
| 68 |
if (clazy::derivesFrom(record, "QDBusAbstractInterface")) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
QtAccessSpecifierType specifierType = a->qtAccessSpecifierType(method); |
| 73 |
|
| 74 |
const bool isSlot = specifierType == QtAccessSpecifier_Slot; |
| 75 |
const bool isSignal = specifierType == QtAccessSpecifier_Signal; |
| 76 |
|
| 77 |
if (!isSlot && !isSignal) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
if (a->isScriptable(method)) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if (isSlot && !method->getReturnType()->isVoidType()) { |
| 86 |
emitWarning(decl, "getter " + method->getQualifiedNameAsString() + " possibly mismarked as a slot"); |
| 87 |
} else if (isSignal) { |
| 88 |
emitWarning(decl, "signal " + method->getQualifiedNameAsString() + " shouldn't be const"); |
| 89 |
} |
| 90 |
} |