clang-tools  14.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 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace abseil {
20 
21 void DurationAdditionCheck::registerMatchers(MatchFinder *Finder) {
22  Finder->addMatcher(
23  binaryOperator(hasOperatorName("+"),
24  hasEitherOperand(expr(ignoringParenImpCasts(
25  callExpr(callee(functionDecl(TimeConversionFunction())
26  .bind("function_decl")))
27  .bind("call")))))
28  .bind("binop"),
29  this);
30 }
31 
32 void DurationAdditionCheck::check(const MatchFinder::MatchResult &Result) {
33  const BinaryOperator *Binop =
34  Result.Nodes.getNodeAs<clang::BinaryOperator>("binop");
35  const CallExpr *Call = Result.Nodes.getNodeAs<clang::CallExpr>("call");
36 
37  // Don't try to replace things inside of macro definitions.
38  if (Binop->getExprLoc().isMacroID() || Binop->getExprLoc().isInvalid())
39  return;
40 
41  llvm::Optional<DurationScale> Scale = getScaleForTimeInverse(
42  Result.Nodes.getNodeAs<clang::FunctionDecl>("function_decl")->getName());
43  if (!Scale)
44  return;
45 
46  llvm::StringRef TimeFactory = getTimeInverseForScale(*Scale);
47 
48  FixItHint Hint;
49  if (Call == Binop->getLHS()->IgnoreParenImpCasts()) {
50  Hint = FixItHint::CreateReplacement(
51  Binop->getSourceRange(),
52  (llvm::Twine(TimeFactory) + "(" +
53  tooling::fixit::getText(*Call->getArg(0), *Result.Context) + " + " +
54  rewriteExprFromNumberToDuration(Result, *Scale, Binop->getRHS()) + ")")
55  .str());
56  } else {
57  assert(Call == Binop->getRHS()->IgnoreParenImpCasts() &&
58  "Call should be found on the RHS");
59  Hint = FixItHint::CreateReplacement(
60  Binop->getSourceRange(),
61  (llvm::Twine(TimeFactory) + "(" +
62  rewriteExprFromNumberToDuration(Result, *Scale, Binop->getLHS()) +
63  " + " + tooling::fixit::getText(*Call->getArg(0), *Result.Context) +
64  ")")
65  .str());
66  }
67 
68  diag(Binop->getBeginLoc(), "perform addition in the duration domain") << Hint;
69 }
70 
71 } // namespace abseil
72 } // namespace tidy
73 } // 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
clang::tidy::modernize::getText
static StringRef getText(const Token &Tok, const SourceManager &Sources)
Definition: UseOverrideCheck.cpp:78
clang::ast_matchers
Definition: AbseilMatcher.h:14
DurationRewriter.h
DurationAdditionCheck.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::rewriteExprFromNumberToDuration
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 ...
Definition: DurationRewriter.cpp:269
clang::tidy::abseil::getTimeInverseForScale
llvm::StringRef getTimeInverseForScale(DurationScale Scale)
Returns the Time factory function name for a given Scale.
Definition: DurationRewriter.cpp:141
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27