11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/FixIt.h"
21 auto CallMatcher = ignoringImpCasts(callExpr(
22 callee(functionDecl(DurationConversionFunction()).bind(
"func_decl")),
23 hasArgument(0, expr().bind(
"arg"))));
27 cxxStaticCastExpr(hasSourceExpression(CallMatcher)).bind(
"cast_expr"),
28 cStyleCastExpr(hasSourceExpression(CallMatcher)).bind(
"cast_expr"),
29 cxxFunctionalCastExpr(hasSourceExpression(CallMatcher))
35 const MatchFinder::MatchResult &Result) {
36 const auto *MatchedCast =
37 Result.Nodes.getNodeAs<ExplicitCastExpr>(
"cast_expr");
42 const auto *
FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"func_decl");
43 const auto *Arg = Result.Nodes.getNodeAs<Expr>(
"arg");
44 StringRef ConversionFuncName =
FuncDecl->getName();
46 std::optional<DurationScale> Scale =
52 if (MatchedCast->getTypeAsWritten()->isIntegerType() &&
53 ConversionFuncName.contains(
"Double")) {
56 diag(MatchedCast->getBeginLoc(),
57 "duration should be converted directly to an integer rather than "
58 "through a type cast")
59 << FixItHint::CreateReplacement(
60 MatchedCast->getSourceRange(),
61 (llvm::Twine(NewFuncName.substr(2)) +
"(" +
62 tooling::fixit::getText(*Arg, *Result.Context) +
")")
67 if (MatchedCast->getTypeAsWritten()->isRealFloatingType() &&
68 ConversionFuncName.contains(
"Int64")) {
71 diag(MatchedCast->getBeginLoc(),
"duration should be converted directly to "
72 "a floating-point number rather than "
73 "through a type cast")
74 << FixItHint::CreateReplacement(
75 MatchedCast->getSourceRange(),
76 (llvm::Twine(NewFuncName.substr(2)) +
"(" +
77 tooling::fixit::getText(*Arg, *Result.Context) +
")")
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.
const std::pair< llvm::StringRef, llvm::StringRef > & getDurationInverseForScale(DurationScale Scale)
Given a Scale return the fully qualified inverse functions for it.
std::optional< DurationScale > getScaleForDurationInverse(llvm::StringRef Name)
Given the name of an inverse Duration function (e.g., ToDoubleSeconds), return its DurationScale,...
bool isInMacro(const MatchFinder::MatchResult &Result, const Expr *E)
static constexpr const char FuncDecl[]