From 326768121b190fc27d06ccec623e9150ef08b01c Mon Sep 17 00:00:00 2001 From: Devajith Valaparambil Sreeramaswamy Date: Wed, 8 May 2024 18:00:19 +0200 Subject: [PATCH] [cling] Make cling::utils::Lookup::Named look into using directives --- interpreter/cling/lib/Utils/AST.cpp | 10 +++++++++- interpreter/cling/test/Lookup/named.C | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/interpreter/cling/lib/Utils/AST.cpp b/interpreter/cling/lib/Utils/AST.cpp index ef34d9595c710..632278eff9155 100644 --- a/interpreter/cling/lib/Utils/AST.cpp +++ b/interpreter/cling/lib/Utils/AST.cpp @@ -1501,7 +1501,15 @@ namespace utils { // No definition, no lookup result. return; } - S->LookupQualifiedName(R, const_cast(primaryWithin)); + bool res = + S->LookupQualifiedName(R, const_cast(primaryWithin)); + + // If the lookup fails and the context is a namespace, try to lookup in + // the namespaces by setting NotForRedeclaration. + if (!res && primaryWithin->isNamespace()) { + R.setRedeclarationKind(Sema::NotForRedeclaration); + res = S->LookupQualifiedName(R, const_cast(primaryWithin)); + } } } diff --git a/interpreter/cling/test/Lookup/named.C b/interpreter/cling/test/Lookup/named.C index 2462d30bbcb56..8ddeb4839241b 100644 --- a/interpreter/cling/test/Lookup/named.C +++ b/interpreter/cling/test/Lookup/named.C @@ -6,6 +6,7 @@ // LICENSE.TXT for details. //------------------------------------------------------------------------------ +// clang-format off // RUN: cat %s | %cling -fno-rtti 2>&1 | FileCheck %s // Test Lookup::Named and Namespace, used in quick simple lookups. @@ -37,6 +38,13 @@ namespace AnotherNext { class Inside_AnotherNext {}; } +namespace A { + class C; +} +namespace B { + using namespace A; +} + .rawInput 0 // ROOT-6095: names introduced in a scopeless enum should be available in the @@ -106,3 +114,20 @@ decl decl = utils::Lookup::Named(&S, "EvenLess", context); decl //CHECK: (const clang::NamedDecl *) {{0x0*$|nullptr}} + +// Lookup the declaration for namespace B. +decl = utils::Lookup::Named(&S, "B", nullptr); +decl +// CHECK: (const clang::NamedDecl *) 0x{{[1-9a-f][0-9a-f]*$}} + +const clang::DeclContext* contextB = llvm::dyn_cast(decl); +contextB +// CHECK: (const clang::DeclContext *) 0x{{[1-9a-f][0-9a-f]*$}} + +// Lookup 'C' from namespace B. +decl = utils::Lookup::Named(&S, "C", contextB); +decl +// CHECK: (const clang::NamedDecl *) 0x{{[1-9a-f][0-9a-f]*$}} + +decl->getQualifiedNameAsString() +// CHECK: (std::string) "A::C"