summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/abseil/DurationFactoryFloatCheck.cpp
blob: 80ecf8a812e089b9da621c729caab42ca925fcdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//===--- DurationFactoryFloatCheck.cpp - clang-tidy -----------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "DurationFactoryFloatCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/FixIt.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace abseil {

// Returns an integer if the fractional part of a `FloatingLiteral` is `0`.
static llvm::Optional<llvm::APSInt>
truncateIfIntegral(const FloatingLiteral &FloatLiteral) {
  double Value = FloatLiteral.getValueAsApproximateDouble();
  if (std::fmod(Value, 1) == 0) {
    if (Value >= static_cast<double>(1u << 31))
      return llvm::None;

    return llvm::APSInt::get(static_cast<int64_t>(Value));
  }
  return llvm::None;
}

// Returns `true` if `Range` is inside a macro definition.
static bool InsideMacroDefinition(const MatchFinder::MatchResult &Result,
                                  SourceRange Range) {
  return !clang::Lexer::makeFileCharRange(
              clang::CharSourceRange::getCharRange(Range),
              *Result.SourceManager, Result.Context->getLangOpts())
              .isValid();
}

void DurationFactoryFloatCheck::registerMatchers(MatchFinder *Finder) {
  Finder->addMatcher(
      callExpr(
          callee(functionDecl(hasAnyName(
              "absl::Nanoseconds", "absl::Microseconds", "absl::Milliseconds",
              "absl::Seconds", "absl::Minutes", "absl::Hours"))),
          hasArgument(0,
                      anyOf(cxxStaticCastExpr(
                                hasDestinationType(realFloatingPointType()),
                                hasSourceExpression(expr().bind("cast_arg"))),
                            cStyleCastExpr(
                                hasDestinationType(realFloatingPointType()),
                                hasSourceExpression(expr().bind("cast_arg"))),
                            cxxFunctionalCastExpr(
                                hasDestinationType(realFloatingPointType()),
                                hasSourceExpression(expr().bind("cast_arg"))),
                            floatLiteral().bind("float_literal"))))
          .bind("call"),
      this);
}

void DurationFactoryFloatCheck::check(const MatchFinder::MatchResult &Result) {
  const auto *MatchedCall = Result.Nodes.getNodeAs<CallExpr>("call");

  // Don't try and replace things inside of macro definitions.
  if (InsideMacroDefinition(Result, MatchedCall->getSourceRange()))
    return;

  const Expr *Arg = MatchedCall->getArg(0)->IgnoreImpCasts();
  // Arguments which are macros are ignored.
  if (Arg->getBeginLoc().isMacroID())
    return;

  // Check for casts to `float` or `double`.
  if (const auto *MaybeCastArg = Result.Nodes.getNodeAs<Expr>("cast_arg")) {
    diag(MatchedCall->getBeginLoc(),
         (llvm::Twine("use the integer version of absl::") +
          MatchedCall->getDirectCallee()->getName())
             .str())
        << FixItHint::CreateReplacement(
               Arg->getSourceRange(),
               tooling::fixit::getText(*MaybeCastArg, *Result.Context));
    return;
  }

  // Check for floats without fractional components.
  if (const auto *LitFloat =
          Result.Nodes.getNodeAs<FloatingLiteral>("float_literal")) {
    // Attempt to simplify a `Duration` factory call with a literal argument.
    if (llvm::Optional<llvm::APSInt> IntValue = truncateIfIntegral(*LitFloat)) {
      diag(MatchedCall->getBeginLoc(),
           (llvm::Twine("use the integer version of absl::") +
            MatchedCall->getDirectCallee()->getName())
               .str())
          << FixItHint::CreateReplacement(LitFloat->getSourceRange(),
                                          IntValue->toString(/*radix=*/10));
      return;
    }
  }
}

} // namespace abseil
} // namespace tidy
} // namespace clang
OpenPOWER on IntegriCloud