2019-02-05 03:28:20 +08:00
|
|
|
//===--- DurationUnnecessaryConversionCheck.cpp - clang-tidy
|
|
|
|
//-----------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DurationUnnecessaryConversionCheck.h"
|
|
|
|
#include "DurationRewriter.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Tooling/FixIt.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace abseil {
|
|
|
|
|
|
|
|
void DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
for (const auto &Scale : {"Hours", "Minutes", "Seconds", "Milliseconds",
|
|
|
|
"Microseconds", "Nanoseconds"}) {
|
|
|
|
std::string DurationFactory = (llvm::Twine("::absl::") + Scale).str();
|
|
|
|
std::string FloatConversion =
|
|
|
|
(llvm::Twine("::absl::ToDouble") + Scale).str();
|
|
|
|
std::string IntegerConversion =
|
|
|
|
(llvm::Twine("::absl::ToInt64") + Scale).str();
|
|
|
|
|
2019-03-14 21:38:16 +08:00
|
|
|
// Matcher which matches the current scale's factory with a `1` argument,
|
|
|
|
// e.g. `absl::Seconds(1)`.
|
2021-01-29 07:49:53 +08:00
|
|
|
auto FactoryMatcher = ignoringElidableConstructorCall(
|
2019-03-14 21:38:16 +08:00
|
|
|
callExpr(callee(functionDecl(hasName(DurationFactory))),
|
2019-06-13 21:52:45 +08:00
|
|
|
hasArgument(0, ignoringImpCasts(integerLiteral(equals(1))))));
|
2019-03-14 21:38:16 +08:00
|
|
|
|
|
|
|
// Matcher which matches either inverse function and binds its argument,
|
|
|
|
// e.g. `absl::ToDoubleSeconds(dur)`.
|
2021-01-29 07:49:53 +08:00
|
|
|
auto InverseFunctionMatcher = callExpr(
|
2019-03-14 21:38:16 +08:00
|
|
|
callee(functionDecl(hasAnyName(FloatConversion, IntegerConversion))),
|
|
|
|
hasArgument(0, expr().bind("arg")));
|
|
|
|
|
|
|
|
// Matcher which matches a duration divided by the factory_matcher above,
|
|
|
|
// e.g. `dur / absl::Seconds(1)`.
|
2021-01-29 07:49:53 +08:00
|
|
|
auto DivisionOperatorMatcher = cxxOperatorCallExpr(
|
2019-03-14 21:38:16 +08:00
|
|
|
hasOverloadedOperatorName("/"), hasArgument(0, expr().bind("arg")),
|
2021-01-29 07:49:53 +08:00
|
|
|
hasArgument(1, FactoryMatcher));
|
2019-03-14 21:38:16 +08:00
|
|
|
|
|
|
|
// Matcher which matches a duration argument to `FDivDuration`,
|
|
|
|
// e.g. `absl::FDivDuration(dur, absl::Seconds(1))`
|
2021-01-29 07:49:53 +08:00
|
|
|
auto FdivMatcher = callExpr(
|
2019-03-14 21:38:16 +08:00
|
|
|
callee(functionDecl(hasName("::absl::FDivDuration"))),
|
2021-01-29 07:49:53 +08:00
|
|
|
hasArgument(0, expr().bind("arg")), hasArgument(1, FactoryMatcher));
|
2019-03-14 21:38:16 +08:00
|
|
|
|
2020-03-04 04:08:03 +08:00
|
|
|
// Matcher which matches a duration argument being scaled,
|
|
|
|
// e.g. `absl::ToDoubleSeconds(dur) * 2`
|
2021-01-29 07:49:53 +08:00
|
|
|
auto ScalarMatcher = ignoringImpCasts(
|
2020-03-04 04:08:03 +08:00
|
|
|
binaryOperator(hasOperatorName("*"),
|
|
|
|
hasEitherOperand(expr(ignoringParenImpCasts(
|
|
|
|
callExpr(callee(functionDecl(hasAnyName(
|
|
|
|
FloatConversion, IntegerConversion))),
|
|
|
|
hasArgument(0, expr().bind("arg")))
|
|
|
|
.bind("inner_call")))))
|
|
|
|
.bind("binop"));
|
|
|
|
|
2019-02-05 03:28:20 +08:00
|
|
|
Finder->addMatcher(
|
2019-03-14 21:38:16 +08:00
|
|
|
callExpr(callee(functionDecl(hasName(DurationFactory))),
|
2021-01-29 07:49:53 +08:00
|
|
|
hasArgument(0, anyOf(InverseFunctionMatcher,
|
|
|
|
DivisionOperatorMatcher, FdivMatcher,
|
|
|
|
ScalarMatcher)))
|
2019-02-05 03:28:20 +08:00
|
|
|
.bind("call"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DurationUnnecessaryConversionCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *OuterCall = Result.Nodes.getNodeAs<Expr>("call");
|
|
|
|
|
2019-03-08 23:37:15 +08:00
|
|
|
if (isInMacro(Result, OuterCall))
|
2019-02-05 03:28:20 +08:00
|
|
|
return;
|
|
|
|
|
2020-03-04 04:08:03 +08:00
|
|
|
FixItHint Hint;
|
|
|
|
if (const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>("binop")) {
|
|
|
|
const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
|
|
|
|
const auto *InnerCall = Result.Nodes.getNodeAs<Expr>("inner_call");
|
|
|
|
const Expr *LHS = Binop->getLHS();
|
|
|
|
const Expr *RHS = Binop->getRHS();
|
|
|
|
|
|
|
|
if (LHS->IgnoreParenImpCasts() == InnerCall) {
|
|
|
|
Hint = FixItHint::CreateReplacement(
|
|
|
|
OuterCall->getSourceRange(),
|
|
|
|
(llvm::Twine(tooling::fixit::getText(*Arg, *Result.Context)) + " * " +
|
|
|
|
tooling::fixit::getText(*RHS, *Result.Context))
|
|
|
|
.str());
|
|
|
|
} else {
|
|
|
|
assert(RHS->IgnoreParenImpCasts() == InnerCall &&
|
|
|
|
"Inner call should be find on the RHS");
|
|
|
|
|
|
|
|
Hint = FixItHint::CreateReplacement(
|
|
|
|
OuterCall->getSourceRange(),
|
|
|
|
(llvm::Twine(tooling::fixit::getText(*LHS, *Result.Context)) + " * " +
|
|
|
|
tooling::fixit::getText(*Arg, *Result.Context))
|
|
|
|
.str());
|
|
|
|
}
|
|
|
|
} else if (const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg")) {
|
|
|
|
Hint = FixItHint::CreateReplacement(
|
|
|
|
OuterCall->getSourceRange(),
|
|
|
|
tooling::fixit::getText(*Arg, *Result.Context));
|
|
|
|
}
|
2019-03-14 21:38:16 +08:00
|
|
|
diag(OuterCall->getBeginLoc(),
|
|
|
|
"remove unnecessary absl::Duration conversions")
|
2020-03-04 04:08:03 +08:00
|
|
|
<< Hint;
|
2019-02-05 03:28:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace abseil
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|