CPP-Python解析头文件

本文主要讲述使用Python解析C++头文件,使用的python库叫robotpy-cppheaderparser
github地址:https://github.com/robotpy/robotpy-cppheaderparser
接口文档:https://cppheaderparser.readthedocs.io/en/stable/index.html

安装

使用下面命令进行安装

1
pip install robotpy-cppheaderparser

示例.h文件

准备一个C++头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef _SAMPLE_CLASS_H_
#define _SAMPLE_CLASS_H_

#include <vector>
#include <string>
#include <stdio.h>

#define DEF_1 1
#define OS_NAME "Linux"

const int MyData1=100;
const int MyData2=200;

using namespace std;

typedef enum {
AA,
BB
} myEnum_t;


class SampleClass
{
public:

typedef enum {
CC,
DD
} myAnother_t;

SampleClass();
/*!
* Method 1
*/
string meth1();

///
/// Method 2 description
///
/// @param v1 Variable 1
///
int meth2(int v1);

/**
* Method 3 description
*
* \param v1 Variable 1
* \param v2 Variable 2
*/
void meth3(const string & v1, vector<string> & v2);

/**********************************
* Method 4 description
*
* @return Return value
*********************************/
unsigned int meth4();
private:
void * meth5(){return NULL};

/// prop1 description
string prop1;
//! prop5 description
int prop5;
};


namespace Alpha
{
class AlphaClass
{
public:
AlphaClass();

void alphaMethod();

string alphaString;
};

namespace Omega
{
class OmegaClass
{
public:
OmegaClass();

string omegaString;
};
};
}

int sampleFreeFunction(int i)
{
return i + 1;
}

int anotherFreeFunction(void);


#endif

使用robotpy-cppheaderparser

1
2
3
import CppHeaderParser
cppHeader = CppHeaderParser.CppHeader("SampleClass.h")
# 解析后的内容放到变量cppHeader里,下面就来看下如何获取头文件里的各种内容

获取include的头文件名

1
2
print(cppHeader.includes)

获取头文件中定义的类名

1
2
for classname in cppHeader.classes.keys():
print(classname)

获取类所属的namespace

1
2
3
4
print(cppHeader.classes['SampleClass']['namespace'])
print(cppHeader.classes['AlphaClass']['namespace'])
print(cppHeader.classes['OmegaClass']['namespace'])
# 没有namespace就打印为空

获取类里的public方法

1
2
3
4
5
for oneMethod in cppHeader.classes['SampleClass']['methods']['public']:
for kv in oneMethod.items():
print(kv[0], " : ", kv[1])

print('---------------------\n')

获取类里的public方法函数参数和返回类型

一个方法会有许多相关属性,可以根据实际需要进行选择。

常规来说只需要函数名,函数参数和返回类型就可以了,对应的key分别为“name”,“parameters”,“rtnType”

1
2
3
4
5
6
7
8
for oneMethod in cppHeader.classes['SampleClass']['methods']['public']:
print('name: ', oneMethod['name'])
print('rtnType: ', oneMethod['rtnType'])
print('parameters: ', end='')
if len(oneMethod['parameters']):
for oneParam in oneMethod['parameters']:
print(oneParam['name'], oneParam['type'], end='; ')
print('\n')

获取类里的public变量

1
2
3
4
5
for oneProperty in cppHeader.classes['AlphaClass']['properties']['public']:
print('name: ', oneProperty['name'])
print('type: ', oneProperty['type'])
# for kv in oneProperty.items():
# print(kv[0], " : ", kv[1])

这里只挑了2个比较重要的属性name和type

获取类里的private方法

1
2
3
4
5
for oneMethod in cppHeader.classes['SampleClass']['methods']['private']:
for kv in oneMethod.items():
print(kv[0], " : ", kv[1])

print('---------------------\n')

获取类里的private变量

1
2
3
4
5
for oneProperty in cppHeader.classes['SampleClass']['properties']['private']:
print('name: ', oneProperty['name'])
print('type: ', oneProperty['type'])
# for kv in oneProperty.items():
# print(kv[0], " : ", kv[1])

获取头文件里定义的函数(类外定义的函数)

1
2
3
4
5
for func in cppHeader.functions:
print('name: {}'.format(func['name']))
print('rtnType: {}'.format(func['rtnType']))
print('parameters: {}'.format(func['parameters']))
print('\n')

获取头文件里定义的宏定义

1
2
for define in cppHeader.defines:
print(define) # 类型是str

获取头文件里定义的enum

1
2
for oneEnum in cppHeader.enums:
print(oneEnum)

获取类里定义的enum

1
2
3
for oneEnum in cppHeader.classes['SampleClass']['enums']['public']:
print(oneEnum)
# 注意,这个enum是定义在SampleClass的public区

获取头文件里定义的变量

1
2
for oneVar in cppHeader.variables:
print(oneVar)

总览

使用下面的命令对整个头文件的解析结果进行总览,结果会以json格式打印出来,

1
2
python -m CppHeaderParser.tojson ./SampleClass.h
#总览的好处是可以根据需要进行查看,因为很多属性不是很清楚,总览看一下就很方便。