adamcz created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
adamcz requested review of this revision.

This fixes a crash when declaring a destructor with a wrong name, then
writing result to pch file and loading it again. The PCH storage uses
DeclarationNameKey as key and it is the same key for both the invalid
destructor and the implicit one that was created because the other one
was invalid. When querying for the Foo::~Foo we end up getting
Foo::~Bar, which is then rejected and we end up with nullptr in
CXXRecordDecl::GetDestructor().

Fixes https://bugs.llvm.org/show_bug.cgi?id=47270


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D86624

Files:
  clang/lib/AST/DeclBase.cpp
  clang/test/PCH/cxx-invalid-destructor.cpp
  clang/test/PCH/cxx-invalid-destructor.h


Index: clang/test/PCH/cxx-invalid-destructor.h
===================================================================
--- /dev/null
+++ clang/test/PCH/cxx-invalid-destructor.h
@@ -0,0 +1,14 @@
+class Base {
+public:
+  ~Base();
+};
+
+class Bar {
+public:
+  ~Bar();
+};
+
+class Foo : public Base {
+public:
+  ~Bar();
+};
Index: clang/test/PCH/cxx-invalid-destructor.cpp
===================================================================
--- /dev/null
+++ clang/test/PCH/cxx-invalid-destructor.cpp
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -x c++ -std=c++11 -emit-pch -o %t 
%S/cxx-invalid-destructor.h -fallow-pch-with-compiler-errors
+// RUN: %clang_cc1 -x c++ -std=c++11 -include-pch %t 
%S/cxx-invalid-destructor.cpp -fsyntax-only -fno-validate-pch
+
+Foo f;
Index: clang/lib/AST/DeclBase.cpp
===================================================================
--- clang/lib/AST/DeclBase.cpp
+++ clang/lib/AST/DeclBase.cpp
@@ -1486,6 +1486,8 @@
   if (auto *FD = dyn_cast<FunctionDecl>(D))
     if (FD->isFunctionTemplateSpecialization())
       return true;
+  if (isa<CXXDestructorDecl>(D) && D->isInvalidDecl())
+    return true;
 
   return false;
 }


Index: clang/test/PCH/cxx-invalid-destructor.h
===================================================================
--- /dev/null
+++ clang/test/PCH/cxx-invalid-destructor.h
@@ -0,0 +1,14 @@
+class Base {
+public:
+  ~Base();
+};
+
+class Bar {
+public:
+  ~Bar();
+};
+
+class Foo : public Base {
+public:
+  ~Bar();
+};
Index: clang/test/PCH/cxx-invalid-destructor.cpp
===================================================================
--- /dev/null
+++ clang/test/PCH/cxx-invalid-destructor.cpp
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -x c++ -std=c++11 -emit-pch -o %t %S/cxx-invalid-destructor.h -fallow-pch-with-compiler-errors
+// RUN: %clang_cc1 -x c++ -std=c++11 -include-pch %t %S/cxx-invalid-destructor.cpp -fsyntax-only -fno-validate-pch
+
+Foo f;
Index: clang/lib/AST/DeclBase.cpp
===================================================================
--- clang/lib/AST/DeclBase.cpp
+++ clang/lib/AST/DeclBase.cpp
@@ -1486,6 +1486,8 @@
   if (auto *FD = dyn_cast<FunctionDecl>(D))
     if (FD->isFunctionTemplateSpecialization())
       return true;
+  if (isa<CXXDestructorDecl>(D) && D->isInvalidDecl())
+    return true;
 
   return false;
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to