From 6dd38daf1495367db8fe9e9a5cacb7420cf08e27 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Thu, 27 Aug 2009 06:03:53 +0000 Subject: [PATCH] When checking whether one declaration context encloses another, make sure to look at the primary contexts. Thanks to Eli for the test case git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80212 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclBase.h | 9 +++------ lib/AST/DeclBase.cpp | 10 ++++++++++ .../temp/temp.decls/temp.class/temp.mem.func/p1.cpp | 3 +++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index 0979733644..e43f15d79a 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -578,12 +578,9 @@ public: /// inline namespaces. bool isTransparentContext() const; - bool Encloses(DeclContext *DC) const { - for (; DC; DC = DC->getParent()) - if (DC == this) - return true; - return false; - } + /// \brief Determine whether this declaration context encloses the + /// declaration context DC. + bool Encloses(DeclContext *DC); /// getPrimaryContext - There may be many different /// declarations of the same entity (including forward declarations diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index 8296f2931b..cd5b0063a6 100644 --- a/lib/AST/DeclBase.cpp +++ b/lib/AST/DeclBase.cpp @@ -448,6 +448,16 @@ bool DeclContext::isTransparentContext() const { return false; } +bool DeclContext::Encloses(DeclContext *DC) { + if (getPrimaryContext() != this) + return getPrimaryContext()->Encloses(DC); + + for (; DC; DC = DC->getParent()) + if (DC->getPrimaryContext() == this) + return true; + return false; +} + DeclContext *DeclContext::getPrimaryContext() { switch (DeclKind) { case Decl::TranslationUnit: diff --git a/test/CXX/temp/temp.decls/temp.class/temp.mem.func/p1.cpp b/test/CXX/temp/temp.decls/temp.class/temp.mem.func/p1.cpp index 28a5d98540..725b61c271 100644 --- a/test/CXX/temp/temp.decls/temp.class/temp.mem.func/p1.cpp +++ b/test/CXX/temp/temp.decls/temp.class/temp.mem.func/p1.cpp @@ -63,3 +63,6 @@ template X0::operator T*() const { return &value; } + +namespace N { template class A {void a();}; } +namespace N { template void A::a() {} }