Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate format code script #759

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
304 changes: 304 additions & 0 deletions scripts/format-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
#!/usr/bin/env bash

# Copyright (c) Prevail Verifier contributors.
# SPDX-License-Identifier: MIT

##==============================================================================
##
## Echo if verbose flag (ignores quiet flag)
##
##==============================================================================
log_verbose()
{
if [[ ${verbose} -eq 1 ]]; then
echo "$1"
fi
}

##==============================================================================
##
## Echo if whatif flag is specified but not quiet flag
##
##==============================================================================
log_whatif()
{
if [[ ${whatif} -eq 1 && ${quiet} -ne 1 ]]; then
echo "$1"
fi
}
Comment on lines +18 to +28
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Enhance function documentation clarity

The comment for log_whatif could be more precise about the condition logic.

Consider updating the comment to:

-## Echo if whatif flag is specified but not quiet flag
+## Echo if both conditions are met:
+## 1. whatif flag is specified (whatif=1)
+## 2. quiet flag is not specified (quiet!=1)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
##==============================================================================
##
## Echo if whatif flag is specified but not quiet flag
##
##==============================================================================
log_whatif()
{
if [[ ${whatif} -eq 1 && ${quiet} -ne 1 ]]; then
echo "$1"
fi
}
##==============================================================================
##
## Echo if both conditions are met:
## 1. whatif flag is specified (whatif=1)
## 2. quiet flag is not specified (quiet!=1)
##
##==============================================================================
log_whatif()
{
if [[ ${whatif} -eq 1 && ${quiet} -ne 1 ]]; then
echo "$1"
fi
}


##==============================================================================
##
## Process command-line options:
##
##==============================================================================

for opt in "$@"
do
case $opt in
-h | --help)
usage=1
;;

-v | --verbose)
verbose=1
;;

-q | --quiet)
quiet=1
;;

-w | --whatif)
whatif=1
;;

-s | --staged)
# Split into array
mapfile -t userFiles <<< "$(git diff --cached --name-only --diff-filter=ACMR)"
;;

--exclude-dirs=*)
userExcludeDirs="${opt#*=}"
;;

--include-exts=*)
userIncludeExts="${opt#*=}"
;;

--files=*)
read -ra userFiles <<< "${opt#*=}"
;;
*)
echo "$0: unknown option: $opt"
exit 1
;;
esac
done

##==============================================================================
##
## Display help
##
##==============================================================================

if [[ ${usage} -eq 1 ]]; then
cat<<EOF

OVERVIEW:

Formats all C/C++ source files based on the .clang-format rules

$ format-code [-h] [-q] [-s] [-v] [-w] [--exclude-dirs="..."] [--include-exts="..."] [--files="..."]

OPTIONS:
-h, --help Print this help message.
-q, --quiet Display only clang-format output and errors.
-s, --staged Only format files which are staged to be committed.
-v, --verbose Display verbose output.
-w, --whatif Run the script without actually modifying the files
and display the diff of expected changes, if any.
--exclude-dirs Subdirectories to exclude. If unspecified, then
./external, ./packages and ./x64 are excluded.
All subdirectories are relative to the current path.
--include-exts File extensions to include for formatting. If
unspecified, then *.h, *.hpp, *.c, *.cpp, *idl, and
*.acf are included.
--files Only run the script against the specified files from
the current directory.

EXAMPLES:

To determine what lines of each file in the default configuration would be
modified by format-code, you can run from the root folder:

$ ./scripts/format-code -w

To update only all .c and .cpp files in src/ except for src/tools/netsh, you
can run from the src folder:

src$ ../scripts/format-code --exclude-dirs="tools/netsh" \
--include-exts="c cpp"

To run only against a specified set of comma separated files in the current directory:

$ ./scripts/format-code -w --files="file1 file2"

EOF
exit 0
fi
Comment on lines +84 to +128
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Add version requirement to help message

The help message should mention the required clang-format version (17.0.3) to help users understand compatibility requirements.

Add to the overview section:

 OVERVIEW:
 
 Formats all C/C++ source files based on the .clang-format rules
