[clang] [clang][Sema] deleted overriding function can have lax except spec (PR #76248)

Sirui Mu via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 22 08:10:42 PST 2023


https://github.com/Lancern created https://github.com/llvm/llvm-project/pull/76248

According to [CWG1351](https://cplusplus.github.io/CWG/issues/1351.html), overriding functions that are defined as deleted can have more lax exception specifications compared to the base version. For example, the following code should compile:

```cpp
struct B {
  virtual void h() noexcept = delete;
};

struct D: B {
  void h() override = delete;  // Should be OK
};
```

Clang incorrectly reports that the exception specification of `D::h` is more lax than base version. This patch fixes this.

>From c219e38a7953b5bd494554760043053ae3b8ff60 Mon Sep 17 00:00:00 2001
From: Sirui Mu <msrlancern at gmail.com>
Date: Sat, 23 Dec 2023 00:02:08 +0800
Subject: [PATCH] [clang][Sema] deleted overriding function can have lax except
 spec

---
 clang/lib/Sema/SemaExceptionSpec.cpp             | 5 +++++
 clang/test/CXX/except/except.spec/p5-virtual.cpp | 4 ++++
 2 files changed, 9 insertions(+)

diff --git a/clang/lib/Sema/SemaExceptionSpec.cpp b/clang/lib/Sema/SemaExceptionSpec.cpp
index 75730ea888afb4..f8f9c9d491a18e 100644
--- a/clang/lib/Sema/SemaExceptionSpec.cpp
+++ b/clang/lib/Sema/SemaExceptionSpec.cpp
@@ -979,6 +979,11 @@ bool Sema::CheckOverridingFunctionExceptionSpec(const CXXMethodDecl *New,
   if (isa<CXXDestructorDecl>(New) && New->getParent()->isDependentType())
     return false;
 
+  // CWG1351: if either of the old function or the new function is defined as
+  // deleted, we don't need this check.
+  if (Old->isDeleted() || New->isDeleted())
+    return false;
+
   // If the old exception specification hasn't been parsed yet, or the new
   // exception specification can't be computed yet, remember that we need to
   // perform this check when we get to the end of the outermost
diff --git a/clang/test/CXX/except/except.spec/p5-virtual.cpp b/clang/test/CXX/except/except.spec/p5-virtual.cpp
index 69daec6ee53347..2cecd6ce51f698 100644
--- a/clang/test/CXX/except/except.spec/p5-virtual.cpp
+++ b/clang/test/CXX/except/except.spec/p5-virtual.cpp
@@ -45,6 +45,8 @@ struct Base
   virtual void f15();
   virtual void f16();
 
+  virtual void f17() noexcept = delete;
+
   virtual void g1() throw(); // expected-note {{overridden virtual function is here}}
   virtual void g2() throw(int); // expected-note {{overridden virtual function is here}}
   virtual void g3() throw(A); // expected-note {{overridden virtual function is here}}
@@ -81,6 +83,8 @@ struct Derived : Base
   virtual void f15() noexcept;
   virtual void f16() throw();
 
+  virtual void f17() = delete;
+
   virtual void g1() throw(int); // expected-error {{exception specification of overriding function is more lax}}
   virtual void g2(); // expected-error {{exception specification of overriding function is more lax}}
   virtual void g3() throw(D); // expected-error {{exception specification of overriding function is more lax}}



More information about the cfe-commits mailing list