[clang-tools-extra] r247489 - [clang-tidy] misc-sizeof-container: whitelist std::bitset<>.

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 11 15:54:44 PDT 2015


Author: alexfh
Date: Fri Sep 11 17:54:44 2015
New Revision: 247489

URL: http://llvm.org/viewvc/llvm-project?rev=247489&view=rev
Log:
[clang-tidy] misc-sizeof-container: whitelist std::bitset<>.

It's fine to use sizeof on std::bitset<>, since it doesn't have any external
storage, everything's inside.

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

Modified: clang-tools-extra/trunk/clang-tidy/misc/SizeofContainerCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/SizeofContainerCheck.cpp?rev=247489&r1=247488&r2=247489&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/SizeofContainerCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/SizeofContainerCheck.cpp Fri Sep 11 17:54:44 2015
@@ -36,6 +36,7 @@ void SizeofContainerCheck::registerMatch
       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"),

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-sizeof-container.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-sizeof-container.cpp?rev=247489&r1=247488&r2=247489&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-sizeof-container.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-sizeof-container.cpp Fri Sep 11 17:54:44 2015
@@ -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];




More information about the cfe-commits mailing list