[clang-tools-extra] 3860b2a - [clang-tidy] Update Abseil Duration Conversion check to find more cases.

2020-03-13 Thread Hyrum Wright via cfe-commits

Author: Hyrum Wright
Date: 2020-03-13T12:52:37-04:00
New Revision: 3860b2a0bd09291a276b0590939961dffe67fbb6

URL: 
https://github.com/llvm/llvm-project/commit/3860b2a0bd09291a276b0590939961dffe67fbb6
DIFF: 
https://github.com/llvm/llvm-project/commit/3860b2a0bd09291a276b0590939961dffe67fbb6.diff

LOG: [clang-tidy] Update Abseil Duration Conversion check to find more cases.

This change improves the check to handle cases with internal scalar
multiplication.

Differential Revision: https://reviews.llvm.org/D75558

Added: 


Modified: 
clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp

clang-tools-extra/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst

clang-tools-extra/test/clang-tidy/checkers/abseil-duration-unnecessary-conversion.cpp

Removed: 




diff  --git 
a/clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp 
b/clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
index 948feee3c504..28f970e17509 100644
--- a/clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
@@ -52,10 +52,22 @@ void 
DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
 callee(functionDecl(hasName("::absl::FDivDuration"))),
 hasArgument(0, expr().bind("arg")), hasArgument(1, factory_matcher));
 
+// Matcher which matches a duration argument being scaled,
+// e.g. `absl::ToDoubleSeconds(dur) * 2`
+auto scalar_matcher = ignoringImpCasts(
+binaryOperator(hasOperatorName("*"),
+   hasEitherOperand(expr(ignoringParenImpCasts(
+   callExpr(callee(functionDecl(hasAnyName(
+FloatConversion, IntegerConversion))),
+hasArgument(0, expr().bind("arg")))
+   .bind("inner_call")
+.bind("binop"));
+
 Finder->addMatcher(
 callExpr(callee(functionDecl(hasName(DurationFactory))),
  hasArgument(0, anyOf(inverse_function_matcher,
-  division_operator_matcher, 
fdiv_matcher)))
+  division_operator_matcher, fdiv_matcher,
+  scalar_matcher)))
 .bind("call"),
 this);
   }
@@ -64,16 +76,41 @@ void 
DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
 void DurationUnnecessaryConversionCheck::check(
 const MatchFinder::MatchResult &Result) {
   const auto *OuterCall = Result.Nodes.getNodeAs("call");
-  const auto *Arg = Result.Nodes.getNodeAs("arg");
 
   if (isInMacro(Result, OuterCall))
 return;
 
+  FixItHint Hint;
+  if (const auto *Binop = Result.Nodes.getNodeAs("binop")) {
+const auto *Arg = Result.Nodes.getNodeAs("arg");
+const auto *InnerCall = Result.Nodes.getNodeAs("inner_call");
+const Expr *LHS = Binop->getLHS();
+const Expr *RHS = Binop->getRHS();
+
+if (LHS->IgnoreParenImpCasts() == InnerCall) {
+  Hint = FixItHint::CreateReplacement(
+  OuterCall->getSourceRange(),
+  (llvm::Twine(tooling::fixit::getText(*Arg, *Result.Context)) + " * " 
+
+   tooling::fixit::getText(*RHS, *Result.Context))
+  .str());
+} else {
+  assert(RHS->IgnoreParenImpCasts() == InnerCall &&
+ "Inner call should be find on the RHS");
+
+  Hint = FixItHint::CreateReplacement(
+  OuterCall->getSourceRange(),
+  (llvm::Twine(tooling::fixit::getText(*LHS, *Result.Context)) + " * " 
+
+   tooling::fixit::getText(*Arg, *Result.Context))
+  .str());
+}
+  } else if (const auto *Arg = Result.Nodes.getNodeAs("arg")) {
+Hint = FixItHint::CreateReplacement(
+OuterCall->getSourceRange(),
+tooling::fixit::getText(*Arg, *Result.Context));
+  }
   diag(OuterCall->getBeginLoc(),
"remove unnecessary absl::Duration conversions")
-  << FixItHint::CreateReplacement(
- OuterCall->getSourceRange(),
- tooling::fixit::getText(*Arg, *Result.Context));
+  << Hint;
 }
 
 } // namespace abseil

diff  --git 
a/clang-tools-extra/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst
 
b/clang-tools-extra/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst
index 2f978f4d57c0..264c5d08b9d2 100644
--- 
a/clang-tools-extra/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst
+++ 
b/clang-tools-extra/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst
@@ -40,6 +40,17 @@ Integer examples:
   // Suggestion - Remove division and conversion
   absl::Duration d2 = d1;
 
+Unwrapping scalar operations:
+
+.. code-block:: c++
+
+  // Original - Multiplication by a scalar
+  absl::Duration d1;
+ 

[clang-tools-extra] r349073 - [clang-tidy] Add the abseil-duration-subtraction check

2018-12-13 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Thu Dec 13 11:23:52 2018
New Revision: 349073

URL: http://llvm.org/viewvc/llvm-project?rev=349073&view=rev
Log:
[clang-tidy] Add the abseil-duration-subtraction check

Summary:
This check uses the context of a subtraction expression as well as knowledge
about the Abseil Time types, to infer the type of the second operand of some
subtraction expressions in Duration conversions. For example:

   absl::ToDoubleSeconds(duration) - foo

can become
   absl::ToDoubleSeconds(duration - absl::Seconds(foo))

This ensures that time calculations are done in the proper domain, and also
makes it easier to further deduce the types of the second operands to these
expressions.

Reviewed By: JonasToth

Tags: #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D55245

Added:
clang-tools-extra/trunk/clang-tidy/abseil/DurationSubtractionCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationSubtractionCheck.h

clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-duration-subtraction.rst
clang-tools-extra/trunk/test/clang-tidy/abseil-duration-subtraction.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst

Modified: clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp?rev=349073&r1=349072&r2=349073&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp Thu Dec 13 
11:23:52 2018
@@ -14,6 +14,7 @@
 #include "DurationDivisionCheck.h"
 #include "DurationFactoryFloatCheck.h"
 #include "DurationFactoryScaleCheck.h"
+#include "DurationSubtractionCheck.h"
 #include "FasterStrsplitDelimiterCheck.h"
 #include "NoInternalDependenciesCheck.h"
 #include "NoNamespaceCheck.h"
@@ -37,6 +38,8 @@ public:
 "abseil-duration-factory-float");
 CheckFactories.registerCheck(
 "abseil-duration-factory-scale");
