| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2020 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 3 |
SPDX-FileContributor: Nicolas Fella <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "jnisignatures.h" |
| 9 |
#include "HierarchyUtils.h" |
| 10 |
#include "StringUtils.h" |
| 11 |
#include "clazy_stl.h" |
| 12 |
|
| 13 |
#include <clang/AST/Decl.h> |
| 14 |
#include <clang/AST/DeclCXX.h> |
| 15 |
#include <clang/AST/Expr.h> |
| 16 |
#include <clang/AST/ExprCXX.h> |
| 17 |
#include <clang/AST/OperationKinds.h> |
| 18 |
#include <clang/AST/Stmt.h> |
| 19 |
#include <clang/Basic/LLVM.h> |
| 20 |
#include <clang/Basic/SourceLocation.h> |
| 21 |
#include <clang/Basic/SourceManager.h> |
| 22 |
#include <llvm/ADT/StringRef.h> |
| 23 |
#include <llvm/Support/Casting.h> |
| 24 |
|
| 25 |
using namespace clang; |
| 26 |
|
| 27 |
static const std::regex methodSignatureRegex("\\((\\[?([ZBCSIJFD]|L([a-zA-Z]+\\/)*[a-zA-Z]+;))*\\)\\[?([ZBCSIJFD]|L([a-zA-Z]+\\/)*[a-zA-Z]+;|V)"); |
| 28 |
|
| 29 |
static const std::regex classNameRegex("([a-zA-Z]+\\/)*[a-zA-Z]+"); |
| 30 |
|
| 31 |
static const std::regex methodNameRegex("[a-zA-Z]+"); |
| 32 |
|
| 33 |
bool checkSignature(const std::string &signature, const std::regex &expr) |
| 34 |
{ |
| 35 |
std::smatch match; |
| 36 |
return std::regex_match(signature, match, expr); |
| 37 |
} |
| 38 |
|
| 39 |
template<typename T> |
| 40 |
void JniSignatures::checkArgAt(T *call, unsigned int index, const std::regex &expr, const std::string &errorMessage) |
| 41 |
{ |
| 42 |
if (call->getNumArgs() < index + 1) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
auto *stringLiteral = clazy::getFirstChildOfType2<StringLiteral>(call->getArg(index)); |
| 47 |
|
| 48 |
if (!stringLiteral) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
if (stringLiteral->getCharByteWidth() != 1) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
const std::string signature = stringLiteral->getString().str(); |
| 57 |
|
| 58 |
const bool valid = checkSignature(signature, expr); |
| 59 |
|
| 60 |
if (!valid) { |
| 61 |
emitWarning(call, errorMessage + ": '" + signature + "'"); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void JniSignatures::checkFunctionCall(Stmt *stm) |
| 66 |
{ |
| 67 |
auto *callExpr = dyn_cast<CallExpr>(stm); |
| 68 |
if (!callExpr) { |
| 69 |
return; |
| 70 |
} |
| 71 |
auto *funDecl = callExpr->getDirectCallee(); |
| 72 |
if (!funDecl) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
const std::string qualifiedName = funDecl->getQualifiedNameAsString(); |
| 77 |
if (!clazy::startsWith(qualifiedName, "QAndroidJniObject::")) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
const std::string name = clazy::name(funDecl).str(); |
| 82 |
|
| 83 |
if (name == "callObjectMethod" || name == "callMethod") { |
| 84 |
checkArgAt(callExpr, 0, methodNameRegex, "Invalid method name"); |
| 85 |
checkArgAt(callExpr, 1, methodSignatureRegex, "Invalid method signature"); |
| 86 |
} else if (name == "callStaticObjectMethod" || name == "callStaticMethod") { |
| 87 |
checkArgAt(callExpr, 0, classNameRegex, "Invalid class name"); |
| 88 |
checkArgAt(callExpr, 1, methodNameRegex, "Invalid method name"); |
| 89 |
checkArgAt(callExpr, 2, methodSignatureRegex, "Invalid method signature"); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void JniSignatures::checkConstructorCall(Stmt *stm) |
| 94 |
{ |
| 95 |
auto *constructExpr = dyn_cast<CXXConstructExpr>(stm); |
| 96 |
if (!constructExpr) { |
| 97 |
return; |
| 98 |
} |
| 99 |
auto *funDecl = constructExpr->getConstructor(); |
| 100 |
|
| 101 |
const std::string qualifiedName = funDecl->getQualifiedNameAsString(); |
| 102 |
if (qualifiedName != "QAndroidJniObject::QAndroidJniObject") { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
checkArgAt(constructExpr, 0, classNameRegex, "Invalid class name"); |
| 107 |
checkArgAt(constructExpr, 1, methodSignatureRegex, "Invalid constructor signature"); |
| 108 |
} |
| 109 |
|
| 110 |
void JniSignatures::VisitStmt(Stmt *stm) |
| 111 |
{ |
| 112 |
checkConstructorCall(stm); |
| 113 |
checkFunctionCall(stm); |
| 114 |
} |