clang-format.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/sh
  2. # Copyright 2020, The Tor Project, Inc.
  3. # See LICENSE for licensing information.
  4. #
  5. # DO NOT COMMIT OR MERGE CODE THAT IS RUN THROUGH THIS TOOL YET.
  6. #
  7. # WE ARE STILL DISCUSSING OUR DESIRED STYLE AND ITERATING ON IT.
  8. # (12 Feb 2020)
  9. #
  10. # This script runs "clang-format" and "codetool" in sequence over each of
  11. # our source files, and replaces the original file only if it has changed.
  12. #
  13. # We can't just use clang-format -i, since we also want to use codetool to
  14. # reformat a few things back to how we want them, and we want avoid changing
  15. # the mtime on files that didn't actually change.
  16. set -e
  17. cd "$(dirname "$0")/../../src/"
  18. # Shellcheck complains that a for loop over find's output is unreliable,
  19. # since there might be special characters in the output. But we happen
  20. # to know that none of our C files have special characters or spaces in
  21. # their names, so this is safe.
  22. #
  23. # shellcheck disable=SC2044
  24. for fname in $(find lib core feature app test tools -name '[^.]*.[ch]'); do
  25. tmpfname="${fname}.clang_fmt.tmp"
  26. rm -f "${tmpfname}"
  27. clang-format --style=file "${fname}" > "${tmpfname}"
  28. ../scripts/maint/codetool.py "${tmpfname}"
  29. if cmp "${fname}" "${tmpfname}" >/dev/null 2>&1; then
  30. echo "No change in ${fname}"
  31. rm -f "${tmpfname}"
  32. else
  33. echo "Change in ${fname}"
  34. mv "${tmpfname}" "${fname}"
  35. fi
  36. done