Retrieving comments using python libclang
Question: In the following header file I'd like to get the corresponding `+reflect`
comment to the class and member variable:

    
    
    #ifndef __HEADER_FOO
    #define __HEADER_FOO
    
    //+reflect
    class Foo
    {
        public:
        private:
            int m_int; //+reflect
    };
    
    #endif
    

Using the python bindings for libclang and the following script:

    
    
    import sys
    import clang.cindex
    
    def dumpnode(node, indent):
        print ' ' * indent, node.kind, node.spelling
        for i in node.get_children():
            dumpnode(i, indent+2)
    
    def main():
        index = clang.cindex.Index.create()
        tu = index.parse(sys.argv[1], args=['-x', 'c++'])
    
        dumpnode(tu.cursor, 0)
    
    if __name__ == '__main__':
        main()
    

Gives me this output:

    
    
    CursorKind.TRANSLATION_UNIT None
      CursorKind.TYPEDEF_DECL __builtin_va_list
      CursorKind.CLASS_DECL type_info
      CursorKind.CLASS_DECL Foo
        CursorKind.CXX_ACCESS_SPEC_DECL
        CursorKind.CXX_ACCESS_SPEC_DECL
        CursorKind.FIELD_DECL m_int
    

The problem is that the comments are missing. Are they stripped by the
preprocessor? Is there any way to prevent that?


Answer: You need to modify the cindex.py script and expose the following function.

    
    
    class Cursor(Structure):
       def getRawComment(self):
        return conf.lib.clang_Cursor_getRawCommentText(self)
    

also add this to the correct spot in cindex.py

    
    
    ("clang_Cursor_getRawCommentText",
     [Cursor],
     _CXString,
     _CXString.from_result),
    

I had to make my comments using

    
    
     /*!
      * +reflect
      */ though
    

