DurationRewriter.h 6.0 KB

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