#!/bin/bash

# Copyright (c) 2021-2023 Red Hat.
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3, or (at your
# option) any later version.
#
# It is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# Test the support for future compiler features

set +x
echo START

TEST_NAME="future__auto_var_init"

. $srcdir/common.sh

# Use the same command line options that normal spec file will use,
# *except* that we disable the use of the annobin plugin, since this
# will pick up the one in the system directory, not the one just built"

OPTS="$(rpm --eval '%undefine _annotated_build %build_cflags %build_ldflags')"

# The rpm macros may not be available, so if necessary use our own

if ! [[ $OPTS == *"redhat-hardened-cc1"* ]];
then
    echo " $TEST_NAME: using built-in option selection instead"
    OPTS="-O2"
fi

# Now add in our newly built plugin.
OPTS+=" -fplugin=$PLUGIN"

start_test

# Use atexit.c rather than main.c as it is bigger.
COMMAND="$GCC $OPTS $srcdir/atexit.c -c -ftrivial-auto-var-init=zero"

A_OPTS="--skip-all --test-auto-var-init --verbose --suppress-version-warnings"
A_COMMAND="$ANNOCHECK $A_OPTS atexit.o"

$COMMAND
if [ $? != 0 ];
then
    echo " $TEST_NAME: SKIP: compiler does not support -ftrivial-auto-var-init"
else
    $A_COMMAND > future-test.out
    if [ $? != 0 ];
    then
        echo " $TEST_NAME: FAIL: annocheck exit code not 0"
        echo " $TEST_NAME: compile command: $COMMAND"
        echo " $TEST_NAME: annocheck command: $A_COMMAND"
        echo " $TEST_NAME: annocheck output"
        $A_COMMAND
	end_test
	exit 1
    fi
    grep -e "PASS: auto-var-init test" future-test.out
    if [ $? != 0 ];
    then
        echo " $TEST_NAME: FAIL: annocheck did not generate a PASS result"
        echo " $TEST_NAME: compile command: $COMMAND"
        echo " $TEST_NAME: annocheck command: $A_COMMAND"
        echo " $TEST_NAME: annocheck output"
        $A_COMMAND
	end_test
	exit 1
    fi
    
    # Try a different version of the option, now that we know that it works
    COMMAND="$GCC $OPTS $srcdir/atexit.c -c -ftrivial-auto-var-init=pattern"

    $COMMAND
    if [ $? != 0 ];
    then
        echo " $TEST_NAME: FAIL: second compilation failed
        echo " $TEST_NAME: compile command: $COMMAND"
        end_test
        exit 1
    fi

    A_OPTS="--skip-all --test-future --verbose"
    $A_COMMAND >> future-test.out
    if [ $? == 0 ];
    then
        echo " $TEST_NAME: FAIL: annocheck exit code is 0"
        echo " $TEST_NAME: compile command: $COMMAND"
        echo " $TEST_NAME: annocheck command: $A_COMMAND"
        echo " $TEST_NAME: annocheck output"
        $A_COMMAND
        end_test
        exit 1
    fi
fi

#-------------------------------------------------------------------------

TEST_NAME="future__zero_call_used_regs"

COMMAND="$GCC $OPTS $srcdir/atexit.c -c -fzero-call-used-regs=all"

A_OPTS="--skip-all --test-call-used-regs --verbose --suppress-version-warnings"
A_COMMAND="$ANNOCHECK $A_OPTS atexit.o"

$COMMAND
if [ $? != 0 ];
then
    echo " $TEST_NAME: SKIP: compiler does not support -fcall-used-regs"
else
    $A_COMMAND >> future-test.out
    if [ $? != 0 ];
    then
        echo " $TEST_NAME: FAIL: annocheck exit code not 0"
        echo " $TEST_NAME: compile command: $COMMAND"
        echo " $TEST_NAME: annocheck command: $A_COMMAND"
        echo " $TEST_NAME: annocheck output"
        $A_COMMAND
	end_test
	exit 1
    fi
    grep -e "PASS: zero-call-used-regs test" future-test.out
    if [ $? != 0 ];
    then
        echo " $TEST_NAME: FAIL: annocheck did not generate a PASS result"
        echo " $TEST_NAME: compile command: $COMMAND"
        echo " $TEST_NAME: annocheck command: $A_COMMAND"
        echo " $TEST_NAME: annocheck output"
        $A_COMMAND
	end_test
	exit 1
    fi
    
    # Try a different version of the option, now that we know that it works
    COMMAND="$GCC $OPTS $srcdir/atexit.c -c -fzero-call-used-regs=skip"

    $COMMAND
    if [ $? != 0 ];
    then
        echo " $TEST_NAME: FAIL: fourth compilation failed
        echo " $TEST_NAME: compile command: $COMMAND"
        end_test
        exit 1
    fi

    A_OPTS="--skip-all --test-future --verbose"
    $A_COMMAND >> future-test.out
    if [ $? == 0 ];
    then
        echo " $TEST_NAME: FAIL: annocheck exit code is 0"
        echo " $TEST_NAME: compile command: $COMMAND"
        echo " $TEST_NAME: annocheck command: $A_COMMAND"
        echo " $TEST_NAME: annocheck output"
        $A_COMMAND
        end_test
       exit 1
    fi
fi

end_test
