[clang-tools-extra] r283886 - [clang-tidy] Ignore empty members and bases in cppcoreguidelines-pro-type-member-init

Malcolm Parsons via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 11 07:49:25 PDT 2016


Author: malcolm.parsons
Date: Tue Oct 11 09:49:24 2016
New Revision: 283886

URL: http://llvm.org/viewvc/llvm-project?rev=283886&view=rev
Log:
[clang-tidy] Ignore empty members and bases in cppcoreguidelines-pro-type-member-init

Summary: Empty/incomplete variables/members/bases don't need to be initialized

Reviewers: mgehre, aaron.ballman, alexfh

Subscribers: nemanjai, cfe-commits

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

Modified:
    clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp

Modified: clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp?rev=283886&r1=283885&r2=283886&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp Tue Oct 11 09:49:24 2016
@@ -317,6 +317,28 @@ void ProTypeMemberInitCheck::storeOption
   Options.store(Opts, "IgnoreArrays", IgnoreArrays);
 }
 
+// FIXME: Copied from clang/lib/Sema/SemaDeclCXX.cpp.
+static bool isIncompleteOrZeroLengthArrayType(ASTContext &Context, QualType T) {
+  if (T->isIncompleteArrayType())
+    return true;
+
+  while (const ConstantArrayType *ArrayT = Context.getAsConstantArrayType(T)) {
+    if (!ArrayT->getSize())
+      return true;
+
+    T = ArrayT->getElementType();
+  }
+
+  return false;
+}
+
+static bool isEmpty(ASTContext &Context, const QualType &Type) {
+  if (const CXXRecordDecl *ClassDecl = Type->getAsCXXRecordDecl()) {
+    return ClassDecl->isEmpty();
+  }
+  return isIncompleteOrZeroLengthArrayType(Context, Type);
+}
+
 void ProTypeMemberInitCheck::checkMissingMemberInitializer(
     ASTContext &Context, const CXXRecordDecl &ClassDecl,
     const CXXConstructorDecl *Ctor) {
@@ -330,7 +352,8 @@ void ProTypeMemberInitCheck::checkMissin
   forEachField(ClassDecl, ClassDecl.fields(), false, [&](const FieldDecl *F) {
     if (!F->hasInClassInitializer() &&
         utils::type_traits::isTriviallyDefaultConstructible(F->getType(),
-                                                            Context))
+                                                            Context) &&
+        !isEmpty(Context, F->getType()))
       FieldsToInit.insert(F);
   });
   if (FieldsToInit.empty())

Modified: clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp?rev=283886&r1=283885&r2=283886&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init.cpp Tue Oct 11 09:49:24 2016
@@ -423,3 +423,30 @@ UNINITIALIZED_FIELD_IN_MACRO_BODY_VIRTUA
 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: constructor does not initialize these fields: F
 UNINITIALIZED_FIELD_IN_MACRO_BODY_VIRTUAL(G);
 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: constructor does not initialize these fields: G
+
+struct NegativeEmpty {
+};
+
+static void NegativeEmptyVar() {
+  NegativeEmpty e;
+  (void)e;
+}
+
+struct NegativeEmptyMember {
+  NegativeEmptyMember() {}
+  NegativeEmpty e;
+};
+
+struct NegativeEmptyBase : NegativeEmpty {
+  NegativeEmptyBase() {}
+};
+
+struct NegativeEmptyArrayMember {
+  NegativeEmptyArrayMember() {}
+  char e[0];
+};
+
+struct NegativeIncompleteArrayMember {
+  NegativeIncompleteArrayMember() {}
+  char e[];
+};




More information about the cfe-commits mailing list