xref: /llvm-project/clang-tools-extra/clang-tidy/abseil/DurationSubtractionCheck.cpp (revision 7d2ea6c422d3f5712b7253407005e1a465a76946)
1 //===--- DurationSubtractionCheck.cpp - clang-tidy ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "DurationSubtractionCheck.h"
10 #include "DurationRewriter.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Tooling/FixIt.h"
14 #include <optional>
15 
16 using namespace clang::ast_matchers;
17 
18 namespace clang::tidy::abseil {
19 
registerMatchers(MatchFinder * Finder)20 void DurationSubtractionCheck::registerMatchers(MatchFinder *Finder) {
21   Finder->addMatcher(
22       binaryOperator(
23           hasOperatorName("-"),
24           hasLHS(callExpr(callee(functionDecl(DurationConversionFunction())
25                                      .bind("function_decl")),
26                           hasArgument(0, expr().bind("lhs_arg")))))
27           .bind("binop"),
28       this);
29 }
30 
check(const MatchFinder::MatchResult & Result)31 void DurationSubtractionCheck::check(const MatchFinder::MatchResult &Result) {
32   const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>("binop");
33   const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("function_decl");
34 
35   // Don't try to replace things inside of macro definitions.
36   if (Binop->getExprLoc().isMacroID() || Binop->getExprLoc().isInvalid())
37     return;
38 
39   std::optional<DurationScale> Scale =
40       getScaleForDurationInverse(FuncDecl->getName());
41   if (!Scale)
42     return;
43 
44   std::string RhsReplacement =
45       rewriteExprFromNumberToDuration(Result, *Scale, Binop->getRHS());
46 
47   const Expr *LhsArg = Result.Nodes.getNodeAs<Expr>("lhs_arg");
48 
49   diag(Binop->getBeginLoc(), "perform subtraction in the duration domain")
50       << FixItHint::CreateReplacement(
51              Binop->getSourceRange(),
52              (llvm::Twine("absl::") + FuncDecl->getName() + "(" +
53               tooling::fixit::getText(*LhsArg, *Result.Context) + " - " +
54               RhsReplacement + ")")
55                  .str());
56 }
57 
58 } // namespace clang::tidy::abseil
59