2013-09-29 29 views
6

在下面的頭文件我想獲得相應的+reflect註釋的類和成員變量:檢索評論使用python libclang

#ifndef __HEADER_FOO 
#define __HEADER_FOO 

//+reflect 
class Foo 
{ 
    public: 
    private: 
     int m_int; //+reflect 
}; 

#endif 

使用的libclang Python綁定與下面的腳本:

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

給了我這樣的輸出:

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 

的概率lem是評論缺失。他們是否被預處理器剝離?有什麼辦法可以防止這種情況發生?

回答

2

要做到這一點,你需要得到令牌,而不是遊標。如果我上面的文件,運行此腳本:

import sys 
import clang.cindex 

def srcrangestr(x): 
    return '%s:%d:%d - %s:%d:%d' % (x.start.file, x.start.line, x.start.column, x.end.file, x.end.line, x.end.column) 

def main(): 
    index = clang.cindex.Index.create() 
    tu = index.parse(sys.argv[1], args=['-x', 'c++']) 

    for x in tu.cursor.get_tokens(): 
     print x.kind 
     print " " + srcrangestr(x.extent) 
     print " '" + str(x.spelling) + "'" 

if __name__ == '__main__': 
    main() 

我得到如下:

TokenKind.PUNCTUATION 
    test2.h:1:1 - test2.h:1:2 
    '#' 
TokenKind.IDENTIFIER 
    test2.h:1:2 - test2.h:1:8 
    'ifndef' 
TokenKind.IDENTIFIER 
    test2.h:1:9 - test2.h:1:21 
    '__HEADER_FOO' 
TokenKind.PUNCTUATION 
    test2.h:2:1 - test2.h:2:2 
    '#' 
TokenKind.IDENTIFIER 
    test2.h:2:2 - test2.h:2:8 
    'define' 
TokenKind.IDENTIFIER 
    test2.h:2:9 - test2.h:2:21 
    '__HEADER_FOO' 
TokenKind.COMMENT 
    test2.h:4:1 - test2.h:4:11 
    '//+reflect' 
TokenKind.KEYWORD 
    test2.h:5:1 - test2.h:5:6 
    'class' 
TokenKind.IDENTIFIER 
    test2.h:5:7 - test2.h:5:10 
    'Foo' 
TokenKind.PUNCTUATION 
    test2.h:6:1 - test2.h:6:2 
    '{' 
TokenKind.KEYWORD 
    test2.h:7:5 - test2.h:7:11 
    'public' 
TokenKind.PUNCTUATION 
    test2.h:7:11 - test2.h:7:12 
    ':' 
TokenKind.KEYWORD 
    test2.h:8:5 - test2.h:8:12 
    'private' 
TokenKind.PUNCTUATION 
    test2.h:8:12 - test2.h:8:13 
    ':' 
TokenKind.KEYWORD 
    test2.h:9:9 - test2.h:9:12 
    'int' 
TokenKind.IDENTIFIER 
    test2.h:9:13 - test2.h:9:18 
    'm_int' 
TokenKind.PUNCTUATION 
    test2.h:9:18 - test2.h:9:19 
    ';' 
TokenKind.COMMENT 
    test2.h:9:20 - test2.h:9:30 
    '//+reflect' 
TokenKind.PUNCTUATION 
    test2.h:10:1 - test2.h:10:2 
    '}' 
TokenKind.PUNCTUATION 
    test2.h:10:2 - test2.h:10:3 
    ';' 
TokenKind.PUNCTUATION 
    test2.h:12:1 - test2.h:12:2 
    '#' 
TokenKind.IDENTIFIER 
    test2.h:12:2 - test2.h:12:7 
    'endif' 

這應該是足以讓我一起工作。

1

是的,所有評論都被預處理器刪除。你可以通過做clang -E mycode.c > mycode.i看到,它會給你一個mycode.i文件,包含所有的預處理,但沒有評論。

您可以使用#pragma或某些未被剝離且被編譯器忽略的內容來執行某些操作。

+1

怎麼樣CXComment使我的意見嗎? http://clang.llvm.org/doxygen/structCXComment.html或PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION? – user408952

2

您需要修改cindex.py腳本並公開以下函數。

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

也該在cindex.py添加到正確的位置

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

我不得不使用

/*! 
    * +reflect 
    */ though