TimeComparisonCheck.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //===--- TimeComparisonCheck.cpp - clang-tidy
  2. //--------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "TimeComparisonCheck.h"
  10. #include "DurationRewriter.h"
  11. #include "clang/AST/ASTContext.h"
  12. #include "clang/ASTMatchers/ASTMatchFinder.h"
  13. #include "clang/Tooling/FixIt.h"
  14. #include <optional>
  15. using namespace clang::ast_matchers;
  16. namespace clang::tidy::abseil {
  17. void TimeComparisonCheck::registerMatchers(MatchFinder *Finder) {
  18. auto Matcher =
  19. expr(comparisonOperatorWithCallee(functionDecl(
  20. functionDecl(TimeConversionFunction()).bind("function_decl"))))
  21. .bind("binop");
  22. Finder->addMatcher(Matcher, this);
  23. }
  24. void TimeComparisonCheck::check(const MatchFinder::MatchResult &Result) {
  25. const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>("binop");
  26. std::optional<DurationScale> Scale = getScaleForTimeInverse(
  27. Result.Nodes.getNodeAs<FunctionDecl>("function_decl")->getName());
  28. if (!Scale)
  29. return;
  30. if (isInMacro(Result, Binop->getLHS()) || isInMacro(Result, Binop->getRHS()))
  31. return;
  32. // In most cases, we'll only need to rewrite one of the sides, but we also
  33. // want to handle the case of rewriting both sides. This is much simpler if
  34. // we unconditionally try and rewrite both, and let the rewriter determine
  35. // if nothing needs to be done.
  36. std::string LhsReplacement =
  37. rewriteExprFromNumberToTime(Result, *Scale, Binop->getLHS());
  38. std::string RhsReplacement =
  39. rewriteExprFromNumberToTime(Result, *Scale, Binop->getRHS());
  40. diag(Binop->getBeginLoc(), "perform comparison in the time domain")
  41. << FixItHint::CreateReplacement(Binop->getSourceRange(),
  42. (llvm::Twine(LhsReplacement) + " " +
  43. Binop->getOpcodeStr() + " " +
  44. RhsReplacement)
  45. .str());
  46. }
  47. } // namespace clang::tidy::abseil