1 //===--- DurationRewriter.h - clang-tidy ------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
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"
17 namespace clang::tidy::abseil
{
19 /// Duration factory and conversion scales
20 enum class DurationScale
: std::uint8_t {
29 /// Given a `Scale`, return the appropriate factory function call for
30 /// constructing a `Duration` for that scale.
31 llvm::StringRef
getDurationFactoryForScale(DurationScale Scale
);
33 /// Given a 'Scale', return the appropriate factory function call for
34 /// constructing a `Time` for that scale.
35 llvm::StringRef
getTimeFactoryForScale(DurationScale Scale
);
37 // Determine if `Node` represents a literal floating point or integral zero.
38 bool isLiteralZero(const ast_matchers::MatchFinder::MatchResult
&Result
,
41 /// Possibly strip a floating point cast expression.
43 /// If `Node` represents an explicit cast to a floating point type, return
44 /// the textual context of the cast argument, otherwise `std::nullopt`.
45 std::optional
<std::string
>
46 stripFloatCast(const ast_matchers::MatchFinder::MatchResult
&Result
,
49 /// Possibly remove the fractional part of a floating point literal.
51 /// If `Node` represents a floating point literal with a zero fractional part,
52 /// return the textual context of the integral part, otherwise `std::nullopt`.
53 std::optional
<std::string
>
54 stripFloatLiteralFraction(const ast_matchers::MatchFinder::MatchResult
&Result
,
57 /// Possibly further simplify a duration factory function's argument, without
58 /// changing the scale of the factory function. Return that simplification or
59 /// the text of the argument if no simplification is possible.
61 simplifyDurationFactoryArg(const ast_matchers::MatchFinder::MatchResult
&Result
,
64 /// Given the name of an inverse Duration function (e.g., `ToDoubleSeconds`),
65 /// return its `DurationScale`, or `std::nullopt` if a match is not found.
66 std::optional
<DurationScale
> getScaleForDurationInverse(llvm::StringRef Name
);
68 /// Given the name of an inverse Time function (e.g., `ToUnixSeconds`),
69 /// return its `DurationScale`, or `std::nullopt` if a match is not found.
70 std::optional
<DurationScale
> getScaleForTimeInverse(llvm::StringRef Name
);
72 /// Given a `Scale` return the fully qualified inverse functions for it.
73 /// The first returned value is the inverse for `double`, and the second
74 /// returned value is the inverse for `int64`.
75 const std::pair
<llvm::StringRef
, llvm::StringRef
> &
76 getDurationInverseForScale(DurationScale Scale
);
78 /// Returns the Time inverse function name for a given `Scale`.
79 llvm::StringRef
getTimeInverseForScale(DurationScale Scale
);
81 /// Assuming `Node` has type `double` or `int` representing a time interval of
82 /// `Scale`, return the expression to make it a suitable `Duration`.
83 std::string
rewriteExprFromNumberToDuration(
84 const ast_matchers::MatchFinder::MatchResult
&Result
, DurationScale Scale
,
87 /// Assuming `Node` has a type `int` representing a time instant of `Scale`
88 /// since The Epoch, return the expression to make it a suitable `Time`.
89 std::string
rewriteExprFromNumberToTime(
90 const ast_matchers::MatchFinder::MatchResult
&Result
, DurationScale Scale
,
93 /// Return `false` if `E` is a either: not a macro at all; or an argument to
94 /// one. In the both cases, we often want to do the transformation.
95 bool isInMacro(const ast_matchers::MatchFinder::MatchResult
&Result
,
98 AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher
<FunctionDecl
>,
99 DurationConversionFunction
) {
100 using namespace clang::ast_matchers
;
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"));
110 AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher
<FunctionDecl
>,
111 DurationFactoryFunction
) {
112 using namespace clang::ast_matchers
;
113 return functionDecl(hasAnyName("::absl::Nanoseconds", "::absl::Microseconds",
114 "::absl::Milliseconds", "::absl::Seconds",
115 "::absl::Minutes", "::absl::Hours"));
118 AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher
<FunctionDecl
>,
119 TimeConversionFunction
) {
120 using namespace clang::ast_matchers
;
121 return functionDecl(hasAnyName(
122 "::absl::ToUnixHours", "::absl::ToUnixMinutes", "::absl::ToUnixSeconds",
123 "::absl::ToUnixMillis", "::absl::ToUnixMicros", "::absl::ToUnixNanos"));
126 AST_MATCHER_FUNCTION_P(ast_matchers::internal::Matcher
<Stmt
>,
127 comparisonOperatorWithCallee
,
128 ast_matchers::internal::Matcher
<Decl
>, funcDecl
) {
129 using namespace clang::ast_matchers
;
130 return binaryOperator(
131 anyOf(hasOperatorName(">"), hasOperatorName(">="), hasOperatorName("=="),
132 hasOperatorName("<="), hasOperatorName("<")),
133 hasEitherOperand(ignoringImpCasts(callExpr(callee(funcDecl
)))));
136 } // namespace clang::tidy::abseil
138 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H