From e078fcefdd890c84f22ed94233fe618e13d296e0 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Thu, 22 Mar 2018 11:33:48 +0100 Subject: [PATCH] Add check-includes.sh: Find redundant includes. Check ifndef:s. --- Makefile | 5 +++- tools/check-includes.sh | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100755 tools/check-includes.sh diff --git a/Makefile b/Makefile index 9b1a9da2e..e766c3cd8 100644 --- a/Makefile +++ b/Makefile @@ -255,7 +255,10 @@ check-python: @# E731 do not assign a lambda expression, use a def @flake8 --ignore=E501,E731 --exclude=contrib/pylightning/lightning/__init__.py ${PYSRC} -check-source: check-makefile check-source-bolt check-whitespace check-markdown check-spelling check-python +check-includes: + @tools/check-includes.sh + +check-source: check-makefile check-source-bolt check-whitespace check-markdown check-spelling check-python check-includes full-check: check check-source diff --git a/tools/check-includes.sh b/tools/check-includes.sh new file mode 100755 index 000000000..ca9e39c11 --- /dev/null +++ b/tools/check-includes.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +EXIT_CODE=0 + +# Check include guards + +HEADER_ID_PREFIX="LIGHTNING_" +HEADER_ID_SUFFIX="_H" +REGEXP_EXCLUDE_FILES_WITH_PREFIX="ccan/" +for HEADER_FILE in $(git ls-files -- "*.h" | grep -vE "^${REGEXP_EXCLUDE_FILES_WITH_PREFIX}") +do + HEADER_ID_BASE=$(tr / _ <<< "${HEADER_FILE/%.h/}" | tr "[:lower:]" "[:upper:]") + HEADER_ID="${HEADER_ID_PREFIX}${HEADER_ID_BASE}${HEADER_ID_SUFFIX}" + if [[ $(grep -cE "^#((ifndef|define) ${HEADER_ID}|endif /\* ${HEADER_ID} \*/)$" "${HEADER_FILE}") != 3 ]]; then + echo "${HEADER_FILE} seems to be missing the expected include guard:" + echo " #ifndef ${HEADER_ID}" + echo " #define ${HEADER_ID}" + echo " ..." + echo " #endif /* ${HEADER_ID} */" + echo + EXIT_CODE=1 + fi +done + +# Check redundant includes + +filter_suffix() { + git ls-files | grep -v 'ccan/' | grep -E "\.${1}"'$' +} + +for HEADER_FILE in $(filter_suffix h); do + DUPLICATE_INCLUDES_IN_HEADER_FILE=$(grep -E "^#include " < "${HEADER_FILE}" | sort | uniq -d) + if [[ ${DUPLICATE_INCLUDES_IN_HEADER_FILE} != "" ]]; then + echo "Duplicate include(s) in ${HEADER_FILE}:" + echo "${DUPLICATE_INCLUDES_IN_HEADER_FILE}" + echo + EXIT_CODE=1 + fi + C_FILE=${HEADER_FILE/%\.h/.c} + if [[ ! -e $C_FILE ]]; then + continue + fi + DUPLICATE_INCLUDES_IN_HEADER_AND_C_FILES=$(grep -hE "^#include " <(sort -u < "${HEADER_FILE}") <(sort -u < "${C_FILE}") | grep -E "^#include " | sort | uniq -d) + if [[ ${DUPLICATE_INCLUDES_IN_HEADER_AND_C_FILES} != "" ]]; then + echo "Include(s) from ${HEADER_FILE} duplicated in ${C_FILE}:" + echo "${DUPLICATE_INCLUDES_IN_HEADER_AND_C_FILES}" + echo + EXIT_CODE=1 + fi +done +for C_FILE in $(filter_suffix c); do + DUPLICATE_INCLUDES_IN_C_FILE=$(grep -E "^#include " < "${C_FILE}" | sort | uniq -d) + if [[ ${DUPLICATE_INCLUDES_IN_C_FILE} != "" ]]; then + echo "Duplicate include(s) in ${C_FILE}:" + echo "${DUPLICATE_INCLUDES_IN_C_FILE}" + echo + EXIT_CODE=1 + fi +done + +exit ${EXIT_CODE}