• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- DurationDivisionCheck.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 
9 #include "DurationDivisionCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace abseil {
17 
18 using namespace clang::ast_matchers;
19 
registerMatchers(MatchFinder * finder)20 void DurationDivisionCheck::registerMatchers(MatchFinder *finder) {
21   const auto DurationExpr =
22       expr(hasType(cxxRecordDecl(hasName("::absl::Duration"))));
23   finder->addMatcher(
24       traverse(ast_type_traits::TK_AsIs,
25                implicitCastExpr(
26                    hasSourceExpression(ignoringParenCasts(
27                        cxxOperatorCallExpr(hasOverloadedOperatorName("/"),
28                                            hasArgument(0, DurationExpr),
29                                            hasArgument(1, DurationExpr))
30                            .bind("OpCall"))),
31                    hasImplicitDestinationType(qualType(unless(isInteger()))),
32                    unless(hasParent(cxxStaticCastExpr())),
33                    unless(hasParent(cStyleCastExpr())),
34                    unless(isInTemplateInstantiation()))),
35       this);
36 }
37 
check(const MatchFinder::MatchResult & result)38 void DurationDivisionCheck::check(const MatchFinder::MatchResult &result) {
39   const auto *OpCall = result.Nodes.getNodeAs<CXXOperatorCallExpr>("OpCall");
40   diag(OpCall->getOperatorLoc(),
41        "operator/ on absl::Duration objects performs integer division; "
42        "did you mean to use FDivDuration()?")
43       << FixItHint::CreateInsertion(OpCall->getBeginLoc(),
44                                     "absl::FDivDuration(")
45       << FixItHint::CreateReplacement(
46              SourceRange(OpCall->getOperatorLoc(), OpCall->getOperatorLoc()),
47              ", ")
48       << FixItHint::CreateInsertion(
49              Lexer::getLocForEndOfToken(
50                  result.SourceManager->getSpellingLoc(OpCall->getEndLoc()), 0,
51                  *result.SourceManager, result.Context->getLangOpts()),
52              ")");
53 }
54 
55 } // namespace abseil
56 } // namespace tidy
57 } // namespace clang
58