clang-tools  14.0.0git
DurationRewriter.h
Go to the documentation of this file.
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 
16 namespace clang {
17 namespace tidy {
18 namespace abseil {
19 
20 /// Duration factory and conversion scales
21 enum class DurationScale : std::uint8_t {
22  Hours = 0,
23  Minutes,
24  Seconds,
28 };
29 
30 /// Given a `Scale`, return the appropriate factory function call for
31 /// constructing a `Duration` for that scale.
32 llvm::StringRef getDurationFactoryForScale(DurationScale Scale);
33 
34 /// Given a 'Scale', return the appropriate factory function call for
35 /// constructing a `Time` for that scale.
36 llvm::StringRef getTimeFactoryForScale(DurationScale Scale);
37 
38 // Determine if `Node` represents a literal floating point or integral zero.
39 bool 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`.
46 llvm::Optional<std::string>
47 stripFloatCast(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`.
54 llvm::Optional<std::string>
55 stripFloatLiteralFraction(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.
61 std::string
62 simplifyDurationFactoryArg(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.
67 llvm::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.
71 llvm::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`.
76 const std::pair<llvm::StringRef, llvm::StringRef> &
78 
79 /// Returns the Time inverse function name for a given `Scale`.
80 llvm::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`.
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`.
90 std::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.
96 bool isInMacro(const ast_matchers::MatchFinder::MatchResult &Result,
97  const Expr *E);
98 
99 AST_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 
111 AST_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 
119 AST_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 
127 AST_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_DURATIONREWRITER_H
clang::tidy::abseil::getScaleForTimeInverse
llvm::Optional< DurationScale > getScaleForTimeInverse(llvm::StringRef Name)
Given the name of an inverse Time function (e.g., ToUnixSeconds), return its DurationScale,...
Definition: DurationRewriter.cpp:253
clang::tidy::abseil::DurationScale::Nanoseconds
@ Nanoseconds
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::tidy::abseil::AST_MATCHER_FUNCTION
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher< FunctionDecl >, DurationConversionFunction)
Definition: DurationRewriter.h:99
clang::tidy::abseil::getScaleForDurationInverse
llvm::Optional< DurationScale > getScaleForDurationInverse(llvm::StringRef Name)
Given the name of an inverse Duration function (e.g., ToDoubleSeconds), return its DurationScale,...
Definition: DurationRewriter.cpp:231
clang::tidy::abseil::isInMacro
bool isInMacro(const MatchFinder::MatchResult &Result, const Expr *E)
Definition: DurationRewriter.cpp:305
clang::tidy::abseil::DurationScale
DurationScale
Duration factory and conversion scales.
Definition: DurationRewriter.h:21
clang::tidy::abseil::getTimeFactoryForScale
llvm::StringRef getTimeFactoryForScale(DurationScale Scale)
Given a 'Scale', return the appropriate factory function call for constructing a Time for that scale.
Definition: DurationRewriter.cpp:122
clang::tidy::abseil::DurationScale::Milliseconds
@ Milliseconds
clang::tidy::abseil::DurationScale::Microseconds
@ Microseconds
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::abseil::DurationScale::Minutes
@ Minutes
clang::tidy::abseil::isLiteralZero
bool isLiteralZero(const MatchFinder::MatchResult &Result, const Expr &Node)
Returns true if Node is a value which evaluates to a literal 0.
Definition: DurationRewriter.cpp:160
clang::tidy::abseil::getDurationInverseForScale
const std::pair< llvm::StringRef, llvm::StringRef > & getDurationInverseForScale(DurationScale Scale)
Given a Scale return the fully qualified inverse functions for it.
Definition: DurationRewriter.cpp:40
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::abseil::DurationScale::Seconds
@ Seconds
clang::tidy::abseil::getDurationFactoryForScale
llvm::StringRef getDurationFactoryForScale(DurationScale Scale)
Returns the factory function name for a given Scale.
Definition: DurationRewriter.cpp:104
clang::tidy::abseil::simplifyDurationFactoryArg
std::string simplifyDurationFactoryArg(const MatchFinder::MatchResult &Result, const Expr &Node)
Definition: DurationRewriter.cpp:216
clang::tidy::abseil::stripFloatCast
llvm::Optional< std::string > stripFloatCast(const ast_matchers::MatchFinder::MatchResult &Result, const Expr &Node)
Possibly strip a floating point cast expression.
Definition: DurationRewriter.cpp:186
clang::tidy::abseil::AST_MATCHER_FUNCTION_P
AST_MATCHER_FUNCTION_P(ast_matchers::internal::Matcher< Stmt >, comparisonOperatorWithCallee, ast_matchers::internal::Matcher< Decl >, funcDecl)
Definition: DurationRewriter.h:127
clang::tidy::abseil::rewriteExprFromNumberToDuration
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 ...
Definition: DurationRewriter.cpp:269
clang::tidy::abseil::stripFloatLiteralFraction
llvm::Optional< std::string > stripFloatLiteralFraction(const MatchFinder::MatchResult &Result, const Expr &Node)
Definition: DurationRewriter.cpp:206
clang::tidy::abseil::getTimeInverseForScale
llvm::StringRef getTimeInverseForScale(DurationScale Scale)
Returns the Time factory function name for a given Scale.
Definition: DurationRewriter.cpp:141
clang::tidy::abseil::rewriteExprFromNumberToTime
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,...
Definition: DurationRewriter.cpp:287
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::abseil::DurationScale::Hours
@ Hours