clang-tools  8.0.0
DurationRewriter.h
Go to the documentation of this file.
1 //===--- DurationRewriter.h - clang-tidy ------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H
12 
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/ASTMatchers/ASTMatchers.h"
15 #include <cinttypes>
16 
17 namespace clang {
18 namespace tidy {
19 namespace abseil {
20 
21 /// Duration factory and conversion scales
22 enum class DurationScale : std::uint8_t {
23  Hours = 0,
24  Minutes,
25  Seconds,
29 };
30 
31 /// Given a `Scale`, return the appropriate factory function call for
32 /// constructing a `Duration` for that scale.
33 llvm::StringRef getFactoryForScale(DurationScale Scale);
34 
35 // Determine if `Node` represents a literal floating point or integral zero.
36 bool IsLiteralZero(const ast_matchers::MatchFinder::MatchResult &Result,
37  const Expr &Node);
38 
39 /// Possibly strip a floating point cast expression.
40 ///
41 /// If `Node` represents an explicit cast to a floating point type, return
42 /// the textual context of the cast argument, otherwise `None`.
43 llvm::Optional<std::string>
44 stripFloatCast(const ast_matchers::MatchFinder::MatchResult &Result,
45  const Expr &Node);
46 
47 /// Possibly remove the fractional part of a floating point literal.
48 ///
49 /// If `Node` represents a floating point literal with a zero fractional part,
50 /// return the textual context of the integral part, otherwise `None`.
51 llvm::Optional<std::string>
52 stripFloatLiteralFraction(const ast_matchers::MatchFinder::MatchResult &Result,
53  const Expr &Node);
54 
55 /// Possibly further simplify a duration factory function's argument, without
56 /// changing the scale of the factory function. Return that simplification or
57 /// the text of the argument if no simplification is possible.
58 std::string
59 simplifyDurationFactoryArg(const ast_matchers::MatchFinder::MatchResult &Result,
60  const Expr &Node);
61 
62 /// Given the name of an inverse Duration function (e.g., `ToDoubleSeconds`),
63 /// return its `DurationScale`, or `None` if a match is not found.
64 llvm::Optional<DurationScale> getScaleForInverse(llvm::StringRef Name);
65 
66 /// Given a `Scale` return the fully qualified inverse functions for it.
67 /// The first returned value is the inverse for `double`, and the second
68 /// returned value is the inverse for `int64`.
69 const std::pair<llvm::StringRef, llvm::StringRef> &
71 
72 /// Assuming `Node` has type `double` or `int` representing a time interval of
73 /// `Scale`, return the expression to make it a suitable `Duration`.
75  const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
76  const Expr *Node);
77 
78 AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
79  DurationConversionFunction) {
80  using namespace clang::ast_matchers;
81  return functionDecl(
82  hasAnyName("::absl::ToDoubleHours", "::absl::ToDoubleMinutes",
83  "::absl::ToDoubleSeconds", "::absl::ToDoubleMilliseconds",
84  "::absl::ToDoubleMicroseconds", "::absl::ToDoubleNanoseconds",
85  "::absl::ToInt64Hours", "::absl::ToInt64Minutes",
86  "::absl::ToInt64Seconds", "::absl::ToInt64Milliseconds",
87  "::absl::ToInt64Microseconds", "::absl::ToInt64Nanoseconds"));
88 }
89 
90 AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
91  DurationFactoryFunction) {
92  using namespace clang::ast_matchers;
93  return functionDecl(hasAnyName("::absl::Nanoseconds", "::absl::Microseconds",
94  "::absl::Milliseconds", "::absl::Seconds",
95  "::absl::Minutes", "::absl::Hours"));
96 }
97 
98 } // namespace abseil
99 } // namespace tidy
100 } // namespace clang
101 
102 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONCOMPARISONCHECK_H
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher< FunctionDecl >, DurationConversionFunction)
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 getFactoryForScale(DurationScale Scale)
Returns the factory function name for a given Scale.
static constexpr llvm::StringLiteral Name
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
std::string simplifyDurationFactoryArg(const MatchFinder::MatchResult &Result, const Expr &Node)
llvm::Optional< DurationScale > getScaleForInverse(llvm::StringRef Name)
Given the name of an inverse Duration function (e.g., ToDoubleSeconds), return its DurationScale...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< std::string > stripFloatCast(const ast_matchers::MatchFinder::MatchResult &Result, const Expr &Node)
Possibly strip a floating point cast expression.
llvm::Optional< std::string > stripFloatLiteralFraction(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.
const std::pair< llvm::StringRef, llvm::StringRef > & getInverseForScale(DurationScale Scale)
Given a Scale return the fully qualified inverse functions for it.
DurationScale
Duration factory and conversion scales.