[PATCH] D91303: Simplify implementation of container-size-empty

Stephen Kelly via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 15:14:48 PST 2020


steveire created this revision.
steveire added a reviewer: alexfh.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
steveire requested review of this revision.

Use IgnoreUnlessSpelledInSource to make the matcher code smaller and
more visibly-related to the code.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91303

Files:
  clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
  clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h


Index: clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h
+++ clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h
@@ -33,6 +33,9 @@
   }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
+    return TK_IgnoreUnlessSpelledInSource;
+  }
 };
 
 } // namespace readability
Index: clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -16,6 +16,11 @@
 using namespace clang::ast_matchers;
 
 namespace clang {
+namespace ast_matchers {
+AST_MATCHER(CXXConstructExpr, isDefaultConstruction) {
+  return Node.getConstructor()->isDefaultConstructor();
+}
+} // namespace ast_matchers
 namespace tidy {
 namespace readability {
 
@@ -48,27 +53,15 @@
   const auto ValidContainer = qualType(
       anyOf(ValidContainerNonTemplateType, ValidContainerTemplateType));
 
-  const auto WrongUse = traverse(
-      ast_type_traits::TK_AsIs,
-      anyOf(
-          hasParent(binaryOperator(isComparisonOperator(),
-                                   hasEitherOperand(ignoringImpCasts(
-                                       anyOf(integerLiteral(equals(1)),
-                                             integerLiteral(equals(0))))))
-                        .bind("SizeBinaryOp")),
-          hasParent(implicitCastExpr(
-              hasImplicitDestinationType(booleanType()),
-              anyOf(hasParent(
-                        unaryOperator(hasOperatorName("!")).bind("NegOnSize")),
-                    anything()))),
-          hasParent(explicitCastExpr(hasDestinationType(booleanType()))),
-          hasParent(ifStmt()), hasParent(whileStmt()),
-          hasParent(binaryOperator(hasAnyOperatorName("&&", "||"))),
-          hasParent(unaryOperator(hasOperatorName("!")).bind("NegOnSize"))));
+  const auto WrongUse = anyOf(
+      hasParent(ifStmt()), hasParent(whileStmt()),
+      hasParent(binaryOperator(isComparisonOperator()).bind("SizeBinaryOp")),
+      hasParent(binaryOperator(hasAnyOperatorName("&&", "||"))),
+      hasParent(unaryOperator(hasOperatorName("!")).bind("NegOnSize")),
+      hasParent(explicitCastExpr(hasDestinationType(booleanType()))));
 
   Finder->addMatcher(
-      cxxMemberCallExpr(unless(isInTemplateInstantiation()),
-                        on(expr(anyOf(hasType(ValidContainer),
+      cxxMemberCallExpr(on(expr(anyOf(hasType(ValidContainer),
                                       hasType(pointsTo(ValidContainer)),
                                       hasType(references(ValidContainer))))
                                .bind("MemberCallObject")),
@@ -92,19 +85,10 @@
           .bind("SizeCallExpr"),
       this);
 
-  // Empty constructor matcher.
-  const auto DefaultConstructor = cxxConstructExpr(
-          hasDeclaration(cxxConstructorDecl(isDefaultConstructor())));
   // Comparison to empty string or empty constructor.
   const auto WrongComparend = anyOf(
-      ignoringImpCasts(stringLiteral(hasSize(0))),
-      ignoringImpCasts(cxxBindTemporaryExpr(has(DefaultConstructor))),
-      ignoringImplicit(DefaultConstructor),
-      cxxConstructExpr(hasDeclaration(cxxConstructorDecl(isCopyConstructor())),
-                       has(expr(ignoringImpCasts(DefaultConstructor)))),
-      cxxConstructExpr(hasDeclaration(cxxConstructorDecl(isMoveConstructor())),
-                       has(expr(ignoringImpCasts(DefaultConstructor)))),
-      cxxUnresolvedConstructExpr(argummentCountIs(0)));
+      stringLiteral(hasSize(0)), cxxConstructExpr(isDefaultConstruction()),
+      cxxUnresolvedConstructExpr(argumentCountIs(0)));
   // Match the object being compared.
   const auto STLArg =
       anyOf(unaryOperator(
@@ -114,7 +98,6 @@
             expr(hasType(ValidContainer)).bind("STLObject"));
   Finder->addMatcher(
       cxxOperatorCallExpr(
-          unless(isInTemplateInstantiation()),
           hasAnyOverloadedOperatorName("==", "!="),
           anyOf(allOf(hasArgument(0, WrongComparend), hasArgument(1, STLArg)),
                 allOf(hasArgument(0, STLArg), hasArgument(1, WrongComparend))),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91303.304669.patch
Type: text/x-patch
Size: 4545 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201111/59383722/attachment-0001.bin>


More information about the cfe-commits mailing list