From 778088516962abb103ec94227555f2a7af1f3591 Mon Sep 17 00:00:00 2001 From: lolyu Date: Tue, 23 Jun 2026 14:11:55 +1000 Subject: [PATCH] Add clang-format config and GitHub Actions CI check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds C++ code formatting enforcement for sonic-sairedis: 1. .clang-format — defines the project's coding style based on analysis of existing source conventions: - Allman brace style (every { on its own line) - 4-space indentation, no tabs - Pointer attached to variable name (type *var) - Namespace body indented 4 spaces - Soft 120-char line limit - No auto-sort includes (preserve existing grouping) 2. .github/workflows/clang-format.yml — PR check that runs clang-format-14 only on C/C++ files changed in the PR (not the entire codebase), so pre-existing code is not affected. The SAI/ submodule and debian/ directories are excluded. This follows the pattern established by sonic-net/sonic-linkmgrd which is currently the only other SONiC C++ repo with clang-format. Developers can fix format issues locally with: git diff --name-only HEAD~1 | grep -E '\.(cpp|h)$' | xargs clang-format -i Signed-off-by: Long Xiang Lyu --- .clang-format | 66 +++++++++++++++++++++++++ .github/workflows/clang-format.yml | 79 ++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 .clang-format create mode 100644 .github/workflows/clang-format.yml diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..dc3ed6d340 --- /dev/null +++ b/.clang-format @@ -0,0 +1,66 @@ +# sonic-sairedis C/C++ formatting configuration +# Based on existing codebase conventions: +# - Allman brace style (every { on its own line) +# - 4-space indentation, no tabs +# - Namespace body indented +# - Pointer attached to variable name (type *var) +# - No hard line length limit (soft 120) + +BasedOnStyle: Google + +# Indentation +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +ContinuationIndentWidth: 8 + +# Allman brace style — every opening brace on its own line +BreakBeforeBraces: Allman + +# Access modifiers indented 4 spaces inside class body +# (i.e., same indent as class keyword, not as members) +AccessModifierOffset: -4 + +# No short constructs on single lines (matches codebase style) +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false + +# Pointer/reference attached to variable name (majority convention) +PointerAlignment: Right + +# Soft line length limit (120 matches current practice) +ColumnLimit: 120 + +# Namespace — indent body (matches current style) +NamespaceIndentation: All + +# Includes — do NOT auto-sort (existing grouping is intentional) +SortIncludes: false +IncludeBlocks: Preserve + +# Constructor initializer list +BreakConstructorInitializers: BeforeColon +ConstructorInitializerIndentWidth: 8 +ConstructorInitializerAllOnOneLineOrOnePerLine: false + +# Function parameters — break before all if they don't fit +BinPackParameters: false +BinPackArguments: false + +# Template parameters +AlwaysBreakTemplateDeclarations: Yes + +# Misc +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Cpp11BracedListStyle: true +Standard: c++17 + +# Empty lines +MaxEmptyLinesToKeep: 1 +KeepEmptyLinesAtTheStartOfBlocks: false diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 0000000000..f4736c4cfb --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,79 @@ +name: C++ clang-format check + +on: + pull_request: + branches: + - master + - "202???*" + - "201???*" + paths: + - "**.cpp" + - "**.h" + - ".clang-format" + - ".github/workflows/clang-format.yml" + +jobs: + clang-format: + name: clang-format (changed files only) + runs-on: ubuntu-22.04 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install clang-format + run: | + sudo apt-get update -qq + sudo apt-get install -y clang-format-14 + sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-14 100 + clang-format --version + + - name: Check formatting on changed C/C++ files + run: | + # Get list of C/C++ files changed in this PR vs base branch + BASE=${{ github.event.pull_request.base.sha }} + HEAD=${{ github.event.pull_request.head.sha }} + + CHANGED=$(git diff --name-only "$BASE" "$HEAD" \ + | grep -E '\.(cpp|h)$' \ + | grep -vE '^SAI/' \ + | grep -vE '^debian/' \ + || true) + + if [ -z "$CHANGED" ]; then + echo "No C/C++ files changed — skipping format check." + exit 0 + fi + + echo "Checking format for:" + echo "$CHANGED" + echo "" + + FORMAT_ERRORS=0 + while IFS= read -r file; do + if [ ! -f "$file" ]; then + continue + fi + DIFF=$(clang-format --style=file "$file" | diff "$file" - || true) + if [ -n "$DIFF" ]; then + echo "::error file=$file::Format violation detected" + echo "--- $file" + echo "$DIFF" + FORMAT_ERRORS=$((FORMAT_ERRORS + 1)) + fi + done <<< "$CHANGED" + + if [ "$FORMAT_ERRORS" -gt 0 ]; then + echo "" + echo "❌ $FORMAT_ERRORS file(s) have formatting issues." + echo "" + echo "Fix with:" + echo " clang-format -i " + echo "Or fix all changed files:" + echo " git diff --name-only HEAD~1 | grep -E '\\.(cpp|h)$' | xargs clang-format -i" + exit 1 + fi + + echo "✅ All changed files pass clang-format check."