Debian Codegraph @

src/checks/level0/qstring-insensitive-allocation.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "qstring-insensitive-allocation.h"
8 #include "StringUtils.h"
9 #include "Utils.h"
10 #include "clazy_stl.h"
11
12 #include <clang/AST/Expr.h>
13 #include <clang/AST/Stmt.h>
14 #include <clang/Basic/LLVM.h>
15 #include <llvm/Support/Casting.h>
16
17 #include <vector>
18
19 using namespace clang;
20
21 static bool isInterestingCall1(CallExpr *call)
22 {
23 FunctionDecl *func = call->getDirectCallee();
24 if (!func) {
25 return false;
26 }
27
28 static const std::vector<std::string> methods{"QString::toUpper", "QString::toLower"};
29 return clazy::contains(methods, clazy::qualifiedMethodName(func));
30 }
31
32 static bool isInterestingCall2(CallExpr *call)
33 {
34 FunctionDecl *func = call->getDirectCallee();
35 if (!func) {
36 return false;
37 }
38
39 static const std::vector<std::string> methods{"QString::endsWith", "QString::startsWith", "QString::contains", "QString::compare"};
40 return clazy::contains(methods, clazy::qualifiedMethodName(func));
41 }
42
43 void QStringInsensitiveAllocation::VisitStmt(clang::Stmt *stmt)
44 {
45 std::vector<CallExpr *> calls = Utils::callListForChain(dyn_cast<CallExpr>(stmt));
46 if (calls.size() < 2) {
47 return;
48 }
49
50 CallExpr *call1 = calls[calls.size() - 1];
51 CallExpr *call2 = calls[calls.size() - 2];
52
53 if (!isInterestingCall1(call1) || !isInterestingCall2(call2)) {
54 return;
55 }
56
57 emitWarning(stmt->getBeginLoc(), "unneeded allocation");
58 }