#!/bin/bash

# Copyright (c) 2023-2024 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.

set +x
echo START

TEST_NAME=implicit-values
. $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 -fexceptions -g -grecord-gcc-switches -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fPIE -fstack-protector-strong -fasynchronous-unwind-tables -fstack-clash-protection -Wl,-z,relro -Wl,--as-needed  -Wl,-z,now -pie"
fi

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

# Modern C compilers will not compile the test because they know that
# malloc does not return an int, so...
OPTS+=" -std=gnu89"


start_test

COMMAND="$GCC $OPTS -c -Wno-implicit-int $srcdir/implicit.c -o implicit-int-test.exe"
$COMMAND
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: unable to compile test file"
    echo " $TEST_NAME: command: $COMMAND"
    end_test
    exit 1
fi

# Run annocheck

SKIPS="--skip-all --test-implicit-values --suppress-version-warnings"
A_COMMAND="$ANNOCHECK implicit-int-test.exe $SKIPS"
$A_COMMAND > implicit-int-test.out
grep -e "Overall: FAIL" implicit-int-test.out
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: compiling with -Wno-implicit-int still produces an executable that passes annocheck"
    echo " $TEST_NAME: compile command: $COMMAND"
    echo " $TEST_NAME: annocheck command: $A_COMMAND"
    echo " $TEST_NAME: annocheck output (with verbose enabled):"
    $A_COMMAND --verbose
    uuencode implicit-int-test.exe implicit-int-test.exe
    end_test
    exit 1
fi

# First test passed.  Now run second test.

COMMAND="$GCC $OPTS -c -Wno-implicit-function-declaration $srcdir/implicit.c -o implicit-func-test.exe"
$COMMAND
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: unable to compile test file"
    echo " $TEST_NAME: command: $COMMAND"
    end_test
    exit 1
fi

# Run annocheck

SKIPS="--skip-all --test-implicit-values --suppress-version-warnings"
A_COMMAND="$ANNOCHECK implicit-func-test.exe $SKIPS"
$A_COMMAND > implicit-func-test.out
grep -e "Overall: FAIL" implicit-func-test.out
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: compiling with -Wno-implicit-function-declaration still produces an executable that passes annocheck"
    echo " $TEST_NAME: compile command: $COMMAND"
    echo " $TEST_NAME: annocheck command: $A_COMMAND"
    echo " $TEST_NAME: annocheck output (with verbose enabled):"
    $A_COMMAND --verbose
    uuencode implicit-func-test.exe implicit-func-test.exe
    end_test
    exit 1
fi

# Success!

end_test
