Parsing C++ Header File

Hi,

Problem: Want to parse C++ classes in a header file and generate
serialize and deserialize methods for this class.
example:
sample.h
#include
class Test
{
std::map<int,string> myMap;
int i;
string s;
A obj;
};

==>Transform it as below

class Test
{
std::map<int,string> myMap;
int i;
string s;
A obj;
public:
void* encode();
};

void* Test::encode()
{
for( i- 0; i<myMap.size(); i++)
encodeString();

encodeInt();

encodeString();
obj.encode();

return buf;
}

With the tutotials around i could setup the compiler instance

and ASTConsumer and ASTVisitor/
In the ASTRecursive visitor… i could overload visitRecordDecl(…);
On getting a CXXRecordDecl…i have iterated through FieldDecl
Now From FieldDecl…i want to extract information like
it is map made from int and string…
Am not able get the relationship of FieldDecl to CTemplateDecl…
and from CTemplateDecl how to extract the params?

Pls provide direction how to proceed further…

regards
ragha

Hi,

I made something like that in the past, but with gccxml, and I more or less achieved the same thing with pyclang.
I made a free function called encode() and a decode() for usual data types, and then I use my small script (https://github.com/mbrucher/clang2xml) to help me generate a encode(const MyClass&) decode(MyClass&) that calls the appropriate encode and decode functions, depending on what I scanned. In my case, I also added wrappers for each public function (which actually was my main goal, clang2xml is not as advanced as what I did in the past http://matt.eifelle.com/2009/07/21/using-gccxml-to-automate-c-wrappers-creation/ ), but you can keep it simpler and only serialize and unserialize your data.
With both libclang and the steps I used with gccxml, I think you will be able to achieve your goal :wink:

Cheers,

Matthieu

Am not able get the relationship of FieldDecl to CTemplateDecl...

Use getParent(), than dyn_cast it to CXXRecordDecl, than use
getDescribedClassTemplate()

and from CTemplateDecl how to extract the params?

TemplateDecl::getTemplateParameters()

Pls provide direction how to proceed further...

Use Rewriter to modify source code.

Thanks.
Current env need to use C++ APIs and i cannot use python.
So can i get help in understanding the data structure relation …
From CXXRecordDecl i get FieldDecl…now how to analyse further on each FieldDecl?
FieldDecl is DeclaratorDecl…from this how can i get CTemplateDecl…
I tried using DeclContext…also tried using getType…
I could get CXXRecordDecl…but when i used getDescribedClassTemplates …it returned NULL…
So am stuck here.

Pls guide me how to handle from here.

regard
ragha

Thanks.
I will try this.
When i read the source code of clang…getParent would return the Definition of Container.
Class X
{
std::map<i,string> _map;
};

_map is one fieldDecl…i did not use it as the documentation says it gives info of X…rather than std::map… i will try it out and let u know.

regards
ragha