| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 3 |
SPDX-FileContributor: Sérgio Martins <[email protected]> |
| 4 |
|
| 5 |
SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]> |
| 6 |
SPDX-FileCopyrightText: 2024 Alexander Lohnau <[email protected]> |
| 7 |
|
| 8 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "qdatetime-utc.h" |
| 12 |
#include "FixItUtils.h" |
| 13 |
#include "Utils.h" |
| 14 |
|
| 15 |
#include <clang/AST/Decl.h> |
| 16 |
#include <clang/AST/DeclCXX.h> |
| 17 |
#include <clang/AST/Expr.h> |
| 18 |
#include <clang/AST/ExprCXX.h> |
| 19 |
#include <clang/AST/Stmt.h> |
| 20 |
#include <clang/Basic/Diagnostic.h> |
| 21 |
#include <clang/Basic/LLVM.h> |
| 22 |
#include <llvm/Support/Casting.h> |
| 23 |
|
| 24 |
#include <vector> |
| 25 |
|
| 26 |
using namespace clang; |
| 27 |
|
| 28 |
void QDateTimeUtc::VisitStmt(clang::Stmt *stmt) |
| 29 |
{ |
| 30 |
auto *secondCall = dyn_cast<CXXMemberCallExpr>(stmt); |
| 31 |
if (!secondCall || !secondCall->getMethodDecl()) { |
| 32 |
return; |
| 33 |
} |
| 34 |
CXXMethodDecl *secondMethod = secondCall->getMethodDecl(); |
| 35 |
std::string secondMethodName = trimQtNamespace(secondMethod->getQualifiedNameAsString()); |
| 36 |
|
| 37 |
const bool isMSecSinceEpoc = secondMethodName == "QDateTime::toMSecsSinceEpoch"; |
| 38 |
const bool isSecSinceEpoc = secondMethodName == "QDateTime::toSecsSinceEpoch" || secondMethodName == "QDateTime::toTime_t"; |
| 39 |
const bool isToUtcConversion = secondMethodName == "QDateTime::toUTC"; |
| 40 |
if (!isMSecSinceEpoc && !isSecSinceEpoc && !isToUtcConversion) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
std::vector<CallExpr *> chainedCalls = Utils::callListForChain(secondCall); |
| 45 |
if (chainedCalls.size() != 2) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
CallExpr *firstCall = chainedCalls[chainedCalls.size() - 1]; |
| 50 |
FunctionDecl *firstFunc = firstCall->getDirectCallee(); |
| 51 |
if (!firstFunc) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if (auto *firstMethod = dyn_cast<CXXMethodDecl>(firstFunc); !firstMethod |
| 56 |
|| (firstMethod->getQualifiedNameAsString() != qtNamespaced("QDateTime::currentDateTime") |
| 57 |
&& firstMethod->getQualifiedNameAsString() != qtNamespaced("QDateTime::currentDateTimeUtc"))) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
std::string replacement = "::currentDateTimeUtc()"; |
| 62 |
if (isMSecSinceEpoc) { |
| 63 |
replacement = "::currentMSecsSinceEpoch()"; |
| 64 |
} else if (isSecSinceEpoc) { |
| 65 |
replacement = "::currentSecsSinceEpoch()"; |
| 66 |
} |
| 67 |
|
| 68 |
std::vector<FixItHint> fixits; |
| 69 |
clazy::transformTwoCallsIntoOneV2(astContext(), secondCall, replacement, fixits); |
| 70 |
emitWarning(stmt->getBeginLoc(), "Use QDateTime" + replacement + " instead. It is significantly faster", fixits); |
| 71 |
} |