| 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-two-soft.h" |
| 8 |
|
| 9 |
#include <clang/AST/Decl.h> |
| 10 |
#include <clang/AST/DeclCXX.h> |
| 11 |
#include <clang/AST/ExprCXX.h> |
| 12 |
#include <clang/AST/Stmt.h> |
| 13 |
#include <clang/Basic/LLVM.h> |
| 14 |
#include <llvm/Support/Casting.h> |
| 15 |
|
| 16 |
using namespace clang; |
| 17 |
|
| 18 |
void RuleOfTwoSoft::VisitStmt(Stmt *s) |
| 19 |
{ |
| 20 |
if (auto *op = dyn_cast<CXXOperatorCallExpr>(s)) { |
| 21 |
FunctionDecl *func = op->getDirectCallee(); |
| 22 |
auto *method = func ? dyn_cast<CXXMethodDecl>(func) : nullptr; |
| 23 |
if (method && method->getParent() && method->isCopyAssignmentOperator()) { |
| 24 |
CXXRecordDecl *record = method->getParent(); |
| 25 |
const bool hasCopyCtor = record->hasNonTrivialCopyConstructor(); |
| 26 |
const bool hasCopyAssignOp = record->hasNonTrivialCopyAssignment() || method->isExplicitlyDefaulted(); |
| 27 |
|
| 28 |
if (hasCopyCtor && !hasCopyAssignOp && !isBlacklisted(record)) { |
| 29 |
std::string msg = "Using assign operator but class " + record->getQualifiedNameAsString() + " has copy-ctor but no assign operator"; |
| 30 |
emitWarning(s->getBeginLoc(), msg); |
| 31 |
} |
| 32 |
} |
| 33 |
} else if (auto *ctorExpr = dyn_cast<CXXConstructExpr>(s)) { |
| 34 |
CXXConstructorDecl *ctorDecl = ctorExpr->getConstructor(); |
| 35 |
CXXRecordDecl *record = ctorDecl->getParent(); |
| 36 |
if (ctorDecl->isCopyConstructor() && record) { |
| 37 |
const bool hasCopyCtor = record->hasNonTrivialCopyConstructor() || ctorDecl->isExplicitlyDefaulted(); |
| 38 |
const bool hasCopyAssignOp = record->hasNonTrivialCopyAssignment(); |
| 39 |
if (!hasCopyCtor && hasCopyAssignOp && !isBlacklisted(record)) { |
| 40 |
std::string msg = |
| 41 |
"Using copy-ctor but class " + record->getQualifiedNameAsString() + " has a trivial copy-ctor but non trivial assign operator"; |
| 42 |
emitWarning(s->getBeginLoc(), msg); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |