aboutsummaryrefslogtreecommitdiff
path: root/clang-tidy
diff options
context:
space:
mode:
authorHyrum Wright <hwright@google.com>2019-02-04 19:28:20 +0000
committerHyrum Wright <hwright@google.com>2019-02-04 19:28:20 +0000
commitcd9ab0d12e688e69741d6cfd71af56a49b8a99f3 (patch)
tree91fb85aa7153b5155765bd316f04e5b25dbd646b /clang-tidy
parent8bbdbd0946bd4ea3d7e95f71073449ea49b106c8 (diff)
downloadclang-tools-extra-cd9ab0d12e688e69741d6cfd71af56a49b8a99f3.tar.gz
[clang-tidy] Add the abseil-duration-unnecessary-conversion check
Differential Revision: https://reviews.llvm.org/D57353 git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/trunk@353079 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clang-tidy')
-rw-r--r--clang-tidy/abseil/AbseilTidyModule.cpp3
-rw-r--r--clang-tidy/abseil/CMakeLists.txt1
-rw-r--r--clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp58
-rw-r--r--clang-tidy/abseil/DurationUnnecessaryConversionCheck.h35
4 files changed, 97 insertions, 0 deletions
diff --git a/clang-tidy/abseil/AbseilTidyModule.cpp b/clang-tidy/abseil/AbseilTidyModule.cpp
index b05318bb..1489b48b 100644
--- a/clang-tidy/abseil/AbseilTidyModule.cpp
+++ b/clang-tidy/abseil/AbseilTidyModule.cpp
@@ -16,6 +16,7 @@
#include "DurationFactoryFloatCheck.h"
#include "DurationFactoryScaleCheck.h"
#include "DurationSubtractionCheck.h"
+#include "DurationUnnecessaryConversionCheck.h"
#include "FasterStrsplitDelimiterCheck.h"
#include "NoInternalDependenciesCheck.h"
#include "NoNamespaceCheck.h"
@@ -45,6 +46,8 @@ public:
"abseil-duration-factory-scale");
CheckFactories.registerCheck<DurationSubtractionCheck>(
"abseil-duration-subtraction");
+ CheckFactories.registerCheck<DurationUnnecessaryConversionCheck>(
+ "abseil-duration-unnecessary-conversion");
CheckFactories.registerCheck<FasterStrsplitDelimiterCheck>(
"abseil-faster-strsplit-delimiter");
CheckFactories.registerCheck<NoInternalDependenciesCheck>(
diff --git a/clang-tidy/abseil/CMakeLists.txt b/clang-tidy/abseil/CMakeLists.txt
index 68247ba4..578dd57d 100644
--- a/clang-tidy/abseil/CMakeLists.txt
+++ b/clang-tidy/abseil/CMakeLists.txt
@@ -10,6 +10,7 @@ add_clang_library(clangTidyAbseilModule
DurationFactoryScaleCheck.cpp
DurationRewriter.cpp
DurationSubtractionCheck.cpp
+ DurationUnnecessaryConversionCheck.cpp
FasterStrsplitDelimiterCheck.cpp
NoInternalDependenciesCheck.cpp
NoNamespaceCheck.cpp
diff --git a/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp b/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
new file mode 100644
index 00000000..83650cd1
--- /dev/null
+++ b/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
@@ -0,0 +1,58 @@
+//===--- DurationUnnecessaryConversionCheck.cpp - clang-tidy
+//-----------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "DurationUnnecessaryConversionCheck.h"
+#include "DurationRewriter.h"
+#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 {
+
+void DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
+ for (const auto &Scale : {"Hours", "Minutes", "Seconds", "Milliseconds",
+ "Microseconds", "Nanoseconds"}) {
+ std::string DurationFactory = (llvm::Twine("::absl::") + Scale).str();
+ std::string FloatConversion =
+ (llvm::Twine("::absl::ToDouble") + Scale).str();
+ std::string IntegerConversion =
+ (llvm::Twine("::absl::ToInt64") + Scale).str();
+
+ Finder->addMatcher(
+ callExpr(
+ callee(functionDecl(hasName(DurationFactory))),
+ hasArgument(0, callExpr(callee(functionDecl(hasAnyName(
+ FloatConversion, IntegerConversion))),
+ hasArgument(0, expr().bind("arg")))))
+ .bind("call"),
+ this);
+ }
+}
+
+void DurationUnnecessaryConversionCheck::check(
+ const MatchFinder::MatchResult &Result) {
+ const auto *OuterCall = Result.Nodes.getNodeAs<Expr>("call");
+ const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
+
+ if (!isNotInMacro(Result, OuterCall))
+ return;
+
+ diag(OuterCall->getBeginLoc(), "remove unnecessary absl::Duration conversions")
+ << FixItHint::CreateReplacement(
+ OuterCall->getSourceRange(),
+ tooling::fixit::getText(*Arg, *Result.Context));
+}
+
+} // namespace abseil
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tidy/abseil/DurationUnnecessaryConversionCheck.h b/clang-tidy/abseil/DurationUnnecessaryConversionCheck.h
new file mode 100644
index 00000000..18061338
--- /dev/null
+++ b/clang-tidy/abseil/DurationUnnecessaryConversionCheck.h
@@ -0,0 +1,35 @@
+//===--- DurationUnnecessaryConversionCheck.h - clang-tidy ------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_TIMEDOUBLECONVERSIONCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_TIMEDOUBLECONVERSIONCHECK_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace abseil {
+
+/// Finds and fixes cases where ``absl::Duration`` values are being converted
+/// to numeric types and back again.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/abseil-duration-unnecessary-conversion.html
+class DurationUnnecessaryConversionCheck : public ClangTidyCheck {
+public:
+ DurationUnnecessaryConversionCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace abseil
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_TIMEDOUBLECONVERSIONCHECK_H