Skip to content

Commit edced63

Browse files
committed
Migrate cpp parser
1 parent 32ee3cb commit edced63

93 files changed

Lines changed: 6572 additions & 1 deletion

File tree

Some content is hidden

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

MODULE.bazel

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,41 @@ crate.spec(
128128
package = "flatbuffers",
129129
version = "25.9.23",
130130
)
131+
crate.spec(
132+
package = "clang",
133+
version = "=1.0.3",
134+
)
135+
crate.spec(
136+
package = "clang-sys",
137+
version = "=1.8.1",
138+
)
139+
crate.spec(
140+
package = "libloading",
141+
version = ">=0.9.0",
142+
)
143+
crate.spec(
144+
package = "assert-json-diff",
145+
version = "2.0.0",
146+
)
147+
148+
# Annotations for libclang
149+
crate.annotation(
150+
crate = "clang-sys",
151+
crate_features = [
152+
"runtime",
153+
"clang_18_0",
154+
],
155+
patch_args = ["-p1"],
156+
patches = [
157+
"//third_party:libclang/patchv1.8.2.patch",
158+
"//third_party:libclang/patchv1.9.0.patch",
159+
],
160+
deps = ["@crates//:libloading"],
161+
)
162+
crate.annotation(
163+
crate = "clang",
164+
crate_features = ["runtime"],
165+
)
131166
crate.from_specs()
132167
use_repo(crate, "crates")
133168

@@ -294,3 +329,37 @@ pip.parse(
294329
},
295330
)
296331
use_repo(pip, "pip_ai_checker")
332+
333+
###############################################################################
334+
# LLVM Toolchain
335+
###############################################################################
336+
bazel_dep(name = "toolchains_llvm", version = "1.6.0", dev_dependency = True)
337+
338+
llvm = use_extension(
339+
"@toolchains_llvm//toolchain/extensions:llvm.bzl",
340+
"llvm",
341+
dev_dependency = True,
342+
)
343+
llvm.toolchain(
344+
compile_flags = {"": [
345+
"-march=nehalem",
346+
"-ffp-model=strict",
347+
# Security
348+
"-U_FORTIFY_SOURCE", # https://github.com/google/sanitizers/issues/247
349+
"-fstack-protector",
350+
"-fno-omit-frame-pointer",
351+
# Diagnostics
352+
"-fcolor-diagnostics",
353+
"-Wno-deprecated-declarations",
354+
"-Wno-error=self-assign-overloaded",
355+
"-Wthread-safety",
356+
]},
357+
cxx_standard = {"": "c++17"},
358+
extra_link_libs = {"": [
359+
"-lrt",
360+
# This is the agreed way to ensure linking for targets using std::atomic operations.
361+
"-latomic",
362+
]},
363+
llvm_version = "19.1.7",
364+
)
365+
use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm")

cpp/libclang/BUILD

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# *******************************************************************************
2+
# Copyright (c) 2026 Contributors to the Eclipse Foundation
3+
#
4+
# See the NOTICE file(s) distributed with this work for additional
5+
# information regarding copyright ownership.
6+
#
7+
# This program and the accompanying materials are made available under the
8+
# terms of the Apache License Version 2.0 which is available at
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# SPDX-License-Identifier: Apache-2.0
12+
# *******************************************************************************
13+
load("@rules_rust//rust:defs.bzl", "rust_binary")
14+
15+
rust_binary(
16+
name = "clang_rs_parser",
17+
srcs = ["src/main.rs"],
18+
data = [
19+
"@llvm_toolchain_llvm//:libclang",
20+
],
21+
env = {
22+
"LIBCLANG_PATH": "$(rootpath @llvm_toolchain_llvm//:libclang)",
23+
},
24+
visibility = ["//visibility:public"],
25+
deps = [
26+
"//cpp/libclang/src/visitor:visit_tu",
27+
"@crates//:clang",
28+
"@crates//:clap",
29+
"@crates//:serde_json",
30+
],
31+
)

cpp/libclang/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Run C++ parser targets
2+
3+
## Configure a parser target in `BUILD`
4+
5+
If you want to parse a specific Bazel target, use the `cpp_parser(...)` rule in the `BUILD` file like:
6+
7+
```
8+
load("//cpp/libclang:cpp_parser.bzl", "cpp_parser")
9+
10+
cpp_parser(
11+
name = "cpp_parser_include_3rdparty",
12+
extra_args = [
13+
],
14+
target = "//cpp/libclang/integration_test/cases/include_3rdparty",
15+
tool = ":clang_rs_parser",
16+
)
17+
```
18+
19+
Where:
20+
21+
- `target` is the Bazel target you want to parse.
22+
23+
Expected result:
24+
25+
- Bazel creates parser output artifact:
26+
- `bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result.json`
27+
28+
## Quick check (optional)
29+
30+
```bash
31+
ls -l bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result.json

0 commit comments

Comments
 (0)