12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Tooling/FixIt.h" 22 void DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
23 for (
const auto &Scale : {
"Hours",
"Minutes",
"Seconds",
"Milliseconds",
24 "Microseconds",
"Nanoseconds"}) {
25 std::string DurationFactory = (llvm::Twine(
"::absl::") + Scale).str();
26 std::string FloatConversion =
27 (llvm::Twine(
"::absl::ToDouble") + Scale).str();
28 std::string IntegerConversion =
29 (llvm::Twine(
"::absl::ToInt64") + Scale).str();
33 auto factory_matcher = ignoringElidableConstructorCall(
34 callExpr(callee(functionDecl(hasName(DurationFactory))),
35 hasArgument(0, ignoringImpCasts(integerLiteral(equals(1))))));
39 auto inverse_function_matcher = callExpr(
40 callee(functionDecl(hasAnyName(FloatConversion, IntegerConversion))),
41 hasArgument(0, expr().bind(
"arg")));
45 auto division_operator_matcher = cxxOperatorCallExpr(
46 hasOverloadedOperatorName(
"/"), hasArgument(0, expr().bind(
"arg")),
47 hasArgument(1, factory_matcher));
51 auto fdiv_matcher = callExpr(
52 callee(functionDecl(hasName(
"::absl::FDivDuration"))),
53 hasArgument(0, expr().bind(
"arg")), hasArgument(1, factory_matcher));
56 callExpr(callee(functionDecl(hasName(DurationFactory))),
57 hasArgument(0, anyOf(inverse_function_matcher,
58 division_operator_matcher, fdiv_matcher)))
64 void DurationUnnecessaryConversionCheck::check(
65 const MatchFinder::MatchResult &
Result) {
66 const auto *OuterCall = Result.Nodes.getNodeAs<Expr>(
"call");
67 const auto *Arg = Result.Nodes.getNodeAs<Expr>(
"arg");
72 diag(OuterCall->getBeginLoc(),
73 "remove unnecessary absl::Duration conversions")
74 << FixItHint::CreateReplacement(
75 OuterCall->getSourceRange(),
76 tooling::fixit::getText(*Arg, *Result.Context));
bool isInMacro(const MatchFinder::MatchResult &Result, const Expr *E)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result