This is an archive of the discontinued LLVM Phabricator instance.

Add using directives to the clang::DeclContext and fix decls for variables inside namespaces
ClosedPublic

Authored by paulherman on Sep 15 2015, 8:37 PM.

Details

Summary

Supports the parsing of the "using namespace XXX" and "using XXX::XXX" directives. Added ambiguity errors when it two decls with the same name are encountered (see comments in TestCppNsImport). Fixes using directives being duplicated for anonymous namespaces. Fixes GetDeclForUID for specification DIEs.

Diff Detail

Event Timeline

paulherman updated this revision to Diff 34871.Sep 15 2015, 8:37 PM
paulherman retitled this revision from to Add using directives to the clang::DeclContext and fix decls for variables inside namespaces.
paulherman updated this object.
paulherman added a subscriber: lldb-commits.
clayborg edited edge metadata.Sep 16 2015, 10:12 AM

If you can explain the need for ParseDeclsForContext() as mentioned in the inlined comment that would help me understand this change better.

Also, Sean suggested that we might be able to fix all of this in another way that might make it easier on everyone: don't do any of the decl context searches manually like we are, but just add the using directives to the expression source code. When we make an expression we make a $_lldb_expr function and we put our expression inside of it. So if I run to "main" and run:

(lldb) expr argc

We generate the code:

void                           
$__lldb_expr(void *$__lldb_arg)          
{                              
argc;                            
}

Now, if we grabbed the using directives, we could translate them into source and place them inside the function lexical block:

void                           
$__lldb_expr(void *$__lldb_arg)          
{                              
using namespace foo;
using namespace bar;
argc;                            
}

Then we might not need to make as any modifications to the name lookups to do this manually. Thoughts?

source/Symbol/ClangASTContext.cpp
8877

Can you explain why you need to call ParseDeclsForContext() here? I would like to always avoid parsing everything within a decl context if possible only allow targeted searches (find "b" in namespace "a", not parse everything in namespace "a").

The need for ParseDeclsInContext is to get a list of namespaces that are contained within a using-directive. This does not actually parse anything but DW_TAG_imported_namespace and DW_TAG_imported_decl. The actual contents of the namespace are parsed only when the search reaches it.

About having the using-directives in the source that is passed to clang, I don't think that will work since there is a comment in ClangASTSource::FindExternalDeclsByName:

// Using directives found in this context.
// Tell Sema we didn't find any or we'll end up getting asked a *lot*.
case DeclarationName::CXXUsingDirective:
  SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
  return false;
clayborg accepted this revision.Sep 16 2015, 10:39 AM
clayborg edited edge metadata.

Thanks for the explanation and it makes sense. Looks good.

This revision is now accepted and ready to land.Sep 16 2015, 10:39 AM
paulherman edited edge metadata.

Add using directives to the clang::DeclContext and fix decls for variables inside namespaces

Rebased the patch.

paulherman closed this revision.Sep 16 2015, 11:49 AM