aaron.ballman created this revision.
aaron.ballman added reviewers: erichkeane, rsmith, rjmccall, jyknight.
aaron.ballman requested review of this revision.
Herald added a project: clang.
When calculating the name to display for inline namespaces, we have custom
logic to try to hide redundant inline namespaces from the diagnostic.
Calculating these redundancies requires performing a lookup in the parent
declaration context, but that lookup should not try to look through transparent
declaration contexts, like linkage specifications. Instead, loop up the
declaration context chain until we find a non-transparent context and use that
instead.
This fixes PR49954.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D108403
Files:
clang/include/clang/AST/Decl.h
clang/test/Misc/diag-inline-namespace.cpp
Index: clang/test/Misc/diag-inline-namespace.cpp
===================================================================
--- clang/test/Misc/diag-inline-namespace.cpp
+++ clang/test/Misc/diag-inline-namespace.cpp
@@ -48,3 +48,14 @@
T<struct A::B::C::i> t4; // expected-error {{implicit instantiation of
undefined template 'N::T<N::A::i>'}}
T<struct A::B::C::j> t5; // expected-error {{implicit instantiation of
undefined template 'N::T<N::B::C::j>'}}
}
+
+namespace dont_crash {
+// A malformed lookup involving inline namespaces in a linkage specification
+// would previous cause an assertion due to the way diagnostics are emitted.
+extern "C++" inline namespace {
+namespace a {
+ a : b // expected-error {{unexpected ':' in nested name specifier; did you
mean '::'?}} \
+ // expected-error {{no type named 'b' in namespace 'dont_crash::a'}}
+} // expected-error {{expected unqualified-id}}
+} // inline namespace
+} // dont_crash
Index: clang/include/clang/AST/Decl.h
===================================================================
--- clang/include/clang/AST/Decl.h
+++ clang/include/clang/AST/Decl.h
@@ -614,7 +614,12 @@
if (!isInline())
return false;
auto X = lookup(Name);
- auto Y = getParent()->lookup(Name);
+ // We should not perform a lookup within a transparent context, so walk
+ // up the context chain until we find a non-transparent context.
+ const DeclContext *Parent = getParent();
+ while (Parent->isTransparentContext())
+ Parent = Parent->getParent();
+ auto Y = Parent->lookup(Name);
return std::distance(X.begin(), X.end()) ==
std::distance(Y.begin(), Y.end());
}
Index: clang/test/Misc/diag-inline-namespace.cpp
===================================================================
--- clang/test/Misc/diag-inline-namespace.cpp
+++ clang/test/Misc/diag-inline-namespace.cpp
@@ -48,3 +48,14 @@
T<struct A::B::C::i> t4; // expected-error {{implicit instantiation of undefined template 'N::T<N::A::i>'}}
T<struct A::B::C::j> t5; // expected-error {{implicit instantiation of undefined template 'N::T<N::B::C::j>'}}
}
+
+namespace dont_crash {
+// A malformed lookup involving inline namespaces in a linkage specification
+// would previous cause an assertion due to the way diagnostics are emitted.
+extern "C++" inline namespace {
+namespace a {
+ a : b // expected-error {{unexpected ':' in nested name specifier; did you mean '::'?}} \
+ // expected-error {{no type named 'b' in namespace 'dont_crash::a'}}
+} // expected-error {{expected unqualified-id}}
+} // inline namespace
+} // dont_crash
Index: clang/include/clang/AST/Decl.h
===================================================================
--- clang/include/clang/AST/Decl.h
+++ clang/include/clang/AST/Decl.h
@@ -614,7 +614,12 @@
if (!isInline())
return false;
auto X = lookup(Name);
- auto Y = getParent()->lookup(Name);
+ // We should not perform a lookup within a transparent context, so walk
+ // up the context chain until we find a non-transparent context.
+ const DeclContext *Parent = getParent();
+ while (Parent->isTransparentContext())
+ Parent = Parent->getParent();
+ auto Y = Parent->lookup(Name);
return std::distance(X.begin(), X.end()) ==
std::distance(Y.begin(), Y.end());
}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits