Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] C Structure fields analysis

Thank you Marco, that helped me a lot.

Also another question, lets say I have that struct :

struct CINEMATIC
{
    T_GeodeticLocation location;
    T_Attitude attitude;
    T_SpeedVector speed;
    char text[50];
    double yaw;
    T_SpeedVector speeds[20];
    QString toto;
    quint64 test_int;
    char* forbidden;
};

For each member I want to know the name, the type, if it is an array (in that case the size) and if it is a pointer.
But I don't see how to retrieve those informations from a CASTSimpleDeclaration instance.

public int visit(IASTDeclaration node)
{
if(node instanceof CASTSimpleDeclaration)
{
CASTSimpleDeclaration cnode = (CASTSimpleDeclaration) node;

System.out.println("instanceof CASTSimpleDeclaration");
IASTCompositeTypeSpecifier struct = (IASTCompositeTypeSpecifier)cnode.getDeclSpecifier();
if(struct.getKey() == IASTCompositeTypeSpecifier.k_struct)
{
System.out.println("struct_name=" + struct.getName());
IASTDeclaration[] members = struct.getMembers();
for (IASTDeclaration member : members)
{
if(member instanceof CASTSimpleDeclaration)
{
CASTSimpleDeclaration cmember = (CASTSimpleDeclaration) member;
try {
System.out.print("member_spec=" + cmember.getDeclSpecifier().getSyntax().toString());
} catch (ExpansionOverlapsBoundaryException e) {
e.printStackTrace();
}
for (IASTDeclarator declarator : cmember.getDeclarators())
{
System.out.println("    member_name=" + declarator.getName());
}
}
}
}
}
return PROCESS_SKIP; // Tell the visitor to not visit children nodes.
}

Thanks for your advise :)

> Date: Fri, 13 Jun 2014 12:42:03 +0200
> From: marco.trudel@xxxxxxxxxxxx
> To: cdt-dev@xxxxxxxxxxx
> Subject: Re: [cdt-dev] C Structure fields analysis
>
> On 06/13/2014 11:22 AM, Adrien G wrote:
> > Hi everyone !
> >
> > I'm trying to do some analysis of C code, I'm mainly interested in C
> > structures analysis. But I cant find out how to get all the fields of a
> > struct.
> > Indeed I'm getting my Structure object like this:
> >
> > public int visit(IASTDeclaration node)
> > {
> > if(node instanceof CASTSimpleDeclaration)
> > {
>
> Note that also IASTFunctionDefinition can declare a struct (quite
> uncommon though).
>
> > CASTSimpleDeclaration cnode = (CASTSimpleDeclaration) node;
> > IASTCompositeTypeSpecifier type =
> > (IASTCompositeTypeSpecifier)cnode.getDeclSpecifier();
> > if(type.getKey() == IASTCompositeTypeSpecifier.k_struct)
> > {
> > // Here I want to get all the fields of this struct.
>
> type.getMembers()
>
> > }
> > }
> > return PROCESS_SKIP; // Tells the visitor to not visit children nodes.
> > }
> >
> > Once I get my CASTSimpleDeclaration object, how can I get its members?
> >
> > Thank you for your advise.
> >
> > Also I'm wondering if CDT can handle multiple files at once, include
> > statement and macros ?
> > Like I'm declaring struct using a macro defined in a file that is included.
>
> Yes.
>
> Hope that helps
> Marco
>
> > All the best,
> > Adrien G.
> >
> >
> > _______________________________________________
> > cdt-dev mailing list
> > cdt-dev@xxxxxxxxxxx
> > https://dev.eclipse.org/mailman/listinfo/cdt-dev
> >
> _______________________________________________
> cdt-dev mailing list
> cdt-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/cdt-dev

Back to the top