| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "rule-of-three.h" |
| 8 |
#include "MacroUtils.h" |
| 9 |
#include "TypeUtils.h" |
| 10 |
#include "Utils.h" |
| 11 |
#include "clazy_stl.h" |
| 12 |
|
| 13 |
#include <clang/AST/DeclBase.h> |
| 14 |
#include <clang/AST/DeclCXX.h> |
| 15 |
#include <clang/Basic/LLVM.h> |
| 16 |
#include <clang/Basic/SourceLocation.h> |
| 17 |
#include <clang/Basic/SourceManager.h> |
| 18 |
#include <clang/Basic/Specifiers.h> |
| 19 |
#include <llvm/ADT/StringRef.h> |
| 20 |
#include <llvm/Support/Casting.h> |
| 21 |
|
| 22 |
#include <vector> |
| 23 |
|
| 24 |
using namespace clang; |
| 25 |
|
| 26 |
RuleOfThree::RuleOfThree(const std::string &name, Options options) |
| 27 |
: RuleOfBase(name, options) |
| 28 |
{ |
| 29 |
m_filesToIgnore = {"qrc_"}; |
| 30 |
} |
| 31 |
|
| 32 |
void RuleOfThree::VisitDecl(clang::Decl *decl) |
| 33 |
{ |
| 34 |
auto *record = dyn_cast<CXXRecordDecl>(decl); |
| 35 |
if (!record || isBlacklisted(record) || !record->hasDefinition() || record->isPolymorphic()) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
// fwd decl is not interesting |
| 40 |
if (record != record->getDefinition()) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
if (shouldIgnoreFile(decl->getBeginLoc())) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
const SourceLocation recordStart = record->getBeginLoc(); |
| 49 |
if (recordStart.isMacroID()) { |
| 50 |
if (clazy::isInMacro(astContext(), recordStart, "Q_GLOBAL_STATIC_INTERNAL")) { |
| 51 |
return; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
CXXConstructorDecl *copyCtor = Utils::copyCtor(record); |
| 56 |
CXXMethodDecl *copyAssign = Utils::copyAssign(record); |
| 57 |
CXXDestructorDecl *destructor = nullptr; |
| 58 |
// Getting the destructor using record->getDestructor() does not work for later clang versions, e.g. clang 16 |
| 59 |
for (auto *decl : record->decls()) { |
| 60 |
if (auto *destructorDecl = dyn_cast<CXXDestructorDecl>(decl)) { |
| 61 |
destructor = destructorDecl; |
| 62 |
break; |
| 63 |
} |
| 64 |
} |
| 65 |
const bool dtorDefaultedByUser = destructor && destructor->isDefaulted() && !destructor->isImplicit(); |
| 66 |
|
| 67 |
const bool hasUserCopyCtor = copyCtor && copyCtor->isUserProvided(); |
| 68 |
const bool hasUserCopyAssign = copyAssign && copyAssign->isUserProvided(); |
| 69 |
const bool hasUserDtor = destructor && destructor->isUserProvided(); |
| 70 |
|
| 71 |
const bool copyCtorIsDeleted = copyCtor && copyCtor->isDeleted(); |
| 72 |
const bool copyAssignIsDeleted = copyAssign && copyAssign->isDeleted(); |
| 73 |
|
| 74 |
bool hasImplicitDeletedCopy = false; |
| 75 |
if (!copyCtor || !copyAssign) { |
| 76 |
for (auto *f : record->fields()) { |
| 77 |
QualType qt = f->getType(); |
| 78 |
if (qt.isConstQualified() || qt->isRValueReferenceType()) { |
| 79 |
hasImplicitDeletedCopy = true; |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if (hasUserDtor && (copyCtorIsDeleted || copyAssignIsDeleted || hasImplicitDeletedCopy)) { |
| 86 |
// One of the copy methods was explicitely deleted, it's safe. |
| 87 |
// The case we want to catch is when one is user-written and the other is |
| 88 |
// compiler-generated. |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
const int numImplemented = hasUserCopyCtor + hasUserCopyAssign + hasUserDtor; |
| 93 |
if (numImplemented == 0 || numImplemented == 3) { // Rule of 3 respected |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
std::vector<StringRef> hasList; |
| 98 |
std::vector<StringRef> missingList; |
| 99 |
if (hasUserDtor) { |
| 100 |
hasList.emplace_back("dtor"); |
| 101 |
} else { |
| 102 |
missingList.emplace_back("dtor"); |
| 103 |
} |
| 104 |
|
| 105 |
if (hasUserCopyCtor) { |
| 106 |
hasList.emplace_back("copy-ctor"); |
| 107 |
} else { |
| 108 |
missingList.emplace_back("copy-ctor"); |
| 109 |
} |
| 110 |
|
| 111 |
if (hasUserCopyAssign) { |
| 112 |
hasList.emplace_back("copy-assignment"); |
| 113 |
} else { |
| 114 |
missingList.emplace_back("copy-assignment"); |
| 115 |
} |
| 116 |
|
| 117 |
const int numNotImplemented = missingList.size(); |
| 118 |
|
| 119 |
if (hasUserDtor && numImplemented == 1) { |
| 120 |
// Protected dtor is a way for a non-polymorphic base class avoid being deleted |
| 121 |
if (destructor->getAccess() == clang::AccessSpecifier::AS_protected) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
if (Utils::functionHasEmptyBody(destructor)) { |
| 126 |
// Lets reduce noise and allow the empty dtor. In theory we could warn, but there's no |
| 127 |
// hidden bug behind this dummy dtor. |
| 128 |
return; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if (!hasUserDtor && (clazy::derivesFrom(record, "QSharedData") || dtorDefaultedByUser)) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
if (Utils::hasMember(record, "QSharedDataPointer")) { |
| 137 |
return; // These need boiler-plate copy ctor and dtor |
| 138 |
} |
| 139 |
|
| 140 |
const std::string className = record->getNameAsString(); |
| 141 |
const std::string classQualifiedName = record->getQualifiedNameAsString(); |
| 142 |
const std::string filename = sm().getFilename(recordStart).str(); |
| 143 |
if (clazy::endsWith(className, "Private") && clazy::endsWithAny(filename, {".cpp", ".cxx", "_p.h"})) { |
| 144 |
return; // Lots of RAII classes fall into this category. And even Private (d-pointer) classes, warning in that case would just be noise |
| 145 |
} |
| 146 |
|
| 147 |
std::string msg = classQualifiedName + " has "; |
| 148 |
|
| 149 |
for (int i = 0; i < numImplemented; ++i) { |
| 150 |
msg += hasList[i]; |
| 151 |
const bool isLast = i == numImplemented - 1; |
| 152 |
if (!isLast) { |
| 153 |
msg += ','; |
| 154 |
} |
| 155 |
msg += ' '; |
| 156 |
} |
| 157 |
|
| 158 |
msg += "but not "; |
| 159 |
for (int i = 0; i < numNotImplemented; ++i) { |
| 160 |
msg += missingList[i]; |
| 161 |
const bool isLast = i == numNotImplemented - 1; |
| 162 |
if (!isLast) { |
| 163 |
msg += ", "; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
emitWarning(decl->getBeginLoc(), msg); |
| 168 |
} |