[cfe-dev] Get name from RecordDecl

Mikhail Ramalho via cfe-dev cfe-dev at lists.llvm.org
Fri May 6 05:02:43 PDT 2016


Hello all,

Currently, I'm using libTooling to generate the program's AST, which later
I translate to our own AST, but I'm having some trouble retrieving the name
of this RecordDecl:

typedef struct
{
  int __count;

*  union*
*  {*
*    int __wch;*
*    char __wchb[4];*
*  } __value;*

} __mbstate_t;

To get the recordDecl name, I'm using two methods:

bool clang_c_convertert::get_tag_name(
  const clang::RecordDecl& rd,
  std::string &name)
{
  name = get_decl_name(rd);

  if(!name.empty())
    return false;

  // Try to get the name from typedef (if one exists)
  if (const clang::TagDecl *tag = llvm::dyn_cast<clang::TagDecl>(&rd))
  {
    if (const clang::TypedefNameDecl *tnd = rd.getTypedefNameForAnonDecl())
    {
      name = get_decl_name(*tnd);
      return false;
    }
    else if (tag->getIdentifier())
    {
      name = get_decl_name(*tag);
      return false;
    }
  }

  *assert(rd.isAnonymousStructOrUnion());*

  return get_anon_tag_name(rd, name); // This build's a name based on the
tag type
}

Where the method get_decl_name is:

std::string clang_c_convertert::get_decl_name(
  const clang::NamedDecl &decl)
{
  if(const clang::IdentifierInfo *identifier = decl.getIdentifier())
    return identifier->getName().str();

  std::string name;
  llvm::raw_string_ostream rso(name);
  decl.printName(rso);
  return rso.str();
}

The problem is the inner union, which is not anonymous but I can't get the
name. The bold assertion always fails.

Is there a way to get the union name, in this case?

Btw, I get there when parsing a varDecl and calling varDecl.getType(). It
returns a RecordType and I call .getDecl() to parse the union declaration.

Thank you,


-- 

Mikhail Ramalho.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160506/7091c5a8/attachment.html>


More information about the cfe-dev mailing list