[clang] e166755 - [C++20] [Modules] [Concepts] Recognize same concepts more precisely in Serialization

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 7 23:14:10 PST 2021


Author: Chuanqi Xu
Date: 2021-12-08T15:00:04+08:00
New Revision: e166755a691921612274fdad945d3a6b05d00439

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

LOG: [C++20] [Modules] [Concepts] Recognize same concepts more precisely in Serialization

The compiler would judge two concepts is same by their addresses.
However, when we use modules, the addresses wouldn't be the same all the
time since one is parsed in their TU and another is imported in another
TU.
This patch fixes this by using isSameEntity to judge the two concepts.

Reviewed By: rsmith

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

Added: 
    clang/test/Modules/Inputs/concept/A.cppm
    clang/test/Modules/Inputs/concept/foo.h
    clang/test/Modules/concept.cppm

Modified: 
    clang/lib/Serialization/ASTReaderDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp
index 62a31f299d6bc..2144befcdb149 100644
--- a/clang/lib/Serialization/ASTReaderDecl.cpp
+++ b/clang/lib/Serialization/ASTReaderDecl.cpp
@@ -2948,6 +2948,7 @@ uint64_t ASTReader::getGlobalBitOffset(ModuleFile &M, uint64_t LocalOffset) {
 static bool isSameTemplateParameterList(const ASTContext &C,
                                         const TemplateParameterList *X,
                                         const TemplateParameterList *Y);
+static bool isSameEntity(NamedDecl *X, NamedDecl *Y);
 
 /// Determine whether two template parameters are similar enough
 /// that they may be used in declarations of the same template.
@@ -2967,7 +2968,9 @@ static bool isSameTemplateParameter(const NamedDecl *X,
     if (!TXTC != !TYTC)
       return false;
     if (TXTC && TYTC) {
-      if (TXTC->getNamedConcept() != TYTC->getNamedConcept())
+      auto *NCX = TXTC->getNamedConcept();
+      auto *NCY = TYTC->getNamedConcept();
+      if (!NCX || !NCY || !isSameEntity(NCX, NCY))
         return false;
       if (TXTC->hasExplicitTemplateArgs() != TYTC->hasExplicitTemplateArgs())
         return false;
@@ -3111,11 +3114,12 @@ static bool hasSameOverloadableAttrs(const FunctionDecl *A,
 
 /// Determine whether the two declarations refer to the same entity.
 static bool isSameEntity(NamedDecl *X, NamedDecl *Y) {
-  assert(X->getDeclName() == Y->getDeclName() && "Declaration name mismatch!");
-
   if (X == Y)
     return true;
 
+  if (X->getDeclName() != Y->getDeclName())
+    return false;
+
   // Must be in the same context.
   //
   // Note that we can't use DeclContext::Equals here, because the DeclContexts

diff  --git a/clang/test/Modules/Inputs/concept/A.cppm b/clang/test/Modules/Inputs/concept/A.cppm
new file mode 100644
index 0000000000000..beb370490617b
--- /dev/null
+++ b/clang/test/Modules/Inputs/concept/A.cppm
@@ -0,0 +1,3 @@
+module;
+#include "foo.h"
+export module A;

diff  --git a/clang/test/Modules/Inputs/concept/foo.h b/clang/test/Modules/Inputs/concept/foo.h
new file mode 100644
index 0000000000000..cf913a33303ca
--- /dev/null
+++ b/clang/test/Modules/Inputs/concept/foo.h
@@ -0,0 +1,13 @@
+#ifndef FOO_H
+#define FOO_H
+
+template <class T>
+concept Range = requires(T &t) { t.begin(); };
+
+struct A {
+public:
+  template <Range T>
+  using range_type = T;
+};
+
+#endif

diff  --git a/clang/test/Modules/concept.cppm b/clang/test/Modules/concept.cppm
new file mode 100644
index 0000000000000..e14a158644afa
--- /dev/null
+++ b/clang/test/Modules/concept.cppm
@@ -0,0 +1,10 @@
+// RUN: rm -rf %t
+// RUN: mkdir %t
+// RUN: %clang -std=c++20 %S/Inputs/concept/A.cppm --precompile -o %t/A.pcm
+// RUN: %clang -std=c++20 -fprebuilt-module-path=%t -I%S/Inputs/concept %s -c -Xclang -verify
+// expected-no-diagnostics
+
+module;
+#include "foo.h"
+export module B;
+import A;


        


More information about the cfe-commits mailing list