/* SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com SPDX-FileContributor: Sérgio Martins SPDX-FileCopyrightText: 2015 Sergio Martins SPDX-FileCopyrightText: 2024 Alexander Lohnau SPDX-License-Identifier: LGPL-2.0-or-later */ #include "qdatetime-utc.h" #include "FixItUtils.h" #include "Utils.h" #include #include #include #include #include #include #include #include #include using namespace clang; void QDateTimeUtc::VisitStmt(clang::Stmt *stmt) { auto *secondCall = dyn_cast(stmt); if (!secondCall || !secondCall->getMethodDecl()) { return; } CXXMethodDecl *secondMethod = secondCall->getMethodDecl(); std::string secondMethodName = trimQtNamespace(secondMethod->getQualifiedNameAsString()); const bool isMSecSinceEpoc = secondMethodName == "QDateTime::toMSecsSinceEpoch"; const bool isSecSinceEpoc = secondMethodName == "QDateTime::toSecsSinceEpoch" || secondMethodName == "QDateTime::toTime_t"; const bool isToUtcConversion = secondMethodName == "QDateTime::toUTC"; if (!isMSecSinceEpoc && !isSecSinceEpoc && !isToUtcConversion) { return; } std::vector chainedCalls = Utils::callListForChain(secondCall); if (chainedCalls.size() != 2) { return; } CallExpr *firstCall = chainedCalls[chainedCalls.size() - 1]; FunctionDecl *firstFunc = firstCall->getDirectCallee(); if (!firstFunc) { return; } if (auto *firstMethod = dyn_cast(firstFunc); !firstMethod || (firstMethod->getQualifiedNameAsString() != qtNamespaced("QDateTime::currentDateTime") && firstMethod->getQualifiedNameAsString() != qtNamespaced("QDateTime::currentDateTimeUtc"))) { return; } std::string replacement = "::currentDateTimeUtc()"; if (isMSecSinceEpoc) { replacement = "::currentMSecsSinceEpoch()"; } else if (isSecSinceEpoc) { replacement = "::currentSecsSinceEpoch()"; } std::vector fixits; clazy::transformTwoCallsIntoOneV2(astContext(), secondCall, replacement, fixits); emitWarning(stmt->getBeginLoc(), "Use QDateTime" + replacement + " instead. It is significantly faster", fixits); }