clang-tools 20.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#include <optional>
16
17using namespace clang::ast_matchers;
18
19namespace clang::tidy::abseil {
20
21void TimeComparisonCheck::registerMatchers(MatchFinder *Finder) {
22 auto Matcher =
23 expr(comparisonOperatorWithCallee(functionDecl(
24 functionDecl(TimeConversionFunction()).bind("function_decl"))))
25 .bind("binop");
26
27 Finder->addMatcher(Matcher, this);
28}
29
30void TimeComparisonCheck::check(const MatchFinder::MatchResult &Result) {
31 const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>("binop");
32
33 std::optional<DurationScale> Scale = getScaleForTimeInverse(
34 Result.Nodes.getNodeAs<FunctionDecl>("function_decl")->getName());
35 if (!Scale)
36 return;
37
38 if (isInMacro(Result, Binop->getLHS()) || isInMacro(Result, Binop->getRHS()))
39 return;
40
41 // In most cases, we'll only need to rewrite one of the sides, but we also
42 // want to handle the case of rewriting both sides. This is much simpler if
43 // we unconditionally try and rewrite both, and let the rewriter determine
44 // if nothing needs to be done.
45 std::string LhsReplacement =
46 rewriteExprFromNumberToTime(Result, *Scale, Binop->getLHS());
47 std::string RhsReplacement =
48 rewriteExprFromNumberToTime(Result, *Scale, Binop->getRHS());
49
50 diag(Binop->getBeginLoc(), "perform comparison in the time domain")
51 << FixItHint::CreateReplacement(Binop->getSourceRange(),
52 (llvm::Twine(LhsReplacement) + " " +
53 Binop->getOpcodeStr() + " " +
54 RhsReplacement)
55 .str());
56}
57
58} // namespace clang::tidy::abseil
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::optional< DurationScale > getScaleForTimeInverse(llvm::StringRef Name)
Given the name of an inverse Time function (e.g., ToUnixSeconds), return its DurationScale,...
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,...
bool isInMacro(const MatchFinder::MatchResult &Result, const Expr *E)