clang-tools 19.0.0git
DurationAdditionCheck.cpp
Go to the documentation of this file.
1//===--- DurationAdditionCheck.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
20void DurationAdditionCheck::registerMatchers(MatchFinder *Finder) {
21 Finder->addMatcher(
22 binaryOperator(hasOperatorName("+"),
23 hasEitherOperand(expr(ignoringParenImpCasts(
24 callExpr(callee(functionDecl(TimeConversionFunction())
25 .bind("function_decl")))
26 .bind("call")))))
27 .bind("binop"),
28 this);
29}
30
31void DurationAdditionCheck::check(const MatchFinder::MatchResult &Result) {
32 const auto *Binop = Result.Nodes.getNodeAs<clang::BinaryOperator>("binop");
33 const auto *Call = Result.Nodes.getNodeAs<clang::CallExpr>("call");
34
35 // Don't try to replace things inside of macro definitions.
36 if (Binop->getExprLoc().isMacroID() || Binop->getExprLoc().isInvalid())
37 return;
38
39 std::optional<DurationScale> Scale = getScaleForTimeInverse(
40 Result.Nodes.getNodeAs<clang::FunctionDecl>("function_decl")->getName());
41 if (!Scale)
42 return;
43
44 llvm::StringRef TimeFactory = getTimeInverseForScale(*Scale);
45
46 FixItHint Hint;
47 if (Call == Binop->getLHS()->IgnoreParenImpCasts()) {
48 Hint = FixItHint::CreateReplacement(
49 Binop->getSourceRange(),
50 (llvm::Twine(TimeFactory) + "(" +
51 tooling::fixit::getText(*Call->getArg(0), *Result.Context) + " + " +
52 rewriteExprFromNumberToDuration(Result, *Scale, Binop->getRHS()) + ")")
53 .str());
54 } else {
55 assert(Call == Binop->getRHS()->IgnoreParenImpCasts() &&
56 "Call should be found on the RHS");
57 Hint = FixItHint::CreateReplacement(
58 Binop->getSourceRange(),
59 (llvm::Twine(TimeFactory) + "(" +
60 rewriteExprFromNumberToDuration(Result, *Scale, Binop->getLHS()) +
61 " + " + tooling::fixit::getText(*Call->getArg(0), *Result.Context) +
62 ")")
63 .str());
64 }
65
66 diag(Binop->getBeginLoc(), "perform addition in the duration domain") << Hint;
67}
68
69} // 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 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 ...
llvm::StringRef getTimeInverseForScale(DurationScale Scale)
Returns the Time factory function name for a given Scale.