11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Tooling/FixIt.h"
21 void DurationSubtractionCheck::registerMatchers(MatchFinder *Finder) {
25 hasLHS(callExpr(callee(functionDecl(DurationConversionFunction())
26 .bind(
"function_decl")),
27 hasArgument(0, expr().bind(
"lhs_arg")))))
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");
37 if (Binop->getExprLoc().isMacroID() || Binop->getExprLoc().isInvalid())
40 llvm::Optional<DurationScale> Scale =
45 std::string RhsReplacement =
48 const Expr *LhsArg = Result.Nodes.getNodeAs<Expr>(
"lhs_arg");
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) +
" - " +