10#include "clang/AST/DeclBase.h"
11#include "clang/AST/Type.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Lex/Lexer.h"
20static llvm::APFloat
getHalf(
const llvm::fltSemantics &Semantics) {
21 return llvm::APFloat(Semantics, 1U) / llvm::APFloat(Semantics, 2U);
32 auto FloatHalf = floatLiteral(floatHalf());
35 auto FloatType = expr(hasType(realFloatingPointType()));
39 MatchFinder->addMatcher(
42 hasImplicitDestinationType(isInteger()),
43 ignoringParenCasts(binaryOperator(
44 hasOperatorName(
"+"), hasOperands(FloatType, FloatType),
45 hasEitherOperand(ignoringParenImpCasts(FloatHalf)))))
51 const auto *CastExpr = Result.Nodes.getNodeAs<ImplicitCastExpr>(
"CastExpr");
52 diag(CastExpr->getBeginLoc(),
53 "casting (double + 0.5) to integer leads to incorrect rounding; "
54 "consider using lround (#include <cmath>) instead");
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
static llvm::APFloat getHalf(const llvm::fltSemantics &Semantics)
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)