| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "skipped-base-method.h" |
| 8 |
#include "FunctionUtils.h" |
| 9 |
#include "HierarchyUtils.h" |
| 10 |
#include "TypeUtils.h" |
| 11 |
|
| 12 |
#include <clang/AST/DeclCXX.h> |
| 13 |
#include <clang/AST/Expr.h> |
| 14 |
#include <clang/AST/ExprCXX.h> |
| 15 |
#include <clang/AST/Stmt.h> |
| 16 |
#include <clang/AST/Type.h> |
| 17 |
#include <clang/Basic/LLVM.h> |
| 18 |
#include <llvm/Support/Casting.h> |
| 19 |
|
| 20 |
#include <vector> |
| 21 |
|
| 22 |
using namespace clang; |
| 23 |
|
| 24 |
void SkippedBaseMethod::VisitStmt(clang::Stmt *stmt) |
| 25 |
{ |
| 26 |
auto *memberCall = dyn_cast<CXXMemberCallExpr>(stmt); |
| 27 |
if (!memberCall) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
auto *expr = memberCall->getImplicitObjectArgument(); |
| 32 |
auto *thisExpr = clazy::unpeal<CXXThisExpr>(expr, clazy::IgnoreImplicitCasts); |
| 33 |
if (!thisExpr) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
const CXXRecordDecl *thisClass = thisExpr->getType()->getPointeeCXXRecordDecl(); |
| 38 |
const CXXRecordDecl *baseClass = memberCall->getRecordDecl(); |
| 39 |
|
| 40 |
std::vector<CXXRecordDecl *> baseClasses; |
| 41 |
if (!clazy::derivesFrom(thisClass, baseClass, &baseClasses) || baseClasses.size() < 2) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
// We're calling a grand-base method, so check if a more direct base also implements it |
| 46 |
for (int i = baseClasses.size() - 1; i > 0; --i) { // the higher indexes have the most derived classes |
| 47 |
CXXRecordDecl *moreDirectBaseClass = baseClasses[i]; |
| 48 |
if (clazy::classImplementsMethod(moreDirectBaseClass, memberCall->getMethodDecl())) { |
| 49 |
std::string msg = |
| 50 |
"Maybe you meant to call " + moreDirectBaseClass->getNameAsString() + "::" + memberCall->getMethodDecl()->getNameAsString() + "() instead"; |
| 51 |
emitWarning(stmt, msg); |
| 52 |
} |
| 53 |
} |
| 54 |
} |