[clang] [clang]improve diagnosing redefined defaulted constructor with different exception specs (PR #69688)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 24 06:40:24 PDT 2023


https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/69688

>From 2629b346123f9838a4fc3d8b6fb6a98508773965 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Fri, 20 Oct 2023 15:45:42 +0800
Subject: [PATCH 1/2] [clang]improve diagnosing redefined defaulted constructor
 with different exception specs

It is misleading when diagnosing 'ExplicitlySpecialMethod' is missing exception specification 'noexcept' for
```c++
struct ExplicitlySpecialMethod {
  ExplicitlySpecialMethod() = default;
};
ExplicitlySpecialMethod::ExplicitlySpecialMethod() {}
```
Fixes: #69094
---
 clang/docs/ReleaseNotes.rst                   |  3 +++
 clang/lib/Sema/SemaDecl.cpp                   | 25 ++++++++++---------
 ...agnoise-prioritiy-exception-redefining.cpp |  9 +++++++
 3 files changed, 25 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/SemaCXX/diagnoise-prioritiy-exception-redefining.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fc8caf9221b9d29..ae995a12cf0fd7d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -325,6 +325,9 @@ Improvements to Clang's diagnostics
       |               ~~~~~~~~~^~~~~~~~
 - Clang now always diagnoses when using non-standard layout types in ``offsetof`` .
   (`#64619: <https://github.com/llvm/llvm-project/issues/64619>`_)
+- Clang now diagnoses redefined defaulted constructor when redefined
+  defaulted constructor with different exception specs.
+  (`#69094: <https://github.com/llvm/llvm-project/issues/69094>`_)
 
 Bug Fixes in This Version
 -------------------------
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b363b0db79f164d..c4979b51e68f5e2 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -3922,18 +3922,6 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, Scope *S,
   }
 
   if (getLangOpts().CPlusPlus) {
-    // C++1z [over.load]p2
-    //   Certain function declarations cannot be overloaded:
-    //     -- Function declarations that differ only in the return type,
-    //        the exception specification, or both cannot be overloaded.
-
-    // Check the exception specifications match. This may recompute the type of
-    // both Old and New if it resolved exception specifications, so grab the
-    // types again after this. Because this updates the type, we do this before
-    // any of the other checks below, which may update the "de facto" NewQType
-    // but do not necessarily update the type of New.
-    if (CheckEquivalentExceptionSpec(Old, New))
-      return true;
     OldQType = Context.getCanonicalType(Old->getType());
     NewQType = Context.getCanonicalType(New->getType());
 
@@ -4055,6 +4043,19 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, Scope *S,
       }
     }
 
+    // C++1z [over.load]p2
+    //   Certain function declarations cannot be overloaded:
+    //     -- Function declarations that differ only in the return type,
+    //        the exception specification, or both cannot be overloaded.
+
+    // Check the exception specifications match. This may recompute the type of
+    // both Old and New if it resolved exception specifications, so grab the
+    // types again after this. Because this updates the type, we do this before
+    // any of the other checks below, which may update the "de facto" NewQType
+    // but do not necessarily update the type of New.
+    if (CheckEquivalentExceptionSpec(Old, New))
+      return true;
+
     // C++11 [dcl.attr.noreturn]p1:
     //   The first declaration of a function shall specify the noreturn
     //   attribute if any declaration of that function specifies the noreturn
diff --git a/clang/test/SemaCXX/diagnoise-prioritiy-exception-redefining.cpp b/clang/test/SemaCXX/diagnoise-prioritiy-exception-redefining.cpp
new file mode 100644
index 000000000000000..ad025bf041ce519
--- /dev/null
+++ b/clang/test/SemaCXX/diagnoise-prioritiy-exception-redefining.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -fcxx-exceptions -std=c++11 %s
+
+struct ExplicitlySpecialMethod {
+  ExplicitlySpecialMethod() = default;
+};
+ExplicitlySpecialMethod::ExplicitlySpecialMethod() {} // expected-error{{definition of explicitly defaulted default constructor}}
+
+struct ImplicitlySpecialMethod {};
+ImplicitlySpecialMethod::ImplicitlySpecialMethod() {} // expected-error{{definition of implicitly declared default constructor}}

>From a58d6f45d5c4e09e655fd9d4d6111a9605b8a434 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sun, 22 Oct 2023 00:14:02 +0800
Subject: [PATCH 2/2] fix typo

---
 ...redefining.cpp => diagnose-prioritiy-exception-redefining.cpp} | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename clang/test/SemaCXX/{diagnoise-prioritiy-exception-redefining.cpp => diagnose-prioritiy-exception-redefining.cpp} (100%)

diff --git a/clang/test/SemaCXX/diagnoise-prioritiy-exception-redefining.cpp b/clang/test/SemaCXX/diagnose-prioritiy-exception-redefining.cpp
similarity index 100%
rename from clang/test/SemaCXX/diagnoise-prioritiy-exception-redefining.cpp
rename to clang/test/SemaCXX/diagnose-prioritiy-exception-redefining.cpp



More information about the cfe-commits mailing list