12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Tooling/FixIt.h" 25 static llvm::Optional<DurationScale>
27 static const std::unordered_map<std::string, DurationScale> ScaleMap(
28 {{
"Nanoseconds", DurationScale::Nanoseconds},
29 {
"Microseconds", DurationScale::Microseconds},
30 {
"Milliseconds", DurationScale::Milliseconds},
31 {
"Seconds", DurationScale::Seconds},
32 {
"Minutes", DurationScale::Minutes},
33 {
"Hours", DurationScale::Hours}});
35 auto ScaleIter = ScaleMap.find(FactoryName);
36 if (ScaleIter == ScaleMap.end())
39 return ScaleIter->second;
44 static double GetValue(
const IntegerLiteral *IntLit,
45 const FloatingLiteral *FloatLit) {
47 return IntLit->getValue().getLimitedValue();
49 assert(FloatLit !=
nullptr &&
"Neither IntLit nor FloatLit set");
50 return FloatLit->getValueAsApproximateDouble();
56 static llvm::Optional<std::tuple<DurationScale, double>>
59 case DurationScale::Hours:
60 if (Multiplier <= 1.0 / 60.0)
61 return std::make_tuple(DurationScale::Minutes, Multiplier * 60.0);
64 case DurationScale::Minutes:
65 if (Multiplier >= 60.0)
66 return std::make_tuple(DurationScale::Hours, Multiplier / 60.0);
67 if (Multiplier <= 1.0 / 60.0)
68 return std::make_tuple(DurationScale::Seconds, Multiplier * 60.0);
71 case DurationScale::Seconds:
72 if (Multiplier >= 60.0)
73 return std::make_tuple(DurationScale::Minutes, Multiplier / 60.0);
74 if (Multiplier <= 1e-3)
75 return std::make_tuple(DurationScale::Milliseconds, Multiplier * 1e3);
78 case DurationScale::Milliseconds:
79 if (Multiplier >= 1e3)
80 return std::make_tuple(DurationScale::Seconds, Multiplier / 1e3);
81 if (Multiplier <= 1e-3)
82 return std::make_tuple(DurationScale::Microseconds, Multiplier * 1e3);
85 case DurationScale::Microseconds:
86 if (Multiplier >= 1e3)
87 return std::make_tuple(DurationScale::Milliseconds, Multiplier / 1e3);
88 if (Multiplier <= 1e-3)
89 return std::make_tuple(DurationScale::Nanoseconds, Multiplier * 1e-3);
92 case DurationScale::Nanoseconds:
93 if (Multiplier >= 1e3)
94 return std::make_tuple(DurationScale::Microseconds, Multiplier / 1e3);
105 while (Multiplier != 1.0) {
106 llvm::Optional<std::tuple<DurationScale, double>> result =
110 if (std::get<1>(*result) == 1.0)
111 return std::get<0>(*result);
112 Multiplier = std::get<1>(*result);
113 OldScale = std::get<0>(*result);
119 void DurationFactoryScaleCheck::registerMatchers(MatchFinder *Finder) {
122 callee(functionDecl(DurationFactoryFunction()).bind(
"call_decl")),
125 ignoringImpCasts(anyOf(
126 cxxFunctionalCastExpr(
128 anyOf(isInteger(), realFloatingPointType())),
129 hasSourceExpression(initListExpr())),
130 integerLiteral(equals(0)), floatLiteral(equals(0.0)),
131 binaryOperator(hasOperatorName(
"*"),
132 hasEitherOperand(ignoringImpCasts(
133 anyOf(integerLiteral(), floatLiteral()))))
135 binaryOperator(hasOperatorName(
"/"), hasRHS(floatLiteral()))
136 .bind(
"div_binop")))))
141 void DurationFactoryScaleCheck::check(
const MatchFinder::MatchResult &
Result) {
142 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
145 if (Call->getExprLoc().isMacroID())
148 const Expr *Arg = Call->getArg(0)->IgnoreParenImpCasts();
150 if (Arg->getBeginLoc().isMacroID())
155 diag(Call->getBeginLoc(),
156 "use ZeroDuration() for zero-length time intervals")
157 << FixItHint::CreateReplacement(Call->getSourceRange(),
158 "absl::ZeroDuration()");
162 const auto *CallDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"call_decl");
163 llvm::Optional<DurationScale> MaybeScale =
169 const Expr *Remainder;
170 llvm::Optional<DurationScale> NewScale;
173 if (
const auto *MultBinOp =
174 Result.Nodes.getNodeAs<BinaryOperator>(
"mult_binop")) {
179 const auto *IntLit = llvm::dyn_cast<IntegerLiteral>(MultBinOp->getLHS());
180 const auto *FloatLit = llvm::dyn_cast<FloatingLiteral>(MultBinOp->getLHS());
181 if (IntLit || FloatLit) {
184 Remainder = MultBinOp->getRHS();
189 IntLit = llvm::dyn_cast<IntegerLiteral>(MultBinOp->getRHS());
190 FloatLit = llvm::dyn_cast<FloatingLiteral>(MultBinOp->getRHS());
191 if (IntLit || FloatLit) {
194 Remainder = MultBinOp->getLHS();
197 }
else if (
const auto *DivBinOp =
198 Result.Nodes.getNodeAs<BinaryOperator>(
"div_binop")) {
201 const auto *FloatLit = llvm::dyn_cast<FloatingLiteral>(DivBinOp->getRHS());
203 llvm::Optional<DurationScale> NewScale =
204 GetNewScale(Scale, 1.0 / FloatLit->getValueAsApproximateDouble());
206 const Expr *Remainder = DivBinOp->getLHS();
210 diag(Call->getBeginLoc(),
"internal duration scaling can be removed")
211 << FixItHint::CreateReplacement(
212 Call->getSourceRange(),
214 tooling::fixit::getText(*Remainder, *Result.Context) +
")")
220 assert(Remainder &&
"No remainder found");
223 diag(Call->getBeginLoc(),
"internal duration scaling can be removed")
224 << FixItHint::CreateReplacement(
225 Call->getSourceRange(),
227 tooling::fixit::getText(*Remainder, *Result.Context) +
")")
static llvm::Optional< std::tuple< DurationScale, double > > GetNewScaleSingleStep(DurationScale OldScale, double Multiplier)
llvm::StringRef getFactoryForScale(DurationScale Scale)
Returns the factory function name for a given Scale.
static llvm::Optional< DurationScale > GetNewScale(DurationScale OldScale, double Multiplier)
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static double GetValue(const IntegerLiteral *IntLit, const FloatingLiteral *FloatLit)
static llvm::Optional< DurationScale > getScaleForFactory(llvm::StringRef FactoryName)
bool IsLiteralZero(const MatchFinder::MatchResult &Result, const Expr &Node)
Returns true if Node is a value which evaluates to a literal 0.
DurationScale
Duration factory and conversion scales.