10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
19 const auto DurationExpr =
20 expr(hasType(cxxRecordDecl(hasName(
"::absl::Duration"))));
24 hasSourceExpression(ignoringParenCasts(
25 cxxOperatorCallExpr(hasOverloadedOperatorName(
"/"),
26 hasArgument(0, DurationExpr),
27 hasArgument(1, DurationExpr))
29 hasImplicitDestinationType(qualType(unless(isInteger()))),
30 unless(hasParent(cxxStaticCastExpr())),
31 unless(hasParent(cStyleCastExpr())),
32 unless(isInTemplateInstantiation()))),
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()),
46 << FixItHint::CreateInsertion(
47 Lexer::getLocForEndOfToken(
48 Result.SourceManager->getSpellingLoc(OpCall->getEndLoc()), 0,
49 *Result.SourceManager, Result.Context->getLangOpts()),
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.