Skip to content

Commit

Permalink
tools: Add clang-formatting tools
Browse files Browse the repository at this point in the history
This adds two tools to the ./tools/ subdirectory:

== clang-formatter.sh ==

This is simply a small shell script that can be executed from the top of
the UHD repository, and it will format all files according the the
.clang-format file. It can be executed as such:

    $ CLANG_FORMAT=clang-format-14 ./tools/clang-formatter.sh apply

Specifying a clang-format executable is optional, but note that
clang-format 14.0 should be used.

== run-clang-format.py ==

This is a Python script that is a modified version from
https://github.com/gnuradio/clang-format-lint-action/blob/ \
    0b0cb14cf220a070d2a8b2610bd74ad1546252a1/run-clang-format.py

It was modified to add --patch-file option.

Alongside this file is a .clang-format-ignore file, which is sourced
from this script. The command can be run as such:

    $ ./tools/run-clang-format.py \
        --clang-format-executable clang-format-14 \
	--extensions c,cpp,h,hpp,hpp.in,ipp \
	-r \
	--patch-file format.patch \
	/path/to/uhd-repo

It will provide both a nice output summary as well as a patch file that
can be consumed with `patch -p0 < format.patch`.
  • Loading branch information
mbr0wn authored and atomita-ni committed Aug 7, 2023
1 parent 030678a commit 0cc18f7
Show file tree
Hide file tree
Showing 3 changed files with 486 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .clang-format-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
./host/cmake
./host/lib/deps
./mpm/lib/mykonos
./mpm/lib/rfdc
./mpm/include/mpm/rfdc
./mpm/tools
./fpga
./tools
./firmware
*cdecode.*
*getopt.*
*_generated.h
*template_lvbitx.*
# Ignore all the junk
.git
.vscode
./host/build
.ccls-cache
48 changes: 48 additions & 0 deletions tools/clang-formatter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

cformat=${CLANG_FORMAT:-}
if [ -z "$cformat" ]; then
cformat=$(which clang-format)
fi

_cmd=$1
if [ -z "$_cmd" ]; then
_cmd="apply"
fi

cformat_args=""
case $_cmd in
apply)
cformat_args="-i"
;;
check)
cformat_args="-Werror --dry-run"
;;
check_apply)
cformat_args="-Werror -i"
;;
*)
echo "Usage: $0 [check|apply|check_apply]"
esac

find . \
-path './host/lib/deps' -prune -o \
-path './host/cmake' -prune -o \
-path './fpga' -prune -o \
-path './firmware' -prune -o \
-path './mpm/lib/mykonos' -prune -o \
-path './mpm/lib/rfdc' -prune -o \
-path './mpm/include/mpm/rfdc' -prune -o \
-path './mpm/tools' -prune -o \
-path './tools' -prune -o \
-name "getopt.*" -prune -o \
-name "cdecode.*" -prune -o \
-name "*_generated.h" -prune -o \
-name "*template_lvbitx.*" -prune -o \
-name "*.cpp" -print -o \
-name "*.hpp" -print -o \
-name "*.cpp.in" -print -o \
-name "*.hpp.in" -print -o \
-name "*.ipp" -print -o \
-name "*.c" -print -o \
-name "*.h" -print | xargs -n 10 -P 2 $cformat $cformat_args
Loading

0 comments on commit 0cc18f7

Please sign in to comment.