| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 3 |
SPDX-FileContributor: Sérgio Martins <[email protected]> |
| 4 |
|
| 5 |
SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]> |
| 6 |
|
| 7 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef FUNCTION_UTILS_H |
| 11 |
#define FUNCTION_UTILS_H |
| 12 |
|
| 13 |
// Contains utility functions regarding functions and methods |
| 14 |
|
| 15 |
#include "HierarchyUtils.h" |
| 16 |
#include "StringUtils.h" |
| 17 |
#include "Utils.h" |
| 18 |
|
| 19 |
#include <clang/AST/Decl.h> |
| 20 |
|
| 21 |
namespace clazy |
| 22 |
{ |
| 23 |
inline clang::ValueDecl *valueDeclForCallArgument(clang::CallExpr *call, unsigned int argIndex) |
| 24 |
{ |
| 25 |
if (!call || call->getNumArgs() <= argIndex) { |
| 26 |
return nullptr; |
| 27 |
} |
| 28 |
|
| 29 |
clang::Expr *firstArg = call->getArg(argIndex); |
| 30 |
auto *declRef = |
| 31 |
llvm::isa<clang::DeclRefExpr>(firstArg) ? llvm::cast<clang::DeclRefExpr>(firstArg) : clazy::getFirstChildOfType2<clang::DeclRefExpr>(firstArg); |
| 32 |
if (!declRef) { |
| 33 |
return nullptr; |
| 34 |
} |
| 35 |
|
| 36 |
return declRef->getDecl(); |
| 37 |
} |
| 38 |
|
| 39 |
inline bool parametersMatch(const clang::FunctionDecl *f1, const clang::FunctionDecl *f2) |
| 40 |
{ |
| 41 |
if (!f1 || !f2) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
auto params1 = f1->parameters(); |
| 46 |
auto params2 = f2->parameters(); |
| 47 |
|
| 48 |
if (params1.size() != params2.size()) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
for (int i = 0, e = params1.size(); i < e; ++i) { |
| 53 |
clang::ParmVarDecl *p1 = params1[i]; |
| 54 |
clang::ParmVarDecl *p2 = params2[i]; |
| 55 |
|
| 56 |
if (p1->getType() != p2->getType()) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns true if a class contains a method with a specific signature. |
| 66 |
* (method->getParent() doesn't need to equal record) |
| 67 |
*/ |
| 68 |
inline bool classImplementsMethod(const clang::CXXRecordDecl *record, const clang::CXXMethodDecl *method) |
| 69 |
{ |
| 70 |
if (!method->getDeclName().isIdentifier()) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
llvm::StringRef methodName = clazy::name(method); |
| 75 |
for (auto *m : record->methods()) { |
| 76 |
if (!m->isPureVirtual() && clazy::name(m) == methodName && parametersMatch(m, method)) { |
| 77 |
return true; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
#endif |