[MemProf] Templatize CallStackRadixTreeBuilder (NFC) (#117014)
[llvm-project.git] / clang-tools-extra / clang-tidy / abseil / DurationDivisionCheck.cpp
blob50e2d0366c768dc558944bbea47111798a208399
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 //===----------------------------------------------------------------------===//
9 #include "DurationDivisionCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
14 namespace clang::tidy::abseil {
16 using namespace clang::ast_matchers;
18 void DurationDivisionCheck::registerMatchers(MatchFinder *Finder) {
19 const auto DurationExpr =
20 expr(hasType(cxxRecordDecl(hasName("::absl::Duration"))));
21 Finder->addMatcher(
22 traverse(TK_AsIs,
23 implicitCastExpr(
24 hasSourceExpression(ignoringParenCasts(
25 cxxOperatorCallExpr(hasOverloadedOperatorName("/"),
26 hasArgument(0, DurationExpr),
27 hasArgument(1, DurationExpr))
28 .bind("OpCall"))),
29 hasImplicitDestinationType(qualType(unless(isInteger()))),
30 unless(hasParent(cxxStaticCastExpr())),
31 unless(hasParent(cStyleCastExpr())),
32 unless(isInTemplateInstantiation()))),
33 this);
36 void DurationDivisionCheck::check(const MatchFinder::MatchResult &Result) {
37 const auto *OpCall = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("OpCall");
38 diag(OpCall->getOperatorLoc(),
39 "operator/ on absl::Duration objects performs integer division; "
40 "did you mean to use FDivDuration()?")
41 << FixItHint::CreateInsertion(OpCall->getBeginLoc(),
42 "absl::FDivDuration(")
43 << FixItHint::CreateReplacement(
44 SourceRange(OpCall->getOperatorLoc(), OpCall->getOperatorLoc()),
45 ", ")
46 << FixItHint::CreateInsertion(
47 Lexer::getLocForEndOfToken(
48 Result.SourceManager->getSpellingLoc(OpCall->getEndLoc()), 0,
49 *Result.SourceManager, Result.Context->getLangOpts()),
50 ")");
53 } // namespace clang::tidy::abseil