From ca410ed8abf588a48b75d9b75a495a6b6a7d1fd9 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/.clang-format | 1 + interpreter/cling/test/Lookup/named.C | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 interpreter/cling/test/.clang-format diff --git a/interpreter/cling/lib/Utils/AST.cpp b/interpreter/cling/lib/Utils/AST.cpp index 29505895b22c7..54dad90487318 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->LookupQualifiedName(R, const_cast(primaryWithin)); + } } } diff --git a/interpreter/cling/test/.clang-format b/interpreter/cling/test/.clang-format new file mode 100644 index 0000000000000..e3845288a2aec --- /dev/null +++ b/interpreter/cling/test/.clang-format @@ -0,0 +1 @@ +DisableFormat: true diff --git a/interpreter/cling/test/Lookup/named.C b/interpreter/cling/test/Lookup/named.C index 2462d30bbcb56..cc18419fe282c 100644 --- a/interpreter/cling/test/Lookup/named.C +++ b/interpreter/cling/test/Lookup/named.C @@ -37,6 +37,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 +113,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"