diff options
author | Sergio Martins <smartins@kde.org> | 2017-10-05 19:14:31 +0100 |
---|---|---|
committer | Sergio Martins <iamsergio@gmail.com> | 2017-10-05 19:14:31 +0100 |
commit | 41665ec136d9408285621139a970ec50791e25f4 (patch) | |
tree | 0e76fa0400d9f3eb814c18498ba740c3e9b5d303 /src | |
parent | d64c73841f73cd68865ce88bfd18336f1954b3a7 (diff) |
Introduce const-signal-or-slot, to find getters unintentionally marked as slots
Warns for:
- non-void const method marked as slot
- const method marked as signal
- connecting to a method which isn't marked as slot, is const and returns non-void
Diffstat (limited to 'src')
-rw-r--r-- | src/checks/level1/README-const-signal-or-slot.md | 14 | ||||
-rw-r--r-- | src/checks/level1/const-signal-or-slot.cpp | 98 | ||||
-rw-r--r-- | src/checks/level1/const-signal-or-slot.h | 39 |
3 files changed, 151 insertions, 0 deletions
diff --git a/src/checks/level1/README-const-signal-or-slot.md b/src/checks/level1/README-const-signal-or-slot.md new file mode 100644 index 00000000..9a3e71b2 --- /dev/null +++ b/src/checks/level1/README-const-signal-or-slot.md @@ -0,0 +1,14 @@ +# const-signal-or-slot + +Warns when a signal or non-void slot is const. + +This aims to prevent unintentionally marking a getter as slot, or connecting to +the wrong method. For signals, it's just pointless to mark them as const. + +Warns for the following cases: + +- non-void const method marked as slot +- const method marked as signal +- connecting to a method which isn't marked as slot, is const and returns non-void + +For exposing methods to QML prefer either Q_PROPERTY or Q_INVOKABLE. diff --git a/src/checks/level1/const-signal-or-slot.cpp b/src/checks/level1/const-signal-or-slot.cpp new file mode 100644 index 00000000..faa580ae --- /dev/null +++ b/src/checks/level1/const-signal-or-slot.cpp @@ -0,0 +1,98 @@ +/* + This file is part of the clazy static checker. + + Copyright (C) 2017 Sergio Martins <smartins@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "const-signal-or-slot.h" +#include "Utils.h" +#include "HierarchyUtils.h" +#include "QtUtils.h" +#include "TypeUtils.h" +#include "checkmanager.h" +#include "ClazyContext.h" +#include "AccessSpecifierManager.h" + +#include <clang/AST/AST.h> + +using namespace clang; +using namespace std; + + +ConstSignalOrSlot::ConstSignalOrSlot(const std::string &name, ClazyContext *context) + : CheckBase(name, context) +{ + context->enableAccessSpecifierManager(); +} + +void ConstSignalOrSlot::VisitStmt(clang::Stmt *stmt) +{ + auto call = dyn_cast<CallExpr>(stmt); + AccessSpecifierManager *accessSpecifierManager = m_context->accessSpecifierManager; + if (!call || !accessSpecifierManager) + return; + + FunctionDecl *func = call->getDirectCallee(); + if (!QtUtils::isConnect(func) || !QtUtils::connectHasPMFStyle(func)) + return; + + CXXMethodDecl *slot = QtUtils::pmfFromConnect(call, 2); + if (!slot) // The slot is either third or fourth argument + slot = QtUtils::pmfFromConnect(call, 3); + + if (!slot || !slot->isConst() || slot->getReturnType()->isVoidType()) // const and returning void must do something, so not a getter + return; + + QtAccessSpecifierType specifierType = accessSpecifierManager->qtAccessSpecifierType(slot); + if (specifierType == QtAccessSpecifier_Slot || specifierType == QtAccessSpecifier_Signal) + return; // For stuff explicitly marked as slots or signals we use VisitDecl + + + // Here the user is connecting to a const method, which isn't marked as slot or signal and returns non-void + // Looks like a getter! + + emitWarning(stmt, slot->getQualifiedNameAsString() + " is not a slot, and is possibly a getter"); +} + +void ConstSignalOrSlot::VisitDecl(Decl *decl) +{ + auto method = dyn_cast<CXXMethodDecl>(decl); + if (!method || !method->isConst()) + return; + + AccessSpecifierManager *a = m_context->accessSpecifierManager; + if (!a) + return; + + if (method->isThisDeclarationADefinition() && !method->hasInlineBody()) // Don't warn twice + return; + + CXXRecordDecl *record = method->getParent(); + if (TypeUtils::derivesFrom(record, "QDBusAbstractInterface")) + return; + + QtAccessSpecifierType specifierType = a->qtAccessSpecifierType(method); + + if (specifierType == QtAccessSpecifier_Slot && !method->getReturnType()->isVoidType()) { + emitWarning(decl, "getter " + method->getQualifiedNameAsString() + " possibly mismarked as a slot"); + } else if (specifierType == QtAccessSpecifier_Signal) { + emitWarning(decl, "signal " + method->getQualifiedNameAsString() + " shouldn't be const"); + } +} + +REGISTER_CHECK("const-signal-or-slot", ConstSignalOrSlot, CheckLevel1) diff --git a/src/checks/level1/const-signal-or-slot.h b/src/checks/level1/const-signal-or-slot.h new file mode 100644 index 00000000..294a6b73 --- /dev/null +++ b/src/checks/level1/const-signal-or-slot.h @@ -0,0 +1,39 @@ +/* + This file is part of the clazy static checker. + + Copyright (C) 2017 Sergio Martins <smartins@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef CLAZY_CONST_SIGNAL_OR_SLOT_H +#define CLAZY_CONST_SIGNAL_OR_SLOT_H + +#include "checkbase.h" + + +/** + * See README-const-signal-or-slot.md for more info. + */ +class ConstSignalOrSlot : public CheckBase +{ +public: + explicit ConstSignalOrSlot(const std::string &name, ClazyContext *context); + void VisitStmt(clang::Stmt *stmt) override; + void VisitDecl(clang::Decl *decl) override; +}; + +#endif |