98 while (Multiplier != 1.0) {
99 std::optional<std::tuple<DurationScale, double>> Result =
103 if (std::get<1>(*Result) == 1.0)
104 return std::get<0>(*Result);
105 Multiplier = std::get<1>(*Result);
106 OldScale = std::get<0>(*Result);
115 callee(functionDecl(DurationFactoryFunction()).bind(
"call_decl")),
118 ignoringImpCasts(anyOf(
119 cxxFunctionalCastExpr(
121 anyOf(isInteger(), realFloatingPointType())),
122 hasSourceExpression(initListExpr())),
123 integerLiteral(equals(0)), floatLiteral(equals(0.0)),
124 binaryOperator(hasOperatorName(
"*"),
125 hasEitherOperand(ignoringImpCasts(
126 anyOf(integerLiteral(), floatLiteral()))))
128 binaryOperator(hasOperatorName(
"/"), hasRHS(floatLiteral()))
129 .bind(
"div_binop")))))
135 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
138 if (Call->getExprLoc().isMacroID())
141 const Expr *Arg = Call->getArg(0)->IgnoreParenImpCasts();
143 if (Arg->getBeginLoc().isMacroID())
148 diag(Call->getBeginLoc(),
149 "use ZeroDuration() for zero-length time intervals")
150 << FixItHint::CreateReplacement(Call->getSourceRange(),
151 "absl::ZeroDuration()");
155 const auto *CallDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"call_decl");
156 std::optional<DurationScale> MaybeScale =
162 const Expr *Remainder =
nullptr;
163 std::optional<DurationScale> NewScale;
166 if (
const auto *MultBinOp =
167 Result.Nodes.getNodeAs<BinaryOperator>(
"mult_binop")) {
172 const auto *IntLit = llvm::dyn_cast<IntegerLiteral>(MultBinOp->getLHS());
173 const auto *FloatLit = llvm::dyn_cast<FloatingLiteral>(MultBinOp->getLHS());
174 if (IntLit || FloatLit) {
177 Remainder = MultBinOp->getRHS();
182 IntLit = llvm::dyn_cast<IntegerLiteral>(MultBinOp->getRHS());
183 FloatLit = llvm::dyn_cast<FloatingLiteral>(MultBinOp->getRHS());
184 if (IntLit || FloatLit) {
187 Remainder = MultBinOp->getLHS();
190 }
else if (
const auto *DivBinOp =
191 Result.Nodes.getNodeAs<BinaryOperator>(
"div_binop")) {
194 const auto *FloatLit = llvm::cast<FloatingLiteral>(DivBinOp->getRHS());
196 std::optional<DurationScale> NewScale =
197 getNewScale(Scale, 1.0 / FloatLit->getValueAsApproximateDouble());
199 const Expr *Remainder = DivBinOp->getLHS();
203 diag(Call->getBeginLoc(),
"internal duration scaling can be removed")
204 << FixItHint::CreateReplacement(
205 Call->getSourceRange(),
207 tooling::fixit::getText(*Remainder, *Result.Context) +
")")
213 assert(Remainder &&
"No remainder found");
216 diag(Call->getBeginLoc(),
"internal duration scaling can be removed")
217 << FixItHint::CreateReplacement(
218 Call->getSourceRange(),
220 tooling::fixit::getText(*Remainder, *Result.Context) +
")")