Elyes Haouas has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/82668?usp=email )
Change subject: util/lint: Warn when <string.h> is included but not used ......................................................................
util/lint: Warn when <string.h> is included but not used
Change-Id: Ifb8f68fe5e3249cc075273b5c2249d80a0b6410e Signed-off-by: Elyes Haouas ehaouas@noos.fr --- A util/lint/lint-stable-032-string-h 1 file changed, 41 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/68/82668/1
diff --git a/util/lint/lint-stable-032-string-h b/util/lint/lint-stable-032-string-h new file mode 100755 index 0000000..bf497c7 --- /dev/null +++ b/util/lint/lint-stable-032-string-h @@ -0,0 +1,41 @@ +#!/usr/bin/env sh +# SPDX-License-Identifier: GPL-2.0-or-later +# +# DESCR: Verify if <string.h> is needed in modified files +# + +# Get the list of modified files +MODIFIED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '.c$|.h$') +if [ -z "$MODIFIED_FILES" ]; then + exit 0 +fi + +# Create temporary files to hold the lists +TMP_INCLUDE=$(mktemp) +TMP_FUNCTIONS=$(mktemp) + +# Find files including <string.h> +for file in $MODIFIED_FILES; do + if grep -q '#include <string.h>' "$file"; then + echo "$file" >> "$TMP_INCLUDE" + fi +done + +# Find files using string.h functions +for file in $MODIFIED_FILES; do + if grep -q 'memcpy|memmove|memset|memcmp|memchr|strdup|strconcat|strnlen|strlen|strchr|strncpy|strcpy|strcmp|strncmp|strspn|strcspn|strstr|strtok_r|strtok|atol|strrchr|skip_atoi' "$file"; then + echo "$file" >> "$TMP_FUNCTIONS" + fi +done + +# Compare the two lists +UNNEEDED_INCLUDE=$(comm -23 "$TMP_INCLUDE" "$TMP_FUNCTIONS") + +# Output warnings for unneeded includes +if [ -n "$UNNEEDED_INCLUDE" ]; then + echo "Warning: The following files include <string.h> but do not use its functions:" + echo "$UNNEEDED_INCLUDE" +fi + +# Clean up temporary files +rm -f "$TMP_INCLUDE" "$TMP_FUNCTIONS"