[clang-tools-extra] r266862 - Fix a crash in cppcoreguidelines-pro-type-member-init related to missing constructor bodies.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 20 01:29:10 PDT 2016


Author: hokein
Date: Wed Apr 20 03:29:08 2016
New Revision: 266862

URL: http://llvm.org/viewvc/llvm-project?rev=266862&view=rev
Log:
Fix a crash in cppcoreguidelines-pro-type-member-init related to missing constructor bodies.

Summary: Fixes a crash in cppcoreguidelines-pro-type-member-init when checking some record types with a constructor without a body. We now check to make sure the constructor has a body before looking for missing members and base initializers.

Patch by Michael Miller!

Reviewers: aaron.ballman, alexfh, hokein

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D19270

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=266862&r1=266861&r2=266862&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp Wed Apr 20 03:29:08 2016
@@ -285,6 +285,9 @@ void ProTypeMemberInitCheck::registerMat
 
 void ProTypeMemberInitCheck::check(const MatchFinder::MatchResult &Result) {
   if (const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor")) {
+    // Skip declarations delayed by late template parsing without a body.
+    if (!Ctor->getBody())
+      return;
     checkMissingMemberInitializer(*Result.Context, Ctor);
     checkMissingBaseClassInitializer(*Result.Context, Ctor);
   } else if (const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var")) {
@@ -304,11 +307,6 @@ void ProTypeMemberInitCheck::checkMissin
   if (IsUnion && ClassDecl->hasInClassInitializer())
     return;
 
-  // Skip declarations delayed by late template parsing without a body.
-  const Stmt *Body = Ctor->getBody();
-  if (!Body)
-    return;
-
   SmallPtrSet<const FieldDecl *, 16> FieldsToInit;
   fieldsRequiringInit(ClassDecl->fields(), Context, FieldsToInit);
   if (FieldsToInit.empty())
@@ -323,7 +321,7 @@ void ProTypeMemberInitCheck::checkMissin
       FieldsToInit.erase(Init->getMember());
     }
   }
-  removeFieldsInitializedInBody(*Body, Context, FieldsToInit);
+  removeFieldsInitializedInBody(*Ctor->getBody(), Context, FieldsToInit);
 
   // Collect all fields in order, both direct fields and indirect fields from
   // anonmyous record types.

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=266862&r1=266861&r2=266862&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 Wed Apr 20 03:29:08 2016
@@ -331,3 +331,10 @@ struct PositiveAnonymousUnionAndStruct {
   int X;
   // CHECK-FIXES: int X{};
 };
+
+// This check results in a CXXConstructorDecl with no body.
+struct NegativeDeletedConstructor : NegativeAggregateType {
+  NegativeDeletedConstructor() = delete;
+
+  Template<int> F;
+};




More information about the cfe-commits mailing list