clang-tools  10.0.0
DurationDivisionCheck.cpp
Go to the documentation of this file.
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 
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 namespace clang {
14 namespace tidy {
15 namespace abseil {
16 
17 using namespace clang::ast_matchers;
18 
19 void DurationDivisionCheck::registerMatchers(MatchFinder *finder) {
20  if (!getLangOpts().CPlusPlus)
21  return;
22 
23  const auto DurationExpr =
24  expr(hasType(cxxRecordDecl(hasName("::absl::Duration"))));
25  finder->addMatcher(
26  implicitCastExpr(
27  hasSourceExpression(ignoringParenCasts(
28  cxxOperatorCallExpr(hasOverloadedOperatorName("/"),
29  hasArgument(0, DurationExpr),
30  hasArgument(1, DurationExpr))
31  .bind("OpCall"))),
32  hasImplicitDestinationType(qualType(unless(isInteger()))),
33  unless(hasParent(cxxStaticCastExpr())),
34  unless(hasParent(cStyleCastExpr())),
35  unless(isInTemplateInstantiation())),
36  this);
37 }
38 
39 void DurationDivisionCheck::check(const MatchFinder::MatchResult &result) {
40  const auto *OpCall = result.Nodes.getNodeAs<CXXOperatorCallExpr>("OpCall");
41  diag(OpCall->getOperatorLoc(),
42  "operator/ on absl::Duration objects performs integer division; "
43  "did you mean to use FDivDuration()?")
44  << FixItHint::CreateInsertion(OpCall->getBeginLoc(),
45  "absl::FDivDuration(")
46  << FixItHint::CreateReplacement(
47  SourceRange(OpCall->getOperatorLoc(), OpCall->getOperatorLoc()),
48  ", ")
49  << FixItHint::CreateInsertion(
50  Lexer::getLocForEndOfToken(
51  result.SourceManager->getSpellingLoc(OpCall->getEndLoc()), 0,
52  *result.SourceManager, result.Context->getLangOpts()),
53  ")");
54 }
55 
56 } // namespace abseil
57 } // namespace tidy
58 } // namespace clang
void check(const ast_matchers::MatchFinder::MatchResult &result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void registerMatchers(ast_matchers::MatchFinder *finder) override
Override this to register AST matchers with Finder.