Skip to content

Add clang-format config and GitHub Actions CI check#1949

Open
lolyu wants to merge 1 commit into
sonic-net:masterfrom
lolyu:add-clang-format
Open

Add clang-format config and GitHub Actions CI check#1949
lolyu wants to merge 1 commit into
sonic-net:masterfrom
lolyu:add-clang-format

Conversation

@lolyu

@lolyu lolyu commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Description of PR

Adds C++ code formatting enforcement to sonic-sairedis using clang-format.

Type of change

  • Code quality improvement
  • CI/CD improvement

What is included

.clang-format - project style config derived from analysis of existing source conventions across syncd/, lib/, meta/, vslib/, and proxylib/:

  • Allman brace style (every { on its own line - matches existing code)
  • 4-space indentation, no tabs
  • Pointer attached to variable name (type *var - matches majority convention)
  • Namespace body indented (matches existing style)
  • Soft 120-char line limit
  • Includes not auto-sorted (preserves existing intentional grouping)

.github/workflows/clang-format.yml - PR check that:

  • Runs on PRs to master and versioned branches
  • Only checks C/C++ files changed in the PR (not the full codebase) - pre-existing code is unaffected
  • Excludes SAI/ submodule and debian/ directory
  • Uses clang-format-14 for consistency
  • Fails with clear error messages showing exactly which files need fixing

Why diff-only?

Enforcing format on the entire existing codebase at once would create a massive diff that breaks git blame. The diff-only approach allows gradual adoption: new/modified code must be formatted, existing untouched code is not flagged.

Developer workflow

Fix format issues locally before pushing:

sudo apt-get install -y clang-format-14
git diff --name-only HEAD~1 | grep -E '\.(cpp|h)$' | xargs clang-format -i

This follows the pattern established by sonic-net/sonic-linkmgrd which is currently the only other SONiC C++ repo with clang-format enforcement.

Back port request

  • 202311
  • 202405
  • 202411
  • 202505
  • 202511
  • 202512
  • 202605

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 <lonxlyu@microsoft.com>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants