[PATCH] D135254: [clang][Sema] Fix crash on invalid base destructor
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 5 06:22:46 PDT 2022
kadircet updated this revision to Diff 465365.
kadircet marked 2 inline comments as done.
kadircet added a comment.
-address comments
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D135254/new/
https://reviews.llvm.org/D135254
Files:
clang/lib/Sema/SemaDeclCXX.cpp
clang/lib/Sema/SemaLookup.cpp
clang/test/SemaCXX/destructor.cpp
Index: clang/test/SemaCXX/destructor.cpp
===================================================================
--- clang/test/SemaCXX/destructor.cpp
+++ clang/test/SemaCXX/destructor.cpp
@@ -550,4 +550,20 @@
template<typename T> void f(T *p) { p->~U(); } // expected-error {{ambiguous}}
}
+namespace crash_on_invalid_base_dtor {
+struct Test {
+ virtual ~Test();
+};
+struct Baz : public Test { // expected-warning {{non-virtual destructor}}
+ Baz() {}
+ ~Baz() = defaul; // expected-error {{undeclared identifier 'defaul'}} \
+ // expected-error {{initializer on function}} \
+ // expected-note {{overridden virtual function is here}}
+};
+struct Foo : public Baz { // expected-error {{cannot override a non-deleted function}} \
+ // expected-note {{destructor of 'Foo' is implicitly deleted}}
+ Foo() {}
+};
+}
+
#endif // BE_THE_HEADER
Index: clang/lib/Sema/SemaLookup.cpp
===================================================================
--- clang/lib/Sema/SemaLookup.cpp
+++ clang/lib/Sema/SemaLookup.cpp
@@ -40,6 +40,7 @@
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/ADT/edit_distance.h"
+#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include <algorithm>
#include <iterator>
@@ -3616,9 +3617,10 @@
///
/// \returns The destructor for this class.
CXXDestructorDecl *Sema::LookupDestructor(CXXRecordDecl *Class) {
- return cast<CXXDestructorDecl>(LookupSpecialMember(Class, CXXDestructor,
- false, false, false,
- false, false).getMethod());
+ return cast_or_null<CXXDestructorDecl>(
+ LookupSpecialMember(Class, CXXDestructor, false, false, false, false,
+ false)
+ .getMethod());
}
/// LookupLiteralOperator - Determine which literal operator should be used for
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -17,6 +17,7 @@
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/CharUnits.h"
#include "clang/AST/ComparisonCategories.h"
+#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/EvaluatedExprVisitor.h"
#include "clang/AST/ExprCXX.h"
@@ -5650,7 +5651,9 @@
continue;
CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl);
- assert(Dtor && "No dtor found for FieldClassDecl!");
+ // Dtor might still be missing, e.g because it's invalid.
+ if (!Dtor)
+ continue;
CheckDestructorAccess(Field->getLocation(), Dtor,
PDiag(diag::err_access_dtor_field)
<< Field->getDeclName()
@@ -5696,7 +5699,9 @@
continue;
CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl);
- assert(Dtor && "No dtor found for BaseClassDecl!");
+ // Dtor might still be missing, e.g because it's invalid.
+ if (!Dtor)
+ continue;
// FIXME: caret should be on the start of the class name
CheckDestructorAccess(Base.getBeginLoc(), Dtor,
@@ -5733,7 +5738,9 @@
continue;
CXXDestructorDecl *Dtor = LookupDestructor(BaseClassDecl);
- assert(Dtor && "No dtor found for BaseClassDecl!");
+ // Dtor might still be missing, e.g because it's invalid.
+ if (!Dtor)
+ continue;
if (CheckDestructorAccess(
ClassDecl->getLocation(), Dtor,
PDiag(diag::err_access_dtor_vbase)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135254.465365.patch
Type: text/x-patch
Size: 3613 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221005/88cbf7f6/attachment-0001.bin>
More information about the cfe-commits
mailing list