Skip to content

Commit 60175d4

Browse files
committed
Add C++ class parsing and exclude_dirs filter
Parse and expose C++ classes/methods in the IR and add directory-based exclusion support. Introduces IRClass and IRMethod (with qualified_name on many IR nodes), includes classes in codegen context, and refactors normalizer to detect C++ records, methods, enums and typedef-backed anonymous types. Replaces the previous export_macro filtering with filters.exclude_dirs (and updates FilterConfig and example config), removes the legacy bindgen.yaml, and updates header discovery to honor exclude_dirs. README updated to remove export_macro recommendations.
1 parent 1902874 commit 60175d4

File tree

8 files changed

+230
-69
lines changed

8 files changed

+230
-69
lines changed

tools/bindgen/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ C++ Headers
6363
## 输入与约束
6464

6565
- 入口:一组 C++ 头文件 + include paths + compile flags
66-
- 建议约定宏:用于筛选可导出 API(例如 `NATIVEAPI_EXPORT`
6766
- 过滤方式:
68-
- `export_macro`:只导出带宏标记的符号
6967
- `// bindgen:ignore`:显式忽略
7068
- 可选白名单/黑名单(按正则匹配函数/类型名)
69+
- 可选排除目录(例如 `capi`
7170
- 只处理(MVP):
72-
- 可导出的 C API 函数(`extern "C"`
71+
- `extern "C"` 函数
72+
- C++ class/struct 的 public 方法(不含继承、多态、异常语义)
7373
- `struct` + POD 类型
7474
- `enum` 常量
7575
- `#define` 的简单常量
@@ -105,6 +105,7 @@ C++ Headers
105105
- headers
106106
- types
107107
- functions
108+
- classes
108109
- constants
109110
- aliases
110111

@@ -209,6 +210,7 @@ render_to_files(templates, context, out_dir)
209210
- `module`: 当前模块信息(headers, namespaces)
210211
- `types`: 结构体/枚举/别名列表(已排序)
211212
- `functions`: 函数列表(已过滤)
213+
- `classes`: C++ 类/struct 列表(public methods)
212214
- `constants`: 常量列表
213215
- `mapping`: 语言映射配置(types/conventions/naming)
214216

@@ -230,21 +232,20 @@ render_to_files(templates, context, out_dir)
230232
```yaml
231233
clang_flags:
232234
- -std=c11
233-
- -DNATIVEAPI_EXPORT
234235
mapping:
235236
language: example
236237
filters:
237-
export_macro: NATIVEAPI_EXPORT
238238
allowlist_regex: []
239239
denylist_regex: []
240+
exclude_dirs:
241+
- capi
240242
```
241243
242244
说明:`entry_headers` 将自动从 `src/` 目录下收集所有 `.h`(忽略 `platform/`),`include_paths` 也会默认指向 `src/`,无需在配置中手动列出。模板目录默认读取 `config.yaml` 同路径下的 `template/`。
243245

244246
### 注解建议(可扩展)
245247

246248
- 使用宏或注释标注 API,如:
247-
- `NATIVEAPI_EXPORT` 标记导出
248249
- `// bindgen:ignore` 跳过
249250
- `// bindgen:nullable` 标记可为空
250251
- `// bindgen:rename <NewName>` 重命名
@@ -295,12 +296,11 @@ PYTHONPATH=../.. python3 -m bindgen \
295296
296297
## 风险与规避
297298
298-
- **宏复杂度高**:MVP 仅处理简单宏与 export 宏
299+
- **宏复杂度高**:MVP 仅处理简单宏
299300
- **C++ 语义**:默认 `extern "C"` 入口
300301
- **跨平台 ABI**:需在生成器中区分平台(Windows/Linux/macOS)
301302
302303
## 下一步
303304
304-
- 补充 `bindgen.yaml` 示例
305-
- 实现 `parser.py` 与 `ir.py` 的最小骨架
306-
- 选择模板引擎(如 `jinja2`)用于 codegen
305+
- 补充更多模板示例
306+
- 扩展 C++ 类方法语义(如继承信息、构造/析构、const 重载等)

tools/bindgen/bindgen.yaml

Lines changed: 0 additions & 11 deletions
This file was deleted.

tools/bindgen/codegen/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def build_context(module: IRModule, mapping: Dict) -> Dict:
1111
"types": module.types,
1212
"enums": module.enums,
1313
"functions": module.functions,
14+
"classes": module.classes,
1415
"constants": module.constants,
1516
"aliases": module.aliases,
1617
"mapping": mapping,

tools/bindgen/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ def _load_yaml(path: Path) -> dict:
1818

1919
@dataclass
2020
class FilterConfig:
21-
export_macro: Optional[str]
2221
allowlist_regex: List[str]
2322
denylist_regex: List[str]
23+
exclude_dirs: List[str]
2424

2525

2626
@dataclass
@@ -44,8 +44,8 @@ def load_config(path: Path) -> BindgenConfig:
4444
clang_flags=data.get("clang_flags", []),
4545
mapping=dict(mapping),
4646
filters=FilterConfig(
47-
export_macro=filters.get("export_macro"),
4847
allowlist_regex=list(filters.get("allowlist_regex", []) or []),
4948
denylist_regex=list(filters.get("denylist_regex", []) or []),
49+
exclude_dirs=list(filters.get("exclude_dirs", []) or []),
5050
),
5151
)

tools/bindgen/example/bindgen/config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ clang_flags:
22
- -x
33
- c++
44
- -std=c++17
5-
- -DNATIVEAPI_EXPORT
65
mapping:
76
language: example
87
output_note: Generated by bindgen example template.
98
filters:
10-
export_macro: NATIVEAPI_EXPORT
119
allowlist_regex: []
1210
denylist_regex: []
11+
exclude_dirs:
12+
- capi

tools/bindgen/ir/model.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class IRField:
2525
class IRStruct:
2626
name: str
2727
fields: List[IRField]
28+
qualified_name: Optional[str] = None
2829

2930

3031
@dataclass
@@ -38,6 +39,7 @@ class IREnum:
3839
name: str
3940
values: List[IREnumValue]
4041
scoped: bool = False
42+
qualified_name: Optional[str] = None
4143

4244

4345
@dataclass
@@ -61,6 +63,25 @@ class IRFunction:
6163
params: List[IRParam]
6264
callconv: Optional[str] = None
6365
variadic: bool = False
66+
qualified_name: Optional[str] = None
67+
68+
69+
@dataclass
70+
class IRMethod:
71+
name: str
72+
return_type: IRType
73+
params: List[IRParam]
74+
static: bool = False
75+
const: bool = False
76+
access: Optional[str] = None
77+
variadic: bool = False
78+
79+
80+
@dataclass
81+
class IRClass:
82+
name: str
83+
methods: List[IRMethod] = field(default_factory=list)
84+
qualified_name: Optional[str] = None
6485

6586

6687
@dataclass
@@ -76,5 +97,6 @@ class IRModule:
7697
types: List[IRStruct] = field(default_factory=list)
7798
enums: List[IREnum] = field(default_factory=list)
7899
functions: List[IRFunction] = field(default_factory=list)
100+
classes: List[IRClass] = field(default_factory=list)
79101
constants: List[IRConstant] = field(default_factory=list)
80102
aliases: List[IRAlias] = field(default_factory=list)

0 commit comments

Comments
 (0)