11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/FixIt.h"
24 hasLHS(callExpr(callee(functionDecl(DurationConversionFunction())
25 .bind(
"function_decl")),
26 hasArgument(0, expr().bind(
"lhs_arg")))))
32 const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>(
"binop");
33 const auto *
FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"function_decl");
36 if (Binop->getExprLoc().isMacroID() || Binop->getExprLoc().isInvalid())
39 std::optional<DurationScale> Scale =
44 std::string RhsReplacement =
47 const Expr *LhsArg = Result.Nodes.getNodeAs<Expr>(
"lhs_arg");
49 diag(Binop->getBeginLoc(),
"perform subtraction in the duration domain")
50 << FixItHint::CreateReplacement(
51 Binop->getSourceRange(),
52 (llvm::Twine(
"absl::") +
FuncDecl->getName() +
"(" +
53 tooling::fixit::getText(*LhsArg, *Result.Context) +
" - " +
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::optional< DurationScale > getScaleForDurationInverse(llvm::StringRef Name)
Given the name of an inverse Duration function (e.g., ToDoubleSeconds), return its DurationScale,...
std::string rewriteExprFromNumberToDuration(const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale, const Expr *Node)
Assuming Node has type double or int representing a time interval of Scale, return the expression to ...
static constexpr const char FuncDecl[]