2018-10-25 01:40:50 +08:00
|
|
|
//===--- DurationFactoryFloatCheck.cpp - clang-tidy -----------------------===//
|
|
|
|
//
|
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
|
2018-10-25 01:40:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DurationFactoryFloatCheck.h"
|
[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
|
|
|
#include "DurationRewriter.h"
|
2018-10-25 01:40:50 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Tooling/FixIt.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace abseil {
|
|
|
|
|
|
|
|
// Returns `true` if `Range` is inside a macro definition.
|
|
|
|
static bool InsideMacroDefinition(const MatchFinder::MatchResult &Result,
|
|
|
|
SourceRange Range) {
|
|
|
|
return !clang::Lexer::makeFileCharRange(
|
|
|
|
clang::CharSourceRange::getCharRange(Range),
|
|
|
|
*Result.SourceManager, Result.Context->getLangOpts())
|
|
|
|
.isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DurationFactoryFloatCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
Finder->addMatcher(
|
[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
|
|
|
callExpr(callee(functionDecl(DurationFactoryFunction())),
|
|
|
|
hasArgument(0, anyOf(cxxStaticCastExpr(hasDestinationType(
|
|
|
|
realFloatingPointType())),
|
|
|
|
cStyleCastExpr(hasDestinationType(
|
|
|
|
realFloatingPointType())),
|
|
|
|
cxxFunctionalCastExpr(hasDestinationType(
|
|
|
|
realFloatingPointType())),
|
|
|
|
floatLiteral())))
|
2018-10-25 01:40:50 +08:00
|
|
|
.bind("call"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DurationFactoryFloatCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("call");
|
|
|
|
|
|
|
|
// Don't try and replace things inside of macro definitions.
|
|
|
|
if (InsideMacroDefinition(Result, MatchedCall->getSourceRange()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Expr *Arg = MatchedCall->getArg(0)->IgnoreImpCasts();
|
|
|
|
// Arguments which are macros are ignored.
|
|
|
|
if (Arg->getBeginLoc().isMacroID())
|
|
|
|
return;
|
|
|
|
|
[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
|
|
|
llvm::Optional<std::string> SimpleArg = stripFloatCast(Result, *Arg);
|
|
|
|
if (!SimpleArg)
|
|
|
|
SimpleArg = stripFloatLiteralFraction(Result, *Arg);
|
|
|
|
|
|
|
|
if (SimpleArg) {
|
2018-10-25 01:40:50 +08:00
|
|
|
diag(MatchedCall->getBeginLoc(),
|
|
|
|
(llvm::Twine("use the integer version of absl::") +
|
|
|
|
MatchedCall->getDirectCallee()->getName())
|
|
|
|
.str())
|
[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
|
|
|
<< FixItHint::CreateReplacement(Arg->getSourceRange(), *SimpleArg);
|
2018-10-25 01:40:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace abseil
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|