diff --git a/clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp b/clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp index 7fd1ca0..603401a 100644 --- a/clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp +++ b/clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp @@ -17,7 +17,6 @@ namespace { AST_MATCHER(CastExpr, isPointerToBoolean) { return Node.getCastKind() == CK_PointerToBoolean; } -AST_MATCHER(QualType, isBoolean) { return Node->isBooleanType(); } } // namespace @@ -31,7 +30,7 @@ void BoolPointerImplicitConversionCheck::registerMatchers(MatchFinder *Finder) { 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"), diff --git a/clang-tidy/misc/IncorrectRoundings.cpp b/clang-tidy/misc/IncorrectRoundings.cpp index 0001911..bf0ef4f 100644 --- a/clang-tidy/misc/IncorrectRoundings.cpp +++ b/clang-tidy/misc/IncorrectRoundings.cpp @@ -24,11 +24,6 @@ AST_MATCHER(FloatingLiteral, floatHalf) { 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 @@ void IncorrectRoundings::registerMatchers(MatchFinder *MatchFinder) { 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. diff --git a/clang-tidy/misc/MacroParenthesesCheck.cpp b/clang-tidy/misc/MacroParenthesesCheck.cpp index e1955de..86e5d27 100644 --- a/clang-tidy/misc/MacroParenthesesCheck.cpp +++ b/clang-tidy/misc/MacroParenthesesCheck.cpp @@ -18,8 +18,8 @@ namespace tidy { 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, diff --git a/clang-tidy/misc/MisplacedWideningCastCheck.cpp b/clang-tidy/misc/MisplacedWideningCastCheck.cpp index db463fc..101c0a1 100644 --- a/clang-tidy/misc/MisplacedWideningCastCheck.cpp +++ b/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 @@ void MisplacedWideningCastCheck::registerMatchers(MatchFinder *Finder) { 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); } diff --git a/clang-tidy/misc/PointerAndIntegralOperationCheck.cpp b/clang-tidy/misc/PointerAndIntegralOperationCheck.cpp index e154e8d..f60da78 100644 --- a/clang-tidy/misc/PointerAndIntegralOperationCheck.cpp +++ b/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 @@ void PointerAndIntegralOperationCheck::registerMatchers(MatchFinder *Finder) { binaryOperator(hasOperatorName("="), hasLHS(PointerExpr)); const auto CompareToPointerExpr = - binaryOperator(anyOf(hasOperatorName("<"), hasOperatorName("<="), - hasOperatorName(">"), hasOperatorName(">=")), + binaryOperator(matchers::isRelationalOperator(), hasEitherOperand(PointerExpr)); // Detect expression like: ptr = (x != y); diff --git a/clang-tidy/misc/SizeofExpressionCheck.cpp b/clang-tidy/misc/SizeofExpressionCheck.cpp index c0a223d..e310e32 100644 --- a/clang-tidy/misc/SizeofExpressionCheck.cpp +++ b/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 misc { namespace { -AST_MATCHER(BinaryOperator, isRelationalOperator) { - return Node.isRelationalOp(); -} - AST_MATCHER_P(IntegerLiteral, isBiggerThan, unsigned, N) { return Node.getValue().getZExtValue() > N; } @@ -139,7 +136,7 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) { // 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)), diff --git a/clang-tidy/modernize/ShrinkToFitCheck.cpp b/clang-tidy/modernize/ShrinkToFitCheck.cpp index 0bcb2a0..0b0be83 100644 --- a/clang-tidy/modernize/ShrinkToFitCheck.cpp +++ b/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 @@ void ShrinkToFitCheck::registerMatchers(MatchFinder *Finder) { 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")), diff --git a/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tidy/readability/ContainerSizeEmptyCheck.cpp index 5639615..b0c6fb5 100644 --- a/clang-tidy/readability/ContainerSizeEmptyCheck.cpp +++ b/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 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) { 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 @@ void ContainerSizeEmptyCheck::registerMatchers(MatchFinder *Finder) { 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"), diff --git a/clang-tidy/readability/ImplicitBoolCastCheck.cpp b/clang-tidy/readability/ImplicitBoolCastCheck.cpp index 0584f58..f412545 100644 --- a/clang-tidy/readability/ImplicitBoolCastCheck.cpp +++ b/clang-tidy/readability/ImplicitBoolCastCheck.cpp @@ -19,16 +19,10 @@ namespace tidy { 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 @@ StatementMatcher createImplicitCastFromBoolMatcher() { allOf(anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), hasSourceExpression(cxxBoolLiteral()))), - hasSourceExpression(expr(hasType(qualType(isBool()))))); + hasSourceExpression(expr(hasType(qualType(booleanType()))))); } StringRef diff --git a/clang-tidy/utils/Matchers.h b/clang-tidy/utils/Matchers.h index 94ed53a..fd386a9 100644 --- a/clang-tidy/utils/Matchers.h +++ b/clang-tidy/utils/Matchers.h @@ -17,6 +17,18 @@ namespace clang { 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());