150
|
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
|
|
15 using namespace clang::ast_matchers;
|
|
16
|
|
17 namespace clang {
|
|
18 namespace tidy {
|
|
19 namespace abseil {
|
|
20
|
|
21 void DurationSubtractionCheck::registerMatchers(MatchFinder *Finder) {
|
|
22 Finder->addMatcher(
|
|
23 binaryOperator(
|
|
24 hasOperatorName("-"),
|
|
25 hasLHS(callExpr(callee(functionDecl(DurationConversionFunction())
|
|
26 .bind("function_decl")),
|
|
27 hasArgument(0, expr().bind("lhs_arg")))))
|
|
28 .bind("binop"),
|
|
29 this);
|
|
30 }
|
|
31
|
|
32 void DurationSubtractionCheck::check(const MatchFinder::MatchResult &Result) {
|
|
33 const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>("binop");
|
|
34 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("function_decl");
|
|
35
|
|
36 // Don't try to replace things inside of macro definitions.
|
|
37 if (Binop->getExprLoc().isMacroID() || Binop->getExprLoc().isInvalid())
|
|
38 return;
|
|
39
|
|
40 llvm::Optional<DurationScale> Scale =
|
|
41 getScaleForDurationInverse(FuncDecl->getName());
|
|
42 if (!Scale)
|
|
43 return;
|
|
44
|
|
45 std::string RhsReplacement =
|
|
46 rewriteExprFromNumberToDuration(Result, *Scale, Binop->getRHS());
|
|
47
|
|
48 const Expr *LhsArg = Result.Nodes.getNodeAs<Expr>("lhs_arg");
|
|
49
|
|
50 diag(Binop->getBeginLoc(), "perform subtraction in the duration domain")
|
|
51 << FixItHint::CreateReplacement(
|
|
52 Binop->getSourceRange(),
|
|
53 (llvm::Twine("absl::") + FuncDecl->getName() + "(" +
|
|
54 tooling::fixit::getText(*LhsArg, *Result.Context) + " - " +
|
|
55 RhsReplacement + ")")
|
|
56 .str());
|
|
57 }
|
|
58
|
|
59 } // namespace abseil
|
|
60 } // namespace tidy
|
|
61 } // namespace clang
|