Skip to content

Commit fdae685

Browse files
committed
Serialize class entity to fbs
1 parent aab5e31 commit fdae685

45 files changed

Lines changed: 212 additions & 116 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cpp/libclang/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ rust_binary(
3939
deps = [
4040
"//cpp/libclang/src/utils",
4141
"//cpp/libclang/src/visitor:visit_tu",
42+
"//tools/metamodel/class:class_diagram",
43+
"//tools/serialization/flatbuffers/class:class_serializer",
4244
"@crates//:clang",
4345
"@crates//:clap",
4446
"@crates//:env_logger",

cpp/libclang/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ load("//cpp/libclang:cpp_parser.bzl", "cpp_parser")
2222
2323
cpp_parser(
2424
name = "cpp_parser_include_3rdparty",
25+
emit_debug_json = True,
2526
extra_args = [
2627
],
2728
target = "//cpp/libclang/integration_test/cases/include_3rdparty",
@@ -32,11 +33,14 @@ cpp_parser(
3233
Where:
3334

3435
- `target` is the Bazel target you want to parse.
36+
- `emit_debug_json` is optional and defaults to `False`. Enable it when you want the aggregated `debug.json` sidecar.
3537

3638
Expected result:
3739

3840
- Bazel creates parser output artifact:
39-
- `bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result.json`
41+
- `bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result.fbs.bin`
42+
- When `emit_debug_json = True`, the parser also writes:
43+
- `bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result/debug.json`
4044

4145
## Configure debug logging
4246

@@ -51,4 +55,6 @@ Accepted values are: `error`, `warn`, `info`, `debug`, `trace`.
5155
## Quick check (optional)
5256

5357
```bash
54-
ls -l bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result.json
58+
ls -l bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result.fbs.bin
59+
ls -l bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result/debug.json
60+
```

cpp/libclang/cpp_parser.bzl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,21 @@ def _collect_required_llvm_include_args(cxx_builtin_include_files, extra_config_
191191
return result
192192

193193
def _cpp_parser_impl(ctx):
194-
output = ctx.actions.declare_file(
195-
ctx.label.name + "_result.json",
194+
output_dir = ctx.actions.declare_directory(
195+
ctx.label.name + "_result",
196196
)
197197
libclang = ctx.file._libclang
198198

199199
args = []
200200

201201
args += [
202-
"--output",
203-
output.path,
202+
"--output-dir",
203+
output_dir.path,
204204
]
205205

206+
if ctx.attr.emit_debug_json:
207+
args.append("--json")
208+
206209
target_compilation_flags_list = ctx.attr.target[CompilationFlagsInfo].flags.to_list()
207210

208211
cxx_builtin_include_files = ctx.attr._llvm_cxx_builtin_include.files.to_list()
@@ -232,7 +235,7 @@ def _cpp_parser_impl(ctx):
232235

233236
ctx.actions.run(
234237
inputs = inputs,
235-
outputs = [output],
238+
outputs = [output_dir],
236239
executable = ctx.executable.tool,
237240
tools = [ctx.attr.tool[DefaultInfo].files_to_run],
238241
arguments = args,
@@ -249,7 +252,8 @@ def _cpp_parser_impl(ctx):
249252
)
250253

251254
return DefaultInfo(
252-
files = depset([output]),
255+
files = depset([output_dir]),
256+
runfiles = ctx.runfiles(files = [output_dir]),
253257
)
254258

255259
cpp_parser = rule(
@@ -267,6 +271,10 @@ cpp_parser = rule(
267271
"extra_args": attr.string_list(
268272
default = [],
269273
),
274+
"emit_debug_json": attr.bool(
275+
default = False,
276+
doc = "Emit debug.json alongside the FlatBuffer output. Intended for tests/debugging.",
277+
),
270278
"_libclang": attr.label(
271279
allow_single_file = True,
272280
default = "@llvm_toolchain_llvm//:lib/libclang.so",

cpp/libclang/integration_test/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ This directory contains integration tests for the C++ libclang parser and relate
1818
## Directory Structure
1919

2020
- `cases/`: Each subdirectory is an independent test case, containing C++ sources, BUILD files, and golden `expected.json` outputs.
21-
- `test_framework.rs`: Rust test framework that invokes the parser and compares output to the golden file.
21+
- `test_framework.rs`: Rust test framework that invokes the parser and compares the debug JSON sidecar to the golden file.
2222
- `BUILD`: Bazel build and test rules for integration.
2323

2424
## Test Workflow
2525

2626
1. Each case directory contains C++ source files, headers, a BUILD file, and an `expected.json` golden output.
27-
3. The Rust test framework uses the parser to process the case and compares the output to `expected.json`.
27+
2. The case `cpp_parser(...)` target must set `emit_debug_json = True` so the parser emits the aggregated `debug.json` sidecar required by the test harness.
28+
3. The Rust test framework reads `debug.json` from the parser output directory and compares it to `expected.json`.
2829
4. To batch test all cases:
2930

3031
```bash

cpp/libclang/integration_test/cases/complex_class/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cc_library(
2828

2929
cpp_parser(
3030
name = "parser",
31+
emit_debug_json = True,
3132
extra_args = [
3233
],
3334
target = ":complex_class",

cpp/libclang/integration_test/cases/complex_class/expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"classes": {
2+
"types": {
33
"MyNamespace::MyNamespace2::Car": {
44
"id": "MyNamespace::MyNamespace2::Car",
55
"name": "Car",

cpp/libclang/integration_test/cases/include_3rdparty/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ cc_library(
3131

3232
cpp_parser(
3333
name = "parser",
34+
emit_debug_json = True,
3435
extra_args = [
3536
],
3637
target = ":include_3rdparty",

cpp/libclang/integration_test/cases/include_3rdparty/expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"classes": {
2+
"types": {
33
"Car": {
44
"id": "Car",
55
"name": "Car",

cpp/libclang/integration_test/cases/method_parameter_type/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ cc_library(
2727

2828
cpp_parser(
2929
name = "parser",
30+
emit_debug_json = True,
3031
extra_args = [],
3132
target = ":method_parameter_type",
3233
tool = "//cpp/libclang:clang_rs_parser",

cpp/libclang/integration_test/cases/method_parameter_type/expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"classes": {
2+
"types": {
33
"MethodParameterTypeSample": {
44
"id": "MethodParameterTypeSample",
55
"name": "MethodParameterTypeSample",

0 commit comments

Comments
 (0)