[clang-tidy] misc-sizeof-container: whitelist std::bitset<>.
authorAlexander Kornienko <alexfh@google.com>
Fri, 11 Sep 2015 22:54:44 +0000 (22:54 +0000)
committerAlexander Kornienko <alexfh@google.com>
Fri, 11 Sep 2015 22:54:44 +0000 (22:54 +0000)
It's fine to use sizeof on std::bitset<>, since it doesn't have any external
storage, everything's inside.

llvm-svn: 247489

clang-tools-extra/clang-tidy/misc/SizeofContainerCheck.cpp
clang-tools-extra/test/clang-tidy/misc-sizeof-container.cpp

index 44753b9..2a5d91a 100644 (file)
@@ -36,6 +36,7 @@ void SizeofContainerCheck::registerMatchers(MatchFinder *Finder) {
       expr(unless(isInTemplateInstantiation()),
            expr(sizeOfExpr(has(expr(hasType(hasCanonicalType(hasDeclaration(
                     recordDecl(matchesName("^(::std::|::string)"),
+                               unless(hasName("::std::bitset")),
                                hasMethod(methodDecl(hasName("size"), isPublic(),
                                                     isConst()))))))))))
                .bind("sizeof"),
index 41f5029..9dd113d 100644 (file)
@@ -19,6 +19,12 @@ struct vector {
   size_t size() const;
 };
 
+// std::bitset<> is not a container. sizeof() is reasonable for it.
+template <size_t N>
+struct bitset {
+  size_t size() const;
+};
+
 class fake_container1 {
   size_t size() const; // non-public
 };
@@ -78,9 +84,11 @@ void f() {
 
   std::fake_container1 f1;
   std::fake_container2 f2;
+  std::bitset<7> bs;
 
   a = sizeof(f1);
   a = sizeof(f2);
+  a = sizeof(bs);
 
 
   std::string arr[3];