Index: clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp =================================================================== --- clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp +++ clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp @@ -17,7 +17,6 @@ AST_MATCHER(CastExpr, isPointerToBoolean) { return Node.getCastKind() == CK_PointerToBoolean; } -AST_MATCHER(QualType, isBoolean) { return Node->isBooleanType(); } } // namespace @@ -31,7 +30,7 @@ ifStmt(hasCondition(findAll(implicitCastExpr( allOf(unless(hasParent(unaryOperator(hasOperatorName("!")))), hasSourceExpression(expr( - hasType(pointerType(pointee(isBoolean()))), + hasType(pointerType(pointee(booleanType()))), ignoringParenImpCasts(declRefExpr().bind("expr")))), isPointerToBoolean())))), unless(isInTemplateInstantiation())).bind("if"), Index: clang-tidy/misc/IncorrectRoundings.cpp =================================================================== --- clang-tidy/misc/IncorrectRoundings.cpp +++ clang-tidy/misc/IncorrectRoundings.cpp @@ -24,11 +24,6 @@ return literal.convertToDouble() == 0.5; return false; } - -// TODO(hokein): Moving it to ASTMatchers.h -AST_MATCHER(BuiltinType, isFloatingPoint) { - return Node.isFloatingPoint(); -} } // namespace ast_matchers } // namespace clang @@ -42,7 +37,7 @@ auto FloatHalf = floatLiteral(floatHalf()); // Match a floating point expression. - auto FloatType = expr(hasType(builtinType(isFloatingPoint()))); + auto FloatType = expr(hasType(realFloatingPointType())); // Match a floating literal of 0.5 or a floating literal of 0.5 implicitly. // cast to floating type. Index: clang-tidy/misc/MacroParenthesesCheck.cpp =================================================================== --- clang-tidy/misc/MacroParenthesesCheck.cpp +++ clang-tidy/misc/MacroParenthesesCheck.cpp @@ -18,8 +18,8 @@ namespace { class MacroParenthesesPPCallbacks : public PPCallbacks { public: - explicit MacroParenthesesPPCallbacks(Preprocessor *PP, - MacroParenthesesCheck *Check) + MacroParenthesesPPCallbacks(Preprocessor *PP, + MacroParenthesesCheck *Check) : PP(PP), Check(Check) {} void MacroDefined(const Token &MacroNameTok, Index: clang-tidy/misc/MisplacedWideningCastCheck.cpp =================================================================== --- clang-tidy/misc/MisplacedWideningCastCheck.cpp +++ clang-tidy/misc/MisplacedWideningCastCheck.cpp @@ -10,6 +10,7 @@ #include "MisplacedWideningCastCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "../utils/Matchers.h" using namespace clang::ast_matchers; @@ -47,9 +48,7 @@ Finder->addMatcher(callExpr(hasAnyArgument(Cast)), this); Finder->addMatcher(binaryOperator(hasOperatorName("="), hasRHS(Cast)), this); Finder->addMatcher( - binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!="), - hasOperatorName("<"), hasOperatorName("<="), - hasOperatorName(">"), hasOperatorName(">=")), + binaryOperator(matchers::isComparisonOperator(), hasEitherOperand(Cast)), this); } Index: clang-tidy/misc/PointerAndIntegralOperationCheck.cpp =================================================================== --- clang-tidy/misc/PointerAndIntegralOperationCheck.cpp +++ clang-tidy/misc/PointerAndIntegralOperationCheck.cpp @@ -10,6 +10,7 @@ #include "PointerAndIntegralOperationCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "../utils/Matchers.h" using namespace clang::ast_matchers; @@ -30,8 +31,7 @@ binaryOperator(hasOperatorName("="), hasLHS(PointerExpr)); const auto CompareToPointerExpr = - binaryOperator(anyOf(hasOperatorName("<"), hasOperatorName("<="), - hasOperatorName(">"), hasOperatorName(">=")), + binaryOperator(matchers::isRelationalOperator(), hasEitherOperand(PointerExpr)); // Detect expression like: ptr = (x != y); Index: clang-tidy/misc/SizeofExpressionCheck.cpp =================================================================== --- clang-tidy/misc/SizeofExpressionCheck.cpp +++ clang-tidy/misc/SizeofExpressionCheck.cpp @@ -10,6 +10,7 @@ #include "SizeofExpressionCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "../utils/Matchers.h" using namespace clang::ast_matchers; @@ -19,10 +20,6 @@ namespace { -AST_MATCHER(BinaryOperator, isRelationalOperator) { - return Node.isRelationalOp(); -} - AST_MATCHER_P(IntegerLiteral, isBiggerThan, unsigned, N) { return Node.getValue().getZExtValue() > N; } @@ -139,7 +136,7 @@ // Detect expression like: sizeof(epxr) <= k for a suspicious constant 'k'. if (WarnOnSizeOfCompareToConstant) { Finder->addMatcher( - binaryOperator(isRelationalOperator(), + binaryOperator(matchers::isRelationalOperator(), hasEitherOperand(ignoringParenImpCasts(SizeOfExpr)), hasEitherOperand(ignoringParenImpCasts( anyOf(integerLiteral(equals(0)), Index: clang-tidy/modernize/ShrinkToFitCheck.cpp =================================================================== --- clang-tidy/modernize/ShrinkToFitCheck.cpp +++ clang-tidy/modernize/ShrinkToFitCheck.cpp @@ -16,22 +16,6 @@ using namespace clang::ast_matchers; namespace clang { -namespace { -bool isShrinkableContainer(llvm::StringRef ClassName) { - static const char *const Shrinkables[] = { - "std::basic_string", - "std::deque", - "std::vector" - }; - return std::binary_search(std::begin(Shrinkables), std::end(Shrinkables), - ClassName); -} - -AST_MATCHER(NamedDecl, stlShrinkableContainer) { - return isShrinkableContainer(Node.getQualifiedNameAsString()); -} -} // namespace - namespace tidy { namespace modernize { @@ -54,7 +38,8 @@ has(declRefExpr(hasDeclaration(equalsBoundNode("ContainerDecl"))))))); Finder->addMatcher( - cxxMemberCallExpr(on(hasType(namedDecl(stlShrinkableContainer()))), + cxxMemberCallExpr(on(hasType(namedDecl( + hasAnyName("std::basic_string", "std::deque", "std::vector")))), callee(cxxMethodDecl(hasName("swap"))), has(memberExpr(hasDescendant(CopyCtorCall))), hasArgument(0, SwapParam.bind("ContainerToShrink")), Index: clang-tidy/readability/ContainerSizeEmptyCheck.cpp =================================================================== --- clang-tidy/readability/ContainerSizeEmptyCheck.cpp +++ clang-tidy/readability/ContainerSizeEmptyCheck.cpp @@ -11,41 +11,11 @@ #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Lex/Lexer.h" #include "llvm/ADT/StringRef.h" +#include "../utils/Matchers.h" using namespace clang::ast_matchers; -static bool isContainerName(llvm::StringRef ClassName) { - static const char *const ContainerNames[] = {"array", - "basic_string", - "deque", - "forward_list", - "list", - "map", - "multimap", - "multiset", - "priority_queue", - "queue", - "set", - "stack", - "unordered_map", - "unordered_multimap", - "unordered_multiset", - "unordered_set", - "vector"}; - return std::binary_search(std::begin(ContainerNames), - std::end(ContainerNames), ClassName); -} - namespace clang { -namespace { -AST_MATCHER(NamedDecl, stlContainer) { - if (!isContainerName(Node.getName())) - return false; - - return StringRef(Node.getQualifiedNameAsString()).startswith("std::"); -} -} // namespace - namespace tidy { namespace readability { @@ -59,11 +29,15 @@ if (!getLangOpts().CPlusPlus) return; + const auto stlContainer = hasAnyName( + "array", "basic_string", "deque", "forward_list", "list", "map", + "multimap", "multiset", "priority_queue", "queue", "set", "stack", + "unordered_map", "unordered_multimap", "unordered_multiset", + "unordered_set", "vector"); + const auto WrongUse = anyOf( hasParent(binaryOperator( - anyOf(hasOperatorName("<"), hasOperatorName(">="), - hasOperatorName(">"), hasOperatorName("<="), - hasOperatorName("=="), hasOperatorName("!=")), + matchers::isComparisonOperator(), hasEitherOperand(ignoringImpCasts(anyOf( integerLiteral(equals(1)), integerLiteral(equals(0)))))) .bind("SizeBinaryOp")), @@ -76,9 +50,9 @@ Finder->addMatcher( cxxMemberCallExpr( - on(expr(anyOf(hasType(namedDecl(stlContainer())), - hasType(pointsTo(namedDecl(stlContainer()))), - hasType(references(namedDecl(stlContainer()))))) + on(expr(anyOf(hasType(namedDecl(stlContainer)), + hasType(pointsTo(namedDecl(stlContainer))), + hasType(references(namedDecl(stlContainer))))) .bind("STLObject")), callee(cxxMethodDecl(hasName("size"))), WrongUse) .bind("SizeCallExpr"), Index: clang-tidy/readability/ImplicitBoolCastCheck.cpp =================================================================== --- clang-tidy/readability/ImplicitBoolCastCheck.cpp +++ clang-tidy/readability/ImplicitBoolCastCheck.cpp @@ -19,16 +19,10 @@ namespace { -const internal::VariadicDynCastAllOfMatcher parenExpr; - AST_MATCHER_P(CastExpr, hasCastKind, CastKind, Kind) { return Node.getCastKind() == Kind; } -AST_MATCHER(QualType, isBool) { - return !Node.isNull() && Node->isBooleanType(); -} - AST_MATCHER(Stmt, isMacroExpansion) { SourceManager &SM = Finder->getASTContext().getSourceManager(); SourceLocation Loc = Node.getLocStart(); @@ -62,7 +56,7 @@ allOf(anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), hasSourceExpression(cxxBoolLiteral()))), - hasSourceExpression(expr(hasType(qualType(isBool()))))); + hasSourceExpression(expr(hasType(qualType(booleanType()))))); } StringRef Index: clang-tidy/utils/Matchers.h =================================================================== --- clang-tidy/utils/Matchers.h +++ clang-tidy/utils/Matchers.h @@ -17,6 +17,18 @@ namespace tidy { namespace matchers { +AST_MATCHER(BinaryOperator, isRelationalOperator) { + return Node.isRelationalOp(); +} + +AST_MATCHER(BinaryOperator, isEqualityOperator) { + return Node.isEqualityOp(); +} + +AST_MATCHER(BinaryOperator, isComparisonOperator) { + return Node.isComparisonOp(); +} + AST_MATCHER(QualType, isExpensiveToCopy) { llvm::Optional IsExpensive = type_traits::isExpensiveToCopy(Node, Finder->getASTContext());