11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/FixIt.h"
22 binaryOperator(hasOperatorName(
"+"),
23 hasEitherOperand(expr(ignoringParenImpCasts(
24 callExpr(callee(functionDecl(TimeConversionFunction())
25 .bind(
"function_decl")))
32 const auto *Binop = Result.Nodes.getNodeAs<clang::BinaryOperator>(
"binop");
33 const auto *Call = Result.Nodes.getNodeAs<clang::CallExpr>(
"call");
36 if (Binop->getExprLoc().isMacroID() || Binop->getExprLoc().isInvalid())
40 Result.Nodes.getNodeAs<clang::FunctionDecl>(
"function_decl")->getName());
47 if (Call == Binop->getLHS()->IgnoreParenImpCasts()) {
48 Hint = FixItHint::CreateReplacement(
49 Binop->getSourceRange(),
50 (llvm::Twine(TimeFactory) +
"(" +
51 tooling::fixit::getText(*Call->getArg(0), *Result.Context) +
" + " +
55 assert(Call == Binop->getRHS()->IgnoreParenImpCasts() &&
56 "Call should be found on the RHS");
57 Hint = FixItHint::CreateReplacement(
58 Binop->getSourceRange(),
59 (llvm::Twine(TimeFactory) +
"(" +
61 " + " + tooling::fixit::getText(*Call->getArg(0), *Result.Context) +
66 diag(Binop->getBeginLoc(),
"perform addition in the duration domain") << Hint;
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 > getScaleForTimeInverse(llvm::StringRef Name)
Given the name of an inverse Time function (e.g., ToUnixSeconds), 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 ...
llvm::StringRef getTimeInverseForScale(DurationScale Scale)
Returns the Time factory function name for a given Scale.