| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "ctor-missing-parent-argument.h" |
| 8 |
#include "QtUtils.h" |
| 9 |
#include "StringUtils.h" |
| 10 |
#include "TypeUtils.h" |
| 11 |
|
| 12 |
#include <clang/AST/DeclBase.h> |
| 13 |
#include <clang/AST/DeclCXX.h> |
| 14 |
#include <clang/Basic/LLVM.h> |
| 15 |
#include <clang/Basic/SourceManager.h> |
| 16 |
#include <llvm/ADT/StringRef.h> |
| 17 |
#include <llvm/Support/Casting.h> |
| 18 |
|
| 19 |
using namespace clang; |
| 20 |
|
| 21 |
std::string CtorMissingParentArgument::expectedParentTypeFor(CXXRecordDecl *decl) |
| 22 |
{ |
| 23 |
if (const auto parent = qtNamespaced("QWidget"); clazy::derivesFrom(decl, parent)) { |
| 24 |
return parent; |
| 25 |
} |
| 26 |
if (const auto parent = qtNamespaced("QQuickItem"); clazy::derivesFrom(decl, parent)) { |
| 27 |
return parent; |
| 28 |
} else if (clazy::derivesFrom(decl, qtNamespaced("Qt3DCore::QEntity"))) { |
| 29 |
return qtNamespaced("Qt3DCore::QNode"); |
| 30 |
} |
| 31 |
|
| 32 |
return qtNamespaced("QObject"); |
| 33 |
} |
| 34 |
|
| 35 |
void CtorMissingParentArgument::VisitDecl(Decl *decl) |
| 36 |
{ |
| 37 |
auto *record = dyn_cast<CXXRecordDecl>(decl); |
| 38 |
bool ok = false; |
| 39 |
|
| 40 |
if (!record || !clazy::isQObject(record, m_context->qtNamespace())) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if (record->hasInheritedConstructor()) { |
| 45 |
// When doing using QObject::QObject you inherit the ctors from QObject, so don't warn. |
| 46 |
// Would be nicer to check if the using directives really refer to QObject::QObject and not to |
| 47 |
// Some other non-object class, but I can't find a way to get to ConstructorUsingShadowDecl from the CxxRecordDecl |
| 48 |
// so we might miss some true-positives |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
const bool hasCtors = record->ctor_begin() != record->ctor_end(); |
| 53 |
if (!hasCtors) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
const std::string parentType = expectedParentTypeFor(record); |
| 58 |
int numCtors = 0; |
| 59 |
const bool hasQObjectParam = clazy::recordHasCtorWithParam(record, parentType, /*by-ref*/ ok, /*by-ref*/ numCtors); |
| 60 |
if (!ok) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
if (numCtors > 0 && !hasQObjectParam) { |
| 65 |
clang::CXXRecordDecl *baseClass = clazy::getQObjectBaseClass(record, m_context->qtNamespace()); |
| 66 |
const bool baseHasQObjectParam = clazy::recordHasCtorWithParam(baseClass, parentType, /*by-ref*/ ok, /*by-ref*/ numCtors); |
| 67 |
if (ok && !baseHasQObjectParam && sm().isInSystemHeader(baseClass->getBeginLoc())) { |
| 68 |
// If the base class ctors don't accept QObject, and it's declared in a system header don't warn |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if (clazy::name(baseClass) == "QCoreApplication") { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
emitWarning(decl, record->getQualifiedNameAsString() + std::string(" should take ") + parentType + std::string(" parent argument in CTOR")); |
| 77 |
} |
| 78 |
} |