[PATCH] D153003: [ODRHash] Fix ODR hashing of template names

Jonas Hahnfeld via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 15 02:00:04 PDT 2023


Hahnfeld created this revision.
Hahnfeld added reviewers: rtrieu, vsapsai, ChuanqiXu, Bigcheese.
Herald added a project: All.
Hahnfeld requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

For hashing, we should not differentiate between templates and qualified
templates and only look at the underlying name. As an example, consider
the added test case of a template template parameter; the two modules
have slightly differing `using` declarations (`using C = B<A>` vs `B<NS::A>`)
which should be treated as identical.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153003

Files:
  clang/lib/AST/ODRHash.cpp
  clang/test/Modules/merge-template-template-parameters.cppm


Index: clang/test/Modules/merge-template-template-parameters.cppm
===================================================================
--- /dev/null
+++ clang/test/Modules/merge-template-template-parameters.cppm
@@ -0,0 +1,44 @@
+// RUN: rm -rf %t
+// RUN: mkdir %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface -I%t %t/module1.cppm -o %t/module1.pcm
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface -I%t %t/module2.cppm -o %t/module2.pcm
+// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/merge.cpp -verify -fsyntax-only
+
+//--- header.h
+namespace NS {
+template <int I>
+class A {
+};
+
+template <template <int I_> class T>
+class B {
+};
+}
+
+//--- module1.cppm
+// inside NS, using C = B<A>
+module;
+export module module1;
+#include "header.h"
+namespace NS {
+using C = B<A>;
+}
+export struct D : NS::C {};
+
+//--- module2.cppm
+// inside NS, using C = B<NS::A>
+module;
+export module module2;
+#include "header.h"
+namespace NS {
+using C = B<NS::A>;
+}
+export struct D : NS::C {};
+
+//--- merge.cpp
+// expected-no-diagnostics
+import module1;
+import module2;
+D d;
Index: clang/lib/AST/ODRHash.cpp
===================================================================
--- clang/lib/AST/ODRHash.cpp
+++ clang/lib/AST/ODRHash.cpp
@@ -139,23 +139,8 @@
 }
 
 void ODRHash::AddTemplateName(TemplateName Name) {
-  auto Kind = Name.getKind();
-  ID.AddInteger(Kind);
-
-  switch (Kind) {
-  case TemplateName::Template:
-    AddDecl(Name.getAsTemplateDecl());
-    break;
-  // TODO: Support these cases.
-  case TemplateName::OverloadedTemplate:
-  case TemplateName::AssumedTemplate:
-  case TemplateName::QualifiedTemplate:
-  case TemplateName::DependentTemplate:
-  case TemplateName::SubstTemplateTemplateParm:
-  case TemplateName::SubstTemplateTemplateParmPack:
-  case TemplateName::UsingTemplate:
-    break;
-  }
+  if (auto *TD = Name.getAsTemplateDecl())
+    AddDecl(TD);
 }
 
 void ODRHash::AddTemplateArgument(TemplateArgument TA) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153003.531649.patch
Type: text/x-patch
Size: 2011 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230615/066d6587/attachment.bin>


More information about the cfe-commits mailing list