Debian Codegraph @

src/checks/level1/qstring-left.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-left.h"
8 #include "StringUtils.h"
9
10 #include <clang/AST/Expr.h>
11 #include <clang/AST/ExprCXX.h>
12 #include <clang/AST/Stmt.h>
13 #include <clang/Basic/LLVM.h>
14 #include <llvm/ADT/APInt.h>
15 #include <llvm/Support/Casting.h>
16
17 using namespace clang;
18
19 void QStringLeft::VisitStmt(clang::Stmt *stmt)
20 {
21 auto *memberCall = dyn_cast<CXXMemberCallExpr>(stmt);
22 if (!memberCall || clazy::qualifiedMethodName(memberCall) != "QString::left") {
23 return;
24 }
25
26 if (memberCall->getNumArgs() == 0) { // Doesn't happen
27 return;
28 }
29
30 Expr *firstArg = memberCall->getArg(0);
31 auto *lt = firstArg ? dyn_cast<IntegerLiteral>(firstArg) : nullptr;
32 if (lt) {
33 const auto value = lt->getValue();
34 if (value == 0) {
35 emitWarning(stmt, "QString::left(0) returns an empty string");
36 } else if (value == 1) {
37 emitWarning(stmt, "Use QString::at(0) instead of QString::left(1) to avoid temporary allocations (just be sure the string isn't empty).");
38 }
39 }
40 }