clang-tools  14.0.0git
TimeComparisonCheck.cpp
Go to the documentation of this file.
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 
10 #include "TimeComparisonCheck.h"
11 #include "DurationRewriter.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/Tooling/FixIt.h"
15 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace abseil {
21 
22 void TimeComparisonCheck::registerMatchers(MatchFinder *Finder) {
23  auto Matcher =
24  expr(comparisonOperatorWithCallee(functionDecl(
25  functionDecl(TimeConversionFunction()).bind("function_decl"))))
26  .bind("binop");
27 
28  Finder->addMatcher(Matcher, this);
29 }
30 
31 void TimeComparisonCheck::check(const MatchFinder::MatchResult &Result) {
32  const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>("binop");
33 
34  llvm::Optional<DurationScale> Scale = getScaleForTimeInverse(
35  Result.Nodes.getNodeAs<FunctionDecl>("function_decl")->getName());
36  if (!Scale)
37  return;
38 
39  if (isInMacro(Result, Binop->getLHS()) || isInMacro(Result, Binop->getRHS()))
40  return;
41 
42  // In most cases, we'll only need to rewrite one of the sides, but we also
43  // want to handle the case of rewriting both sides. This is much simpler if
44  // we unconditionally try and rewrite both, and let the rewriter determine
45  // if nothing needs to be done.
46  std::string LhsReplacement =
47  rewriteExprFromNumberToTime(Result, *Scale, Binop->getLHS());
48  std::string RhsReplacement =
49  rewriteExprFromNumberToTime(Result, *Scale, Binop->getRHS());
50 
51  diag(Binop->getBeginLoc(), "perform comparison in the time domain")
52  << FixItHint::CreateReplacement(Binop->getSourceRange(),
53  (llvm::Twine(LhsReplacement) + " " +
54  Binop->getOpcodeStr() + " " +
55  RhsReplacement)
56  .str());
57 }
58 
59 } // namespace abseil
60 } // namespace tidy
61 } // namespace clang
clang::tidy::abseil::getScaleForTimeInverse
llvm::Optional< DurationScale > getScaleForTimeInverse(llvm::StringRef Name)
Given the name of an inverse Time function (e.g., ToUnixSeconds), return its DurationScale,...
Definition: DurationRewriter.cpp:253
TimeComparisonCheck.h
clang::tidy::abseil::isInMacro
bool isInMacro(const MatchFinder::MatchResult &Result, const Expr *E)
Definition: DurationRewriter.cpp:305
clang::ast_matchers
Definition: AbseilMatcher.h:14
DurationRewriter.h
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:258
clang::tidy::abseil::rewriteExprFromNumberToTime
std::string rewriteExprFromNumberToTime(const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale, const Expr *Node)
Assuming Node has a type int representing a time instant of Scale since The Epoch,...
Definition: DurationRewriter.cpp:287
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27