[clang-tools-extra] r267003 - [clang-tidy] Cleanup some ast-matchers and lift some to utils.

Etienne Bergeron via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 21 09:57:56 PDT 2016


Author: etienneb
Date: Thu Apr 21 11:57:56 2016
New Revision: 267003

URL: http://llvm.org/viewvc/llvm-project?rev=267003&view=rev
Log:
[clang-tidy] Cleanup some ast-matchers and lift some to utils.

Summary:
Little cleanup to lift-out and to remove some frequently used
ast-matchers.

Some of theses matchers are candidates to be lifted to ASTMatchers.h.

Reviewers: alexfh

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D19200

Modified:
    clang-tools-extra/trunk/clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp
    clang-tools-extra/trunk/clang-tidy/misc/IncorrectRoundings.cpp
    clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp
    clang-tools-extra/trunk/clang-tidy/misc/MisplacedWideningCastCheck.cpp
    clang-tools-extra/trunk/clang-tidy/misc/PointerAndIntegralOperationCheck.cpp
    clang-tools-extra/trunk/clang-tidy/misc/SizeofExpressionCheck.cpp
    clang-tools-extra/trunk/clang-tidy/modernize/ShrinkToFitCheck.cpp
    clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
    clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp
    clang-tools-extra/trunk/clang-tidy/utils/Matchers.h

Modified: clang-tools-extra/trunk/clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp?rev=267003&r1=267002&r2=267003&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/BoolPointerImplicitConversionCheck.cpp Thu Apr 21 11:57:56 2016
@@ -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:
       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"),

Modified: clang-tools-extra/trunk/clang-tidy/misc/IncorrectRoundings.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/IncorrectRoundings.cpp?rev=267003&r1=267002&r2=267003&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/IncorrectRoundings.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/IncorrectRoundings.cpp Thu Apr 21 11:57:56 2016
@@ -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::registerMatcher
   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.

Modified: clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp?rev=267003&r1=267002&r2=267003&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp Thu Apr 21 11:57:56 2016
@@ -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,

Modified: clang-tools-extra/trunk/clang-tidy/misc/MisplacedWideningCastCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/MisplacedWideningCastCheck.cpp?rev=267003&r1=267002&r2=267003&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/MisplacedWideningCastCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/MisplacedWideningCastCheck.cpp Thu Apr 21 11:57:56 2016
@@ -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::registe
   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);
 }

Modified: clang-tools-extra/trunk/clang-tidy/misc/PointerAndIntegralOperationCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/PointerAndIntegralOperationCheck.cpp?rev=267003&r1=267002&r2=267003&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/PointerAndIntegralOperationCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/PointerAndIntegralOperationCheck.cpp Thu Apr 21 11:57:56 2016
@@ -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::r
       binaryOperator(hasOperatorName("="), hasLHS(PointerExpr));
 
   const auto CompareToPointerExpr =
-      binaryOperator(anyOf(hasOperatorName("<"), hasOperatorName("<="),
-                           hasOperatorName(">"), hasOperatorName(">=")),
+      binaryOperator(matchers::isRelationalOperator(),
                      hasEitherOperand(PointerExpr));
 
   // Detect expression like: ptr = (x != y);

Modified: clang-tools-extra/trunk/clang-tidy/misc/SizeofExpressionCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/SizeofExpressionCheck.cpp?rev=267003&r1=267002&r2=267003&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/SizeofExpressionCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/SizeofExpressionCheck.cpp Thu Apr 21 11:57:56 2016
@@ -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::registerMatc
   // 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)),

Modified: clang-tools-extra/trunk/clang-tidy/modernize/ShrinkToFitCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/ShrinkToFitCheck.cpp?rev=267003&r1=267002&r2=267003&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/ShrinkToFitCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/ShrinkToFitCheck.cpp Thu Apr 21 11:57:56 2016
@@ -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(
           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")),

Modified: clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp?rev=267003&r1=267002&r2=267003&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/ContainerSizeEmptyCheck.cpp Thu Apr 21 11:57:56 2016
@@ -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::registerMa
   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::registerMa
 
   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"),

Modified: clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp?rev=267003&r1=267002&r2=267003&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/ImplicitBoolCastCheck.cpp Thu Apr 21 11:57:56 2016
@@ -19,16 +19,10 @@ namespace tidy {
 
 namespace {
 
-const internal::VariadicDynCastAllOfMatcher<Stmt, ParenExpr> 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 createImplicitCastFromB
             allOf(anyOf(hasCastKind(CK_NullToPointer),
                         hasCastKind(CK_NullToMemberPointer)),
                   hasSourceExpression(cxxBoolLiteral()))),
-      hasSourceExpression(expr(hasType(qualType(isBool())))));
+      hasSourceExpression(expr(hasType(qualType(booleanType())))));
 }
 
 StringRef

Modified: clang-tools-extra/trunk/clang-tidy/utils/Matchers.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/Matchers.h?rev=267003&r1=267002&r2=267003&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/Matchers.h (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/Matchers.h Thu Apr 21 11:57:56 2016
@@ -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<bool> IsExpensive =
       type_traits::isExpensiveToCopy(Node, Finder->getASTContext());




More information about the cfe-commits mailing list