#!/bin/sh
# $Id: gcc-stricter,v 1.25 2025/07/20 19:47:10 tom Exp $
#
# _CONST_X_STRING is my own ifdef to X11/Intrinsic.h, to make
#	typedef const char *String;
# Doing that will help me make the "const" strings in xterm, xvile match with X.
#
# I also changed X11/XResource.h to use _Xconst in XrmOptionDescRec.
#
OPTS=

: ${ACTUAL_GCC:=gcc}
case "$ACTUAL_GCC" in
gcc-[ceins]*)
	ACTUAL_GCC=gcc
	;;
esac

NEXT_STAGE=gcc-strict
case "$*" in
*-E*|*--version*)
	NEXT_STAGE=$ACTUAL_GCC
	;;
*)
VERS=`$ACTUAL_GCC --version 2>/dev/null | \
head -n 1 | \
sed 	-e 's/([^)][^)]*)//g' \
	-e 's/[[:space:]]*$//' \
	-e 's/[[:space:]]\{1,\}[[:digit:]]\{8,8\}$//' \
	-e 's/^.*[[:space:]]//'`
MAIN=`echo "$VERS" | sed -e 's/\..*//'`

# Not useful:
#	-Wc90-c99-compat unnecessarily reminds about boolean type
#	-Wformat=2
#	-Wmissing-format-attribute
#	-Wsuggest-attribute=format does not work for va_list parameter
# Possibly useful:
#	OPTS="$OPTS -Wsuggest-attribute=const"
#	OPTS="$OPTS -Wsuggest-attribute=pure"

case x$VERS in
x[1-4][0-9].[0-9]*|x[5-9].[0-9]*.*[0-9])
	OPTS="$OPTS -Wdeclaration-after-statement"
	OPTS="$OPTS -Werror=format-security"
	OPTS="$OPTS -Wformat=1"
	OPTS="$OPTS -Wmissing-noreturn"
	OPTS="$OPTS -Wshadow"
	OPTS="$OPTS -fstack-protector-strong"
	if [ $MAIN -ge 10 ]
	then
		OPTS="$OPTS -Walloc-zero"
		OPTS="$OPTS -Wduplicated-branches"
		OPTS="$OPTS -Wduplicated-cond"
		OPTS="$OPTS -Wformat-truncation=2"
	fi
	if [ $MAIN -gt 14 ]
	then
		OPTS="$OPTS -Wdeprecated-non-prototype"
		OPTS="$OPTS -Wmissing-parameter-name"
		OPTS="$OPTS -Wtrailing-whitespace"
		OPTS="$OPTS -Wunterminated-string-initialization"
		OPTS="$OPTS -Wzero-as-null-pointer-constant"
	fi
	if [ $MAIN -ge 14 ]
	then
		OPTS="$OPTS -Walloc-size"
		OPTS="$OPTS -Wcalloc-transposed-args"
	fi
	if [ $MAIN -ge 14 ]
	then
		OPTS="$OPTS -std=c2x"
	elif [ $MAIN -ge 12 ]
	then
		OPTS="$OPTS -std=c11"
	elif [ $MAIN -ge 9 ]
	then
		OPTS="$OPTS -Wlogical-op"
		OPTS="$OPTS -Wsuggest-attribute=malloc"
		OPTS="$OPTS -Wsuggest-attribute=noreturn"
	fi
	case x`partition` in
	x*debian*)
		OPTS="$OPTS -fsanitize=undefined"
		;;
	esac
	;;
x)
	$ACTUAL_GCC --version
	exit 1
	;;
esac

case "$*" in
*-D_FORTIFY_SOURCE=*)
	;;
*)
	OPTS="$OPTS -D_FORTIFY_SOURCE=2"
	;;
esac
;;
esac

exec $NEXT_STAGE \
	-O2 \
	-D_CONST_X_STRING \
	$OPTS "$@"
