[cfe-dev] UsingDirectiveDecl inconsistently (not) added to their DeclContext.

Enea Zaffanella zaffanella at cs.unipr.it
Mon Aug 6 07:30:08 PDT 2012


We are observing an inconsistency (affecting our AST visitors) in the 
addition of UsingDirectiveDecl to their DeclContext.

When parsing a using directive, it gets added to its DeclContext only if 
it is NOT a function/method:

===================== SemaDeclCXX.cpp
void Sema::PushUsingDirective(Scope *S, UsingDirectiveDecl *UDir) {
   // If the scope has an associated entity and the using directive is at
   // namespace or translation unit scope, add the UsingDirectiveDecl into
   // its lookup structure so qualified name lookup can find it.
   DeclContext *Ctx = static_cast<DeclContext*>(S->getEntity());
   if (Ctx && !Ctx->isFunctionOrMethod())
     Ctx->addDecl(UDir);
=====================


However, when such a using declaration is subject to template 
instantiations, it is unconditionally added to its DeclContext, no 
matter if it is a function/method or not:

===================== SemaTemplateInstantiateDecl.cpp
Decl 
*TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
   // Using directives are never dependent (and never contain any types or
   // expressions), so they require no explicit instantiation work.

   UsingDirectiveDecl *Inst
     = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
                                  D->getNamespaceKeyLocation(),
                                  D->getQualifierLoc(),
                                  D->getIdentLocation(),
                                  D->getNominatedNamespace(),
                                  D->getCommonAncestor());
   Owner->addDecl(Inst);
=====================

Therefore, when visiting the instantiated DeclContext we will see more 
declarations than when visiting its instantiation pattern.
We would like to remove this inconsistency, but it is unclear to us 
which is the right fix to apply: should we always add the using 
declaration to its DeclContext or should we repeat the check for 
function/method when instantiating?

Enea.



More information about the cfe-dev mailing list