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/ASTMatchers/ASTMatchFinder.h"
13#include <optional>
14
15using namespace clang::ast_matchers;
16
17namespace clang::tidy::abseil {
18
19void TimeComparisonCheck::registerMatchers(MatchFinder *Finder) {
20 auto Matcher =
21 expr(comparisonOperatorWithCallee(funcDecl: functionDecl(
22 functionDecl(TimeConversionFunction()).bind(ID: "function_decl"))))
23 .bind(ID: "binop");
24
25 Finder->addMatcher(NodeMatch: Matcher, Action: this);
26}
27
28void TimeComparisonCheck::check(const MatchFinder::MatchResult &Result) {
29 const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>(ID: "binop");
30
31 std::optional<DurationScale> Scale = getScaleForTimeInverse(
32 Result.Nodes.getNodeAs<FunctionDecl>(ID: "function_decl")->getName());
33 if (!Scale)
34 return;
35
36 if (isInMacro(Result, E: Binop->getLHS()) || isInMacro(Result, E: Binop->getRHS()))
37 return;
38
39 // In most cases, we'll only need to rewrite one of the sides, but we also
40 // want to handle the case of rewriting both sides. This is much simpler if
41 // we unconditionally try and rewrite both, and let the rewriter determine
42 // if nothing needs to be done.
43 std::string LhsReplacement =
44 rewriteExprFromNumberToTime(Result, Scale: *Scale, Node: Binop->getLHS());
45 std::string RhsReplacement =
46 rewriteExprFromNumberToTime(Result, Scale: *Scale, Node: Binop->getRHS());
47
48 diag(Loc: Binop->getBeginLoc(), Description: "perform comparison in the time domain")
49 << FixItHint::CreateReplacement(Binop->getSourceRange(),
50 (llvm::Twine(LhsReplacement) + " " +
51 Binop->getOpcodeStr() + " " +
52 RhsReplacement)
53 .str());
54}
55
56} // namespace clang::tidy::abseil
57

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang-tools-extra/clang-tidy/abseil/TimeComparisonCheck.cpp