[clang] 60f8266 - [C++20] [Modules] Namespace Declaration shouldn't have module linkage

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 7 21:54:41 PST 2021


Author: Chuanqi Xu
Date: 2021-12-08T13:54:04+08:00
New Revision: 60f826663d86e9bdf7adef9a8f1b2fc398e07f2a

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

LOG: [C++20] [Modules] Namespace Declaration shouldn't have module linkage

According to [basic.namespace.general]/p2, a namespace declaration
shouldn't have a module linkage.
> A namespace is never attached to a named module and never has a name
> with module linkage.

Without this patch, the compiler would crash for the test in assertion
enabled build due to inconsistent linkage for redeclaration for
namespaces.

Reviewed by: rsmith

Differential Revision: https://reviews.llvm.org/D115132

Added: 
    clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.cppm
    clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.h
    clang/test/CXX/basic/basic.namespace/basic.namespace.general/p2.cppm

Modified: 
    clang/lib/AST/Decl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 68dfef248f65a..c033563592d28 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -604,8 +604,14 @@ static LinkageInfo getExternalLinkageFor(const NamedDecl *D) {
   //   - A name declared at namespace scope that does not have internal linkage
   //     by the previous rules and that is introduced by a non-exported
   //     declaration has module linkage.
-  if (isInModulePurview(D) && !isExportedFromModuleInterfaceUnit(
-                                  cast<NamedDecl>(D->getCanonicalDecl())))
+  //
+  // [basic.namespace.general]/p2
+  //   A namespace is never attached to a named module and never has a name with
+  //   module linkage.
+  if (isInModulePurview(D) &&
+      !isExportedFromModuleInterfaceUnit(
+          cast<NamedDecl>(D->getCanonicalDecl())) &&
+      !isa<NamespaceDecl>(D))
     return LinkageInfo(ModuleLinkage, DefaultVisibility, false);
 
   return LinkageInfo::external();

diff  --git a/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.cppm b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.cppm
new file mode 100644
index 0000000000000..4266a4b510a49
--- /dev/null
+++ b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.cppm
@@ -0,0 +1,7 @@
+module;
+#include "p2.h"
+export module Y;
+export namespace foo {
+// We need to export something at least
+void print();
+}

diff  --git a/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.h b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.h
new file mode 100644
index 0000000000000..c1d86b942074c
--- /dev/null
+++ b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/Inputs/p2.h
@@ -0,0 +1,7 @@
+#ifndef P2_H
+#define P2_H
+namespace foo {
+namespace bar {
+}
+} // namespace foo
+#endif

diff  --git a/clang/test/CXX/basic/basic.namespace/basic.namespace.general/p2.cppm b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/p2.cppm
new file mode 100644
index 0000000000000..86c67452aa148
--- /dev/null
+++ b/clang/test/CXX/basic/basic.namespace/basic.namespace.general/p2.cppm
@@ -0,0 +1,14 @@
+// Check that the compiler wouldn't crash due to inconsistent namesapce linkage
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: %clang -std=c++20 %S/Inputs/p2.cppm --precompile -o %t/Y.pcm
+// RUN: %clang -std=c++20 -fprebuilt-module-path=%t -I%S/Inputs %s -c -Xclang -verify
+// expected-no-diagnostics
+export module X;
+import Y;
+
+export namespace foo {
+namespace bar{
+    void baz();
+}
+}


        


More information about the cfe-commits mailing list