[clang] d0ce367 - [C++20] [Modules] Fix a crash when instantiate hidden friends

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 2 21:39:44 PST 2023


Author: Chuanqi Xu
Date: 2023-01-03T13:37:57+08:00
New Revision: d0ce367a97137e6c2b28f1e18988fd7888bcea0a

URL: https://github.com/llvm/llvm-project/commit/d0ce367a97137e6c2b28f1e18988fd7888bcea0a
DIFF: https://github.com/llvm/llvm-project/commit/d0ce367a97137e6c2b28f1e18988fd7888bcea0a.diff

LOG: [C++20] [Modules] Fix a crash when instantiate hidden friends

Closes https://github.com/llvm/llvm-project/issues/54457.

This removes a FIXME we found previously. But we didn't remove the FIXME
that time due to the lack of the corresponding test. And now we found
the corresponding test so we can remove it.

Added: 
    clang/test/Modules/pr54457.cppm

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a06f409d78d97..0fe0097400823 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -329,6 +329,8 @@ Bug Fixes
 - Fix sanity check when value initializing an empty union so that it takes into
   account anonymous structs which is a GNU extension. This fixes
   `Issue 58800 <https://github.com/llvm/llvm-project/issues/58800>`_
+- Fix an issue that triggers a crash if we instantiate a hidden friend functions.
+  This fixes `Issue 54457 <https://github.com/llvm/llvm-project/issues/54457>`_
 
 Improvements to Clang's diagnostics
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index a05eceac73988..a645ffe1cf291 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6155,9 +6155,8 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
 
       // Move to the outer template scope.
       if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
-        // FIXME: We should use `getNonTransparentDeclContext()` here instead
-        // of `getDeclContext()` once we find the invalid test case.
-        if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
+        if (FD->getFriendObjectKind() &&
+            FD->getNonTransparentDeclContext()->isFileContext()) {
           DC = FD->getLexicalDeclContext();
           continue;
         }

diff  --git a/clang/test/Modules/pr54457.cppm b/clang/test/Modules/pr54457.cppm
new file mode 100644
index 0000000000000..ed67ec1065376
--- /dev/null
+++ b/clang/test/Modules/pr54457.cppm
@@ -0,0 +1,61 @@
+// https://github.com/llvm/llvm-project/issues/54457
+//
+// RUN: rm -rf %t
+// RUN: mkdir %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 %t/A.cppm -verify -S -o -
+// RUN: %clang_cc1 -std=c++20 %t/B.cppm -verify -S -o -
+// RUN: %clang_cc1 -std=c++20 %t/C.cppm -emit-module-interface -o %t/C.pcm
+// RUN: %clang_cc1 -std=c++20 %t/UseC.cppm -fprebuilt-module-path=%t -verify -S -o -
+
+//--- A.cppm
+// expected-no-diagnostics
+export module A;
+
+export template<typename T>
+struct s {
+	friend s f(s) {
+		return s();
+	}
+};
+
+void g() {
+	f(s<int>());
+}
+
+//--- B.cppm
+// expected-no-diagnostics
+export module B;
+
+export template<typename T>
+struct s {
+	friend constexpr auto f(s) -> s {
+		return s();
+	}
+};
+
+void g() {
+	constexpr auto first = f(s<int>());
+}
+
+//--- C.cppm
+// expected-no-diagnostics
+export module C;
+
+export template<typename StandardCharT, int N>
+struct basic_symbol_text {
+  template<int N2>
+  constexpr friend basic_symbol_text operator+(
+    const basic_symbol_text&, const basic_symbol_text<char, N2>&) noexcept
+  {
+    return basic_symbol_text{};
+  }
+};
+
+constexpr auto xxx = basic_symbol_text<char, 1>{} + basic_symbol_text<char, 1>{};
+
+//--- UseC.cppm
+// expected-no-diagnostics
+import C;
+void foo() {}


        


More information about the cfe-commits mailing list