Extracting struct name for typedef struct { ... } <name>

Hi all,

I tried both RecordDecl as well as TypedefNameDecl to extract names of such struct declarations, but I always get an empty string. Could you please let me know how to extract the struct name?

For example:

typedef struct {

} tstr;

I want to extract ‘tstr’ name for this struct typedef.

Thanks!

In the clang padding checker, I use this snippet of code…

Os << “Excessive padding in '”;
Os << QualType::getAsString(RD->getTypeForDecl(), Qualifiers()) << “’”;

RD is a RecordDecl. I get text like this:

Excessive padding in ‘tstr’

Hi all,

I tried both RecordDecl as well as TypedefNameDecl to extract names of
such struct declarations, but I always get an empty string. Could you
please let me know how to extract the struct name?

If you have the RecordDecl, you can use getTypedefNameForAnonDecl() to get
the corresponding TypedefNameDecl, which will have the name 'tstr'. That
only works in cases like the one below where the typedef provides a name
for linkage purposes to the struct (in particular, only in C++ and only if
the typedef is for exactly the struct type and not, say, a pointer to it).

If you want a more general solution, you'd need to walk the enclosing
DeclContext looking for the matching typedef.

For example: