My first GitHub Actions extension: A tool to apply Clang format to C++ code

Wait 5 sec.

[This article was first published on pacha.dev/blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. If this post is useful to you I kindly ask a minimal donation on Buy Me a Coffee. It shall be used to continue my Open Source efforts. The full explanation is here: A Personal Message from an Open Source Contributor.You can send me questions for the blog using this form and subscribe to receive an email when there is a new post.I have been trying to contribute back to the cpp11 package for around one year. I found it useful for my PhD project, and I wanted to help with its maintenance. However, a major challenge I found contribute was the C++ code style used in the package.Earlier in 2025 I switched to Manjaro Linux and it no longer provides binaries for Clang 12, which is the version used in cpp11. Building clang-format-12 required me to download and compile Clang from the LLVM project’s source code, which was time consuming.Some PRs sent to cpp11 will fail when these do not strictly follow the Clang 12 linting style, as the actions will fail for any non-empty diff. For reasons beyond my knowledge, using linting tools for VSCode or Positron does not always produce the same results as doing this:clang_format=`which clang-format`format: $(shell find . -name '*.hpp') $(shell find . -name '*.cpp') @${clang_format} -i $?So I decided to create my own extension that would apply Clang format to C++ code and amend a pushed commit if the linting does not produce the same results as the unmodified files. This way, I could send PRs to cpp11 without worrying about the code style, and it would save compilation time for many users like me.I created the clang-format repository to automate the process of compiling clang-format-11, clang-format-12, …, and clang-format-19 from the LLVM source code, and made the compiled binaries available for any 64-bit Linux distribution either for local use or in GitHub Actions workflows.Here is an example repository that shows how to use the clang-format extension with any GitHub repository.How it works:The user pushes a code with formatting issuesGitHub Actions run on the push/PRThis extension will use clang-format-11, clang-format-12, …, or clang-format-19 as requested to fix formatting and implement the changes in the repositoryWhen the code is properly formatted, the action will do nothing.Correctly formatted files on a previous runSimple example workflow to use the extension to fix code and commit back automatically:name: Auto-format C++ Codeon: push: branches: [ main, master, develop ] pull_request: branches: [ main, master, develop ]permissions: contents: write pull-requests: writejobs: format: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: token: ${{ secrets.GITHUB_TOKEN }} persist-credentials: true - name: Format C++ code uses: pachadotdev/clang-format@main with: version: '18' auto-commit: true commit-message: 'style: auto-format C++ code with clang-format-18'After pushing, you should see: Action runs successfully Code gets properly formatted Automatic commit with formatted changes Clean, consistent code styleThe example repository shows more examples and other use cases for Pull Requests. To leave a comment for the author, please follow the link and comment on their blog: pacha.dev/blog.R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.Continue reading: My first GitHub Actions extension: A tool to apply Clang format to C++ code