| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2018 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "empty-qstringliteral.h" |
| 8 |
#include "ClazyContext.h" |
| 9 |
#include "HierarchyUtils.h" |
| 10 |
#include "PreProcessorVisitor.h" |
| 11 |
#include "QtUtils.h" |
| 12 |
|
| 13 |
#include <clang/AST/Decl.h> |
| 14 |
#include <clang/AST/Expr.h> |
| 15 |
#include <clang/AST/Stmt.h> |
| 16 |
#include <clang/Basic/LLVM.h> |
| 17 |
#include <clang/Basic/SourceLocation.h> |
| 18 |
#include <llvm/ADT/StringRef.h> |
| 19 |
#include <llvm/Support/Casting.h> |
| 20 |
|
| 21 |
using namespace clang; |
| 22 |
|
| 23 |
void EmptyQStringliteral::VisitStmt(clang::Stmt *stmt) |
| 24 |
{ |
| 25 |
if (!stmt->getBeginLoc().isMacroID()) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
const std::string filepath = sm().getFilename(sm().getExpansionLoc(stmt->getBeginLoc())).str(); |
| 30 |
if (clazy::contains(filepath, ".rcc/qmlcache/")) { |
| 31 |
return; // This is an autogenerated file |
| 32 |
} |
| 33 |
if (auto splitted = clazy::splitString(filepath, '/'); !splitted.empty()) { |
| 34 |
if (std::string filename = splitted[splitted.size() - 1]; clazy::startsWith(filename, "ui_") && clazy::endsWith(filename, ".h")) { |
| 35 |
return; // Also ignore all ui files |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
if (m_context->preprocessorVisitor && m_context->preprocessorVisitor->qtVersion() >= 60000) { // The AST looks very different in Qt6 |
| 40 |
handleQt6StringLiteral(stmt); |
| 41 |
} else { |
| 42 |
handleQt5StringLiteral(stmt); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
void EmptyQStringliteral::handleQt6StringLiteral(clang::Stmt *stmt) |
| 47 |
{ |
| 48 |
if (auto c = dyn_cast<CallExpr>(stmt)) { |
| 49 |
if (clazy::qualifiedMethodName(c->getDirectCallee()) == qtNamespaced("QtPrivate::qMakeStringPrivate")) { |
| 50 |
if (auto lt = clazy::getFirstChildOfType<StringLiteral>(c); lt && lt->getByteLength() == 0) { |
| 51 |
emitWarning(stmt, "Use QLatin1String(\"\") or QString() instead of an empty QStringLiteral"); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
void EmptyQStringliteral::handleQt5StringLiteral(clang::Stmt *stmt) |
| 58 |
{ |
| 59 |
auto *declstm = dyn_cast<DeclStmt>(stmt); |
| 60 |
if (!declstm || !declstm->isSingleDecl()) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
auto *vd = dyn_cast<VarDecl>(declstm->getSingleDecl()); |
| 65 |
if (!vd || clazy::name(vd) != "qstring_literal") { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
Expr *expr = vd->getInit(); |
| 70 |
auto *initListExpr = expr ? dyn_cast<InitListExpr>(expr) : nullptr; |
| 71 |
if (!initListExpr || initListExpr->getNumInits() != 2) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
Expr *init = initListExpr->getInit(1); |
| 76 |
auto *literal = init ? dyn_cast<StringLiteral>(init) : nullptr; |
| 77 |
if (!literal || literal->getByteLength() != 0) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
emitWarning(stmt, "Use QLatin1String(\"\") or QString() instead of an empty QStringLiteral"); |
| 82 |
} |