| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2020 Jesper K. Pedersen <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "use-chrono-in-qtimer.h" |
| 8 |
#include "ClazyContext.h" |
| 9 |
#include "FixItUtils.h" |
| 10 |
#include "HierarchyUtils.h" |
| 11 |
#include "PreProcessorVisitor.h" |
| 12 |
#include <clang/AST/AST.h> |
| 13 |
|
| 14 |
using namespace clang; |
| 15 |
|
| 16 |
static int unpackValue(clang::Expr *expr) |
| 17 |
{ |
| 18 |
auto *value = dyn_cast<IntegerLiteral>(expr); |
| 19 |
if (value) { |
| 20 |
return static_cast<int>(*value->getValue().getRawData()); |
| 21 |
} |
| 22 |
|
| 23 |
auto *binaryOp = dyn_cast<BinaryOperator>(expr); |
| 24 |
if (!binaryOp) { |
| 25 |
return -1; |
| 26 |
} |
| 27 |
|
| 28 |
int left = unpackValue(binaryOp->getLHS()); |
| 29 |
int right = unpackValue(binaryOp->getRHS()); |
| 30 |
if (left == -1 || right == -1) { |
| 31 |
return -1; |
| 32 |
} |
| 33 |
|
| 34 |
if (binaryOp->getOpcode() == BO_Mul) { |
| 35 |
return left * right; |
| 36 |
} |
| 37 |
|
| 38 |
if (binaryOp->getOpcode() == BO_Div) { |
| 39 |
return left / right; |
| 40 |
} |
| 41 |
|
| 42 |
return -1; |
| 43 |
} |
| 44 |
|
| 45 |
void UseChronoInQTimer::warn(const clang::Stmt *stmt, int value) |
| 46 |
{ |
| 47 |
if (value == 0) { |
| 48 |
return; // ignore zero times; |
| 49 |
} |
| 50 |
|
| 51 |
std::string suggestion; |
| 52 |
if (value % (1000 * 3600) == 0) { |
| 53 |
suggestion = std::to_string(value / 1000 / 3600) + "h"; |
| 54 |
} else if (value % (1000 * 60) == 0) { |
| 55 |
suggestion = std::to_string(value / 1000 / 60) + "min"; |
| 56 |
} else if (value % 1000 == 0) { |
| 57 |
suggestion = std::to_string(value / 1000) + "s"; |
| 58 |
} else { |
| 59 |
suggestion = std::to_string(value) + "ms"; |
| 60 |
} |
| 61 |
|
| 62 |
std::vector<FixItHint> fixits; |
| 63 |
fixits.push_back(FixItHint::CreateReplacement(stmt->getSourceRange(), suggestion)); |
| 64 |
|
| 65 |
if (!m_hasInsertedInclude && m_context->preprocessorVisitor && !m_context->preprocessorVisitor->hasInclude("chrono", true)) { |
| 66 |
fixits.push_back(clazy::createInsertion(m_context->preprocessorVisitor->endOfIncludeSection(), |
| 67 |
"\n" |
| 68 |
"#include <chrono>\n\n" |
| 69 |
"using namespace std::chrono_literals;")); |
| 70 |
} |
| 71 |
m_hasInsertedInclude = true; |
| 72 |
|
| 73 |
emitWarning(stmt->getBeginLoc(), "make code more robust: use " + suggestion + " instead.", fixits); |
| 74 |
} |
| 75 |
|
| 76 |
static std::string functionName(CallExpr *callExpr) |
| 77 |
{ |
| 78 |
auto *memberCall = clazy::getFirstChildOfType<MemberExpr>(callExpr); |
| 79 |
if (memberCall) { |
| 80 |
auto *methodDecl = dyn_cast<CXXMethodDecl>(memberCall->getMemberDecl()); |
| 81 |
if (!methodDecl) { |
| 82 |
return {}; |
| 83 |
} |
| 84 |
return methodDecl->getQualifiedNameAsString(); |
| 85 |
} |
| 86 |
|
| 87 |
FunctionDecl *fdecl = callExpr->getDirectCallee(); |
| 88 |
if (fdecl) { |
| 89 |
return fdecl->getQualifiedNameAsString(); |
| 90 |
} |
| 91 |
|
| 92 |
return {}; |
| 93 |
} |
| 94 |
|
| 95 |
void UseChronoInQTimer::VisitStmt(clang::Stmt *stmt) |
| 96 |
{ |
| 97 |
auto *callExpr = dyn_cast<CallExpr>(stmt); |
| 98 |
if (!callExpr) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
if (callExpr->getNumArgs() == 0) { |
| 103 |
return; // start() doesn't take any arguments. |
| 104 |
} |
| 105 |
|
| 106 |
std::string name = trimQtNamespace(functionName(callExpr)); |
| 107 |
if (name != "QTimer::setInterval" && name != "QTimer::start" && name != "QTimer::singleShot") { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
const int value = unpackValue(callExpr->getArg(0)); |
| 112 |
if (value == -1) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
warn(callExpr->getArg(0), value); |
| 117 |
} |