Nicholas Chin has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/69712 )
Change subject: util/scripts: Add script to summarize post code usages ......................................................................
util/scripts: Add script to summarize post code usages
This script finds all usages of post_code() and postcode macros in the coreboot tree, sorts them by the raw hex value it evaluates to, and then prints out the collected information including file and line number. This is meant to assist with efforts to organize and standardize postcodes in coreboot.
Change-Id: Idcc11ce2e5f6c9760537c549e36145b871276206 Signed-off-by: Nicholas Chin nic.c3.14@gmail.com --- M util/scripts/description.md A util/scripts/post_code_usage.sh 2 files changed, 66 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/12/69712/1
diff --git a/util/scripts/description.md b/util/scripts/description.md index 780e9c9..fabad0ee 100644 --- a/util/scripts/description.md +++ b/util/scripts/description.md @@ -23,6 +23,8 @@ headers `Shell` * _parse-maintainers.pl_ - Script to alphabetize MAINTAINERS file `Perl` + * _post_code_usage.sh_ - Summarize all usages of post_code() and post + code macros in the tree `Bash` * _rm_unused_code_ - Remove all code not used for a platform from the local git repository for auditing or release `Bash` * _show_platforms.sh_ - Makes a list of platforms in the tree. Does diff --git a/util/scripts/post_code_usage.sh b/util/scripts/post_code_usage.sh new file mode 100755 index 0000000..a182572 --- /dev/null +++ b/util/scripts/post_code_usage.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# +# SPDX-License-Identifier: GPL-2.0-or-later + +# Searches the src tree for all calls to post_code() as well as all usages of +# the post code macros. May miss post code like calls (board/vendor/platform +# specific functions) that use raw hex values + +IFS=$'\n' + +# Header files with post code defines +post_code_header="src/commonlib/include/commonlib/console/post_codes.h" +amd_post_code_header="src/soc/amd/common/psp_verstage/include/psp_verstage.h" + +# Format defines as "MACRO_NAME 0xHH" +post_macros=$(grep -Pho "(?<=#define )POST.*\s*0x.." $post_code_header \ + $amd_post_code_header | sed -E "s/\s+/ /g") + +# All calls to post_code() using numeric literals instead of macros +post_usage=$(grep -Pnr "^\s*post_code(\d.*)" src | \ + sed -E "s/(.*:)\s*?(post_code((.*)))/\3\t\1\t\2/") + +# Search the tree for all usages of each post code macro (will take a while) +for line in $post_macros; do + # Extract MACRO_NAME + macro=$(echo $line | cut -d " " -f 1) + # Extract hex value associated with macro + hex_code=$(echo $line | cut -d " " -f 2 | tr '[:upper:]' '[:lower:]') + # The negative lookbehind is to ensure it doesn't match cases where + # the macro is the prefix of another macro + # Format as "0xhh\t<grep filename:line>\t<raw line of code>" + macro_usage=$(grep -rPn "$macro(?![A-Z_])" src | \ + sed -E "s/\s+/ /g" | sed -E "s/(.*:)\s*(.*)/$hex_code\t\1\t\2/") + post_usage=$post_usage$'\n'$macro_usage +done + +post_usage=$(sort <<< $post_usage) +last_hex_code="" + +# Print out the collected information +for line in $post_usage; do + hex_code=$(echo $line | cut -d $'\t' -f 1) + if [[ $last_hex_code != $hex_code ]]; then + echo "" + fi + last_hex_code=$hex_code + echo $line +done