Questions about CXXRecordDecl

Hi, I'm writing a small clang plugin to inspect a codebase I work on and perform various custom semantic analyses. I've figured out most of what I need but I've run into a couple of small problems:

Given a CXXRecordDecl which represents an implicitly instantiated template class (mydecl->getTemplateSpecializationKind() == TSK_ImplicitInstantiation). I want to find the original unspecialized CXXRecordDecl / QualType. mydecl->getDescribedClassTemplate() is returning null, perhaps there's some functionality in Sema I should be using instead?

Given a CXXRecordDecl I want to print the class name along with all namespace qualifiers. doing a "->getDeclName().getAsString()" gives me something like "class foons::fooclass", I don't want the "class". I tried to use a PrintingPolicy with SupressTag=True but that seemed to suppress everything. I've written some code to walk up the declaring context chain to find all the enclosing namespace decls and this works just fine, but surely there's already some code to do this in clang.

Thanks,
Alex Mac

Alexander MacDonald wrote:

Hi, I'm writing a small clang plugin to inspect a codebase I work on and
perform various custom semantic analyses. I've figured out most of what I
need but I've run into a couple of small problems:

Given a CXXRecordDecl which represents an implicitly instantiated template
class (mydecl->getTemplateSpecializationKind() ==
TSK_ImplicitInstantiation). I want to find the original unspecialized
CXXRecordDecl / QualType. mydecl->getDescribedClassTemplate() is returning
null, perhaps there's some functionality in Sema I should be using
instead?

It sounds like you want

    mydecl->getInstantiatedFrom()

(cast it to ClassTemplateSpecializationDecl first). If it was implicitly
instantiated from a primary class template (instead of a partial
specialization), you may need to call getTemplatedDecl() on the resulting
pointer to get the CXXRecordDecl of the template.

Assuming mydecl is a "A<int>", mydecl->getDescribedClassTemplate() would
return true for the CXXRecordDecl of "A<T>". The class A<int> is not
templated, so has no class template is describes.

This is just the result of my doxygen checkup. I've no experience with clang
code, but it makes sense :slight_smile:

That did the trick, thanks! Somehow I missed the ClassTemplateSpecializationDecl in all my searches through the code