AST structrue of enums in clang


Assume the follwing code

class A
{
public:
enum E { // E is EnumDecl
ec1, // EnumConstantDecl
ec2
};

void foo()
{
E eVar = ec1; // this is a DeclStmt, but what is the type of eVar and ec1?
}
};

In the above code we know that the line E eVar = ec1; is a DeclStmt. What are the subcomponents of that.

What is the type of ec1?.

|

The AST dump is a good way to see this information:

$ clang -cc1 -ast-dump t3.cpp
typedef __int128_t __int128_t;
typedef __uint128_t __uint128_t;
struct __va_list_tag {
struct __va_list_tag;
unsigned int gp_offset;
unsigned int fp_offset;
void *overflow_arg_area;
void *reg_save_area;
};
typedef struct __va_list_tag __va_list_tag;
typedef __va_list_tag __builtin_va_list[1];
class A {
class A;
public:
enum E {
ec1,
ec2
};
void foo() (CompoundStmt 0x101843320 <t3.cpp:10:8, line:12:8>
(DeclStmt 0x101843300 <line:11:16, col:28>
0x101843280 “A::E eVar =
(DeclRefExpr 0x1018432d0 col:25 ‘enum A::E’ EnumConstant=‘ec1’ 0x1018430f0)”))

};

  • Doug

Thanks for reply,
Given an EnumConstantDecl(E::e1) and DeclStmt( E eVar1 = ec1;), how to find that the DeclStmt covers the EnumConstantDecl.

I don’t know what you mean by “covers”, but if you look at the AST dump



> void foo() (CompoundStmt 0x101843320 <t3.cpp:10:8, line:12:8>
> (DeclStmt 0x101843300 <line:11:16, col:28>
> 0x101843280 “A::E eVar =
> (DeclRefExpr 0x1018432d0 col:25 ‘enum A::E’ EnumConstant=‘ec1’ 0x1018430f0)”))

You can see that that the DeclStmt has the declaration eVar as one of its child nodes, and the initializer of eVar is a DeclRefExpr that points back at the EnumConstantDecl ec1.

  • Doug

Thanks,
Calling VarDecl::getInit() to retrieve the DeclRefExpr is what i want to know. By the way, “coverage” wheather the enum has been used in the statment(as in code coverage).