[clang-tools-extra] r267700 - Fix a crash in cppcoreguidelines-pro-type-member-init when checking a type with a template parameter as a base class.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 27 05:17:05 PDT 2016


Author: hokein
Date: Wed Apr 27 07:17:04 2016
New Revision: 267700

URL: http://llvm.org/viewvc/llvm-project?rev=267700&view=rev
Log:
Fix a crash in cppcoreguidelines-pro-type-member-init when checking a type with a template parameter as a base class.

Summary: Fixed a crash in cppcoreguidelines-pro-type-member-init when encountering a type that uses one of its template parameters as a base when compiling for C++98.

Patch by Michael Miller!

Reviewers: aaron.ballman, alexfh, hokein

Subscribers: cfe-commits

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

Modified:
    clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init-cxx98.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=267700&r1=267699&r2=267700&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp Wed Apr 27 07:17:04 2016
@@ -208,8 +208,12 @@ computeInsertions(const CXXConstructorDe
 void getInitializationsInOrder(const CXXRecordDecl *ClassDecl,
                                SmallVectorImpl<const NamedDecl *> &Decls) {
   Decls.clear();
-  for (const auto &Base : ClassDecl->bases())
-    Decls.emplace_back(getCanonicalRecordDecl(Base.getType()));
+  for (const auto &Base : ClassDecl->bases()) {
+    // Decl may be null if the base class is a template parameter.
+    if (const NamedDecl *Decl = getCanonicalRecordDecl(Base.getType())) {
+      Decls.emplace_back(Decl);
+    }
+  }
   Decls.append(ClassDecl->fields().begin(), ClassDecl->fields().end());
 }
 

Modified: clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init-cxx98.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init-cxx98.cpp?rev=267700&r1=267699&r2=267700&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init-cxx98.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/cppcoreguidelines-pro-type-member-init-cxx98.cpp Wed Apr 27 07:17:04 2016
@@ -70,26 +70,36 @@ struct NegativeAggregateType {
   int Z;
 };
 
-struct NonTrivialType {
+struct TrivialType {
   int X;
   int Y;
 };
 
 struct PositiveUninitializedBaseOrdering : public NegativeAggregateType,
-                                           public NonTrivialType {
-  PositiveUninitializedBaseOrdering() : NegativeAggregateType(), NonTrivialType(), B() {}
+                                           public TrivialType {
+  PositiveUninitializedBaseOrdering() : NegativeAggregateType(), TrivialType(), B() {}
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these fields: A
-  // CHECK-FIXES: PositiveUninitializedBaseOrdering() : NegativeAggregateType(), NonTrivialType(), A(), B() {}
+  // CHECK-FIXES: PositiveUninitializedBaseOrdering() : NegativeAggregateType(), TrivialType(), A(), B() {}
 
   // This is somewhat pathological with the base class initializer at the end...
-  PositiveUninitializedBaseOrdering(int) : B(), NonTrivialType(), A() {}
+  PositiveUninitializedBaseOrdering(int) : B(), TrivialType(), A() {}
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these bases: NegativeAggregateType
-  // CHECK-FIXES: PositiveUninitializedBaseOrdering(int) : B(), NegativeAggregateType(), NonTrivialType(), A() {}
+  // CHECK-FIXES: PositiveUninitializedBaseOrdering(int) : B(), NegativeAggregateType(), TrivialType(), A() {}
 
   PositiveUninitializedBaseOrdering(float) : NegativeAggregateType(), A() {}
-  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these bases: NonTrivialType
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these bases: TrivialType
   // CHECK-MESSAGES: :[[@LINE-2]]:3: warning: constructor does not initialize these fields: B
-  // CHECK-FIXES: PositiveUninitializedBaseOrdering(float) : NegativeAggregateType(), NonTrivialType(), A(), B() {}
+  // CHECK-FIXES: PositiveUninitializedBaseOrdering(float) : NegativeAggregateType(), TrivialType(), A(), B() {}
 
   int A, B;
 };
+
+template <class T>
+class PositiveTemplateBase : T {
+public:
+  PositiveTemplateBase() {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these fields: X
+  // CHECK-FIXES: PositiveTemplateBase() : X() {}
+
+  int X;
+};




More information about the cfe-commits mailing list