Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
32 changes: 32 additions & 0 deletions scripts/format-check.sh
Original file line number Diff line number Diff line change
@@ -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."
20 changes: 20 additions & 0 deletions scripts/format.sh
Original file line number Diff line number Diff line change
@@ -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[@]}"