| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "overridden-signal.h" |
| 8 |
#include "AccessSpecifierManager.h" |
| 9 |
#include "ClazyContext.h" |
| 10 |
#include "FunctionUtils.h" |
| 11 |
#include "QtUtils.h" |
| 12 |
#include "StringUtils.h" |
| 13 |
|
| 14 |
#include <clang/AST/DeclCXX.h> |
| 15 |
#include <clang/Basic/LLVM.h> |
| 16 |
#include <llvm/ADT/StringRef.h> |
| 17 |
#include <llvm/Support/Casting.h> |
| 18 |
|
| 19 |
using namespace clang; |
| 20 |
|
| 21 |
void OverriddenSignal::VisitDecl(clang::Decl *decl) |
| 22 |
{ |
| 23 |
const AccessSpecifierManager *accessSpecifierManager = m_context->accessSpecifierManager; |
| 24 |
auto *method = dyn_cast<CXXMethodDecl>(decl); |
| 25 |
if (!accessSpecifierManager || !method) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
if (method->isThisDeclarationADefinition() && !method->hasInlineBody()) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
CXXRecordDecl *record = method->getParent(); |
| 34 |
CXXRecordDecl *baseClass = clazy::getQObjectBaseClass(record, m_context->qtNamespace()); |
| 35 |
if (!baseClass) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
const bool methodIsSignal = accessSpecifierManager->qtAccessSpecifierType(method) == QtAccessSpecifier_Signal; |
| 40 |
const StringRef methodName = clazy::name(method); |
| 41 |
|
| 42 |
std::string warningMsg; |
| 43 |
while (baseClass) { |
| 44 |
for (auto *baseMethod : baseClass->methods()) { |
| 45 |
if (clazy::name(baseMethod) == methodName) { |
| 46 |
if (!clazy::parametersMatch(method, baseMethod)) { // overloading is permitted. |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
const bool baseMethodIsSignal = accessSpecifierManager->qtAccessSpecifierType(baseMethod) == QtAccessSpecifier_Signal; |
| 51 |
|
| 52 |
if (methodIsSignal && baseMethodIsSignal) { |
| 53 |
warningMsg = "Overriding signal with signal: " + method->getQualifiedNameAsString(); |
| 54 |
} else if (methodIsSignal && !baseMethodIsSignal) { |
| 55 |
warningMsg = "Overriding non-signal with signal: " + method->getQualifiedNameAsString(); |
| 56 |
} else if (!methodIsSignal && baseMethodIsSignal) { |
| 57 |
warningMsg = "Overriding signal with non-signal: " + method->getQualifiedNameAsString(); |
| 58 |
} |
| 59 |
|
| 60 |
if (!warningMsg.empty()) { |
| 61 |
emitWarning(decl, warningMsg); |
| 62 |
return; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
baseClass = clazy::getQObjectBaseClass(baseClass, m_context->qtNamespace()); |
| 68 |
} |
| 69 |
} |