From 3e51aa57760008a6f86611940caf97db667c7250 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 | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/interpreter/cling/lib/Utils/AST.cpp b/interpreter/cling/lib/Utils/AST.cpp index ef34d9595c7100..9b59da6607b3e4 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); + S->LookupName(R, S->TUScope); + } } } diff --git a/interpreter/cling/test/Lookup/named.C b/interpreter/cling/test/Lookup/named.C index 2462d30bbcb56c..02c4c29d6b0e7d 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,21 @@ 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"