1//===--- DurationRewriter.h - clang-tidy ------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H
11
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include <cinttypes>
15
16namespace clang {
17namespace tidy {
18namespace abseil {
19
20/// Duration factory and conversion scales
21enum class DurationScale : std::uint8_t {
22 Hours = 0,
23 Minutes,
24 Seconds,
25 Milliseconds,
26 Microseconds,
27 Nanoseconds,
28};
29
30/// Given a `Scale`, return the appropriate factory function call for
31/// constructing a `Duration` for that scale.
32llvm::StringRef getDurationFactoryForScale(DurationScale Scale);
33
34/// Given a 'Scale', return the appropriate factory function call for
35/// constructing a `Time` for that scale.
36llvm::StringRef getTimeFactoryForScale(DurationScale scale);
37
38// Determine if `Node` represents a literal floating point or integral zero.
39bool IsLiteralZero(const ast_matchers::MatchFinder::MatchResult &Result,
40 const Expr &Node);
41
42/// Possibly strip a floating point cast expression.
43///
44/// If `Node` represents an explicit cast to a floating point type, return
45/// the textual context of the cast argument, otherwise `None`.
46llvm::Optional<std::string>
47stripFloatCast(const ast_matchers::MatchFinder::MatchResult &Result,
48 const Expr &Node);
49
50/// Possibly remove the fractional part of a floating point literal.
51///
52/// If `Node` represents a floating point literal with a zero fractional part,
53/// return the textual context of the integral part, otherwise `None`.
54llvm::Optional<std::string>
55stripFloatLiteralFraction(const ast_matchers::MatchFinder::MatchResult &Result,
56 const Expr &Node);
57
58/// Possibly further simplify a duration factory function's argument, without
59/// changing the scale of the factory function. Return that simplification or
60/// the text of the argument if no simplification is possible.
61std::string
62simplifyDurationFactoryArg(const ast_matchers::MatchFinder::MatchResult &Result,
63 const Expr &Node);
64
65/// Given the name of an inverse Duration function (e.g., `ToDoubleSeconds`),
66/// return its `DurationScale`, or `None` if a match is not found.
67llvm::Optional<DurationScale> getScaleForDurationInverse(llvm::StringRef Name);
68
69/// Given the name of an inverse Time function (e.g., `ToUnixSeconds`),
70/// return its `DurationScale`, or `None` if a match is not found.
71llvm::Optional<DurationScale> getScaleForTimeInverse(llvm::StringRef Name);
72
73/// Given a `Scale` return the fully qualified inverse functions for it.
74/// The first returned value is the inverse for `double`, and the second
75/// returned value is the inverse for `int64`.
76const std::pair<llvm::StringRef, llvm::StringRef> &
77getDurationInverseForScale(DurationScale Scale);
78
79/// Returns the Time inverse function name for a given `Scale`.
80llvm::StringRef getTimeInverseForScale(DurationScale scale);
81
82/// Assuming `Node` has type `double` or `int` representing a time interval of
83/// `Scale`, return the expression to make it a suitable `Duration`.
84std::string rewriteExprFromNumberToDuration(
85 const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
86 const Expr *Node);
87
88/// Assuming `Node` has a type `int` representing a time instant of `Scale`
89/// since The Epoch, return the expression to make it a suitable `Time`.
90std::string rewriteExprFromNumberToTime(
91 const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
92 const Expr *Node);
93
94/// Return `false` if `E` is a either: not a macro at all; or an argument to
95/// one. In the both cases, we often want to do the transformation.
96bool isInMacro(const ast_matchers::MatchFinder::MatchResult &Result,
97 const Expr *E);
98
99AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
100 DurationConversionFunction) {
101 using namespace clang::ast_matchers;
102 return functionDecl(
103 hasAnyName("::absl::ToDoubleHours", "::absl::ToDoubleMinutes",
104 "::absl::ToDoubleSeconds", "::absl::ToDoubleMilliseconds",
105 "::absl::ToDoubleMicroseconds", "::absl::ToDoubleNanoseconds",
106 "::absl::ToInt64Hours", "::absl::ToInt64Minutes",
107 "::absl::ToInt64Seconds", "::absl::ToInt64Milliseconds",
108 "::absl::ToInt64Microseconds", "::absl::ToInt64Nanoseconds"));
109}
110
111AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
112 DurationFactoryFunction) {
113 using namespace clang::ast_matchers;
114 return functionDecl(hasAnyName("::absl::Nanoseconds", "::absl::Microseconds",
115 "::absl::Milliseconds", "::absl::Seconds",
116 "::absl::Minutes", "::absl::Hours"));
117}
118
119AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
120 TimeConversionFunction) {
121 using namespace clang::ast_matchers;
122 return functionDecl(hasAnyName(
123 "::absl::ToUnixHours", "::absl::ToUnixMinutes", "::absl::ToUnixSeconds",
124 "::absl::ToUnixMillis", "::absl::ToUnixMicros", "::absl::ToUnixNanos"));
125}
126
127AST_MATCHER_FUNCTION_P(ast_matchers::internal::Matcher<Stmt>,
128 comparisonOperatorWithCallee,
129 ast_matchers::internal::Matcher<Decl>, funcDecl) {
130 using namespace clang::ast_matchers;
131 return binaryOperator(
132 anyOf(hasOperatorName(">"), hasOperatorName(">="), hasOperatorName("=="),
133 hasOperatorName("<="), hasOperatorName("<")),
134 hasEitherOperand(ignoringImpCasts(callExpr(callee(funcDecl)))));
135}
136
137} // namespace abseil
138} // namespace tidy
139} // namespace clang
140
141#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONCOMPARISONCHECK_H
142