[cfe-dev] How to print the forward declaration of an object visited through RecursiveASTVisitor ?

Manasij Mukherjee manasij7479 at gmail.com
Mon Jun 9 00:20:43 PDT 2014


Thanks, that was it.
The code is hairy though.
I'd be helpful if there was some function for this.

  bool VisitClassTemplateDecl(clang::ClassTemplateDecl* Declaration) {

   if(Declaration->getName().startswith("_")
          || Declaration->getName().size()==0)
     return true;

    std::vector<NamespacePrinterRAII> scope;
    clang::DeclContext*
c=Declaration->getTemplatedDecl()->getEnclosingNamespaceContext();
    while(c->isNamespace()) {
      clang::NamespaceDecl* n=llvm::cast<clang::NamespaceDecl>(c);
      scope.emplace_back(n->getNameAsString());
      c=c->getParent();
    }

    llvm::outs()<<"template <";
    clang::TemplateParameterList* tl=Declaration->getTemplateParameters();
    for(auto it=tl->begin();it!=tl->end();++it) {
      if(llvm::isa<clang::NonTypeTemplateParmDecl>(*it)) {
        clang::NonTypeTemplateParmDecl*
td=llvm::cast<clang::NonTypeTemplateParmDecl>(*it);
        llvm::outs()<<td->getType().getAsString();
      }
      else llvm::outs()<<"typename";
      llvm::outs()<<" "<<(*it)->getName();
      if((it+1)!=tl->end())
        llvm::outs()<<", ";
    }
    llvm::outs()<<"> class "
                << Declaration->getName() << ";\n";

    return false;
  }

Using dump or print prints the whole body, but I have to display only what
is necessary for a forward declaration.



On Mon, Jun 9, 2014 at 11:22 AM, James Dennett <james.dennett at gmail.com>
wrote:

> On Sun, Jun 8, 2014 at 9:31 PM, Manasij Mukherjee <manasij7479 at gmail.com>
> wrote:
> > As a follow up to this question, I am now generating the
> > (forward)declarations manually, not having found anything in the API to
> do
> > it.
> > The only thing that I can't get working is the way to get the type name
> of a
> > non-type template parameter.
> > If I have:
> > template<typename T, typename U, int V> class foo
> > {};
> > From where do I get the string "int" when overriding
> VisitClassTemplateDecl
> > ?
> >
> > I can get the names of the identifiers T, U, V as:
> >
> >     clang::TemplateParameterList*
> tl=Declaration->getTemplateParameters();
> >     for(auto it=tl->begin();it!=tl->end();++it) {
> >         p->getName();
> >     }
> >
> > But I can't find any obvious way to get its type as a string.
> > p->getDeclKindName() only returns "TemplateTypeParm" or
> > "NonTypeTemplateParm"
>
> If the NamedDecl* is a NonTypeTemplateParmDecl* then it has a
> getType() member function (inherited from ValueDecl) returning a
> QualType.  You might need to be careful in case
> NonTypeTemplateParmDecl::isParameterPack() is true; I don't know what
> getType() returns for a parameter pack (though it clearly wouldn't be
> hard to check).
>
> -- James
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140609/20eae28b/attachment.html>


More information about the cfe-dev mailing list