xref: /llvm-project/clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp (revision 7d2ea6c422d3f5712b7253407005e1a465a76946)
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 
16 using namespace clang::ast_matchers;
17 
18 namespace clang::tidy::abseil {
19 
registerMatchers(MatchFinder * Finder)20 void 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         callExpr(callee(functionDecl(hasName(DurationFactory))),
33                  hasArgument(0, ignoringImpCasts(integerLiteral(equals(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(functionDecl(hasAnyName(FloatConversion, IntegerConversion))),
39         hasArgument(0, expr().bind("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("/"), hasArgument(0, expr().bind("arg")),
45         hasArgument(1, 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(functionDecl(hasName("::absl::FDivDuration"))),
51         hasArgument(0, expr().bind("arg")), hasArgument(1, FactoryMatcher));
52 
53     // Matcher which matches a duration argument being scaled,
54     // e.g. `absl::ToDoubleSeconds(dur) * 2`
55     auto ScalarMatcher = ignoringImpCasts(
56         binaryOperator(hasOperatorName("*"),
57                        hasEitherOperand(expr(ignoringParenImpCasts(
58                            callExpr(callee(functionDecl(hasAnyName(
59                                         FloatConversion, IntegerConversion))),
60                                     hasArgument(0, expr().bind("arg")))
61                                .bind("inner_call")))))
62             .bind("binop"));
63 
64     Finder->addMatcher(
65         callExpr(callee(functionDecl(hasName(DurationFactory))),
66                  hasArgument(0, anyOf(InverseFunctionMatcher,
67                                       DivisionOperatorMatcher, FdivMatcher,
68                                       ScalarMatcher)))
69             .bind("call"),
70         this);
71   }
72 }
73 
check(const MatchFinder::MatchResult & Result)74 void DurationUnnecessaryConversionCheck::check(
75     const MatchFinder::MatchResult &Result) {
76   const auto *OuterCall = Result.Nodes.getNodeAs<Expr>("call");
77 
78   if (isInMacro(Result, OuterCall))
79     return;
80 
81   FixItHint Hint;
82   if (const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>("binop")) {
83     const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
84     const auto *InnerCall = Result.Nodes.getNodeAs<Expr>("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(*Arg, *Result.Context)) + " * " +
92            tooling::fixit::getText(*RHS, *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(*LHS, *Result.Context)) + " * " +
101            tooling::fixit::getText(*Arg, *Result.Context))
102               .str());
103     }
104   } else if (const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg")) {
105     Hint = FixItHint::CreateReplacement(
106         OuterCall->getSourceRange(),
107         tooling::fixit::getText(*Arg, *Result.Context));
108   }
109   diag(OuterCall->getBeginLoc(),
110        "remove unnecessary absl::Duration conversions")
111       << Hint;
112 }
113 
114 } // namespace clang::tidy::abseil
115