clang-tools 8.0.0

DurationDivisionCheck.cpp
Go to the documentation of this file.
1 //===--- DurationDivisionCheck.cpp - clang-tidy----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "DurationDivisionCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.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 if (!getLangOpts().CPlusPlus)
22 return;
23
24 const auto DurationExpr =
25 expr(hasType(cxxRecordDecl(hasName("::absl::Duration"))));
26 finder->addMatcher(
27 implicitCastExpr(
28 hasSourceExpression(ignoringParenCasts(
29 cxxOperatorCallExpr(hasOverloadedOperatorName("/"),
30 hasArgument(0, DurationExpr),
31 hasArgument(1, DurationExpr))
32 .bind("OpCall"))),
33 hasImplicitDestinationType(qualType(unless(isInteger()))),
34 unless(hasParent(cxxStaticCastExpr())),
35 unless(hasParent(cStyleCastExpr())),
36 unless(isInTemplateInstantiation())),
37 this);
38 }
39
40 void DurationDivisionCheck::check(const MatchFinder::MatchResult &result) {
41 const auto *OpCall = result.Nodes.getNodeAs<CXXOperatorCallExpr>("OpCall");
42 diag(OpCall->getOperatorLoc(),
43 "operator/ on absl::Duration objects performs integer division; "
44 "did you mean to use FDivDuration()?")
45 << FixItHint::CreateInsertion(OpCall->getBeginLoc(),
46 "absl::FDivDuration(")
47 << FixItHint::CreateReplacement(
48 SourceRange(OpCall->getOperatorLoc(), OpCall->getOperatorLoc()),
49 ", ")
50 << FixItHint::CreateInsertion(
51 Lexer::getLocForEndOfToken(
52 result.SourceManager->getSpellingLoc(OpCall->getEndLoc()), 0,
53 *result.SourceManager, result.Context->getLangOpts()),
54 ")");
55 }
56
57 } // namespace abseil
58 } // namespace tidy
59 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:15
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:40
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::tidy::abseil::DurationDivisionCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *finder) override
Override this to register AST matchers with Finder.
Definition: DurationDivisionCheck.cpp:20