[cfe-dev] Parsing C++ Header File

Matthieu Brucher matthieu.brucher at gmail.com
Wed May 29 03:32:42 PDT 2013


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
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 ;)



2013/5/29 Raghavendra Keshav <ragavendrakk at gmail.com>

> 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
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

Information System Engineer, Ph.D.
Blog: http://matt.eifelle.com
LinkedIn: http://www.linkedin.com/in/matthieubrucher
Music band: http://liliejay.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130529/68ddf7ad/attachment.html>

More information about the cfe-dev mailing list