Debian Codegraph @

src/checks/level0/qfileinfo-exists.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]>
3 SPDX-FileCopyrightText: 2023 Alexander Lohnau <[email protected]>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8 #include "qfileinfo-exists.h"
9 #include "HierarchyUtils.h"
10 #include "StringUtils.h"
11
12 #include <clang/AST/DeclCXX.h>
13 #include <clang/AST/ExprCXX.h>
14 #include <clang/AST/Stmt.h>
15 #include <clang/Basic/LLVM.h>
16 #include <llvm/Support/Casting.h>
17
18 using namespace clang;
19
20 void QFileInfoExists::VisitStmt(clang::Stmt *stmt)
21 {
22 auto *existsCall = dyn_cast<CXXMemberCallExpr>(stmt);
23 std::string methodName = clazy::qualifiedMethodName(existsCall);
24 if (methodName != "QFileInfo::exists") {
25 return;
26 }
27
28 auto *ctorExpr = clazy::getFirstChildOfType<CXXConstructExpr>(existsCall);
29 if (!ctorExpr || clazy::simpleArgTypeName(ctorExpr->getConstructor(), 0, lo()) != "QString") {
30 return;
31 }
32
33 std::string userArgText = Lexer::getSourceText(CharSourceRange::getTokenRange(ctorExpr->getArg(0)->getSourceRange()), sm(), lo()).str();
34 emitWarning(stmt->getBeginLoc(),
35 "Use the static QFileInfo::exists() instead. It's documented to be faster.",
36 {clang::FixItHint::CreateReplacement(stmt->getSourceRange(), "QFileInfo::exists(" + userArgText + ")")});
37 }