File indexing completed on 2024-05-12 05:40:56

0001 /*
0002     SPDX-FileCopyrightText: 2016 Sergio Martins <smartins@kde.org>
0003     SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "qfileinfo-exists.h"
0009 #include "HierarchyUtils.h"
0010 #include "StringUtils.h"
0011 
0012 #include <clang/AST/DeclCXX.h>
0013 #include <clang/AST/ExprCXX.h>
0014 #include <clang/AST/Stmt.h>
0015 #include <clang/Basic/LLVM.h>
0016 #include <llvm/Support/Casting.h>
0017 
0018 class ClazyContext;
0019 
0020 using namespace clang;
0021 
0022 QFileInfoExists::QFileInfoExists(const std::string &name, ClazyContext *context)
0023     : CheckBase(name, context, Option_CanIgnoreIncludes)
0024 {
0025 }
0026 
0027 void QFileInfoExists::VisitStmt(clang::Stmt *stmt)
0028 {
0029     auto *existsCall = dyn_cast<CXXMemberCallExpr>(stmt);
0030     std::string methodName = clazy::qualifiedMethodName(existsCall);
0031     if (methodName != "QFileInfo::exists") {
0032         return;
0033     }
0034 
0035     auto *ctorExpr = clazy::getFirstChildOfType<CXXConstructExpr>(existsCall);
0036     if (!ctorExpr || clazy::simpleArgTypeName(ctorExpr->getConstructor(), 0, lo()) != "QString") {
0037         return;
0038     }
0039 
0040     std::string userArgText = Lexer::getSourceText(CharSourceRange::getTokenRange(ctorExpr->getArg(0)->getSourceRange()), sm(), lo()).str();
0041     emitWarning(stmt->getBeginLoc(),
0042                 "Use the static QFileInfo::exists() instead. It's documented to be faster.",
0043                 {clang::FixItHint::CreateReplacement(stmt->getSourceRange(), "QFileInfo::exists(" + userArgText + ")")});
0044 }