#!/usr/bin/env bash1#***************************************************************************2# _ _ ____ _3# Project ___| | | | _ \| |4# / __| | | | |_) | |5# | (__| |_| | _ <| |___6# \___|\___/|_| \_\_____|7#8# Copyright (C) Viktor Szakats9#10# This software is licensed as described in the file COPYING, which11# you should have received as part of this distribution. The terms12# are also available at https://curl.se/docs/copyright.html.13#14# You may opt to use, copy, modify, merge, publish, distribute and/or sell15# copies of the Software, and permit persons to whom the Software is16# furnished to do so, under the terms of the COPYING file.17#18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY19# KIND, either express or implied.20#21# SPDX-License-Identifier: curl22#23###########################################################################2425detect_in_reused_sources=12627if [ "$detect_in_reused_sources" = '1' ]; then28# Make symlinks for all re-used sources29grep -E '^(lib|unit)[0-9]+_SOURCES = ' libtest/Makefile.inc unit/Makefile.inc \30| sed -E 's@^([a-z]+)/[a-zA-Z.]+:(lib|unit)([0-9]+)_SOURCES = (lib|unit)([0-9]+).+@\1 \2 \3 \5@g' | \31while read -r l; do32if [[ "${l}" =~ ([a-z]+)\ ([a-z]+)\ ([0-9]+)\ ([0-9]+) ]]; then33trg="${BASH_REMATCH[3]}"34src="${BASH_REMATCH[4]}"35if [ "${trg}" != "${src}" ]; then36dir="${BASH_REMATCH[1]}"37pfx="${BASH_REMATCH[2]}"38ln -s "${pfx}${src}.c" "${dir}/${pfx}${trg}.c"39fi40fi41done42fi4344# Look for symbols possibly re-used in multiple sources.45#46# Falsely picks ups symbols in re-used sources, but guarded for a single use.47# Misses shadowed variables.48# shellcheck disable=SC204649grep -E '^ *(static|struct) +' $(find libtest unit -maxdepth 1 -name 'lib*.c' -o -name 'unit*.c' -o -name 'mk-*.pl') \50| grep -E '^(libtest|unit)/' \51| grep -E '\.(c|pl):(static|struct)( +[a-zA-Z_* ]+)? +[a-zA-Z_][a-zA-Z0-9_]+ *' | sort -u \52| grep -o -E '[a-zA-Z_][a-zA-Z0-9_]+ *[=;[({]' | tr -d '=;[({ ' \53| grep -v -E '^(NULL$|sizeof$|CURLE_)' \54| sort | uniq -c | sort -k 2 | grep -v -E '^ +1 ' \55| awk '{print " \"" $2 "\","}'5657echo '---'5859# Extract list of macros that may be re-used by multiple tests.60#61# Picks up false-positive when the macro is defined to the same value everywhere.62# shellcheck disable=SC204663grep -E '^ *# *define +' $(find libtest unit -maxdepth 1 -name 'lib*.c' -o -name 'unit*.c' -o -name 'mk-*.pl') \64| grep -E '^(libtest|unit)/' \65| grep -o -E '.+\.(c|pl): *# *define +[A-Z_][A-Z0-9_]+' | sort -u \66| grep -o -E '[A-Z_][A-Z0-9_]+' \67| sort | uniq -c | sort -k 2 | grep -v -E '^ +1 ' \68| awk '{print " \"" $2 "\","}'6970if [ "$detect_in_reused_sources" = '1' ]; then71# Delete symlinks for all re-used sources72find libtest unit -type l -delete73fi747576