clang-tools 20.0.0git
DurationComparisonCheck.cpp
Go to the documentation of this file.
1//===--- DurationComparisonCheck.cpp - clang-tidy -------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
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
16using namespace clang::ast_matchers;
17
18namespace clang::tidy::abseil {
19
21 auto Matcher = expr(comparisonOperatorWithCallee(functionDecl(
22 functionDecl(DurationConversionFunction())
23 .bind("function_decl"))))
24 .bind("binop");
25
26 Finder->addMatcher(Matcher, this);
27}
28
29void DurationComparisonCheck::check(const MatchFinder::MatchResult &Result) {
30 const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>("binop");
31
32 std::optional<DurationScale> Scale = getScaleForDurationInverse(
33 Result.Nodes.getNodeAs<FunctionDecl>("function_decl")->getName());
34 if (!Scale)
35 return;
36
37 // In most cases, we'll only need to rewrite one of the sides, but we also
38 // want to handle the case of rewriting both sides. This is much simpler if
39 // we unconditionally try and rewrite both, and let the rewriter determine
40 // if nothing needs to be done.
41 if (isInMacro(Result, Binop->getLHS()) || isInMacro(Result, Binop->getRHS()))
42 return;
43 std::string LhsReplacement =
44 rewriteExprFromNumberToDuration(Result, *Scale, Binop->getLHS());
45 std::string RhsReplacement =
46 rewriteExprFromNumberToDuration(Result, *Scale, Binop->getRHS());
47
48 diag(Binop->getBeginLoc(), "perform comparison in the duration domain")
49 << FixItHint::CreateReplacement(Binop->getSourceRange(),
50 (llvm::Twine(LhsReplacement) + " " +
51 Binop->getOpcodeStr() + " " +
52 RhsReplacement)
53 .str());
54}
55
56} // 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 > getScaleForDurationInverse(llvm::StringRef Name)
Given the name of an inverse Duration function (e.g., ToDoubleSeconds), return its DurationScale,...
bool isInMacro(const MatchFinder::MatchResult &Result, const Expr *E)
std::string rewriteExprFromNumberToDuration(const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale, const Expr *Node)
Assuming Node has type double or int representing a time interval of Scale, return the expression to ...