[clang] d9d63fc - [AST] lookup in parent DeclContext for transparent DeclContext
Chuanqi Xu via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 10 18:17:13 PST 2022
Author: Chuanqi Xu
Date: 2022-01-11T10:16:26+08:00
New Revision: d9d63fc1088c22129cde9c3d3a84f356c875190e
URL: https://github.com/llvm/llvm-project/commit/d9d63fc1088c22129cde9c3d3a84f356c875190e
DIFF: https://github.com/llvm/llvm-project/commit/d9d63fc1088c22129cde9c3d3a84f356c875190e.diff
LOG: [AST] lookup in parent DeclContext for transparent DeclContext
The compiler would crash if we lookup for name in transparent decl
context. See the tests attached for example.
I think this should make sense since the member declared in transparent
DeclContext are semantically defined in the enclosing (non-transparent)
DeclContext, this is the definition for transparent DeclContext.
Reviewed By: erichkeane
Differential Revision: https://reviews.llvm.org/D116792
Added:
clang/test/Modules/Inputs/template_name_lookup/foo.cppm
clang/test/Modules/template_name_lookup.cpp
clang/test/SemaCXX/lookup-template-name-extern-CXX.cpp
Modified:
clang/lib/AST/DeclBase.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index 52b8a45721105..98a5c6b664713 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -1644,9 +1644,9 @@ void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
DeclContext::lookup_result
DeclContext::lookup(DeclarationName Name) const {
- assert(getDeclKind() != Decl::LinkageSpec &&
- getDeclKind() != Decl::Export &&
- "should not perform lookups into transparent contexts");
+ // For transparent DeclContext, we should lookup in their enclosing context.
+ if (getDeclKind() == Decl::LinkageSpec || getDeclKind() == Decl::Export)
+ return getParent()->lookup(Name);
const DeclContext *PrimaryContext = getPrimaryContext();
if (PrimaryContext != this)
diff --git a/clang/test/Modules/Inputs/template_name_lookup/foo.cppm b/clang/test/Modules/Inputs/template_name_lookup/foo.cppm
new file mode 100644
index 0000000000000..ee1973f65e716
--- /dev/null
+++ b/clang/test/Modules/Inputs/template_name_lookup/foo.cppm
@@ -0,0 +1,3 @@
+export module foo;
+export template <typename T>
+class X {};
diff --git a/clang/test/Modules/template_name_lookup.cpp b/clang/test/Modules/template_name_lookup.cpp
new file mode 100644
index 0000000000000..29375e514025f
--- /dev/null
+++ b/clang/test/Modules/template_name_lookup.cpp
@@ -0,0 +1,11 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: %clang_cc1 -std=c++20 %S/Inputs/template_name_lookup/foo.cppm -emit-module-interface -o %t/foo.pcm
+// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %s -fsyntax-only -verify
+
+import foo;
+void use() {
+ X x; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'X'}}
+ // expected-note at Inputs/template_name_lookup/foo.cppm:3 {{candidate template ignored: couldn't infer template argument 'T'}}
+ // expected-note at Inputs/template_name_lookup/foo.cppm:3 {{candidate function template not viable: requires 1 argument, but 0 were provided}}
+}
diff --git a/clang/test/SemaCXX/lookup-template-name-extern-CXX.cpp b/clang/test/SemaCXX/lookup-template-name-extern-CXX.cpp
new file mode 100644
index 0000000000000..93e7fb6199354
--- /dev/null
+++ b/clang/test/SemaCXX/lookup-template-name-extern-CXX.cpp
@@ -0,0 +1,12 @@
+// Tests that the lookup in transparent declaration context
+// (linkage specifiaction context) wouldn't cause compiler crash.
+// RUN: %clang_cc1 -std=c++20 %s -fsyntax-only -verify
+extern "C++" {
+template <class T>
+class X {}; // expected-note {{candidate template ignored: couldn't infer template argument 'T'}}
+ // expected-note at -1 {{candidate function template not viable: requires 1 argument, but 0 were provided}}
+}
+
+void foo() {
+ X x; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'X'}}
+}
More information about the cfe-commits
mailing list