From ff9d5fcee2aa51425962631d4513e8e9ac63a56b Mon Sep 17 00:00:00 2001 From: Hugo Date: Fri, 30 Jan 2026 04:40:23 +0100 Subject: [PATCH 1/2] chore: add clang-format configuration --- .clang-format | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..0149e40 --- /dev/null +++ b/.clang-format @@ -0,0 +1,12 @@ +BasedOnStyle: LLVM +Standard: c++17 +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +BreakBeforeBraces: Allman +AllowShortFunctionsOnASingleLine: Empty +ColumnLimit: 100 +PointerAlignment: Left +NamespaceIndentation: All +SortIncludes: Never +ReflowComments: false From 1597ac80e0131227eb1f1c2b6aa7a0a5fa3a7c96 Mon Sep 17 00:00:00 2001 From: Hugo Date: Fri, 30 Jan 2026 04:46:33 +0100 Subject: [PATCH 2/2] chore: add clang-format scripts and CI --- .github/workflows/clang-format.yml | 22 ++++++++++++++++++++ CMakeLists.txt | 13 ++++++++++++ README.md | 8 ++++++++ scripts/format-check.sh | 32 ++++++++++++++++++++++++++++++ scripts/format.sh | 20 +++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 .github/workflows/clang-format.yml create mode 100755 scripts/format-check.sh create mode 100755 scripts/format.sh diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 0000000..ea07d8b --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,22 @@ +name: clang-format + +on: + push: + branches: [main, master] + pull_request: + +jobs: + format-check: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install clang-format 17 + run: | + sudo apt-get update + sudo apt-get install -y clang-format-17 + sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-17 100 + + - name: Run format-check + run: ./scripts/format-check.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f09521..7bc03ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -176,3 +176,16 @@ coretrace_add_tool( CLI_SOURCE ${TOOL_CLI_SOURCE} LLVM_COMPONENTS ${LLVM_COMPONENTS} ) + +# ============ +# FORMATTING +# ============ +add_custom_target(format + COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/scripts/format.sh" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMENT "Run clang-format on source files") + +add_custom_target(format-check + COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/scripts/format-check.sh" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMENT "Verify clang-format compliance") diff --git a/README.md b/README.md index d8f2508..344ace4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ # coretrace-tool-template Standardized template repository for building CoreTrace tools with a unified architecture, CI pipeline, and best practices for scalable static and dynamic analysis tooling. + +## Code style (clang-format) + +- Version cible : `clang-format` 17 (utilisée dans la CI). +- Formater : `./scripts/format.sh` +- Vérifier sans modifier : `./scripts/format-check.sh` +- CMake : `cmake --build build --target format` ou `--target format-check` +- CI : job GitHub Actions `clang-format` qui échoue si le formatage diverge. diff --git a/scripts/format-check.sh b/scripts/format-check.sh new file mode 100755 index 0000000..35ac7f0 --- /dev/null +++ b/scripts/format-check.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)" + +files=() +while IFS= read -r -d '' file; do + files+=("$file") +done < <(find "${REPO_ROOT}" \ + \( -path "${REPO_ROOT}/build" -o -path "${REPO_ROOT}/extern-project" -o -path "${REPO_ROOT}/external" -o -path "${REPO_ROOT}/third_party" -o -path "${REPO_ROOT}/vendor" -o -path "${REPO_ROOT}/.git" \) -prune -o \ + -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.h' -o -name '*.hh' -o -name '*.hpp' -o -name '*.hxx' \) -print0) + +if [ "${#files[@]}" -eq 0 ]; then + echo "No source files to check." + exit 0 +fi + +echo "Checking formatting on ${#files[@]} files..." +failed=0 +for file in "${files[@]}"; do + if ! clang-format --dry-run --Werror "${file}"; then + failed=1 + fi +done + +if [ "${failed}" -ne 0 ]; then + echo "Formatting check failed. Run scripts/format.sh to fix." + exit 1 +fi + +echo "Formatting is clean." diff --git a/scripts/format.sh b/scripts/format.sh new file mode 100755 index 0000000..1f595b4 --- /dev/null +++ b/scripts/format.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)" + +files=() +while IFS= read -r -d '' file; do + files+=("$file") +done < <(find "${REPO_ROOT}" \ + \( -path "${REPO_ROOT}/build" -o -path "${REPO_ROOT}/extern-project" -o -path "${REPO_ROOT}/external" -o -path "${REPO_ROOT}/third_party" -o -path "${REPO_ROOT}/vendor" -o -path "${REPO_ROOT}/.git" \) -prune -o \ + -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.cxx' -o -name '*.h' -o -name '*.hh' -o -name '*.hpp' -o -name '*.hxx' \) -print0) + +if [ "${#files[@]}" -eq 0 ]; then + echo "No source files to format." + exit 0 +fi + +echo "Formatting ${#files[@]} files with clang-format (style from ${REPO_ROOT}/.clang-format)..." +clang-format -i "${files[@]}"