[clang-tidy] Extend readability-container-size-empty to arbitrary class with size() and empty()

This patch extends readability-container-size-empty check allowing it to produce
warnings not only for STL containers, but also for containers, which provide two
functions matching following signatures:

* `size_type size() const;`
* `bool empty() const;`

Where `size_type` can be any kind of integer type.

This functionality was proposed in https://llvm.org/bugs/show_bug.cgi?id=26823
by Eugene Zelenko.

Approval: alexfh

Reviewers: alexfh, aaron.ballman, Eugene.Zelenko

Subscribers: etienneb, Prazek, hokein, xazax.hun, cfe-commits

Differential Revision: https://reviews.llvm.org/D24349

llvm-svn: 281307
diff --git a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
index b0c6fb5..0894c78 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.cpp
@@ -7,11 +7,11 @@
 //
 //===----------------------------------------------------------------------===//
 #include "ContainerSizeEmptyCheck.h"
+#include "../utils/Matchers.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
 #include "clang/Lex/Lexer.h"
 #include "llvm/ADT/StringRef.h"
-#include "../utils/Matchers.h"
 
 using namespace clang::ast_matchers;
 
@@ -29,11 +29,16 @@
   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 validContainer = cxxRecordDecl(isSameOrDerivedFrom(
+      namedDecl(
+          has(cxxMethodDecl(
+                  isConst(), parameterCountIs(0), isPublic(), hasName("size"),
+                  returns(qualType(isInteger(), unless(booleanType()))))
+                  .bind("size")),
+          has(cxxMethodDecl(isConst(), parameterCountIs(0), isPublic(),
+                            hasName("empty"), returns(booleanType()))
+                  .bind("empty")))
+          .bind("container")));
 
   const auto WrongUse = anyOf(
       hasParent(binaryOperator(
@@ -49,12 +54,11 @@
       hasParent(explicitCastExpr(hasDestinationType(booleanType()))));
 
   Finder->addMatcher(
-      cxxMemberCallExpr(
-          on(expr(anyOf(hasType(namedDecl(stlContainer)),
-                        hasType(pointsTo(namedDecl(stlContainer))),
-                        hasType(references(namedDecl(stlContainer)))))
-                 .bind("STLObject")),
-          callee(cxxMethodDecl(hasName("size"))), WrongUse)
+      cxxMemberCallExpr(on(expr(anyOf(hasType(validContainer),
+                                      hasType(pointsTo(validContainer)),
+                                      hasType(references(validContainer))))
+                               .bind("STLObject")),
+                        callee(cxxMethodDecl(hasName("size"))), WrongUse)
           .bind("SizeCallExpr"),
       this);
 }
@@ -142,9 +146,17 @@
       Hint = FixItHint::CreateReplacement(MemberCall->getSourceRange(),
                                           "!" + ReplacementText);
   }
+
   diag(MemberCall->getLocStart(), "the 'empty' method should be used to check "
                                   "for emptiness instead of 'size'")
       << Hint;
+
+  const auto *Container = Result.Nodes.getNodeAs<NamedDecl>("container");
+  const auto *Empty = Result.Nodes.getNodeAs<FunctionDecl>("empty");
+
+  diag(Empty->getLocation(), "method %0::empty() defined here",
+       DiagnosticIDs::Note)
+      << Container;
 }
 
 } // namespace readability