Skip to content

Commit 136fb66

Browse files
nunoplopesHenriquePretojoaotgouveiajsillllucic71
committed
initial import
Co-authored-by: Henrique Preto <henrique.preto@tecnico.ulisboa.pt> Co-authored-by: João Gouveia <joaotalonegouveia@tecnico.ulisboa.pt> Co-authored-by: João Silveira <joao.freixial.silveira@tecnico.ulisboa.pt> Co-authored-by: Lucian Popescu <lucian.popescu@tecnico.ulisboa.pt>
0 parents  commit 136fb66

810 files changed

Lines changed: 75997 additions & 0 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.

.clang-format

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
BasedOnStyle: LLVM
2+
IncludeBlocks: Regroup
3+
IncludeCategories:
4+
- Regex: '^<(clang|llvm)/'
5+
Priority: 1
6+
- Regex: '^<'
7+
Priority: 2
8+
- Regex: '^"'
9+
Priority: 3
10+
SortIncludes: CaseSensitive

.clang-tidy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Checks: "*,
2+
-abseil-*,
3+
-altera-*,
4+
-android-*,
5+
-fuchsia-*,
6+
-google-*,
7+
-llvm*,
8+
-modernize-use-trailing-return-type,
9+
-zircon-*,
10+
-readability-else-after-return,
11+
-readability-static-accessed-through-instance,
12+
-readability-avoid-const-params-in-decls,
13+
-cppcoreguidelines-non-private-member-variables-in-classes,
14+
-misc-non-private-member-variables-in-classes,
15+
-readability-identifier-length,
16+
"

.github/workflows/format.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Format Check
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
format:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout code
10+
uses: actions/checkout@v6
11+
12+
- name: Setup LLVM 22
13+
uses: ZhongRuoyu/setup-llvm@v0
14+
with:
15+
llvm-version: 22
16+
17+
- name: Install Rust toolchain
18+
run: |
19+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain 1.94.0
20+
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
21+
22+
- name: Check C++ formatting
23+
run: find cpp2rust tests -name '*.cpp' -o -name '*.h' | xargs clang-format --dry-run --Werror
24+
25+
- name: Check Rust formatting
26+
run: |
27+
cargo fmt --manifest-path rules/Cargo.toml -- --check
28+
cargo fmt --manifest-path rule_preprocessor/Cargo.toml -- --check
29+
cargo fmt --manifest-path libcc2rs/Cargo.toml -- --check

.github/workflows/run-tests.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Run tests
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
test:
7+
strategy:
8+
fail-fast: false
9+
matrix:
10+
os: [ubuntu-latest, macos-latest]
11+
build_type: [Debug, Release]
12+
test:
13+
- name: unit-tests
14+
target: check
15+
- name: benchmarks (skip-run)
16+
target: check-benchmarks
17+
skip_run: "1"
18+
19+
runs-on: ${{ matrix.os }}
20+
name: ${{ matrix.test.name }} (${{ matrix.os }}, ${{ matrix.build_type }})
21+
22+
steps:
23+
- name: Checkout code
24+
uses: actions/checkout@v6
25+
26+
- name: Setup LLVM 22
27+
uses: ZhongRuoyu/setup-llvm@v0
28+
with:
29+
llvm-version: 22
30+
31+
- name: Install dependencies (Linux)
32+
if: runner.os == 'Linux'
33+
run: sudo apt-get install -y python3-tomli
34+
35+
- name: Install dependencies (macOS)
36+
if: runner.os == 'macOS'
37+
run: |
38+
pip3 install tomli --break-system-packages
39+
40+
- name: Install Rust toolchain
41+
run: |
42+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain 1.94.0
43+
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
44+
45+
- name: Configure
46+
run: |
47+
mkdir build && cd build
48+
cmake -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
49+
-DLIT_FLAGS="-v" \
50+
-GNinja ..
51+
52+
- name: Build
53+
run: ninja
54+
working-directory: build
55+
56+
- name: Run tests
57+
run: ninja ${{ matrix.test.target }}
58+
working-directory: build
59+
env:
60+
SKIP_RUN: ${{ matrix.test.skip_run }}

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# CMake
2+
build/
3+
4+
# Cargo
5+
target/
6+
Cargo.lock
7+
8+
# Python
9+
*.pyc
10+
__pycache__
11+
12+
# VSCode
13+
.vscode
14+
15+
# Clangd
16+
.cache/
17+
18+
tmp
19+
*.swp
20+
21+
# Merge/rebase conflict artifacts
22+
*.orig
23+
*.BACKUP.*
24+
*.BASE.*
25+
*.LOCAL.*
26+
*.REMOTE.*
27+
28+
# Integration tests
29+
tests/cpp2rust-tests/

