| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "copyable-polymorphic.h" |
| 8 |
#include "AccessSpecifierManager.h" |
| 9 |
#include "ClazyContext.h" |
| 10 |
#include "FixItUtils.h" |
| 11 |
#include "StringUtils.h" |
| 12 |
#include "Utils.h" |
| 13 |
|
| 14 |
#include <clang/AST/DeclCXX.h> |
| 15 |
#include <clang/Basic/LLVM.h> |
| 16 |
#include <clang/Basic/Specifiers.h> |
| 17 |
#include <llvm/Support/Casting.h> |
| 18 |
|
| 19 |
using namespace clang; |
| 20 |
|
| 21 |
/// Returns whether the class has non-private copy-ctor or copy-assign |
| 22 |
static bool hasPublicCopy(const CXXRecordDecl *record) |
| 23 |
{ |
| 24 |
CXXConstructorDecl *copyCtor = Utils::copyCtor(record); |
| 25 |
const bool hasCallableCopyCtor = copyCtor && !copyCtor->isDeleted() && copyCtor->getAccess() == clang::AS_public; |
| 26 |
if (!hasCallableCopyCtor) { |
| 27 |
CXXMethodDecl *copyAssign = Utils::copyAssign(record); |
| 28 |
const bool hasCallableCopyAssign = copyAssign && !copyAssign->isDeleted() && copyAssign->getAccess() == clang::AS_public; |
| 29 |
if (!hasCallableCopyAssign) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
return true; |
| 35 |
} |
| 36 |
|
| 37 |
/// Checks if there's any base class with public copy |
| 38 |
static bool hasPublicCopyInAncestors(const CXXRecordDecl *record) |
| 39 |
{ |
| 40 |
if (!record) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
for (auto base : record->bases()) { |
| 45 |
if (const Type *t = base.getType().getTypePtrOrNull()) { |
| 46 |
const CXXRecordDecl *baseRecord = t->getAsCXXRecordDecl(); |
| 47 |
if (hasPublicCopy(baseRecord)) { |
| 48 |
return true; |
| 49 |
} |
| 50 |
if (hasPublicCopyInAncestors(t->getAsCXXRecordDecl())) { |
| 51 |
return true; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
void CopyablePolymorphic::VisitDecl(clang::Decl *decl) |
| 60 |
{ |
| 61 |
auto *record = dyn_cast<CXXRecordDecl>(decl); |
| 62 |
if (!record || !record->hasDefinition() || record->getDefinition() != record || !record->isPolymorphic()) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
if (!hasPublicCopy(record)) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
if (record->isEffectivelyFinal() && !hasPublicCopyInAncestors(record)) { |
| 71 |
// If the derived class is final, and all the base classes copy-ctors are protected or private then it's ok |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
emitWarning(record->getBeginLoc(), "Polymorphic class " + record->getQualifiedNameAsString() + " is copyable. Potential slicing.", fixits(record)); |
| 76 |
} |
| 77 |
|
| 78 |
std::vector<clang::FixItHint> CopyablePolymorphic::fixits(clang::CXXRecordDecl *record) |
| 79 |
{ |
| 80 |
std::vector<FixItHint> result; |
| 81 |
if (!m_context->accessSpecifierManager) { |
| 82 |
return {}; |
| 83 |
} |
| 84 |
|
| 85 |
const StringRef className = clazy::name(record); |
| 86 |
|
| 87 |
// Insert Q_DISABLE_COPY(classname) in the private section if one exists, |
| 88 |
// otherwise at the end of the class declaration |
| 89 |
SourceLocation pos = m_context->accessSpecifierManager->firstLocationOfSection(clang::AccessSpecifier::AS_private, record); |
| 90 |
|
| 91 |
if (pos.isValid()) { |
| 92 |
pos = Lexer::findLocationAfterToken(pos, clang::tok::colon, sm(), lo(), false); |
| 93 |
result.push_back(clazy::createInsertion(pos, std::string("\n\tQ_DISABLE_COPY(") + className.data() + std::string(")"))); |
| 94 |
} else { |
| 95 |
pos = record->getBraceRange().getEnd(); |
| 96 |
result.push_back(clazy::createInsertion(pos, std::string("\tQ_DISABLE_COPY(") + className.data() + std::string(")\n"))); |
| 97 |
} |
| 98 |
|
| 99 |
// If the class has a default constructor, then we need to readd it, |
| 100 |
// as the disabled copy constructor removes it. |
| 101 |
// Add it in the public section if one exists, otherwise add a |
| 102 |
// public section at the top of the class declaration. |
| 103 |
if (record->hasDefaultConstructor()) { |
| 104 |
pos = m_context->accessSpecifierManager->firstLocationOfSection(clang::AccessSpecifier::AS_public, record); |
| 105 |
if (pos.isInvalid()) { |
| 106 |
pos = record->getBraceRange().getBegin().getLocWithOffset(1); |
| 107 |
result.push_back(clazy::createInsertion(pos, std::string("\npublic:\n\t") + className.data() + std::string("() = default;"))); |
| 108 |
} else { |
| 109 |
pos = Lexer::findLocationAfterToken(pos, clang::tok::colon, sm(), lo(), false); |
| 110 |
result.push_back(clazy::createInsertion(pos, std::string("\n\t") + className.data() + std::string("() = default;"))); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return result; |
| 115 |
} |