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(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