[PATCH] D129748: [C++20] [Modules] Warn for the use of preferred_name in modules

Chuanqi Xu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 21 02:53:52 PDT 2022


ChuanqiXu updated this revision to Diff 446404.
ChuanqiXu retitled this revision from "[Modules] Disable preferred_name attribute in C++20 Modules" to "[C++20] [Modules] Warn for the use of preferred_name in modules".
ChuanqiXu edited the summary of this revision.
ChuanqiXu added a comment.

Don't try to skip the problem. Warns for it instead.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D129748/new/

https://reviews.llvm.org/D129748

Files:
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Modules/preferred_name.cppm


Index: clang/test/Modules/preferred_name.cppm
===================================================================
--- /dev/null
+++ clang/test/Modules/preferred_name.cppm
@@ -0,0 +1,43 @@
+// Tests that the ODR check wouldn't produce false-positive result for preferred_name attribute.
+//
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm -verify
+// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm -DDISABLE_PREFERRED_NAME
+// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t -I%t %t/Use.cppm -verify -fsyntax-only -DDISABLE_PREFERRED_NAME
+//
+
+//--- foo.h
+template<class _CharT>
+class foo_templ;
+
+typedef foo_templ<char> foo;
+
+template<class _CharT>
+class
+#ifndef DISABLE_PREFERRED_NAME
+__attribute__((__preferred_name__(foo)))
+#endif
+foo_templ {
+public:
+    foo_templ() {}
+};
+
+inline foo_templ<char> bar()
+{
+  return foo_templ<char>();
+}
+
+//--- A.cppm
+module;
+#include "foo.h" // expected-warning at foo.h:11 {{preferred_name in modules may cause incorrect ODR checks now. Considering to not use it}}
+export module A;
+
+//--- Use.cppm
+// expected-no-diagnostics
+module;
+#include "foo.h"
+export module Use;
+import A;
Index: clang/lib/Sema/SemaDeclAttr.cpp
===================================================================
--- clang/lib/Sema/SemaDeclAttr.cpp
+++ clang/lib/Sema/SemaDeclAttr.cpp
@@ -1424,6 +1424,11 @@
 }
 
 static void handlePreferredName(Sema &S, Decl *D, const ParsedAttr &AL) {
+  // FIXME: See https://github.com/llvm/llvm-project/issues/56490 for
+  // details. Remove this one when we fix it actually.
+  if (S.getLangOpts().CPlusPlusModules && D->getOwningModule())
+    S.Diag(D->getLocation(), clang::diag::warn_preferred_name_in_modules);
+
   auto *RD = cast<CXXRecordDecl>(D);
   ClassTemplateDecl *CTD = RD->getDescribedClassTemplate();
   assert(CTD && "attribute does not appertain to this declaration");
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -11196,6 +11196,10 @@
   "'%0' included multiple times, additional include site in header from module '%1'">;
 def note_redefinition_include_same_file : Note<
   "'%0' included multiple times, additional include site here">;
+
+def warn_preferred_name_in_modules : Warning<
+  "preferred_name in modules may cause incorrect ODR checks now. Considering to not use it">,
+  InGroup<UnsupportedAttrInModule>;
 }
 
 let CategoryName = "Coroutines Issue" in {
Index: clang/include/clang/Basic/DiagnosticGroups.td
===================================================================
--- clang/include/clang/Basic/DiagnosticGroups.td
+++ clang/include/clang/Basic/DiagnosticGroups.td
@@ -442,6 +442,7 @@
 def IncompleteModule : DiagGroup<"incomplete-module",
     [IncompleteUmbrella, NonModularIncludeInModule]>;
 def PrivateModule : DiagGroup<"private-module">;
+def UnsupportedAttrInModule : DiagGroup<"unsupported-attr-in-module">;
 
 def CXX11InlineNamespace : DiagGroup<"c++11-inline-namespace">;
 def InlineNamespaceReopenedNoninline


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129748.446404.patch
Type: text/x-patch
Size: 3260 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220721/d61db522/attachment-0001.bin>


More information about the cfe-commits mailing list