[Dwarf-Discuss] name lookup w/ using directives

Michael Eager eager at eagercon.com
Wed Aug 20 08:45:46 PDT 2008


Kendrick Wong wrote:
> 
> While working with namespaces, I've ran into cases where there is no 
> good way to determine the correct name lookup:
> 
> scenario A:
> namespace A {
>   namespace B {
>     int a = 1;
>   }
>   namespace C {
>     int func () {
>       using namespace B;
>       return a;     // returns A::B::a
>     }
>   }
> }
> 
> Sample DWARF:
> 10$: DW_TAG_namespace
>        DW_AT_name("A")
> 20$:   DW_TAG_namespace
>          DW_AT_name("B")
> 30$:     DW_TAG_variable
>            DW_AT_name("a")
> 40$:   DW_TAG_namespace
>          DW_AT_name("C")
> 50$:     DW_TAG_subprogram
>            DW_AT_name("func")
> 60$:       DW_TAG_imported_module
>              DW_AT_import(reference to 20$)
> 
> If a debugger stops in func(), and asks to examine 'a'.  A debugger can 
> try to search for 'a' within func(), failing that, it would look inside 
> namespace B, and in this case, it finds A::B::a, which is the correct 
> 'a' to find.
> 
> scenario B:
> namespace A {
>   namespace B {
>     int a = 1;
>   }
>   namespace C {
> *    int a = 2;    // introduce A::C::a*
>     int func () {
>       using namespace B;
>       return a;     // returns A::C::a
>     }
>   }
> }
> 
> Sample DWARF:
> 10$: DW_TAG_namespace
>        DW_AT_name("A")
> 20$:   DW_TAG_namespace
>          DW_AT_name("B")
> 30$:     DW_TAG_variable
>            DW_AT_name("a")
> 40$:   DW_TAG_namespace
>          DW_AT_name("C")
> *45$:     DW_TAG_variable         // introduce A::C::a*
> *           DW_AT_name("a")*
> 50$:     DW_TAG_subprogram
>            DW_AT_name("func")
> 60$:       DW_TAG_imported_module
>              DW_AT_import(reference to 20$)
> 
> If a debugger use the same name lookup technique as in scenario A, it's 
> going to find A::B::a, which is incorrect.

How does this happen?

If the successively enclosing scopes are searched for "a", it is not
found in "func", then a search within namespace "C" does find A::C::a.
What search method would skip searching the enclosing namespace?

> I think debugger needs a little more help (in the form of additional 
> DWARF info) to be able to perform the same name lookup as the compiler. 

There are a number of quirks in C++ name resolution.  But I thought
that DWARF did provide the debugger with the same structural info that
the compiler uses in this process.

>  I do have a solution in my head to solve the problem as well, but I 
> will wait until I get a confirmation that this is indeed a problem worth 
> diving into.

-- 
Michael Eager	 eager at eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077



More information about the Dwarf-Discuss mailing list