1//===--- DurationAdditionCheck.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 "DurationAdditionCheck.h"
10#include "DurationRewriter.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/FixIt.h"
14
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
19namespace abseil {
20
21void DurationAdditionCheck::registerMatchers(MatchFinder *Finder) {
22 Finder->addMatcher(
23 binaryOperator(hasOperatorName("+"),
24 hasEitherOperand(expr(ignoringParenImpCasts(
25 callExpr(callee(functionDecl(TimeConversionFunction())
26 .bind("function_decl")))
27 .bind("call")))))
28 .bind("binop"),
29 this);
30}
31
32void DurationAdditionCheck::check(const MatchFinder::MatchResult &Result) {
33 const BinaryOperator *Binop =
34 Result.Nodes.getNodeAs<clang::BinaryOperator>("binop");
35 const CallExpr *Call = Result.Nodes.getNodeAs<clang::CallExpr>("call");
36
37 // Don't try to replace things inside of macro definitions.
38 if (Binop->getExprLoc().isMacroID() || Binop->getExprLoc().isInvalid())
39 return;
40
41 llvm::Optional<DurationScale> Scale = getScaleForTimeInverse(
42 Result.Nodes.getNodeAs<clang::FunctionDecl>("function_decl")->getName());
43 if (!Scale)
44 return;
45
46 llvm::StringRef TimeFactory = getTimeInverseForScale(*Scale);
47
48 FixItHint Hint;
49 if (Call == Binop->getLHS()->IgnoreParenImpCasts()) {
50 Hint = FixItHint::CreateReplacement(
51 Binop->getSourceRange(),
52 (llvm::Twine(TimeFactory) + "(" +
53 tooling::fixit::getText(*Call->getArg(0), *Result.Context) + " + " +
54 rewriteExprFromNumberToDuration(Result, *Scale, Binop->getRHS()) + ")")
55 .str());
56 } else {
57 assert(Call == Binop->getRHS()->IgnoreParenImpCasts() &&
58 "Call should be found on the RHS");
59 Hint = FixItHint::CreateReplacement(
60 Binop->getSourceRange(),
61 (llvm::Twine(TimeFactory) + "(" +
62 rewriteExprFromNumberToDuration(Result, *Scale, Binop->getLHS()) +
63 " + " + tooling::fixit::getText(*Call->getArg(0), *Result.Context) +
64 ")")
65 .str());
66 }
67
68 diag(Binop->getBeginLoc(), "perform addition in the duration domain") << Hint;
69}
70
71} // namespace abseil
72} // namespace tidy
73} // namespace clang
74