CMakeLists.txt

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
set(CMAKE_CXX_STANDARD 23)
4+
set(CMAKE_CXX_EXTENSIONS OFF)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
project(Cpp2Rust)
8+
9+
if (NOT CMAKE_BUILD_TYPE)
10+
set(CMAKE_BUILD_TYPE Debug)
11+
endif()
12+
13+
if (MSVC)
14+
message(FATAL_ERROR "MSVC is not supported")
15+
else()
16+
# Set up the flags for GCC/Clang
17+
set(CMAKE_CXX_FLAGS_RELEASE "-O3 ${CMAKE_CXX_FLAGS_RELEASE}")
18+
set(CMAKE_CXX_FLAGS_DEBUG "-g -Og ${CMAKE_CXX_FLAGS_DEBUG}")
19+
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -g ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
20+
set(CMAKE_CXX_FLAGS "-Wall -Werror ${CMAKE_CXX_FLAGS}")
21+
22+
# Sanitizers
23+
# set(CMAKE_CXX_FLAGS_DEBUG "-fsanitize=address -fsanitize=undefined -fsanitize=leak ${CMAKE_CXX_FLAGS_DEBUG}")
24+
# set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-fsanitize=address -fsanitize=undefined -fsanitize=leak ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
25+
endif()
26+
27+
find_package(LLVM REQUIRED CONFIG)
28+
find_package(Clang REQUIRED CONFIG)
29+
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
30+
message(STATUS "Using ClangConfig.cmake in: ${Clang_DIR}")
31+
32+
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
33+
list(APPEND CMAKE_MODULE_PATH "${CLANG_CMAKE_DIR}")
34+
include(AddLLVM)
35+
include(AddClang)
36+
37+
if (NOT CMAKE_CXX_COMPILER MATCHES "clang")
38+
set(CMAKE_CXX_COMPILER "${CLANG_CMAKE_DIR}/../../../bin/clang++")
39+
endif()
40+
41+
message(STATUS "C++ flags: ${CMAKE_CXX_FLAGS}")
42+
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
43+
message(STATUS "C Compiler: ${CMAKE_C_COMPILER}")
44+
message(STATUS "C++ Compiler: ${CMAKE_CXX_COMPILER}")
45+
46+
execute_process(
47+
COMMAND ${CMAKE_CXX_COMPILER} --version
48+
OUTPUT_VARIABLE CLANG_OUTPUT
49+
OUTPUT_STRIP_TRAILING_WHITESPACE
50+
)
51+
message(STATUS ${CLANG_OUTPUT})
52+
53+
execute_process(
54+
COMMAND rustc --version --verbose
55+
OUTPUT_VARIABLE RUSTC_OUTPUT
56+
OUTPUT_STRIP_TRAILING_WHITESPACE
57+
)
58+
string(REGEX MATCH "LLVM version: .*" RUSTC_LLVM_VERSION_LINE "${RUSTC_OUTPUT}")
59+
string(REGEX REPLACE "LLVM version: " "" RUSTC_LLVM_VERSION "${RUSTC_LLVM_VERSION_LINE}")
60+
message(STATUS "rustc using LLVM ${RUSTC_LLVM_VERSION}")
61+
62+
if (NOT RUSTC_LLVM_VERSION STREQUAL LLVM_PACKAGE_VERSION)
63+
message(WARNING "The LLVM versions used by rustc and clang are different!")
64+
endif()
65+
66+
set(RUST_STABLE_VERSION "1.94.0")
67+
68+
function(write_rust_toolchain DIR)
69+
set(_content "[toolchain]\nchannel = \"${RUST_STABLE_VERSION}\"\n")
70+
set(_file ${DIR}/rust-toolchain.toml)
71+
if (EXISTS ${_file})
72+
file(READ ${_file} _existing)
73+
endif()
74+
if (NOT "${_existing}" STREQUAL "${_content}")
75+
file(WRITE ${_file} ${_content})
76+
endif()
77+
endfunction()
78+
79+
# rule_preprocessor uses nightly
80+
foreach(_dir libcc2rs rules)
81+
write_rust_toolchain(${CMAKE_SOURCE_DIR}/${_dir})
82+
endforeach()
83+
84+
add_custom_target("install-rust-toolchain"
85+
COMMAND rustup toolchain install ${RUST_STABLE_VERSION}
86+
USES_TERMINAL
87+
)
88+
89+
add_subdirectory(cpp2rust)
90+
add_subdirectory(libcc2rs)
91+
add_subdirectory(tests)
92+
93+
find_program(CLANG_FORMAT clang-format-22 clang-format)
94+
file(GLOB_RECURSE ALL_CXX_SOURCES
95+
cpp2rust/*.cpp cpp2rust/*.h tests/*.cpp)
96+
97+
add_custom_target("format"
98+
COMMAND ${CLANG_FORMAT} -i ${ALL_CXX_SOURCES}
99+
COMMAND rustup run ${RUST_STABLE_VERSION} cargo fmt --manifest-path ${PROJECT_SOURCE_DIR}/rules/Cargo.toml
100+
COMMAND rustup run ${RUST_STABLE_VERSION} cargo fmt --manifest-path ${PROJECT_SOURCE_DIR}/rule_preprocessor/Cargo.toml
101+
COMMAND rustup run ${RUST_STABLE_VERSION} cargo fmt --manifest-path ${PROJECT_SOURCE_DIR}/libcc2rs/Cargo.toml
102+
DEPENDS "install-rust-toolchain"
103+
COMMENT "Running clang-format and cargo fmt on all source files"
104+
VERBATIM
105+
)
106+
107+
if (NOT LIT_FLAGS)
108+
set(LIT_FLAGS "-s;--show-xfail;-vv")
109+
endif()
110+
111+
add_custom_target("check-libcc2rs"
112+
COMMAND cargo test
113+
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/libcc2rs"
114+
DEPENDS install-rust-toolchain
115+
USES_TERMINAL
116+
)
117+
118+
add_custom_target("check-unit"
119+
COMMAND "python3"
120+
"${PROJECT_SOURCE_DIR}/tests/lit/lit.py"
121+
${LIT_FLAGS}
122+
"${PROJECT_SOURCE_DIR}/tests/unit"
123+
"${PROJECT_SOURCE_DIR}/tests/ub"
124+
DEPENDS cpp2rust libcc2rs
125+
USES_TERMINAL
126+
)
127+
128+
add_custom_target("check-benchmarks"
129+
COMMAND "python3"
130+
"${PROJECT_SOURCE_DIR}/tests/lit/lit.py"
131+
${LIT_FLAGS}
132+
"${PROJECT_SOURCE_DIR}/tests/benchmarks"
133+
DEPENDS cpp2rust libcc2rs
134+
USES_TERMINAL
135+
)
136+
137+
add_custom_target("check"
138+
DEPENDS check-libcc2rs check-unit
139+
)
140+
141+
add_custom_target("gen-rules"
142+
COMMAND find ${PROJECT_SOURCE_DIR}/rules -name "ir*.json" -delete
143+
COMMAND cargo build
144+
COMMAND ${CMAKE_COMMAND} -E chdir ${PROJECT_SOURCE_DIR}/rule_preprocessor
145+
${CMAKE_COMMAND} -E env
146+
CARGO_TARGET_DIR=${PROJECT_SOURCE_DIR}/rule_preprocessor/target
147+
cargo clean
148+
COMMAND ${CMAKE_COMMAND} -E chdir ${PROJECT_SOURCE_DIR}/rule_preprocessor
149+
${CMAKE_COMMAND} -E env
150+
CARGO_TARGET_DIR=${PROJECT_SOURCE_DIR}/rule_preprocessor/target
151+
cargo run
152+
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/rules"
153+
DEPENDS install-rust-toolchain
154+
USES_TERMINAL
155+
)

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022-present INESC-ID.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)