clang-tools 10.0.0

DurationUnnecessaryConversionCheck.cpp
Go to the documentation of this file.
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 {
19 namespace tidy {
20 namespace abseil {
21
22 void DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
23 for (const auto &Scale : {"Hours", "Minutes", "Seconds", "Milliseconds",
24 "Microseconds", "Nanoseconds"}) {
25 std::string DurationFactory = (llvm::Twine("::absl::") + Scale).str();
26 std::string FloatConversion =
27 (llvm::Twine("::absl::ToDouble") + Scale).str();
28 std::string IntegerConversion =
29 (llvm::Twine("::absl::ToInt64") + Scale).str();
30
31 // Matcher which matches the current scale's factory with a `1` argument,
32 // e.g. `absl::Seconds(1)`.
33 auto factory_matcher = ignoringElidableConstructorCall(
34 callExpr(callee(functionDecl(hasName(DurationFactory))),
35 hasArgument(0, ignoringImpCasts(integerLiteral(equals(1))))));
36
37 // Matcher which matches either inverse function and binds its argument,
38 // e.g. `absl::ToDoubleSeconds(dur)`.
39 auto inverse_function_matcher = callExpr(
40 callee(functionDecl(hasAnyName(FloatConversion, IntegerConversion))),
41 hasArgument(0, expr().bind("arg")));
42
43 // Matcher which matches a duration divided by the factory_matcher above,
44 // e.g. `dur / absl::Seconds(1)`.
45 auto division_operator_matcher = cxxOperatorCallExpr(
46 hasOverloadedOperatorName("/"), hasArgument(0, expr().bind("arg")),
47 hasArgument(1, factory_matcher));
48
49 // Matcher which matches a duration argument to `FDivDuration`,
50 // e.g. `absl::FDivDuration(dur, absl::Seconds(1))`
51 auto fdiv_matcher = callExpr(
52 callee(functionDecl(hasName("::absl::FDivDuration"))),
53 hasArgument(0, expr().bind("arg")), hasArgument(1, factory_matcher));
54
55 Finder->addMatcher(
56 callExpr(callee(functionDecl(hasName(DurationFactory))),
57 hasArgument(0, anyOf(inverse_function_matcher,
58 division_operator_matcher, fdiv_matcher)))
59 .bind("call"),
60 this);
61 }
62 }
63
64 void DurationUnnecessaryConversionCheck::check(
65 const MatchFinder::MatchResult &Result) {
66 const auto *OuterCall = Result.Nodes.getNodeAs<Expr>("call");
67 const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
68
69 if (isInMacro(Result, OuterCall))
70 return;
71
72 diag(OuterCall->getBeginLoc(),
73 "remove unnecessary absl::Duration conversions")
74 << FixItHint::CreateReplacement(
75 OuterCall->getSourceRange(),
76 tooling::fixit::getText(*Arg, *Result.Context));
77 }
78
79 } // namespace abseil
80 } // namespace tidy
81 } // namespace clang
DurationRewriter.h
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::abseil::isInMacro
bool isInMacro(const MatchFinder::MatchResult &Result, const Expr *E)
Definition: DurationRewriter.cpp:305
DurationUnnecessaryConversionCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27