clang-tools  10.0.0
DurationFactoryFloatCheck.cpp
Go to the documentation of this file.
1 //===--- DurationFactoryFloatCheck.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 // Returns `true` if `Range` is inside a macro definition.
22 static bool InsideMacroDefinition(const MatchFinder::MatchResult &Result,
23  SourceRange Range) {
24  return !clang::Lexer::makeFileCharRange(
25  clang::CharSourceRange::getCharRange(Range),
26  *Result.SourceManager, Result.Context->getLangOpts())
27  .isValid();
28 }
29 
30 void DurationFactoryFloatCheck::registerMatchers(MatchFinder *Finder) {
31  Finder->addMatcher(
32  callExpr(callee(functionDecl(DurationFactoryFunction())),
33  hasArgument(0, anyOf(cxxStaticCastExpr(hasDestinationType(
34  realFloatingPointType())),
35  cStyleCastExpr(hasDestinationType(
36  realFloatingPointType())),
37  cxxFunctionalCastExpr(hasDestinationType(
38  realFloatingPointType())),
39  floatLiteral())))
40  .bind("call"),
41  this);
42 }
43 
44 void DurationFactoryFloatCheck::check(const MatchFinder::MatchResult &Result) {
45  const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("call");
46 
47  // Don't try and replace things inside of macro definitions.
48  if (InsideMacroDefinition(Result, MatchedCall->getSourceRange()))
49  return;
50 
51  const Expr *Arg = MatchedCall->getArg(0)->IgnoreImpCasts();
52  // Arguments which are macros are ignored.
53  if (Arg->getBeginLoc().isMacroID())
54  return;
55 
56  llvm::Optional<std::string> SimpleArg = stripFloatCast(Result, *Arg);
57  if (!SimpleArg)
58  SimpleArg = stripFloatLiteralFraction(Result, *Arg);
59 
60  if (SimpleArg) {
61  diag(MatchedCall->getBeginLoc(),
62  (llvm::Twine("use the integer version of absl::") +
63  MatchedCall->getDirectCallee()->getName())
64  .str())
65  << FixItHint::CreateReplacement(Arg->getSourceRange(), *SimpleArg);
66  }
67 }
68 
69 } // namespace abseil
70 } // namespace tidy
71 } // namespace clang
static bool InsideMacroDefinition(const MatchFinder::MatchResult &Result, SourceRange Range)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
llvm::Optional< std::string > stripFloatCast(const ast_matchers::MatchFinder::MatchResult &Result, const Expr &Node)
Possibly strip a floating point cast expression.
llvm::Optional< std::string > stripFloatLiteralFraction(const MatchFinder::MatchResult &Result, const Expr &Node)