+
+Requires clang-format version 17.0.3
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if [[ ${usage} -eq 1 ]]; then
cat<<EOF
OVERVIEW:
Formats all C/C++ source files based on the .clang-format rules
$ format-code [-h] [-q] [-s] [-v] [-w] [--exclude-dirs="..."] [--include-exts="..."] [--files="..."]
OPTIONS:
-h, --help Print this help message.
-q, --quiet Display only clang-format output and errors.
-s, --staged Only format files which are staged to be committed.
-v, --verbose Display verbose output.
-w, --whatif Run the script without actually modifying the files
and display the diff of expected changes, if any.
--exclude-dirs Subdirectories to exclude. If unspecified, then
./external, ./packages and ./x64 are excluded.
All subdirectories are relative to the current path.
--include-exts File extensions to include for formatting. If
unspecified, then *.h, *.hpp, *.c, *.cpp, *idl, and
*.acf are included.
--files Only run the script against the specified files from
the current directory.
EXAMPLES:
To determine what lines of each file in the default configuration would be
modified by format-code, you can run from the root folder:
$ ./scripts/format-code -w
To update only all .c and .cpp files in src/ except for src/tools/netsh, you
can run from the src folder:
src$ ../scripts/format-code --exclude-dirs="tools/netsh" \
--include-exts="c cpp"
To run only against a specified set of comma separated files in the current directory:
$ ./scripts/format-code -w --files="file1 file2"
EOF
exit 0
fi
if [[ ${usage} -eq 1 ]]; then
cat<<EOF
OVERVIEW:
Formats all C/C++ source files based on the .clang-format rules
Requires clang-format version 17.0.3
$ format-code [-h] [-q] [-s] [-v] [-w] [--exclude-dirs="..."] [--include-exts="..."] [--files="..."]
OPTIONS:
-h, --help Print this help message.
-q, --quiet Display only clang-format output and errors.
-s, --staged Only format files which are staged to be committed.
-v, --verbose Display verbose output.
-w, --whatif Run the script without actually modifying the files
and display the diff of expected changes, if any.
--exclude-dirs Subdirectories to exclude. If unspecified, then
./external, ./packages and ./x64 are excluded.
All subdirectories are relative to the current path.
--include-exts File extensions to include for formatting. If
unspecified, then *.h, *.hpp, *.c, *.cpp, *idl, and
*.acf are included.
--files Only run the script against the specified files from
the current directory.
EXAMPLES:
To determine what lines of each file in the default configuration would be
modified by format-code, you can run from the root folder:
$ ./scripts/format-code -w
To update only all .c and .cpp files in src/ except for src/tools/netsh, you
can run from the src folder:
src$ ../scripts/format-code --exclude-dirs="tools/netsh" \
--include-exts="c cpp"
To run only against a specified set of comma separated files in the current directory:
$ ./scripts/format-code -w --files="file1 file2"
EOF
exit 0
fi


##==============================================================================
##
## Check for acceptable version of clang-format
##
##==============================================================================
check_version()
{
# shellcheck disable=SC2206
v1=(${1//./ })
# shellcheck disable=SC2206
v2=(${2//./ })
if [[ ${#v1[@]} -ne ${#v2[@]} ]]; then
echo "Cannot parse clang-format version number: $2"
exit 1
fi
for ((i=0; i<${#v1[@]}; i++));
do
# Because clang-format is not invariant across versions, this
# is an explicit equivalence check.
if [[ ${v1[$i]} -ne ${v2[$i]} ]]; then
echo "Warning: format-code prefers clang-format version $1, installed version is $2"
fi
done
}

##==============================================================================
##
## Check for installed clang-format tool
##
##==============================================================================
check_clang-format()
{
# First check explicitly for the clang-format-7 executable.
cf=$(command -v clang-format-7 2> /dev/null)

if [[ -x ${cf} ]]; then
cf="clang-format-7"
return
else
cf=$(command -v clang-format 2> /dev/null)
if [[ ! -x ${cf} ]]; then
echo "clang-format is not installed"
exit 1
fi
cf="clang-format"
fi

local required_cfver='17.0.3'
# shellcheck disable=SC2155
local cfver=$(${cf} --version | grep -o -E '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
check_version "${required_cfver}" "${cfver}"
}
Comment on lines +160 to +181
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Revise clang-format version handling

Several issues with the version checking:

  1. The script preferentially looks for clang-format-7 but requires version 17.0.3
  2. The version comparison only warns but continues execution
  3. Function name check_clang-format uses a hyphen which is unconventional in Bash

Consider these changes:

-check_clang-format()
+check_clang_format()
 {
-    # First check explicitly for the clang-format-7 executable.
-    cf=$(command -v clang-format-7 2> /dev/null)
-
-    if [[ -x ${cf} ]]; then
-        cf="clang-format-7"
-        return
-    else
-        cf=$(command -v clang-format 2> /dev/null)
-        if [[ ! -x ${cf} ]]; then
-            echo "clang-format is not installed"
-            exit 1
-        fi
-        cf="clang-format"
-    fi
+    cf=$(command -v clang-format 2> /dev/null)
+    if [[ ! -x ${cf} ]]; then
+        echo "clang-format is not installed"
+        exit 1
+    fi
+    cf="clang-format"

     local required_cfver='17.0.3'
     # shellcheck disable=SC2155
     local cfver=$(${cf} --version | grep -o -E '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
-    check_version "${required_cfver}" "${cfver}"
+    if ! check_version "${required_cfver}" "${cfver}"; then
+        exit 1
+    fi
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
check_clang-format()
{
# First check explicitly for the clang-format-7 executable.
cf=$(command -v clang-format-7 2> /dev/null)
if [[ -x ${cf} ]]; then
cf="clang-format-7"
return
else
cf=$(command -v clang-format 2> /dev/null)
if [[ ! -x ${cf} ]]; then
echo "clang-format is not installed"
exit 1
fi
cf="clang-format"
fi
local required_cfver='17.0.3'
# shellcheck disable=SC2155
local cfver=$(${cf} --version | grep -o -E '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
check_version "${required_cfver}" "${cfver}"
}
check_clang_format()
{
cf=$(command -v clang-format 2> /dev/null)
if [[ ! -x ${cf} ]]; then
echo "clang-format is not installed"
exit 1
fi
cf="clang-format"
local required_cfver='17.0.3'
# shellcheck disable=SC2155
local cfver=$(${cf} --version | grep -o -E '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
if ! check_version "${required_cfver}" "${cfver}"; then
exit 1
fi
}


check_clang-format

##==============================================================================
##
## Determine parameters for finding files to format
##
##==============================================================================

findargs='find .'

get_find_args()
{
local defaultExcludeDirs='./.git ./external ./packages ./x64'
local defaultIncludeExts='h hpp c cpp idl acf'

if [[ -z ${userExcludeDirs} ]]; then
read -r -a excludeDirs <<< "${defaultExcludeDirs}"
else
log_verbose "Using user directory exclusions: ${userExcludeDirs}"
read -r -a excludeDirs <<< "${userExcludeDirs}"
fi

for exc in "${excludeDirs[@]}"
do
findargs="${findargs} ! \( -path '${exc}' -prune \)"
done

if [[ -z ${userIncludeExts} ]]; then
# not local as this is used in get_file_list() too
read -r -a includeExts <<< "${defaultIncludeExts}"
else
log_verbose "Using user extension inclusions: ${userIncludeExts}"
read -r -a includeExts <<< "${userIncludeExts}"
fi

findargs="${findargs} \("
for ((i=0; i<${#includeExts[@]}; i++));
do
findargs="${findargs} -iname '*.${includeExts[$i]}'"
if [[ $((i + 1)) -lt ${#includeExts[@]} ]]; then
findargs="${findargs} -o"
fi
done
findargs="${findargs} \)"
}

get_find_args

log_verbose "Query for files for format:"
log_verbose "${findargs}"
log_verbose ""

Comment on lines +191 to +234
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve file finding security and validation

The current implementation has potential security and robustness issues:

  1. Command injection possible through userExcludeDirs
  2. No validation that found files exist and are readable

Consider these improvements:

 for exc in "${excludeDirs[@]}"
 do
+    # Validate directory name
+    if [[ "$exc" =~ [^a-zA-Z0-9_./-] ]]; then
+        echo "Invalid directory name: $exc"
+        exit 1
+    fi
     findargs="${findargs} ! \( -path '${exc}' -prune \)"
 done

Also add file validation:

 get_file_list()
 {
     if [[ -z "${userFiles[*]}" ]]; then
         mapfile -t file_list < <(eval "$findargs")
         if [[ ${#file_list[@]} -eq 0 ]]; then
             echo "No files were found to format!"
             exit 1
         fi
+        # Validate files
+        for file in "${file_list[@]}"; do
+            if [[ ! -r "$file" ]]; then
+                echo "Cannot read file: $file"
+                exit 1
+            fi
+        done
     else

Committable suggestion was skipped due to low confidence.

##==============================================================================
##
## Call clang-format for each file to be formatted
##
##==============================================================================

filecount=0
changecount=0

cfargs="${cf} -style=file"
if [[ ${whatif} -ne 1 ]]; then
cfargs="${cfargs} -i"
fi

get_file_list()
{
if [[ -z "${userFiles[*]}" ]]; then
mapfile -t file_list < <(eval "$findargs")
if [[ ${#file_list[@]} -eq 0 ]]; then
echo "No files were found to format!"
exit 1
fi
else
log_verbose "Using user files: ${userFiles[*]}"
file_list=()
for file in "${userFiles[@]}"; do
for ext in "${includeExts[@]}"; do
if [[ ${file##*\.} == "$ext" ]]; then
file_list+=( "$file" )
log_verbose "Checking user file: ${file}"
break
fi
done
done
fi
}

get_file_list

for file in "${file_list[@]}"
do
((filecount+=1))
cf="${cfargs} $file"

log_whatif "Formatting $file ..."

if [[ ${whatif} -eq 1 ]]; then
${cf} | diff -u "$file" -
else
if [[ ${verbose} -eq 1 ]]; then
${cf}
else
${cf} > /dev/null
fi
fi

#shellcheck disable=SC2181
if [[ $? -ne 0 ]]; then
if [[ ${whatif} -eq 1 ]]; then
((changecount+=1))
else
echo "clang-format failed on file: $file."
fi
fi
done
elazarg marked this conversation as resolved.
Show resolved Hide resolved

log_whatif "${filecount} files processed, ${changecount} changed."

# If files are being edited, this count is zero so we exit with success.
exit "$changecount"
Loading
Loading