[clang-tidy] Recommit: Add the abseil-duration-comparison check
Summary:
This check finds instances where Duration values are being converted to a numeric value in a comparison expression, and suggests that the conversion happen on the other side of the expression to a Duration. See documentation for examples.
This also shuffles some code around so that the new check may perform in sone step simplifications also caught by other checks.
Compilation is unbroken, because the hash-function is now directly
specified for std::unordered_map, as 'enum class' does not compile as
key (seamingly only on some compilers).
Patch by hwright.
Reviewers: aaron.ballman, JonasToth, alexfh, hokein
Reviewed By: JonasToth
Subscribers: sammccall, Eugene.Zelenko, xazax.hun, cfe-commits, mgorny
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D54737
llvm-svn: 348169
2018-12-04 03:22:08 +08:00
|
|
|
//===--- DurationRewriter.h - clang-tidy ------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
[clang-tidy] Recommit: Add the abseil-duration-comparison check
Summary:
This check finds instances where Duration values are being converted to a numeric value in a comparison expression, and suggests that the conversion happen on the other side of the expression to a Duration. See documentation for examples.
This also shuffles some code around so that the new check may perform in sone step simplifications also caught by other checks.
Compilation is unbroken, because the hash-function is now directly
specified for std::unordered_map, as 'enum class' does not compile as
key (seamingly only on some compilers).
Patch by hwright.
Reviewers: aaron.ballman, JonasToth, alexfh, hokein
Reviewed By: JonasToth
Subscribers: sammccall, Eugene.Zelenko, xazax.hun, cfe-commits, mgorny
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D54737
llvm-svn: 348169
2018-12-04 03:22:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONREWRITER_H
|
|
|
|
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace abseil {
|
|
|
|
|
|
|
|
/// Duration factory and conversion scales
|
2018-12-14 03:23:52 +08:00
|
|
|
enum class DurationScale : std::uint8_t {
|
|
|
|
Hours = 0,
|
[clang-tidy] Recommit: Add the abseil-duration-comparison check
Summary:
This check finds instances where Duration values are being converted to a numeric value in a comparison expression, and suggests that the conversion happen on the other side of the expression to a Duration. See documentation for examples.
This also shuffles some code around so that the new check may perform in sone step simplifications also caught by other checks.
Compilation is unbroken, because the hash-function is now directly
specified for std::unordered_map, as 'enum class' does not compile as
key (seamingly only on some compilers).
Patch by hwright.
Reviewers: aaron.ballman, JonasToth, alexfh, hokein
Reviewed By: JonasToth
Subscribers: sammccall, Eugene.Zelenko, xazax.hun, cfe-commits, mgorny
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D54737
llvm-svn: 348169
2018-12-04 03:22:08 +08:00
|
|
|
Minutes,
|
|
|
|
Seconds,
|
|
|
|
Milliseconds,
|
|
|
|
Microseconds,
|
|
|
|
Nanoseconds,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Given a `Scale`, return the appropriate factory function call for
|
|
|
|
/// constructing a `Duration` for that scale.
|
2019-01-25 03:23:50 +08:00
|
|
|
llvm::StringRef getDurationFactoryForScale(DurationScale Scale);
|
[clang-tidy] Recommit: Add the abseil-duration-comparison check
Summary:
This check finds instances where Duration values are being converted to a numeric value in a comparison expression, and suggests that the conversion happen on the other side of the expression to a Duration. See documentation for examples.
This also shuffles some code around so that the new check may perform in sone step simplifications also caught by other checks.
Compilation is unbroken, because the hash-function is now directly
specified for std::unordered_map, as 'enum class' does not compile as
key (seamingly only on some compilers).
Patch by hwright.
Reviewers: aaron.ballman, JonasToth, alexfh, hokein
Reviewed By: JonasToth
Subscribers: sammccall, Eugene.Zelenko, xazax.hun, cfe-commits, mgorny
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D54737
llvm-svn: 348169
2018-12-04 03:22:08 +08:00
|
|
|
|
2019-02-28 04:08:50 +08:00
|
|
|
/// Given a 'Scale', return the appropriate factory function call for
|
|
|
|
/// constructing a `Time` for that scale.
|
|
|
|
llvm::StringRef getTimeFactoryForScale(DurationScale scale);
|
|
|
|
|
[clang-tidy] Recommit: Add the abseil-duration-comparison check
Summary:
This check finds instances where Duration values are being converted to a numeric value in a comparison expression, and suggests that the conversion happen on the other side of the expression to a Duration. See documentation for examples.
This also shuffles some code around so that the new check may perform in sone step simplifications also caught by other checks.
Compilation is unbroken, because the hash-function is now directly
specified for std::unordered_map, as 'enum class' does not compile as
key (seamingly only on some compilers).
Patch by hwright.
Reviewers: aaron.ballman, JonasToth, alexfh, hokein
Reviewed By: JonasToth
Subscribers: sammccall, Eugene.Zelenko, xazax.hun, cfe-commits, mgorny
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D54737
llvm-svn: 348169
2018-12-04 03:22:08 +08:00
|
|
|
// Determine if `Node` represents a literal floating point or integral zero.
|
|
|
|
bool IsLiteralZero(const ast_matchers::MatchFinder::MatchResult &Result,
|
|
|
|
const Expr &Node);
|
|
|
|
|
|
|
|
/// Possibly strip a floating point cast expression.
|
|
|
|
///
|
|
|
|
/// If `Node` represents an explicit cast to a floating point type, return
|
|
|
|
/// the textual context of the cast argument, otherwise `None`.
|
|
|
|
llvm::Optional<std::string>
|
|
|
|
stripFloatCast(const ast_matchers::MatchFinder::MatchResult &Result,
|
|
|
|
const Expr &Node);
|
|
|
|
|
|
|
|
/// Possibly remove the fractional part of a floating point literal.
|
|
|
|
///
|
|
|
|
/// If `Node` represents a floating point literal with a zero fractional part,
|
|
|
|
/// return the textual context of the integral part, otherwise `None`.
|
|
|
|
llvm::Optional<std::string>
|
|
|
|
stripFloatLiteralFraction(const ast_matchers::MatchFinder::MatchResult &Result,
|
|
|
|
const Expr &Node);
|
|
|
|
|
|
|
|
/// Possibly further simplify a duration factory function's argument, without
|
|
|
|
/// changing the scale of the factory function. Return that simplification or
|
|
|
|
/// the text of the argument if no simplification is possible.
|
|
|
|
std::string
|
|
|
|
simplifyDurationFactoryArg(const ast_matchers::MatchFinder::MatchResult &Result,
|
|
|
|
const Expr &Node);
|
|
|
|
|
2018-12-14 03:23:52 +08:00
|
|
|
/// Given the name of an inverse Duration function (e.g., `ToDoubleSeconds`),
|
|
|
|
/// return its `DurationScale`, or `None` if a match is not found.
|
2019-01-25 03:23:50 +08:00
|
|
|
llvm::Optional<DurationScale> getScaleForDurationInverse(llvm::StringRef Name);
|
2018-12-14 03:23:52 +08:00
|
|
|
|
2019-01-28 22:03:09 +08:00
|
|
|
/// Given the name of an inverse Time function (e.g., `ToUnixSeconds`),
|
|
|
|
/// return its `DurationScale`, or `None` if a match is not found.
|
|
|
|
llvm::Optional<DurationScale> getScaleForTimeInverse(llvm::StringRef Name);
|
|
|
|
|
2018-12-29 03:04:21 +08:00
|
|
|
/// Given a `Scale` return the fully qualified inverse functions for it.
|
|
|
|
/// The first returned value is the inverse for `double`, and the second
|
|
|
|
/// returned value is the inverse for `int64`.
|
|
|
|
const std::pair<llvm::StringRef, llvm::StringRef> &
|
2019-01-25 03:23:50 +08:00
|
|
|
getDurationInverseForScale(DurationScale Scale);
|
2018-12-29 03:04:21 +08:00
|
|
|
|
2019-02-03 03:57:37 +08:00
|
|
|
/// Returns the Time inverse function name for a given `Scale`.
|
|
|
|
llvm::StringRef getTimeInverseForScale(DurationScale scale);
|
|
|
|
|
2018-12-14 03:23:52 +08:00
|
|
|
/// Assuming `Node` has type `double` or `int` representing a time interval of
|
|
|
|
/// `Scale`, return the expression to make it a suitable `Duration`.
|
2018-12-20 00:03:34 +08:00
|
|
|
std::string rewriteExprFromNumberToDuration(
|
2018-12-14 03:23:52 +08:00
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
|
|
|
|
const Expr *Node);
|
|
|
|
|
2019-02-28 04:08:50 +08:00
|
|
|
/// Assuming `Node` has a type `int` representing a time instant of `Scale`
|
|
|
|
/// since The Epoch, return the expression to make it a suitable `Time`.
|
|
|
|
std::string rewriteExprFromNumberToTime(
|
|
|
|
const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
|
|
|
|
const Expr *Node);
|
|
|
|
|
2019-03-08 23:37:15 +08:00
|
|
|
/// Return `false` if `E` is a either: not a macro at all; or an argument to
|
2019-01-16 22:49:32 +08:00
|
|
|
/// one. In the both cases, we often want to do the transformation.
|
2019-03-08 23:37:15 +08:00
|
|
|
bool isInMacro(const ast_matchers::MatchFinder::MatchResult &Result,
|
|
|
|
const Expr *E);
|
2019-01-16 22:49:32 +08:00
|
|
|
|
[clang-tidy] Recommit: Add the abseil-duration-comparison check
Summary:
This check finds instances where Duration values are being converted to a numeric value in a comparison expression, and suggests that the conversion happen on the other side of the expression to a Duration. See documentation for examples.
This also shuffles some code around so that the new check may perform in sone step simplifications also caught by other checks.
Compilation is unbroken, because the hash-function is now directly
specified for std::unordered_map, as 'enum class' does not compile as
key (seamingly only on some compilers).
Patch by hwright.
Reviewers: aaron.ballman, JonasToth, alexfh, hokein
Reviewed By: JonasToth
Subscribers: sammccall, Eugene.Zelenko, xazax.hun, cfe-commits, mgorny
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D54737
llvm-svn: 348169
2018-12-04 03:22:08 +08:00
|
|
|
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
|
|
|
|
DurationConversionFunction) {
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
return functionDecl(
|
|
|
|
hasAnyName("::absl::ToDoubleHours", "::absl::ToDoubleMinutes",
|
|
|
|
"::absl::ToDoubleSeconds", "::absl::ToDoubleMilliseconds",
|
|
|
|
"::absl::ToDoubleMicroseconds", "::absl::ToDoubleNanoseconds",
|
|
|
|
"::absl::ToInt64Hours", "::absl::ToInt64Minutes",
|
|
|
|
"::absl::ToInt64Seconds", "::absl::ToInt64Milliseconds",
|
|
|
|
"::absl::ToInt64Microseconds", "::absl::ToInt64Nanoseconds"));
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
|
|
|
|
DurationFactoryFunction) {
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
return functionDecl(hasAnyName("::absl::Nanoseconds", "::absl::Microseconds",
|
|
|
|
"::absl::Milliseconds", "::absl::Seconds",
|
|
|
|
"::absl::Minutes", "::absl::Hours"));
|
|
|
|
}
|
|
|
|
|
2019-01-28 22:03:09 +08:00
|
|
|
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
|
2019-02-03 03:57:37 +08:00
|
|
|
TimeConversionFunction) {
|
2019-01-28 22:03:09 +08:00
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
return functionDecl(hasAnyName(
|
|
|
|
"::absl::ToUnixHours", "::absl::ToUnixMinutes", "::absl::ToUnixSeconds",
|
|
|
|
"::absl::ToUnixMillis", "::absl::ToUnixMicros", "::absl::ToUnixNanos"));
|
|
|
|
}
|
|
|
|
|
2019-03-12 00:47:45 +08:00
|
|
|
AST_MATCHER_FUNCTION_P(ast_matchers::internal::Matcher<Stmt>,
|
|
|
|
comparisonOperatorWithCallee,
|
|
|
|
ast_matchers::internal::Matcher<Decl>, funcDecl) {
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
return binaryOperator(
|
|
|
|
anyOf(hasOperatorName(">"), hasOperatorName(">="), hasOperatorName("=="),
|
|
|
|
hasOperatorName("<="), hasOperatorName("<")),
|
|
|
|
hasEitherOperand(ignoringImpCasts(callExpr(callee(funcDecl)))));
|
|
|
|
}
|
|
|
|
|
[clang-tidy] Recommit: Add the abseil-duration-comparison check
Summary:
This check finds instances where Duration values are being converted to a numeric value in a comparison expression, and suggests that the conversion happen on the other side of the expression to a Duration. See documentation for examples.
This also shuffles some code around so that the new check may perform in sone step simplifications also caught by other checks.
Compilation is unbroken, because the hash-function is now directly
specified for std::unordered_map, as 'enum class' does not compile as
key (seamingly only on some compilers).
Patch by hwright.
Reviewers: aaron.ballman, JonasToth, alexfh, hokein
Reviewed By: JonasToth
Subscribers: sammccall, Eugene.Zelenko, xazax.hun, cfe-commits, mgorny
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D54737
llvm-svn: 348169
2018-12-04 03:22:08 +08:00
|
|
|
} // namespace abseil
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_DURATIONCOMPARISONCHECK_H
|