9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
45std::optional<std::string>
46stripFloatCast(
const ast_matchers::MatchFinder::MatchResult &Result,
53std::optional<std::string>
75const std::pair<llvm::StringRef, llvm::StringRef> &
84 const ast_matchers::MatchFinder::MatchResult &Result,
DurationScale Scale,
90 const ast_matchers::MatchFinder::MatchResult &Result,
DurationScale Scale,
95bool isInMacro(
const ast_matchers::MatchFinder::MatchResult &Result,
99 DurationConversionFunction) {
102 hasAnyName(
"::absl::ToDoubleHours",
"::absl::ToDoubleMinutes",
103 "::absl::ToDoubleSeconds",
"::absl::ToDoubleMilliseconds",
104 "::absl::ToDoubleMicroseconds",
"::absl::ToDoubleNanoseconds",
105 "::absl::ToInt64Hours",
"::absl::ToInt64Minutes",
106 "::absl::ToInt64Seconds",
"::absl::ToInt64Milliseconds",
107 "::absl::ToInt64Microseconds",
"::absl::ToInt64Nanoseconds"));
111 DurationFactoryFunction) {
113 return functionDecl(hasAnyName(
"::absl::Nanoseconds",
"::absl::Microseconds",
114 "::absl::Milliseconds",
"::absl::Seconds",
115 "::absl::Minutes",
"::absl::Hours"));
119 TimeConversionFunction) {
121 return functionDecl(hasAnyName(
122 "::absl::ToUnixHours",
"::absl::ToUnixMinutes",
"::absl::ToUnixSeconds",
123 "::absl::ToUnixMillis",
"::absl::ToUnixMicros",
"::absl::ToUnixNanos"));
127 comparisonOperatorWithCallee,
128 ast_matchers::internal::Matcher<Decl>, funcDecl) {
130 return binaryOperator(
131 anyOf(hasOperatorName(
">"), hasOperatorName(
">="), hasOperatorName(
"=="),
132 hasOperatorName(
"<="), hasOperatorName(
"<")),
133 hasEitherOperand(ignoringImpCasts(callExpr(callee(funcDecl)))));
llvm::SmallString< 256U > Name
const std::pair< llvm::StringRef, llvm::StringRef > & getDurationInverseForScale(DurationScale Scale)
Given a Scale return the fully qualified inverse functions for it.
std::optional< DurationScale > getScaleForTimeInverse(llvm::StringRef Name)
Given the name of an inverse Time function (e.g., ToUnixSeconds), return its DurationScale,...
std::optional< std::string > stripFloatCast(const ast_matchers::MatchFinder::MatchResult &Result, const Expr &Node)
Possibly strip a floating point cast expression.
std::optional< std::string > stripFloatLiteralFraction(const MatchFinder::MatchResult &Result, const Expr &Node)
std::string simplifyDurationFactoryArg(const MatchFinder::MatchResult &Result, const Expr &Node)
bool isLiteralZero(const MatchFinder::MatchResult &Result, const Expr &Node)
Returns true if Node is a value which evaluates to a literal 0.
llvm::StringRef getDurationFactoryForScale(DurationScale Scale)
Returns the factory function name for a given Scale.
std::string rewriteExprFromNumberToTime(const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale, const Expr *Node)
Assuming Node has a type int representing a time instant of Scale since The Epoch,...
DurationScale
Duration factory and conversion scales.
AST_MATCHER_FUNCTION_P(ast_matchers::internal::Matcher< Stmt >, comparisonOperatorWithCallee, ast_matchers::internal::Matcher< Decl >, funcDecl)
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher< FunctionDecl >, DurationConversionFunction)
std::optional< DurationScale > getScaleForDurationInverse(llvm::StringRef Name)
Given the name of an inverse Duration function (e.g., ToDoubleSeconds), return its DurationScale,...
bool isInMacro(const MatchFinder::MatchResult &Result, const Expr *E)
llvm::StringRef getTimeFactoryForScale(DurationScale Scale)
Given a 'Scale', return the appropriate factory function call for constructing a Time for that scale.
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.