+CheckFactories.registerCheck(
+"abseil-duration-subtraction");
 CheckFactories.registerCheck(
 "abseil-faster-strsplit-delimiter");
 CheckFactories.registerCheck(

Modified: clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt?rev=349073&r1=349072&r2=349073&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt Thu Dec 13 
11:23:52 2018
@@ -7,6 +7,7 @@ add_clang_library(clangTidyAbseilModule
   DurationFactoryFloatCheck.cpp
   DurationFactoryScaleCheck.cpp
   DurationRewriter.cpp
+  DurationSubtractionCheck.cpp
   FasterStrsplitDelimiterCheck.cpp
   NoInternalDependenciesCheck.cpp
   NoNamespaceCheck.cpp

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp?rev=349073&r1=349072&r2=349073&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp Thu 
Dec 13 11:23:52 2018
@@ -19,101 +19,6 @@ namespace clang {
 namespace tidy {
 namespace abseil {
 
-/// Given the name of an inverse Duration function (e.g., `ToDoubleSeconds`),
-/// return its `DurationScale`, or `None` if a match is not found.
-static llvm::Optional getScaleForInverse(llvm::StringRef Name) {
-  static const llvm::DenseMap ScaleMap(
-  {{"ToDoubleHours", DurationScale::Hours},
-   {"ToInt64Hours", DurationScale::Hours},
-   {"ToDoubleMinutes", DurationScale::Minutes},
-   {"ToInt64Minutes", DurationScale::Minutes},
-   {"ToDoubleSeconds", DurationScale::Seconds},
-   {"ToInt64Seconds", DurationScale::Seconds},
-   {"ToDoubleMilliseconds", DurationScale::Milliseconds},
-   {"ToInt64Milliseconds", DurationScale::Milliseconds},
-   {"ToDoubleMicroseconds", DurationScale::Microseconds},
-   {"ToInt64Microseconds", DurationScale::Microseconds},
-   {"ToDoubleNanoseconds", DurationScale::Nanoseconds},
-   {"ToInt64Nanoseconds", DurationScale::Nanoseconds}});
-
-  auto ScaleIter = ScaleMap.find(std::string(Name));
-  if (ScaleIter == ScaleMap.end())
-return

[clang-tools-extra] r349636 - [clang-tidy] Diagnose abseil-duration-comparison on macro arguments

2018-12-19 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Wed Dec 19 08:03:34 2018
New Revision: 349636

URL: http://llvm.org/viewvc/llvm-project?rev=349636&view=rev
Log:
[clang-tidy] Diagnose abseil-duration-comparison on macro arguments

Summary:
This change relaxes the requirements on the utility
`rewriteExprFromNumberToDuration` function, and introduces new checking
inside of the `abseil-duration-comparison` check to allow macro argument
expression transformation.

Differential Revision: https://reviews.llvm.org/D55784

Modified:
clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
clang-tools-extra/trunk/clang-tidy/abseil/DurationSubtractionCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/abseil-duration-comparison.cpp

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp?rev=349636&r1=349635&r2=349636&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp Wed 
Dec 19 08:03:34 2018
@@ -19,6 +19,26 @@ namespace clang {
 namespace tidy {
 namespace abseil {
 
+/// Return `true` if `E` is a either: not a macro at all; or an argument to
+/// one. In the latter case, we should still transform it.
+static bool IsValidMacro(const MatchFinder::MatchResult &Result,
+ const Expr *E) {
+  if (!E->getBeginLoc().isMacroID())
+return true;
+
+  SourceLocation Loc = E->getBeginLoc();
+  // We want to get closer towards the initial macro typed into the source only
+  // if the location is being expanded as a macro argument.
+  while (Result.SourceManager->isMacroArgExpansion(Loc)) {
+// We are calling getImmediateMacroCallerLoc, but note it is essentially
+// equivalent to calling getImmediateSpellingLoc in this context according
+// to Clang implementation. We are not calling getImmediateSpellingLoc
+// because Clang comment says it "should not generally be used by clients."
+Loc = Result.SourceManager->getImmediateMacroCallerLoc(Loc);
+  }
+  return !Loc.isMacroID();
+}
+
 void DurationComparisonCheck::registerMatchers(MatchFinder *Finder) {
   auto Matcher =
   binaryOperator(anyOf(hasOperatorName(">"), hasOperatorName(">="),
@@ -35,10 +55,6 @@ void DurationComparisonCheck::registerMa
 void DurationComparisonCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *Binop = Result.Nodes.getNodeAs("binop");
 
-  // Don't try to replace things inside of macro definitions.
-  if (Binop->getExprLoc().isMacroID())
-return;
-
   llvm::Optional Scale = getScaleForInverse(
   Result.Nodes.getNodeAs("function_decl")->getName());
   if (!Scale)
@@ -48,19 +64,19 @@ void DurationComparisonCheck::check(cons
   // want to handle the case of rewriting both sides. This is much simpler if
   // we unconditionally try and rewrite both, and let the rewriter determine
   // if nothing needs to be done.
-  llvm::Optional LhsReplacement =
+  if (!IsValidMacro(Result, Binop->getLHS()) ||
+  !IsValidMacro(Result, Binop->getRHS()))
+return;
+  std::string LhsReplacement =
   rewriteExprFromNumberToDuration(Result, *Scale, Binop->getLHS());
-  llvm::Optional RhsReplacement =
+  std::string RhsReplacement =
   rewriteExprFromNumberToDuration(Result, *Scale, Binop->getRHS());
 
-  if (!(LhsReplacement && RhsReplacement))
-return;
-
   diag(Binop->getBeginLoc(), "perform comparison in the duration domain")
   << FixItHint::CreateReplacement(Binop->getSourceRange(),
-  (llvm::Twine(*LhsReplacement) + " " +
+  (llvm::Twine(LhsReplacement) + " " +
Binop->getOpcodeStr() + " " +
-   *RhsReplacement)
+   RhsReplacement)
   .str());
 }
 

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp?rev=349636&r1=349635&r2=349636&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp Wed Dec 19 
08:03:34 2018
@@ -183,14 +183,11 @@ llvm::Optional getScaleFo
   return ScaleIter->second;
 }
 
-llvm::Optional rewriteExprFromNumberToDuration(
+std::string rewriteExprFromNumberToDuration(
 const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
 const Expr *Node) {
   const Ex

[clang-tools-extra] r349953 - [clang-tidy] Be more liberal about literal zeroes in abseil checks

2018-12-21 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Fri Dec 21 13:07:11 2018
New Revision: 349953

URL: http://llvm.org/viewvc/llvm-project?rev=349953&view=rev
Log:
[clang-tidy] Be more liberal about literal zeroes in abseil checks

Summary:
Previously, we'd only match on literal floating or integral zeroes, but I've 
now also learned that some users spell that value as int{0} or float{0}, which 
also need to be matched.

Differential Revision: https://reviews.llvm.org/D56012

Modified:
clang-tools-extra/trunk/clang-tidy/abseil/DurationFactoryScaleCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
clang-tools-extra/trunk/test/clang-tidy/abseil-duration-factory-scale.cpp

Modified: 
clang-tools-extra/trunk/clang-tidy/abseil/DurationFactoryScaleCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationFactoryScaleCheck.cpp?rev=349953&r1=349952&r2=349953&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationFactoryScaleCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationFactoryScaleCheck.cpp Fri 
Dec 21 13:07:11 2018
@@ -123,6 +123,10 @@ void DurationFactoryScaleCheck::register
   hasArgument(
   0,
   ignoringImpCasts(anyOf(
+  cxxFunctionalCastExpr(
+  hasDestinationType(
+  anyOf(isInteger(), realFloatingPointType())),
+  hasSourceExpression(initListExpr())),
   integerLiteral(equals(0)), floatLiteral(equals(0.0)),
   binaryOperator(hasOperatorName("*"),
  hasEitherOperand(ignoringImpCasts(

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp?rev=349953&r1=349952&r2=349953&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp Fri Dec 21 
13:07:11 2018
@@ -105,14 +105,44 @@ llvm::StringRef getFactoryForScale(Durat
   llvm_unreachable("unknown scaling factor");
 }
 
+/// Matches the n'th item of an initializer list expression.
+///
+/// Example matches y.
+/// (matcher = initListExpr(hasInit(0, expr(
+/// \code
+///   int x{y}.
+/// \endcode
+AST_MATCHER_P2(InitListExpr, hasInit, unsigned, N,
+   ast_matchers::internal::Matcher, InnerMatcher) {
+  return N < Node.getNumInits() &&
+  InnerMatcher.matches(*Node.getInit(N)->IgnoreParenImpCasts(), Finder,
+   Builder);
+}
+
 /// Returns `true` if `Node` is a value which evaluates to a literal `0`.
 bool IsLiteralZero(const MatchFinder::MatchResult &Result, const Expr &Node) {
-  return selectFirst(
- "val",
- match(expr(ignoringImpCasts(anyOf(integerLiteral(equals(0)),
-   floatLiteral(equals(0.0)
-   .bind("val"),
-   Node, *Result.Context)) != nullptr;
+  auto ZeroMatcher =
+  anyOf(integerLiteral(equals(0)), floatLiteral(equals(0.0)));
+
+  // Check to see if we're using a zero directly.
+  if (selectFirst(
+  "val", match(expr(ignoringImpCasts(ZeroMatcher)).bind("val"), Node,
+   *Result.Context)) != nullptr)
+return true;
+
+  // Now check to see if we're using a functional cast with a scalar
+  // initializer expression, e.g. `int{0}`.
+  if (selectFirst(
+  "val",
+  match(cxxFunctionalCastExpr(
+hasDestinationType(
+anyOf(isInteger(), realFloatingPointType())),
+hasSourceExpression(initListExpr(hasInit(0, ZeroMatcher
+.bind("val"),
+Node, *Result.Context)) != nullptr)
+return true;
+
+  return false;
 }
 
 llvm::Optional

Modified: 
clang-tools-extra/trunk/test/clang-tidy/abseil-duration-factory-scale.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/abseil-duration-factory-scale.cpp?rev=349953&r1=349952&r2=349953&view=diff
==
--- clang-tools-extra/trunk/test/clang-tidy/abseil-duration-factory-scale.cpp 
(original)
+++ clang-tools-extra/trunk/test/clang-tidy/abseil-duration-factory-scale.cpp 
Fri Dec 21 13:07:11 2018
@@ -2,6 +2,9 @@
 
 #include "absl/time/time.h"
 
+namespace std { typedef long long int64_t; }
+using int64_t = std::int64_t;
+
 void ScaleTest() {
   absl::Duration d;
 
@@ -30,6 +33,15 @@ void ScaleTest() {
   d = absl::Seconds(0x0.01p-126f);
   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for 
zero-length time intervals [abseil-duration-factory-scale]
   // C

[clang-tools-extra] r350133 - [clang-tidy] Export the abseil duration inverse lookup function, NFC

2018-12-28 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Fri Dec 28 11:04:21 2018
New Revision: 350133

URL: http://llvm.org/viewvc/llvm-project?rev=350133&view=rev
Log:
[clang-tidy] Export the abseil duration inverse lookup function, NFC

This allows other tools to use this function.

Modified:
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp?rev=350133&r1=350132&r2=350133&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp Fri Dec 28 
11:04:21 2018
@@ -37,8 +37,7 @@ truncateIfIntegral(const FloatingLiteral
   return llvm::None;
 }
 
-/// Given a `Scale` return the inverse functions for it.
-static const std::pair &
+const std::pair &
 getInverseForScale(DurationScale Scale) {
   static const llvm::IndexedMap,
 DurationScale2IndexFunctor>

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h?rev=350133&r1=350132&r2=350133&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h Fri Dec 28 
11:04:21 2018
@@ -63,6 +63,12 @@ simplifyDurationFactoryArg(const ast_mat
 /// return its `DurationScale`, or `None` if a match is not found.
 llvm::Optional getScaleForInverse(llvm::StringRef Name);
 
+/// 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 &
+getInverseForScale(DurationScale Scale);
+
 /// Assuming `Node` has type `double` or `int` representing a time interval of
 /// `Scale`, return the expression to make it a suitable `Duration`.
 std::string rewriteExprFromNumberToDuration(


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


r350523 - [clang] Add AST matcher for initializer list members

2019-01-07 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Mon Jan  7 06:14:36 2019
New Revision: 350523

URL: http://llvm.org/viewvc/llvm-project?rev=350523&view=rev
Log:
[clang] Add AST matcher for initializer list members

Summary:
Much like hasArg for various call expressions, this allows LibTooling users to
match against a member of an initializer list.

This is currently being used as part of the abseil-duration-scale clang-tidy
check.

Differential Revision: https://reviews.llvm.org/D56090

Modified:
cfe/trunk/docs/LibASTMatchersReference.html
cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h
cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp
cfe/trunk/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Modified: cfe/trunk/docs/LibASTMatchersReference.html
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/LibASTMatchersReference.html?rev=350523&r1=350522&r2=350523&view=diff
==
--- cfe/trunk/docs/LibASTMatchersReference.html (original)
+++ cfe/trunk/docs/LibASTMatchersReference.html Mon Jan  7 06:14:36 2019
@@ -718,7 +718,7 @@ Example matches a || b
 
 
 MatcherStmt>blockExprMatcherBlockExpr>...
-MAtches a reference to a 
block.
+Matches a reference to a 
block.
 
 Example: matches "^{}":
   void f() { ^{}(); }
@@ -5866,6 +5866,15 @@ FIXME: Unit test this matcher
 
 
 
+MatcherInitListExpr>hasInitunsigned N, 
ast_matchers::MatcherExpr> 
InnerMatcher
+Matches the n'th item of an 
initializer list expression.
+
+Example matches y.
+(matcher = initListExpr(hasInit(0, expr(
+  int x{y}.
+
+
+
 MatcherInitListExpr>hasSyntacticFormMatcherExpr> 
InnerMatcher
 Matches the 
syntactic form of init list expressions
 (if expression have it).

Modified: cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h?rev=350523&r1=350522&r2=350523&view=diff
==
--- cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h (original)
+++ cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h Mon Jan  7 06:14:36 2019
@@ -3514,6 +3514,19 @@ AST_POLYMORPHIC_MATCHER_P2(hasArgument,
   *Node.getArg(N)->IgnoreParenImpCasts(), Finder, Builder));
 }
 
+/// Matches the n'th item of an initializer list expression.
+///
+/// Example matches y.
+/// (matcher = initListExpr(hasInit(0, expr(
+/// \code
+///   int x{y}.
+/// \endcode
+AST_MATCHER_P2(InitListExpr, hasInit, unsigned, N,
+   ast_matchers::internal::Matcher, InnerMatcher) {
+  return N < Node.getNumInits() &&
+  InnerMatcher.matches(*Node.getInit(N), Finder, Builder);
+}
+
 /// Matches declaration statements that contain a specific number of
 /// declarations.
 ///

Modified: cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp?rev=350523&r1=350522&r2=350523&view=diff
==
--- cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp (original)
+++ cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp Mon Jan  7 06:14:36 2019
@@ -273,6 +273,7 @@ RegistryMaps::RegistryMaps() {
   REGISTER_MATCHER(hasInClassInitializer);
   REGISTER_MATCHER(hasIncrement);
   REGISTER_MATCHER(hasIndex);
+  REGISTER_MATCHER(hasInit);
   REGISTER_MATCHER(hasInitializer);
   REGISTER_MATCHER(hasKeywordSelector);
   REGISTER_MATCHER(hasLHS);

Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp?rev=350523&r1=350522&r2=350523&view=diff
==
--- cfe/trunk/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp (original)
+++ cfe/trunk/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp Mon Jan  7 
06:14:36 2019
@@ -2255,6 +2255,18 @@ TEST(IsAssignmentOperator, Basic) {
   notMatches("void x() { int a; if(a == 0) return; }", BinAsgmtOperator));
 }
 
+TEST(HasInit, Basic) {
+  EXPECT_TRUE(
+matches("int x{0};",
+initListExpr(hasInit(0, expr();
+  EXPECT_FALSE(
+matches("int x{0};",
+initListExpr(hasInit(1, expr();
+  EXPECT_FALSE(
+matches("int x;",
+initListExpr(hasInit(0, expr();
+}
+
 TEST(Matcher, isMain) {
   EXPECT_TRUE(
 matches("int main() {}", functionDecl(isMain(;


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi

[clang-tools-extra] r350526 - [clang-tidy] Use the public hasInit matcher, rather than defining our own, NFC

2019-01-07 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Mon Jan  7 06:36:47 2019
New Revision: 350526

URL: http://llvm.org/viewvc/llvm-project?rev=350526&view=rev
Log:
[clang-tidy] Use the public hasInit matcher, rather than defining our own, NFC

Modified:
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp?rev=350526&r1=350525&r2=350526&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp Mon Jan  7 
06:36:47 2019
@@ -104,20 +104,6 @@ llvm::StringRef getFactoryForScale(Durat
   llvm_unreachable("unknown scaling factor");
 }
 
-/// Matches the n'th item of an initializer list expression.
-///
-/// Example matches y.
-/// (matcher = initListExpr(hasInit(0, expr(
-/// \code
-///   int x{y}.
-/// \endcode
-AST_MATCHER_P2(InitListExpr, hasInit, unsigned, N,
-   ast_matchers::internal::Matcher, InnerMatcher) {
-  return N < Node.getNumInits() &&
-  InnerMatcher.matches(*Node.getInit(N)->IgnoreParenImpCasts(), Finder,
-   Builder);
-}
-
 /// Returns `true` if `Node` is a value which evaluates to a literal `0`.
 bool IsLiteralZero(const MatchFinder::MatchResult &Result, const Expr &Node) {
   auto ZeroMatcher =
@@ -132,13 +118,13 @@ bool IsLiteralZero(const MatchFinder::Ma
   // Now check to see if we're using a functional cast with a scalar
   // initializer expression, e.g. `int{0}`.
   if (selectFirst(
-  "val",
-  match(cxxFunctionalCastExpr(
-hasDestinationType(
-anyOf(isInteger(), realFloatingPointType())),
-hasSourceExpression(initListExpr(hasInit(0, ZeroMatcher
-.bind("val"),
-Node, *Result.Context)) != nullptr)
+  "val", match(cxxFunctionalCastExpr(
+   hasDestinationType(
+   anyOf(isInteger(), realFloatingPointType())),
+   hasSourceExpression(initListExpr(
+   hasInit(0, 
ignoringParenImpCasts(ZeroMatcher)
+   .bind("val"),
+   Node, *Result.Context)) != nullptr)
 return true;
 
   return false;


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] r355702 - [clang-tidy] NFC: Negate the name and semantics of the isNotInMacro function.

2019-03-08 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Fri Mar  8 07:37:15 2019
New Revision: 355702

URL: http://llvm.org/viewvc/llvm-project?rev=355702&view=rev
Log:
[clang-tidy] NFC: Negate the name and semantics of the isNotInMacro function.

This function is always used in a context where its result was also
negated, which made for confusing naming and code.

Modified:
clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h

clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp?rev=355702&r1=355701&r2=355702&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp Fri 
Mar  8 07:37:15 2019
@@ -43,8 +43,7 @@ void DurationComparisonCheck::check(cons
   // want to handle the case of rewriting both sides. This is much simpler if
   // we unconditionally try and rewrite both, and let the rewriter determine
   // if nothing needs to be done.
-  if (!isNotInMacro(Result, Binop->getLHS()) ||
-  !isNotInMacro(Result, Binop->getRHS()))
+  if (isInMacro(Result, Binop->getLHS()) || isInMacro(Result, Binop->getRHS()))
 return;
   std::string LhsReplacement =
   rewriteExprFromNumberToDuration(Result, *Scale, Binop->getLHS());

Modified: 
clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp?rev=355702&r1=355701&r2=355702&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp 
Fri Mar  8 07:37:15 2019
@@ -37,7 +37,7 @@ void DurationConversionCastCheck::check(
   const auto *MatchedCast =
   Result.Nodes.getNodeAs("cast_expr");
 
-  if (!isNotInMacro(Result, MatchedCast))
+  if (isInMacro(Result, MatchedCast))
 return;
 
   const auto *FuncDecl = Result.Nodes.getNodeAs("func_decl");

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp?rev=355702&r1=355701&r2=355702&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp Fri Mar  8 
07:37:15 2019
@@ -302,9 +302,9 @@ std::string rewriteExprFromNumberToTime(
   .str();
 }
 
-bool isNotInMacro(const MatchFinder::MatchResult &Result, const Expr *E) {
+bool isInMacro(const MatchFinder::MatchResult &Result, const Expr *E) {
   if (!E->getBeginLoc().isMacroID())
-return true;
+return false;
 
   SourceLocation Loc = E->getBeginLoc();
   // We want to get closer towards the initial macro typed into the source only
@@ -316,7 +316,7 @@ bool isNotInMacro(const MatchFinder::Mat
 // because Clang comment says it "should not generally be used by clients."
 Loc = Result.SourceManager->getImmediateMacroCallerLoc(Loc);
   }
-  return !Loc.isMacroID();
+  return Loc.isMacroID();
 }
 
 } // namespace abseil

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h?rev=355702&r1=355701&r2=355702&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h Fri Mar  8 
07:37:15 2019
@@ -91,10 +91,10 @@ std::string rewriteExprFromNumberToTime(
 const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
 const Expr *Node);
 
-/// Return `true` if `E` is a either: not a macro at all; or an argument to
+/// Return `false` if `E` is a either: not a macro at all; or an argument to
 /// one.  In the both cases, we often want to do the transformation.
-bool isNotInMacro(const ast_matchers::MatchFinder::MatchResult &Result,
-  const Expr *E);
+bool isInMacro(const ast_matchers::MatchFinder::MatchResult &Result,
+   const Expr *E);
 
 AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher,
  DurationConversionFunction) {

Modified: 
clang-tools-extra/trunk/clang-tidy/abseil/Duratio

[clang-tools-extra] r355835 - [clang-tidy] Add the abseil-time-compare check

2019-03-11 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Mon Mar 11 09:47:45 2019
New Revision: 355835

URL: http://llvm.org/viewvc/llvm-project?rev=355835&view=rev
Log:
[clang-tidy] Add the abseil-time-compare check

This is an analog of the abseil-duration-comparison check, but for the
absl::Time domain. It has a similar implementation and tests.

Differential Revision: https://reviews.llvm.org/D58977

Added:
clang-tools-extra/trunk/clang-tidy/abseil/TimeComparisonCheck.cpp
  - copied, changed from r355820, 
clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/TimeComparisonCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-time-comparison.rst
clang-tools-extra/trunk/test/clang-tidy/abseil-time-comparison.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst

Modified: clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp?rev=355835&r1=355834&r2=355835&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp Mon Mar 11 
09:47:45 2019
@@ -23,6 +23,7 @@
 #include "RedundantStrcatCallsCheck.h"
 #include "StringFindStartswithCheck.h"
 #include "StrCatAppendCheck.h"
+#include "TimeComparisonCheck.h"
 #include "TimeSubtractionCheck.h"
 #include "UpgradeDurationConversionsCheck.h"
 
@@ -60,6 +61,8 @@ public:
 "abseil-str-cat-append");
 CheckFactories.registerCheck(
 "abseil-string-find-startswith");
+CheckFactories.registerCheck(
+"abseil-time-comparison");
 CheckFactories.registerCheck(
 "abseil-time-subtraction");
 CheckFactories.registerCheck(

Modified: clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt?rev=355835&r1=355834&r2=355835&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt Mon Mar 11 
09:47:45 2019
@@ -17,6 +17,7 @@ add_clang_library(clangTidyAbseilModule
   RedundantStrcatCallsCheck.cpp
   StrCatAppendCheck.cpp
   StringFindStartswithCheck.cpp
+  TimeComparisonCheck.cpp
   TimeSubtractionCheck.cpp
   UpgradeDurationConversionsCheck.cpp
 

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp?rev=355835&r1=355834&r2=355835&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp Mon 
Mar 11 09:47:45 2019
@@ -19,14 +19,10 @@ namespace tidy {
 namespace abseil {
 
 void DurationComparisonCheck::registerMatchers(MatchFinder *Finder) {
-  auto Matcher =
-  binaryOperator(anyOf(hasOperatorName(">"), hasOperatorName(">="),
-   hasOperatorName("=="), hasOperatorName("<="),
-   hasOperatorName("<")),
- hasEitherOperand(ignoringImpCasts(callExpr(
- callee(functionDecl(DurationConversionFunction())
-.bind("function_decl"))
-  .bind("binop");
+  auto Matcher = expr(comparisonOperatorWithCallee(functionDecl(
+  functionDecl(DurationConversionFunction())
+  .bind("function_decl"
+ .bind("binop");
 
   Finder->addMatcher(Matcher, this);
 }

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h?rev=355835&r1=355834&r2=355835&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h Mon Mar 11 
09:47:45 2019
@@ -124,6 +124,16 @@ AST_MATCHER_FUNCTION(ast_matchers::inter
   "::absl::ToUnixMillis", "::absl::ToUnixMicros", "::absl::ToUnixNanos"));
 }
 
+AST_MATCHER_FUNCTION_P(ast_matchers::internal::Matcher,
+   comparisonOperatorWithCallee,
+   ast_matchers::

[clang-tools-extra] r356141 - [clang-tidy] Add additional patterns to the abseil-duration-unnecessary-conversion check.

2019-03-14 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Thu Mar 14 06:38:16 2019
New Revision: 356141

URL: http://llvm.org/viewvc/llvm-project?rev=356141&view=rev
Log:
[clang-tidy] Add additional patterns to the 
abseil-duration-unnecessary-conversion check.

Differential Revision: https://reviews.llvm.org/D59183

Modified:

clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp

clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst
clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/time/time.h

clang-tools-extra/trunk/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp

Modified: 
clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp?rev=356141&r1=356140&r2=356141&view=diff
==
--- 
clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
 (original)
+++ 
clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
 Thu Mar 14 06:38:16 2019
@@ -28,12 +28,35 @@ void DurationUnnecessaryConversionCheck:
 std::string IntegerConversion =
 (llvm::Twine("::absl::ToInt64") + Scale).str();
 
+// Matcher which matches the current scale's factory with a `1` argument,
+// e.g. `absl::Seconds(1)`.
+auto factory_matcher = cxxConstructExpr(hasArgument(
+0,
+callExpr(callee(functionDecl(hasName(DurationFactory))),
+ hasArgument(0, 
ignoringImpCasts(integerLiteral(equals(1)));
+
+// Matcher which matches either inverse function and binds its argument,
+// e.g. `absl::ToDoubleSeconds(dur)`.
+auto inverse_function_matcher = callExpr(
+callee(functionDecl(hasAnyName(FloatConversion, IntegerConversion))),
+hasArgument(0, expr().bind("arg")));
+
+// Matcher which matches a duration divided by the factory_matcher above,
+// e.g. `dur / absl::Seconds(1)`.
+auto division_operator_matcher = cxxOperatorCallExpr(
+hasOverloadedOperatorName("/"), hasArgument(0, expr().bind("arg")),
+hasArgument(1, factory_matcher));
+
+// Matcher which matches a duration argument to `FDivDuration`,
+// e.g. `absl::FDivDuration(dur, absl::Seconds(1))`
+auto fdiv_matcher = callExpr(
+callee(functionDecl(hasName("::absl::FDivDuration"))),
+hasArgument(0, expr().bind("arg")), hasArgument(1, factory_matcher));
+
 Finder->addMatcher(
-callExpr(
-callee(functionDecl(hasName(DurationFactory))),
-hasArgument(0, callExpr(callee(functionDecl(hasAnyName(
-FloatConversion, IntegerConversion))),
-hasArgument(0, expr().bind("arg")
+callExpr(callee(functionDecl(hasName(DurationFactory))),
+ hasArgument(0, anyOf(inverse_function_matcher,
+  division_operator_matcher, 
fdiv_matcher)))
 .bind("call"),
 this);
   }
@@ -47,7 +70,8 @@ void DurationUnnecessaryConversionCheck:
   if (isInMacro(Result, OuterCall))
 return;
 
-  diag(OuterCall->getBeginLoc(), "remove unnecessary absl::Duration 
conversions")
+  diag(OuterCall->getBeginLoc(),
+   "remove unnecessary absl::Duration conversions")
   << FixItHint::CreateReplacement(
  OuterCall->getSourceRange(),
  tooling::fixit::getText(*Arg, *Result.Context));

Modified: 
clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst?rev=356141&r1=356140&r2=356141&view=diff
==
--- 
clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst
 (original)
+++ 
clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst
 Thu Mar 14 06:38:16 2019
@@ -6,7 +6,7 @@ abseil-duration-unnecessary-conversion
 Finds and fixes cases where ``absl::Duration`` values are being converted to
 numeric types and back again.
 
-Examples:
+Floating-point examples:
 
 .. code-block:: c++
 
@@ -17,6 +17,15 @@ Examples:
   // Suggestion - Remove unnecessary conversions
   absl::Duration d2 = d1;
 
+  // Original - Division to convert to double and back again
+  absl::Duration d2 = absl::Seconds(absl::FDivDuration(d1, absl::Seconds(1)));
+
+  // Suggestion - Remove division and conversion
+  absl::Duration d2 = d1;
+
+Integer examples:
+
+.. code-block:: c++
 
   // Original - Conversion to integer and back again
   absl::Duration d1;
@@ -25,6 +34,12 @@ Examples:
   // Suggestion - Remove unnecessary conversions
   absl::Duration d2 = d1;
 
+  // Original - Integer division follow

[clang-tools-extra] r351348 - [clang-tidy] Move the macro helper function to a common location; NFC

2019-01-16 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Wed Jan 16 06:49:32 2019
New Revision: 351348

URL: http://llvm.org/viewvc/llvm-project?rev=351348&view=rev
Log:
[clang-tidy] Move the macro helper function to a common location; NFC

This is useful for multiple checks.

Modified:
clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp?rev=351348&r1=351347&r2=351348&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp Wed 
Jan 16 06:49:32 2019
@@ -19,26 +19,6 @@ namespace clang {
 namespace tidy {
 namespace abseil {
 
-/// Return `true` if `E` is a either: not a macro at all; or an argument to
-/// one. In the latter case, we should still transform it.
-static bool IsValidMacro(const MatchFinder::MatchResult &Result,
- const Expr *E) {
-  if (!E->getBeginLoc().isMacroID())
-return true;
-
-  SourceLocation Loc = E->getBeginLoc();
-  // We want to get closer towards the initial macro typed into the source only
-  // if the location is being expanded as a macro argument.
-  while (Result.SourceManager->isMacroArgExpansion(Loc)) {
-// We are calling getImmediateMacroCallerLoc, but note it is essentially
-// equivalent to calling getImmediateSpellingLoc in this context according
-// to Clang implementation. We are not calling getImmediateSpellingLoc
-// because Clang comment says it "should not generally be used by clients."
-Loc = Result.SourceManager->getImmediateMacroCallerLoc(Loc);
-  }
-  return !Loc.isMacroID();
-}
-
 void DurationComparisonCheck::registerMatchers(MatchFinder *Finder) {
   auto Matcher =
   binaryOperator(anyOf(hasOperatorName(">"), hasOperatorName(">="),
@@ -64,8 +44,8 @@ void DurationComparisonCheck::check(cons
   // want to handle the case of rewriting both sides. This is much simpler if
   // we unconditionally try and rewrite both, and let the rewriter determine
   // if nothing needs to be done.
-  if (!IsValidMacro(Result, Binop->getLHS()) ||
-  !IsValidMacro(Result, Binop->getRHS()))
+  if (!isNotInMacro(Result, Binop->getLHS()) ||
+  !isNotInMacro(Result, Binop->getRHS()))
 return;
   std::string LhsReplacement =
   rewriteExprFromNumberToDuration(Result, *Scale, Binop->getLHS());

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp?rev=351348&r1=351347&r2=351348&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp Wed Jan 16 
06:49:32 2019
@@ -216,6 +216,23 @@ std::string rewriteExprFromNumberToDurat
   .str();
 }
 
+bool isNotInMacro(const MatchFinder::MatchResult &Result, const Expr *E) {
+  if (!E->getBeginLoc().isMacroID())
+return true;
+
+  SourceLocation Loc = E->getBeginLoc();
+  // We want to get closer towards the initial macro typed into the source only
+  // if the location is being expanded as a macro argument.
+  while (Result.SourceManager->isMacroArgExpansion(Loc)) {
+// We are calling getImmediateMacroCallerLoc, but note it is essentially
+// equivalent to calling getImmediateSpellingLoc in this context according
+// to Clang implementation. We are not calling getImmediateSpellingLoc
+// because Clang comment says it "should not generally be used by clients."
+Loc = Result.SourceManager->getImmediateMacroCallerLoc(Loc);
+  }
+  return !Loc.isMacroID();
+}
+
 } // namespace abseil
 } // namespace tidy
 } // namespace clang

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h?rev=351348&r1=351347&r2=351348&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h Wed Jan 16 
06:49:32 2019
@@ -75,6 +75,11 @@ std::string rewriteExprFromNumberToDurat
 const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
 const Expr *Node);
 
+/// Return `true` if `E` is a either: not a macro at all; or an argument to
+/// one.  In the both cases, we often want to do the transformation.
+bool isNotInMacro(const ast_matchers::MatchFinder::MatchResult &Result,
+  

[clang-tools-extra] r351473 - [clang-tidy] Add abseil-duration-conversion-cast check

2019-01-17 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Thu Jan 17 12:37:35 2019
New Revision: 351473

URL: http://llvm.org/viewvc/llvm-project?rev=351473&view=rev
Log:
[clang-tidy] Add abseil-duration-conversion-cast check

Differential Revision: https://reviews.llvm.org/D56532

Added:
clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.h

clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-duration-conversion-cast.rst
clang-tools-extra/trunk/test/clang-tidy/abseil-duration-conversion-cast.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst

Modified: clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp?rev=351473&r1=351472&r2=351473&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp Thu Jan 17 
12:37:35 2019
@@ -11,6 +11,7 @@
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
 #include "DurationComparisonCheck.h"
+#include "DurationConversionCastCheck.h"
 #include "DurationDivisionCheck.h"
 #include "DurationFactoryFloatCheck.h"
 #include "DurationFactoryScaleCheck.h"
@@ -32,6 +33,8 @@ public:
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
 CheckFactories.registerCheck(
 "abseil-duration-comparison");
+CheckFactories.registerCheck(
+"abseil-duration-conversion-cast");
 CheckFactories.registerCheck(
 "abseil-duration-division");
 CheckFactories.registerCheck(

Modified: clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt?rev=351473&r1=351472&r2=351473&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt Thu Jan 17 
12:37:35 2019
@@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support)
 add_clang_library(clangTidyAbseilModule
   AbseilTidyModule.cpp
   DurationComparisonCheck.cpp
+  DurationConversionCastCheck.cpp
   DurationDivisionCheck.cpp
   DurationFactoryFloatCheck.cpp
   DurationFactoryScaleCheck.cpp

Added: clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp?rev=351473&view=auto
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp 
(added)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp 
Thu Jan 17 12:37:35 2019
@@ -0,0 +1,85 @@
+//===--- DurationConversionCastCheck.cpp - clang-tidy 
-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#include "DurationConversionCastCheck.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 DurationConversionCastCheck::registerMatchers(MatchFinder *Finder) {
+  auto CallMatcher = ignoringImpCasts(callExpr(
+  callee(functionDecl(DurationConversionFunction()).bind("func_decl")),
+  hasArgument(0, expr().bind("arg";
+
+  Finder->addMatcher(
+  expr(anyOf(
+  
cxxStaticCastExpr(hasSourceExpression(CallMatcher)).bind("cast_expr"),
+  cStyleCastExpr(hasSourceExpression(CallMatcher)).bind("cast_expr"),
+  cxxFunctionalCastExpr(hasSourceExpression(CallMatcher))
+  .bind("cast_expr"))),
+  this);
+}
+
+void DurationConversionCastCheck::check(
+const MatchFinder::MatchResult &Result) {
+  const auto *MatchedCast =
+  Result.Nodes.getNodeAs("cast_expr");
+
+  if (!isNotInMacro(Result, MatchedCast))
+return;
+
+  const auto *FuncDecl = Result.Nodes.getNodeAs("func_decl");
+  const auto *Arg = Result.Nodes.getNodeAs("arg");
+  StringRef ConversionFuncName = FuncDecl->getName();
+
+  llvm::Optional Scale = getScaleForInverse(ConversionFuncName);
+  if (!Scale)
+return;
+
+  // Casting a double to an integer.
+  if (MatchedCast->getTypeAsWritten()->isIntegerType() &&
+  ConversionF

[clang-tools-extra] r352088 - [clang-tidy] Rename the absl duration helper functions; NFC

2019-01-24 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Thu Jan 24 11:23:50 2019
New Revision: 352088

URL: http://llvm.org/viewvc/llvm-project?rev=352088&view=rev
Log:
[clang-tidy] Rename the absl duration helper functions; NFC

Modified:
clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationFactoryScaleCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
clang-tools-extra/trunk/clang-tidy/abseil/DurationSubtractionCheck.cpp

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp?rev=352088&r1=352087&r2=352088&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationComparisonCheck.cpp Thu 
Jan 24 11:23:50 2019
@@ -34,7 +34,7 @@ void DurationComparisonCheck::registerMa
 void DurationComparisonCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *Binop = Result.Nodes.getNodeAs("binop");
 
-  llvm::Optional Scale = getScaleForInverse(
+  llvm::Optional Scale = getScaleForDurationInverse(
   Result.Nodes.getNodeAs("function_decl")->getName());
   if (!Scale)
 return;

Modified: 
clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp?rev=352088&r1=352087&r2=352088&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationConversionCastCheck.cpp 
Thu Jan 24 11:23:50 2019
@@ -44,14 +44,15 @@ void DurationConversionCastCheck::check(
   const auto *Arg = Result.Nodes.getNodeAs("arg");
   StringRef ConversionFuncName = FuncDecl->getName();
 
-  llvm::Optional Scale = getScaleForInverse(ConversionFuncName);
+  llvm::Optional Scale =
+  getScaleForDurationInverse(ConversionFuncName);
   if (!Scale)
 return;
 
   // Casting a double to an integer.
   if (MatchedCast->getTypeAsWritten()->isIntegerType() &&
   ConversionFuncName.contains("Double")) {
-llvm::StringRef NewFuncName = getInverseForScale(*Scale).second;
+llvm::StringRef NewFuncName = getDurationInverseForScale(*Scale).second;
 
 diag(MatchedCast->getBeginLoc(),
  "duration should be converted directly to an integer rather than "
@@ -66,7 +67,7 @@ void DurationConversionCastCheck::check(
   // Casting an integer to a double.
   if (MatchedCast->getTypeAsWritten()->isRealFloatingType() &&
   ConversionFuncName.contains("Int64")) {
-llvm::StringRef NewFuncName = getInverseForScale(*Scale).first;
+llvm::StringRef NewFuncName = getDurationInverseForScale(*Scale).first;
 
 diag(MatchedCast->getBeginLoc(), "duration should be converted directly to 
"
  "a floating-piont number rather than "

Modified: 
clang-tools-extra/trunk/clang-tidy/abseil/DurationFactoryScaleCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationFactoryScaleCheck.cpp?rev=352088&r1=352087&r2=352088&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationFactoryScaleCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationFactoryScaleCheck.cpp Thu 
Jan 24 11:23:50 2019
@@ -209,7 +209,7 @@ void DurationFactoryScaleCheck::check(co
   diag(Call->getBeginLoc(), "internal duration scaling can be removed")
   << FixItHint::CreateReplacement(
  Call->getSourceRange(),
- (llvm::Twine(getFactoryForScale(*NewScale)) + "(" +
+ (llvm::Twine(getDurationFactoryForScale(*NewScale)) + "(" +
   tooling::fixit::getText(*Remainder, *Result.Context) + ")")
  .str());
 }
@@ -222,7 +222,7 @@ void DurationFactoryScaleCheck::check(co
 diag(Call->getBeginLoc(), "internal duration scaling can be removed")
 << FixItHint::CreateReplacement(
Call->getSourceRange(),
-   (llvm::Twine(getFactoryForScale(*NewScale)) + "(" +
+   (llvm::Twine(getDurationFactoryForScale(*NewScale)) + "(" +
 tooling::fixit::getText(*Remainder, *Result.Context) + ")")
.str());
   }

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp?rev=352088&r1=352087&r2=352088&view=d

[clang-tools-extra] r352362 - [clang-tidy] Add the abseil-duration-addition check

2019-01-28 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Mon Jan 28 06:03:09 2019
New Revision: 352362

URL: http://llvm.org/viewvc/llvm-project?rev=352362&view=rev
Log:
[clang-tidy] Add the abseil-duration-addition check

Differential Revision: https://reviews.llvm.org/D57185

Added:
clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-duration-addition.rst
clang-tools-extra/trunk/test/clang-tidy/abseil-duration-addition.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/time/time.h

Modified: clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp?rev=352362&r1=352361&r2=352362&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp Mon Jan 28 
06:03:09 2019
@@ -9,6 +9,7 @@
 #include "../ClangTidy.h"
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
+#include "DurationAdditionCheck.h"
 #include "DurationComparisonCheck.h"
 #include "DurationConversionCastCheck.h"
 #include "DurationDivisionCheck.h"
@@ -30,6 +31,8 @@ namespace abseil {
 class AbseilModule : public ClangTidyModule {
 public:
   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+CheckFactories.registerCheck(
+"abseil-duration-addition");
 CheckFactories.registerCheck(
 "abseil-duration-comparison");
 CheckFactories.registerCheck(

Modified: clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt?rev=352362&r1=352361&r2=352362&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt Mon Jan 28 
06:03:09 2019
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support)
 
 add_clang_library(clangTidyAbseilModule
   AbseilTidyModule.cpp
+  DurationAdditionCheck.cpp
   DurationComparisonCheck.cpp
   DurationConversionCastCheck.cpp
   DurationDivisionCheck.cpp

Added: clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.cpp?rev=352362&view=auto
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.cpp Mon Jan 
28 06:03:09 2019
@@ -0,0 +1,73 @@
+//===--- DurationAdditionCheck.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 "DurationAdditionCheck.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 DurationAdditionCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  binaryOperator(hasOperatorName("+"),
+ hasEitherOperand(expr(ignoringParenImpCasts(
+ callExpr(callee(functionDecl(TimeFactoryFunction())
+ .bind("function_decl")))
+ .bind("call")
+  .bind("binop"),
+  this);
+}
+
+void DurationAdditionCheck::check(const MatchFinder::MatchResult &Result) {
+  const BinaryOperator *Binop =
+  Result.Nodes.getNodeAs("binop");
+  const CallExpr *Call = Result.Nodes.getNodeAs("call");
+
+  // Don't try to replace things inside of macro definitions.
+  if (Binop->getExprLoc().isMacroID() || Binop->getExprLoc().isInvalid())
+return;
+
+  llvm::Optional Scale = getScaleForTimeInverse(
+  Result.Nodes.getNodeAs("function_decl")->getName());
+  if (!Scale)
+return;
+
+  llvm::StringRef TimeFactory = getTimeFactoryForScale(*Scale);
+
+  FixItHint Hint;
+  if (Call == Binop->getLHS()->IgnoreParenIm

[clang-tools-extra] r352964 - [clang-tidy] Rename time lookup functions; NFC

2019-02-02 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Sat Feb  2 11:57:37 2019
New Revision: 352964

URL: http://llvm.org/viewvc/llvm-project?rev=352964&view=rev
Log:
[clang-tidy] Rename time lookup functions; NFC

Modified:
clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.cpp?rev=352964&r1=352963&r2=352964&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.cpp 
(original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationAdditionCheck.cpp Sat Feb 
 2 11:57:37 2019
@@ -22,7 +22,7 @@ void DurationAdditionCheck::registerMatc
   Finder->addMatcher(
   binaryOperator(hasOperatorName("+"),
  hasEitherOperand(expr(ignoringParenImpCasts(
- callExpr(callee(functionDecl(TimeFactoryFunction())
+ callExpr(callee(functionDecl(TimeConversionFunction())
  .bind("function_decl")))
  .bind("call")
   .bind("binop"),
@@ -43,7 +43,7 @@ void DurationAdditionCheck::check(const
   if (!Scale)
 return;
 
-  llvm::StringRef TimeFactory = getTimeFactoryForScale(*Scale);
+  llvm::StringRef TimeFactory = getTimeInverseForScale(*Scale);
 
   FixItHint Hint;
   if (Call == Binop->getLHS()->IgnoreParenImpCasts()) {

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp?rev=352964&r1=352963&r2=352964&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp Sat Feb  2 
11:57:37 2019
@@ -104,7 +104,7 @@ llvm::StringRef getDurationFactoryForSca
 }
 
 /// Returns the Time factory function name for a given `Scale`.
-llvm::StringRef getTimeFactoryForScale(DurationScale scale) {
+llvm::StringRef getTimeInverseForScale(DurationScale scale) {
   switch (scale) {
   case DurationScale::Hours:
 return "absl::ToUnixHours";

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h?rev=352964&r1=352963&r2=352964&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h Sat Feb  2 
11:57:37 2019
@@ -31,9 +31,6 @@ enum class DurationScale : std::uint8_t
 /// constructing a `Duration` for that scale.
 llvm::StringRef getDurationFactoryForScale(DurationScale Scale);
 
-/// Returns the Time factory function name for a given `Scale`.
-llvm::StringRef getTimeFactoryForScale(DurationScale scale);
-
 // Determine if `Node` represents a literal floating point or integral zero.
 bool IsLiteralZero(const ast_matchers::MatchFinder::MatchResult &Result,
const Expr &Node);
@@ -75,6 +72,9 @@ llvm::Optional getScaleFo
 const std::pair &
 getDurationInverseForScale(DurationScale Scale);
 
+/// Returns the Time inverse function name for a given `Scale`.
+llvm::StringRef getTimeInverseForScale(DurationScale scale);
+
 /// Assuming `Node` has type `double` or `int` representing a time interval of
 /// `Scale`, return the expression to make it a suitable `Duration`.
 std::string rewriteExprFromNumberToDuration(
@@ -107,7 +107,7 @@ AST_MATCHER_FUNCTION(ast_matchers::inter
 }
 
 AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher,
- TimeFactoryFunction) {
+ TimeConversionFunction) {
   using namespace clang::ast_matchers;
   return functionDecl(hasAnyName(
   "::absl::ToUnixHours", "::absl::ToUnixMinutes", "::absl::ToUnixSeconds",


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] r353079 - [clang-tidy] Add the abseil-duration-unnecessary-conversion check

2019-02-04 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Mon Feb  4 11:28:20 2019
New Revision: 353079

URL: http://llvm.org/viewvc/llvm-project?rev=353079&view=rev
Log:
[clang-tidy] Add the abseil-duration-unnecessary-conversion check

Differential Revision: https://reviews.llvm.org/D57353

Added:

clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp

clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.h

clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-duration-unnecessary-conversion.rst

clang-tools-extra/trunk/test/clang-tidy/abseil-duration-unnecessary-conversion.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/time/time.h

Modified: clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp?rev=353079&r1=353078&r2=353079&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp Mon Feb  4 
11:28:20 2019
@@ -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(
 "abseil-duration-subtraction");
+CheckFactories.registerCheck(
+"abseil-duration-unnecessary-conversion");
 CheckFactories.registerCheck(
 "abseil-faster-strsplit-delimiter");
 CheckFactories.registerCheck(

Modified: clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt?rev=353079&r1=353078&r2=353079&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt Mon Feb  4 
11:28:20 2019
@@ -10,6 +10,7 @@ add_clang_library(clangTidyAbseilModule
   DurationFactoryScaleCheck.cpp
   DurationRewriter.cpp
   DurationSubtractionCheck.cpp
+  DurationUnnecessaryConversionCheck.cpp
   FasterStrsplitDelimiterCheck.cpp
   NoInternalDependenciesCheck.cpp
   NoNamespaceCheck.cpp

Added: 
clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp?rev=353079&view=auto
==
--- 
clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
 (added)
+++ 
clang-tools-extra/trunk/clang-tidy/abseil/DurationUnnecessaryConversionCheck.cpp
 Mon Feb  4 11:28:20 2019
@@ -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

[clang-tools-extra] r355024 - [clang-tidy] Add the abseil-time-subtraction check

2019-02-27 Thread Hyrum Wright via cfe-commits
Author: hwright
Date: Wed Feb 27 12:08:50 2019
New Revision: 355024

URL: http://llvm.org/viewvc/llvm-project?rev=355024&view=rev
Log:
[clang-tidy] Add the abseil-time-subtraction check

Differential Revision: https://reviews.llvm.org/D58137

Added:
clang-tools-extra/trunk/clang-tidy/abseil/TimeSubtractionCheck.cpp
clang-tools-extra/trunk/clang-tidy/abseil/TimeSubtractionCheck.h
clang-tools-extra/trunk/docs/clang-tidy/checks/abseil-time-subtraction.rst
clang-tools-extra/trunk/test/clang-tidy/abseil-time-subtraction.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.h
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
clang-tools-extra/trunk/test/clang-tidy/Inputs/absl/time/time.h

Modified: clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp?rev=355024&r1=355023&r2=355024&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/AbseilTidyModule.cpp Wed Feb 27 
12:08:50 2019
@@ -23,6 +23,7 @@
 #include "RedundantStrcatCallsCheck.h"
 #include "StringFindStartswithCheck.h"
 #include "StrCatAppendCheck.h"
+#include "TimeSubtractionCheck.h"
 #include "UpgradeDurationConversionsCheck.h"
 
 namespace clang {
@@ -59,6 +60,8 @@ public:
 "abseil-str-cat-append");
 CheckFactories.registerCheck(
 "abseil-string-find-startswith");
+CheckFactories.registerCheck(
+"abseil-time-subtraction");
 CheckFactories.registerCheck(
 "abseil-upgrade-duration-conversions");
   }

Modified: clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt?rev=355024&r1=355023&r2=355024&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/CMakeLists.txt Wed Feb 27 
12:08:50 2019
@@ -17,6 +17,7 @@ add_clang_library(clangTidyAbseilModule
   RedundantStrcatCallsCheck.cpp
   StrCatAppendCheck.cpp
   StringFindStartswithCheck.cpp
+  TimeSubtractionCheck.cpp
   UpgradeDurationConversionsCheck.cpp
 
   LINK_LIBS

Modified: clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp?rev=355024&r1=355023&r2=355024&view=diff
==
--- clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/abseil/DurationRewriter.cpp Wed Feb 27 
12:08:50 2019
@@ -84,6 +84,22 @@ rewriteInverseDurationCall(const MatchFi
   return llvm::None;
 }
 
+/// If `Node` is a call to the inverse of `Scale`, return that inverse's
+/// argument, otherwise None.
+static llvm::Optional
+rewriteInverseTimeCall(const MatchFinder::MatchResult &Result,
+   DurationScale Scale, const Expr &Node) {
+  llvm::StringRef InverseFunction = getTimeInverseForScale(Scale);
+  if (const auto *MaybeCallArg = selectFirst(
+  "e", match(callExpr(callee(functionDecl(hasName(InverseFunction))),
+  hasArgument(0, expr().bind("e"))),
+ Node, *Result.Context))) {
+return tooling::fixit::getText(*MaybeCallArg, *Result.Context).str();
+  }
+
+  return llvm::None;
+}
+
 /// Returns the factory function name for a given `Scale`.
 llvm::StringRef getDurationFactoryForScale(DurationScale Scale) {
   switch (Scale) {
@@ -103,6 +119,24 @@ llvm::StringRef getDurationFactoryForSca
   llvm_unreachable("unknown scaling factor");
 }
 
+llvm::StringRef getTimeFactoryForScale(DurationScale Scale) {
+  switch (Scale) {
+  case DurationScale::Hours:
+return "absl::FromUnixHours";
+  case DurationScale::Minutes:
+return "absl::FromUnixMinutes";
+  case DurationScale::Seconds:
+return "absl::FromUnixSeconds";
+  case DurationScale::Milliseconds:
+return "absl::FromUnixMillis";
+  case DurationScale::Microseconds:
+return "absl::FromUnixMicros";
+  case DurationScale::Nanoseconds:
+return "absl::FromUnixNanos";
+  }
+  llvm_unreachable("unknown scaling factor");
+}
+
 /// Returns the Time factory function name for a given `Scale`.
 llvm::StringRef getTimeInverseForScale(DurationScale scale) {
   switch (scale) {
@@ -250,6 +284,24 @@ std::string rewriteExprFromNumberToDurat
   .str();
 }
 
+std::string rewriteExprFromNumberToTime(
+c