| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "base-class-event.h" |
| 8 |
#include "HierarchyUtils.h" |
| 9 |
#include "QtUtils.h" |
| 10 |
#include "clazy_stl.h" |
| 11 |
|
| 12 |
#include <clang/AST/DeclCXX.h> |
| 13 |
#include <clang/AST/ExprCXX.h> |
| 14 |
#include <clang/AST/Stmt.h> |
| 15 |
#include <clang/Basic/LLVM.h> |
| 16 |
#include <llvm/ADT/StringRef.h> |
| 17 |
#include <llvm/Support/Casting.h> |
| 18 |
|
| 19 |
#include <array> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
using namespace clang; |
| 23 |
|
| 24 |
void BaseClassEvent::VisitDecl(Decl *decl) |
| 25 |
{ |
| 26 |
auto *method = dyn_cast<CXXMethodDecl>(decl); |
| 27 |
if (!method || !method->hasBody() || !method->isThisDeclarationADefinition()) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
const std::string methodName = method->getNameAsString(); |
| 32 |
const bool isEvent = methodName == "event"; |
| 33 |
const bool isEventFilter = isEvent ? false : methodName == "eventFilter"; |
| 34 |
|
| 35 |
if (!isEvent && !isEventFilter) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
CXXRecordDecl *classDecl = method->getParent(); |
| 40 |
if (!clazy::isQObject(classDecl, m_context->qtNamespace())) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if (clazy::contains(std::array<StringRef, 2>({"QObject", "QWidget"}), trimQtNamespace(classDecl->getQualifiedNameAsString()))) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
CXXRecordDecl *baseClass = clazy::getQObjectBaseClass(classDecl, m_context->qtNamespace()); |
| 49 |
const std::string baseClassName = baseClass ? trimQtNamespace(baseClass->getQualifiedNameAsString()) : std::string("BaseClass"); |
| 50 |
|
| 51 |
if (isEventFilter && clazy::contains(std::array<StringRef, 2>({"QObject", "QWidget"}), baseClassName)) { |
| 52 |
// This is fine, QObject and QWidget eventFilter() don't do anything |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
Stmt *body = method->getBody(); |
| 57 |
std::vector<ReturnStmt *> returns; |
| 58 |
clazy::getChilds<ReturnStmt>(body, /*by-ref*/ returns); |
| 59 |
for (ReturnStmt *returnStmt : returns) { |
| 60 |
Stmt *maybeBoolExpr = clazy::childAt(returnStmt, 0); |
| 61 |
if (!maybeBoolExpr) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
auto *boolExpr = dyn_cast<CXXBoolLiteralExpr>(maybeBoolExpr); |
| 65 |
if (!boolExpr || boolExpr->getValue()) { // if getValue() is true that's a return true, which is fine |
| 66 |
continue; |
| 67 |
} |
| 68 |
|
| 69 |
emitWarning(returnStmt->getBeginLoc(), "Return " + baseClassName + "::" + methodName + "() instead of false"); |
| 70 |
} |
| 71 |
} |