const String fontToCode (const Font& font) { String s ("Font ("); String name (font.getTypefaceName()); if (name != Font::getDefaultSansSerifFontName()) { if (name == Font::getDefaultSerifFontName()) name = "Font::getDefaultSerifFontName()"; else if (name == Font::getDefaultMonospacedFontName()) name = "Font::getDefaultMonospacedFontName()"; else name = stringLiteral (font.getTypefaceName()); s << name << ", "; } s << floatLiteral (font.getHeight()); if (font.isBold() && font.isItalic()) s << ", Font::bold | Font::italic"; else if (font.isBold()) s << ", Font::bold"; else if (font.isItalic()) s << ", Font::italic"; else if (name != Font::getDefaultSansSerifFontName()) // need this param if we're using the typeface name constructor s << ", Font::plain"; return s + ")"; }
void IncorrectRoundings::registerMatchers(MatchFinder *MatchFinder) { // Match a floating literal with value 0.5. auto FloatHalf = floatLiteral(floatHalf()); // Match a floating point expression. auto FloatType = expr(hasType(realFloatingPointType())); // Match a floating literal of 0.5 or a floating literal of 0.5 implicitly. // cast to floating type. auto FloatOrCastHalf = anyOf(FloatHalf, implicitCastExpr(FloatType, has(FloatHalf))); // Match if either the LHS or RHS is a floating literal of 0.5 or a floating // literal of 0.5 and the other is of type double or vice versa. auto OneSideHalf = anyOf(allOf(hasLHS(FloatOrCastHalf), hasRHS(FloatType)), allOf(hasRHS(FloatOrCastHalf), hasLHS(FloatType))); // Find expressions of cast to int of the sum of a floating point expression // and 0.5. MatchFinder->addMatcher( implicitCastExpr( hasImplicitDestinationType(isInteger()), ignoringParenCasts(binaryOperator(hasOperatorName("+"), OneSideHalf))) .bind("CastExpr"), this); }
/// Returns `true` if `Node` is a value which evaluates to a literal `0`. bool IsLiteralZero(const MatchFinder::MatchResult &Result, const Expr &Node) { auto ZeroMatcher = anyOf(integerLiteral(equals(0)), floatLiteral(equals(0.0))); // Check to see if we're using a zero directly. if (selectFirst<const clang::Expr>( "val", match(expr(ignoringImpCasts(ZeroMatcher)).bind("val"), Node, *Result.Context)) != nullptr) return true; // Now check to see if we're using a functional cast with a scalar // initializer expression, e.g. `int{0}`. if (selectFirst<const clang::Expr>( "val", match(cxxFunctionalCastExpr( hasDestinationType( anyOf(isInteger(), realFloatingPointType())), hasSourceExpression(initListExpr( hasInit(0, ignoringParenImpCasts(ZeroMatcher))))) .bind("val"), Node, *Result.Context)) != nullptr) return true; return false; }
void MagicNumbersCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher(integerLiteral().bind("integer"), this); if (!IgnoreAllFloatingPointValues) Finder->addMatcher(floatLiteral().bind("float"), this); }
#include "ExceptNumber.h" #include "ASTUtility.h" //TODO @TestNeed static int count = 0; //Declare a matcher of ASTNode to match magic number set StatementMatcher FloatLiteralMatcher = floatLiteral().bind("floatliteral"); StatementMatcher IntLiteralMatcher = integerLiteral().bind("intliteral"); StatementMatcher StringLiteralMatcher = stringLiteral().bind("stringliteral"); //Declare a class Inherit clang::ast_matchers::MatchFinder::MatchCallback class FloatChecker : public MatchFinder::MatchCallback { public: virtual void run(const MatchFinder::MatchResult &Result) { //get the ASTContext clang::ASTContext *Context = Result.Context; //get the MagicNumberASTNode const clang::FloatingLiteral *fl = Result.Nodes.getNodeAs<clang::FloatingLiteral>("floatliteral"); //TODO handle the condition "without vaild data" if(!fl || ASTUtility::IsStmtInSTDFile(fl, Context)) return; ASTUtility::Print(fl, Context, "Rule049"); } };