Erik Rigtorp

ClangFormat usage tips

In this note I provide useful tips on how to use ClangFormat (somtimes called clang-format).

Run clang-format to check if a file is already formatted:

clang-format --dry-run -Werror foo.c || echo "not formatted!"

Run clang-format in parallel to check if files are formatted:

find . -iname "*.c" -or -iname "*.h" | xargs -P0 -I{} clang-format --dry-run -Werror {}

Run clang-format in parallel to reformat files:

find . -iname "*.c" -or -iname "*.h" | xargs -P0 -I{} clang-format -i {}

Useful command line options:

  • -Werror: causes clang-format to return non-zero exit status if any input file is not already formatted.
  • --dry-run: don’t actually format, only check if files are formatted