Skip to content

Commit fbfef8e

Browse files
authored
[rules_score] libclang parser
- Add parser for cpp code on libclang basis - Include Rust Crates
1 parent 32ee3cb commit fbfef8e

90 files changed

Lines changed: 6433 additions & 0 deletions

Some content is hidden

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

.github/workflows/tests.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@ jobs:
5757
- name: Ensure correct dependency resolution
5858
run: |
5959
bazel mod deps --lockfile_mode=update
60+
- name: Run Libclang Parser Tooling clippy
61+
run: |
62+
bazel build //cpp/libclang/... --config=clippy
63+
- name: Run Libclang Parser Tooling tests
64+
run: |
65+
bazel test //cpp/libclang/...

MODULE.bazel

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,36 @@ 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+
deps = ["@crates//:libloading"],
156+
)
157+
crate.annotation(
158+
crate = "clang",
159+
crate_features = ["runtime"],
160+
)
131161
crate.from_specs()
132162
use_repo(crate, "crates")
133163

@@ -294,3 +324,37 @@ pip.parse(
294324
},
295325
)
296326
use_repo(pip, "pip_ai_checker")
327+
328+
###############################################################################
329+
# LLVM Toolchain
330+
###############################################################################
331+
bazel_dep(name = "toolchains_llvm", version = "1.6.0", dev_dependency = True)
332+
333+
llvm = use_extension(
334+
"@toolchains_llvm//toolchain/extensions:llvm.bzl",
335+
"llvm",
336+
dev_dependency = True,
337+
)
338+
llvm.toolchain(
339+
compile_flags = {"": [
340+
"-march=nehalem",
341+
"-ffp-model=strict",
342+
# Security
343+
"-U_FORTIFY_SOURCE", # https://github.com/google/sanitizers/issues/247
344+
"-fstack-protector",
345+
"-fno-omit-frame-pointer",
346+
# Diagnostics
347+
"-fcolor-diagnostics",
348+
"-Wno-deprecated-declarations",
349+
"-Wno-error=self-assign-overloaded",
350+
"-Wthread-safety",
351+
]},
352+
cxx_standard = {"": "c++17"},
353+
extra_link_libs = {"": [
354+
"-lrt",
355+
# This is the agreed way to ensure linking for targets using std::atomic operations.
356+
"-latomic",
357+
]},
358+
llvm_version = "19.1.7",
359+
)
360+
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
14+
# Run C++ parser targets
15+
16+
## Configure a parser target in `BUILD`
17+
18+
If you want to parse a specific Bazel target, use the `cpp_parser(...)` rule in the `BUILD` file like:
19+
20+
```
21+
load("//cpp/libclang:cpp_parser.bzl", "cpp_parser")
22+
23+
cpp_parser(
24+
name = "cpp_parser_include_3rdparty",
25+
extra_args = [
26+
],
27+
target = "//cpp/libclang/integration_test/cases/include_3rdparty",
28+
tool = ":clang_rs_parser",
29+
)
30+
```
31+
32+
Where:
33+
34+
- `target` is the Bazel target you want to parse.
35+
36+
Expected result:
37+
38+
- Bazel creates parser output artifact:
39+
- `bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result.json`
40+
41+
## Quick check (optional)
42+
43+
```bash
44+
ls -l bazel-bin/cpp/libclang/cpp_parser_include_3rdparty_result.json

0 commit comments

Comments
 (0)