#!/bin/bash

# Copyright (c) 2018-2026 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_NAME=note-format
. $srcdir/common.sh

start_test

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

# Make sure than an ordinary compilation works
COMMAND="$GCC -fplugin=$PLUGIN -c $srcdir/hello.c"
$COMMAND > $G_OUT 2>&1
if [ $? != 0 ];
then
    echo " $TEST_NAME: SKIP: Could not compile source file with ELF format notes"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_SKIPPED
fi

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

# Now add in a note format selection option
PLUGIN_OPTS="-fplugin-arg-annobin-note-format=note"

COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS -c $srcdir/hello.c"
$COMMAND > $G_OUT 2>&1
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: Could not compile source file with ELF format notes"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

COMMAND="$ANNOCHECK -v --ignore-gaps --skip-all hello.o"
$COMMAND > $A_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: annocheck did not like ELF format notes"
    echo " $TEST_NAME: command: $COMMAND"
    cat $A_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

echo " $TEST_NAME: PASS: ELF note format test"

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

PLUGIN_OPTS="-fplugin-arg-annobin-note-format=string"

COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS -c $srcdir/hello.c"
$COMMAND > $G_OUT 2>&1
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: Could not compile source file with string format notes"
    echo " $TEST_NAME: command: $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

COMMAND="$ANNOCHECK -v --ignore-gaps --skip-all hello.o"
$COMMAND > $A_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: annocheck did not like string format notes"
    echo " $TEST_NAME: command: $COMMAND"
    cat $A_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

echo " $TEST_NAME: PASS: string note format test"

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

PLUGIN_OPTS=""

COMMAND="$GCC -fplugin=$PLUGIN $PLUGIN_OPTS -c $srcdir/unused_code.c"
ANNOBIN=note-format=string $COMMAND > $G_OUT 2>&1
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: Could not compile unused.c source file with string format notes"
    echo " $TEST_NAME: command: ANNOBIN=note-format=string $COMMAND"
    cat $G_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

COMMAND="$ANNOCHECK -v --ignore-gaps --skip-all --test-optimization unused_code.o"
$COMMAND > $A_OUT
if [ $? == 0 ];
then
    echo " $TEST_NAME: FAIL: annocheck did not issue a FAIL result"
    echo " $TEST_NAME: command: $COMMAND"
    cat $A_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

grep -q -e"level too low" $A_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: annocheck did not report optimization level being too low"
    echo " $TEST_NAME: command: $COMMAND"
    cat $A_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

grep -q -e"unused_code.c" $A_OUT
if [ $? != 0 ];
then
    echo " $TEST_NAME: FAIL: annocheck did not report the source file"
    echo " $TEST_NAME: command: $COMMAND"
    cat $A_OUT
    end_test
    exit $EXIT_TEST_FAILED
fi

echo " $TEST_NAME: PASS: string note format test"

end_test


