1//===--- DurationUnnecessaryConversionCheck.cpp - clang-tidy
2//-----------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#include "DurationUnnecessaryConversionCheck.h"
11#include "DurationRewriter.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Tooling/FixIt.h"
15
16using namespace clang::ast_matchers;
17
18namespace clang::tidy::abseil {
19
20void DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
21 for (const auto &Scale : {"Hours", "Minutes", "Seconds", "Milliseconds",
22 "Microseconds", "Nanoseconds"}) {
23 std::string DurationFactory = (llvm::Twine("::absl::") + Scale).str();
24 std::string FloatConversion =
25 (llvm::Twine("::absl::ToDouble") + Scale).str();
26 std::string IntegerConversion =
27 (llvm::Twine("::absl::ToInt64") + Scale).str();
28
29 // Matcher which matches the current scale's factory with a `1` argument,
30 // e.g. `absl::Seconds(1)`.
31 auto FactoryMatcher = ignoringElidableConstructorCall(
32 InnerMatcher: callExpr(callee(InnerMatcher: functionDecl(hasName(Name: DurationFactory))),
33 hasArgument(N: 0, InnerMatcher: ignoringImpCasts(InnerMatcher: integerLiteral(equals(Value: 1))))));
34
35 // Matcher which matches either inverse function and binds its argument,
36 // e.g. `absl::ToDoubleSeconds(dur)`.
37 auto InverseFunctionMatcher = callExpr(
38 callee(InnerMatcher: functionDecl(hasAnyName(FloatConversion, IntegerConversion))),
39 hasArgument(N: 0, InnerMatcher: expr().bind(ID: "arg")));
40
41 // Matcher which matches a duration divided by the factory_matcher above,
42 // e.g. `dur / absl::Seconds(1)`.
43 auto DivisionOperatorMatcher = cxxOperatorCallExpr(
44 hasOverloadedOperatorName(Name: "/"), hasArgument(N: 0, InnerMatcher: expr().bind(ID: "arg")),
45 hasArgument(N: 1, InnerMatcher: FactoryMatcher));
46
47 // Matcher which matches a duration argument to `FDivDuration`,
48 // e.g. `absl::FDivDuration(dur, absl::Seconds(1))`
49 auto FdivMatcher = callExpr(
50 callee(InnerMatcher: functionDecl(hasName(Name: "::absl::FDivDuration"))),
51 hasArgument(N: 0, InnerMatcher: expr().bind(ID: "arg")), hasArgument(N: 1, InnerMatcher: FactoryMatcher));
52
53 // Matcher which matches a duration argument being scaled,
54 // e.g. `absl::ToDoubleSeconds(dur) * 2`
55 auto ScalarMatcher = ignoringImpCasts(
56 InnerMatcher: binaryOperator(hasOperatorName(Name: "*"),
57 hasEitherOperand(InnerMatcher: expr(ignoringParenImpCasts(
58 InnerMatcher: callExpr(callee(InnerMatcher: functionDecl(hasAnyName(
59 FloatConversion, IntegerConversion))),
60 hasArgument(N: 0, InnerMatcher: expr().bind(ID: "arg")))
61 .bind(ID: "inner_call")))))
62 .bind(ID: "binop"));
63
64 Finder->addMatcher(
65 NodeMatch: callExpr(callee(InnerMatcher: functionDecl(hasName(Name: DurationFactory))),
66 hasArgument(N: 0, InnerMatcher: anyOf(InverseFunctionMatcher,
67 DivisionOperatorMatcher, FdivMatcher,
68 ScalarMatcher)))
69 .bind(ID: "call"),
70 Action: this);
71 }
72}
73
74void DurationUnnecessaryConversionCheck::check(
75 const MatchFinder::MatchResult &Result) {
76 const auto *OuterCall = Result.Nodes.getNodeAs<Expr>(ID: "call");
77
78 if (isInMacro(Result, E: OuterCall))
79 return;
80
81 FixItHint Hint;
82 if (const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>(ID: "binop")) {
83 const auto *Arg = Result.Nodes.getNodeAs<Expr>(ID: "arg");
84 const auto *InnerCall = Result.Nodes.getNodeAs<Expr>(ID: "inner_call");
85 const Expr *LHS = Binop->getLHS();
86 const Expr *RHS = Binop->getRHS();
87
88 if (LHS->IgnoreParenImpCasts() == InnerCall) {
89 Hint = FixItHint::CreateReplacement(
90 OuterCall->getSourceRange(),
91 (llvm::Twine(tooling::fixit::getText(Node: *Arg, Context: *Result.Context)) + " * " +
92 tooling::fixit::getText(Node: *RHS, Context: *Result.Context))
93 .str());
94 } else {
95 assert(RHS->IgnoreParenImpCasts() == InnerCall &&
96 "Inner call should be find on the RHS");
97
98 Hint = FixItHint::CreateReplacement(
99 OuterCall->getSourceRange(),
100 (llvm::Twine(tooling::fixit::getText(Node: *LHS, Context: *Result.Context)) + " * " +
101 tooling::fixit::getText(Node: *Arg, Context: *Result.Context))
102 .str());
103 }
104 } else if (const auto *Arg = Result.Nodes.getNodeAs<Expr>(ID: "arg")) {
105 Hint = FixItHint::CreateReplacement(
106 OuterCall->getSourceRange(),
107 tooling::fixit::getText(Node: *Arg, Context: *Result.Context));
108 }
109 diag(OuterCall->getBeginLoc(),
110 "remove unnecessary absl::Duration conversions")
111 << Hint;
112}
113
114} // namespace clang::tidy::abseil
115

source code of clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp