This Bash script is designed to locate and use the...
This Bash script is designed to locate and use the clang-format
tool (a code formatting tool) to recursively format C and C++ source code files in one or more specified directories. The script includes fallback mechanisms to find compatible versions of clang-format
, ensuring its functionality across various system configurations. Here's a detailed breakdown of what the code does:
Steps:
-
Shebang Header:
#!/bin/bash
This defines the script as a Bash script.
-
Usage Function:
function usage { echo "Usage: $0 DIR..." exit 1 }
This defines a helper function named
usage
to print a usage message if the supplied arguments are invalid. -
Find the
clang-format
Command:FMT="" for clangfmt in clang-format{,-{4,3}.{9,8,7,6,5,4,3,2,1,0}}; do if which "$clangfmt" &>/dev/null; then FMT="$clangfmt" break fi done
- Initializes a variable,
FMT
, which will store the name of theclang-format
executable. - Uses a loop to search for
clang-format
or specific versioned binaries (e.g.,clang-format-4.0
,clang-format-3.9
, etc.) in decreasing version order. - It uses the
which
command to check if the executable is available, and if found, assigns it toFMT
.
- Initializes a variable,
-
Handle Missing
clang-format
:if [ -z "$FMT" ]; then echo "failed to find clang-format" exit 1 fi
If no
clang-format
executable was found, the script terminates with an error message. -
Process Input Arguments:
SRC_PATH="$@" if [ -z "$SRC_PATH" ]; then SRC_PATH="../../../src" fi
- Reads directories passed as arguments into the variable
SRC_PATH
. - If no arguments were provided, defaults to the relative path
../../../src
.
- Reads directories passed as arguments into the variable
-
Check Validity of Directories:
for dir in "$SRC_PATH"; do if [ ! -d "${dir}" ]; then echo "${dir} is not a directory" usage fi done
- Iterates over all directories in
SRC_PATH
. - If any argument is not a valid directory, the script prints an error message and exits.
- Iterates over all directories in
-
Format Code Files:
for dir in "$SRC_PATH"; do pushd "${dir}" &>/dev/null find . \ \( -name '*.c' \ -o -name '*.cc' \ -o -name '*.cpp' \ -o -name '*.h' \ -o -name '*.hh' \ -o -name '*.hpp' \) \ -exec "${FMT}" -style=file -i '{}' \; popd &>/dev/null done
- For each valid directory, navigates into it using
pushd
, suppressing output with&>/dev/null
. - Uses the
find
command to locate files with extensions commonly used for C and C++ source or header files (.c
,.cc
,.cpp
,.h
,.hh
,.hpp
). - Executes the
clang-format
tool on each file (-exec "${FMT}" ...
) with the-style=file
option (to use style settings from a.clang-format
file) and the-i
option (to apply changes in-place). - Exits the directory using
popd
, suppressing output.
- For each valid directory, navigates into it using
Summary:
The script:
- Searches for the
clang-format
tool or its versioned alternatives. - Ensures appropriate arguments (one or more directories) are provided.
- Defaults to
../../../src
if no directories are specified. - Validates that all provided arguments are directories.
- Recursively formats C and C++ source code files in the specified directories using
clang-format
.