File indexing completed on 2026-05-10 08:36:18
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H
0011
0012 #include "clang/ASTMatchers/ASTMatchFinder.h"
0013 #include "clang/ASTMatchers/ASTMatchers.h"
0014 #include <cinttypes>
0015 #include <optional>
0016
0017 namespace clang::tidy::abseil {
0018
0019
0020 enum class DurationScale : std::uint8_t {
0021 Hours = 0,
0022 Minutes,
0023 Seconds,
0024 Milliseconds,
0025 Microseconds,
0026 Nanoseconds,
0027 };
0028
0029
0030
0031 llvm::StringRef getDurationFactoryForScale(DurationScale Scale);
0032
0033
0034
0035 llvm::StringRef getTimeFactoryForScale(DurationScale Scale);
0036
0037
0038 bool isLiteralZero(const ast_matchers::MatchFinder::MatchResult &Result,
0039 const Expr &Node);
0040
0041
0042
0043
0044
0045 std::optional<std::string>
0046 stripFloatCast(const ast_matchers::MatchFinder::MatchResult &Result,
0047 const Expr &Node);
0048
0049
0050
0051
0052
0053 std::optional<std::string>
0054 stripFloatLiteralFraction(const ast_matchers::MatchFinder::MatchResult &Result,
0055 const Expr &Node);
0056
0057
0058
0059
0060 std::string
0061 simplifyDurationFactoryArg(const ast_matchers::MatchFinder::MatchResult &Result,
0062 const Expr &Node);
0063
0064
0065
0066 std::optional<DurationScale> getScaleForDurationInverse(llvm::StringRef Name);
0067
0068
0069
0070 std::optional<DurationScale> getScaleForTimeInverse(llvm::StringRef Name);
0071
0072
0073
0074
0075 const std::pair<llvm::StringRef, llvm::StringRef> &
0076 getDurationInverseForScale(DurationScale Scale);
0077
0078
0079 llvm::StringRef getTimeInverseForScale(DurationScale Scale);
0080
0081
0082
0083 std::string rewriteExprFromNumberToDuration(
0084 const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
0085 const Expr *Node);
0086
0087
0088
0089 std::string rewriteExprFromNumberToTime(
0090 const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
0091 const Expr *Node);
0092
0093
0094
0095 bool isInMacro(const ast_matchers::MatchFinder::MatchResult &Result,
0096 const Expr *E);
0097
0098 AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
0099 DurationConversionFunction) {
0100 using namespace clang::ast_matchers;
0101 return functionDecl(
0102 hasAnyName("::absl::ToDoubleHours", "::absl::ToDoubleMinutes",
0103 "::absl::ToDoubleSeconds", "::absl::ToDoubleMilliseconds",
0104 "::absl::ToDoubleMicroseconds", "::absl::ToDoubleNanoseconds",
0105 "::absl::ToInt64Hours", "::absl::ToInt64Minutes",
0106 "::absl::ToInt64Seconds", "::absl::ToInt64Milliseconds",
0107 "::absl::ToInt64Microseconds", "::absl::ToInt64Nanoseconds"));
0108 }
0109
0110 AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
0111 DurationFactoryFunction) {
0112 using namespace clang::ast_matchers;
0113 return functionDecl(hasAnyName("::absl::Nanoseconds", "::absl::Microseconds",
0114 "::absl::Milliseconds", "::absl::Seconds",
0115 "::absl::Minutes", "::absl::Hours"));
0116 }
0117
0118 AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
0119 TimeConversionFunction) {
0120 using namespace clang::ast_matchers;
0121 return functionDecl(hasAnyName(
0122 "::absl::ToUnixHours", "::absl::ToUnixMinutes", "::absl::ToUnixSeconds",
0123 "::absl::ToUnixMillis", "::absl::ToUnixMicros", "::absl::ToUnixNanos"));
0124 }
0125
0126 AST_MATCHER_FUNCTION_P(ast_matchers::internal::Matcher<Stmt>,
0127 comparisonOperatorWithCallee,
0128 ast_matchers::internal::Matcher<Decl>, funcDecl) {
0129 using namespace clang::ast_matchers;
0130 return binaryOperator(
0131 anyOf(hasOperatorName(">"), hasOperatorName(">="), hasOperatorName("=="),
0132 hasOperatorName("<="), hasOperatorName("<")),
0133 hasEitherOperand(ignoringImpCasts(callExpr(callee(funcDecl)))));
0134 }
0135
0136 }
0137
0138 #endif