Elyes Haouas has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/82713?usp=email )
Change subject: util/lint: Warn if {stddef,stdint,string}.h included but not used ......................................................................
util/lint: Warn if {stddef,stdint,string}.h included but not used
Change-Id: I94104df7a2e25c0197b4027eb7fddd52cbc4c6ad Signed-off-by: Elyes Haouas ehaouas@noos.fr --- A util/lint/lint-stable-032-includes 1 file changed, 55 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/13/82713/1
diff --git a/util/lint/lint-stable-032-includes b/util/lint/lint-stable-032-includes new file mode 100755 index 0000000..7bc2f26 --- /dev/null +++ b/util/lint/lint-stable-032-includes @@ -0,0 +1,55 @@ +#!/usr/bin/env sh +# SPDX-License-Identifier: GPL-2.0-or-later +# +# DESCR: Verify if header files are 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) + +# Define the headers and their associated functions/types to check +declare -A HEADERS +HEADERS["<stddef.h>"]="size_t\b|ptrdiff_t\b|NULL\b|offsetof\b|wchar_t\b" +HEADERS["<stdint.h>"]="int8_t\b|int16_t\b|int32_t\b|int64_t\b|uint8_t\b|uint16_t\b|uint32_t\b|uint64_t\b|intptr_t\b|uintptr_t\b" +HEADERS["<string.h>"]="memcpy|memmove|memset|memcmp|memchr|strdup|strconcat|strnlen|strlen|strchr|strncpy|strcpy|strcmp|strncmp|strspn|strcspn|strstr|strtok_r|strtok|atol|strrchr|skip_atoi" + +for header in "${!HEADERS[@]}"; do + INCLUDE_LIST=$(mktemp) + FUNCTION_LIST=$(mktemp) + + # Find files including the header + for file in $MODIFIED_FILES; do + if grep -q "#include $header" "$file"; then + echo "$file" >> "$INCLUDE_LIST" + fi + done + + # Find files using the functions/types associated with the header + for file in $MODIFIED_FILES; do + if grep -q "${HEADERS[$header]}" "$file"; then + echo "$file" >> "$FUNCTION_LIST" + fi + done + + # Compare the two lists to find unnecessary includes + UNNEEDED_INCLUDE=$(comm -23 <(sort "$INCLUDE_LIST") <(sort "$FUNCTION_LIST")) + + # Output warnings for unneeded includes + if [ -n "$UNNEEDED_INCLUDE" ]; then + echo "Warning: The following files include $header but do not use its functions/types:" + echo "$UNNEEDED_INCLUDE" + fi + + # Clean up temporary files + rm -f "$INCLUDE_LIST" "$FUNCTION_LIST" +done + +# Clean up temporary files +rm -f "$TMP_INCLUDE" "$TMP_FUNCTIONS"