clang-tools  14.0.0git
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 #include "clang/Lex/Lexer.h"
13 
14 namespace clang {
15 namespace tidy {
16 namespace abseil {
17 
18 using namespace clang::ast_matchers;
19 
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 
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
clang::tidy::abseil::DurationDivisionCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: DurationDivisionCheck.cpp:20
DurationDivisionCheck.h
clang::tidy::abseil::DurationDivisionCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: DurationDivisionCheck.cpp:38
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27