[cfe-commits] r108195 - in /cfe/trunk: include/clang/AST/DeclBase.h lib/AST/DeclBase.cpp lib/Sema/SemaTemplateInstantiateDecl.cpp lib/Sema/TreeTransform.h

Douglas Gregor dgregor at apple.com
Mon Jul 12 16:46:47 PDT 2010


On Jul 12, 2010, at 2:12 PM, Fariborz Jahanian wrote:

> Author: fjahanian
> Date: Mon Jul 12 16:12:19 2010
> New Revision: 108195
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=108195&view=rev
> Log:
> Copy over attributes to instantiated variable.
> 
> 
> Modified:
>    cfe/trunk/include/clang/AST/DeclBase.h
>    cfe/trunk/lib/AST/DeclBase.cpp
>    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
>    cfe/trunk/lib/Sema/TreeTransform.h
> 
> Modified: cfe/trunk/include/clang/AST/DeclBase.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=108195&r1=108194&r2=108195&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/AST/DeclBase.h (original)
> +++ cfe/trunk/include/clang/AST/DeclBase.h Mon Jul 12 16:12:19 2010
> @@ -312,6 +312,7 @@
>     return getAttrsImpl();    // Uncommon case, out of line hash lookup.
>   }
>   void swapAttrs(Decl *D);
> +  void copyAttrs(Decl *D);
>   void invalidateAttrs();
> 
>   template<typename T> const T *getAttr() const {
> 
> Modified: cfe/trunk/lib/AST/DeclBase.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=108195&r1=108194&r2=108195&view=diff
> ==============================================================================
> --- cfe/trunk/lib/AST/DeclBase.cpp (original)
> +++ cfe/trunk/lib/AST/DeclBase.cpp Mon Jul 12 16:12:19 2010
> @@ -372,6 +372,17 @@
>   RHS->HasAttrs = true;
> }
> 
> +void Decl::copyAttrs(Decl *SRC) {
> +  if (!SRC->hasAttrs())
> +    return;
> +  ASTContext &Context = getASTContext();
> +  for (const Attr *attr = SRC->getAttrs(); attr; attr = attr->getNext()) {
> +    Attr *NewAttr = attr->clone(Context);
> +    addAttr(const_cast<Attr*>(NewAttr));
> +  }
> +  HasAttrs = true;
> +}
> +
> 
> void Decl::Destroy(ASTContext &C) {
>   // Free attributes for this decl.
> 
> Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=108195&r1=108194&r2=108195&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Mon Jul 12 16:12:19 2010
> @@ -2591,7 +2591,10 @@
>       ParentDC->isFunctionOrMethod()) {
>     // D is a local of some kind. Look into the map of local
>     // declarations to their instantiations.
> -    return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
> +    NamedDecl *ND = 
> +      cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
> +    ND->copyAttrs(D);
> +    return ND;
>   }

This occurs at the point where we try to find the instantiated variable given the variable in the template, and is too late to copy the attributes. It looks like we're missing an InstantiateAttrs call inside TemplateDeclInstantiator::VisitVarDecl that would instantiate attributes when the instantiated VarDecl is initially built.

	- Doug



More information about the cfe-commits mailing list