File indexing completed on 2024-05-12 05:41:03

0001 /*
0002     SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com
0003     SPDX-FileContributor: SĂ©rgio Martins <sergio.martins@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "tr-non-literal.h"
0009 #include "HierarchyUtils.h"
0010 
0011 #include <clang/AST/Decl.h>
0012 #include <clang/AST/Expr.h>
0013 #include <clang/AST/Stmt.h>
0014 #include <clang/Basic/LLVM.h>
0015 #include <llvm/Support/Casting.h>
0016 
0017 class ClazyContext;
0018 
0019 using namespace clang;
0020 
0021 TrNonLiteral::TrNonLiteral(const std::string &name, ClazyContext *context)
0022     : CheckBase(name, context, Option_CanIgnoreIncludes)
0023 {
0024 }
0025 
0026 void TrNonLiteral::VisitStmt(clang::Stmt *stmt)
0027 {
0028     auto *callExpr = dyn_cast<CallExpr>(stmt);
0029     if (!callExpr || callExpr->getNumArgs() <= 0) {
0030         return;
0031     }
0032 
0033     FunctionDecl *func = callExpr->getDirectCallee();
0034     if (!func || func->getQualifiedNameAsString() != "QObject::tr") {
0035         return;
0036     }
0037 
0038     Expr *arg1 = callExpr->getArg(0);
0039     if (clazy::getFirstChildOfType2<StringLiteral>(arg1) == nullptr) {
0040         emitWarning(stmt, "tr() without a literal string");
0041     }
0042 }