This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] improve value tracking warnings


Tested on i686-linux, committed on trunk.

We already have all the mechanisms for tracking comparisons into if
statements and loops. This patch adds circuitry to Compile_Time_Compare
to take advantage of this information to evaluate additional comparisons
at compile time, and most importantly, to warn about resulting detected
cases of conditions that are always false or always true, due to the
execution of redundant tests.

The following test program gets new warnings as indicated:

     1. procedure r is
     2.    a, b, c, d : integer;
           1  2  3  4
        >>> warning: variable "a" is read but never assigned
        >>> warning: variable "b" is read but never assigned
        >>> warning: variable "c" is read but never assigned
        >>> warning: variable "d" is read but never assigned

     3. begin
     4.    if (a > 1 and b <= 2) and then c = 4 and then d /= 2 then
     5.       if a >  0 then null; end if;
                   |
        >>> warning: condition is always True

     6.       if a >= 2 then null; end if;
                   |
        >>> warning: condition is always True

     7.       if a <  1 then null; end if;
                   |
        >>> warning: condition is always False

     8.       if a <= 1 then null; end if;
                   |
        >>> warning: condition is always False

     9.       if a >  2 then null; end if; -- no warn
    10.
    11.       if b < 4  then null; end if;
                   |
        >>> warning: condition is always True

    12.       if b < 3  then null; end if;
                   |
        >>> warning: condition is always True

    13.       if b > 4  then null; end if;
                   |
        >>> warning: condition is always False

    14.       if 2 >= b then null; end if;
                   |
        >>> warning: condition is always True

    15.       if b < 1  then null; end if; -- no warn
    16.
    17.       if c = 4  then null; end if;
                   |
        >>> warning: condition is always True

    18.       if c /= 5 then null; end if;
                   |
        >>> warning: condition is always True

    19.       if c /= 4 then null; end if;
                   |
        >>> warning: condition is always False

    20.       if c > 3  then null; end if;
                   |
        >>> warning: condition is always True

    21.       if c <= 4 then null; end if;
                   |
        >>> warning: condition is always True

    22.
    23.       if d /= 2 then null; end if;
                   |
        >>> warning: condition is always True

    24.       if d = 2  then null; end if;
                   |
        >>> warning: condition is always False

    25.       if d >  5 then null; end if; -- no warn
    26.       if d <= 2 then null; end if; -- no warn
    27.    end if;
    28. end;

2006-10-31  Robert Dewar  <dewar@adacore.com>

	* sem_eval.adb (Compile_Time_Compare): Make use of information from
	Current_Value in the conditional case, to evaluate additional
	comparisons at compile time.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]