Hi,
gpc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/specs gpc version 20011222, based on gcc-2.8.1
I have to use gcc-2.8.1 because later versions have a bug with goto out multiple levels (as discussed in this forum before).
My new problem is with source:-
program Hello ;
function covers( var p1 , p2 : integer ) : boolean ; begin if ( p1 = p2 ) then begin covers := false ; writeln( 'mgcd covers returns false 1' ) ; return ; end ;
covers := true ; writeln( 'mgcd covers returns true' ) ; end ;
var i1, i2 : integer ; begin i1 := 1 ; i2 := 1 ; writeln( 'covers=' , covers( i1 , i2 ) ) ; end.
When I run it I get:-
mgcd covers returns false 1 covers=True
I could not find a reference to this problem. The problem was not introduced in 20011222 because 20010924 shows the same problem.
Can anyone help?
Cheers, Martin.
Martin G C Davies wrote:
gpc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/specs gpc version 20011222, based on gcc-2.8.1
I have to use gcc-2.8.1 because later versions have a bug with goto out multiple levels (as discussed in this forum before).
My new problem is with source:-
program Hello ;
function covers( var p1 , p2 : integer ) : boolean ; begin if ( p1 = p2 ) then begin covers := false ; writeln( 'mgcd covers returns false 1' ) ; return ; end ;
covers := true ; writeln( 'mgcd covers returns true' ) ;
end ;
var i1, i2 : integer ; begin i1 := 1 ; i2 := 1 ; writeln( 'covers=' , covers( i1 , i2 ) ) ; end.
When I run it I get:-
mgcd covers returns false 1 covers=True
I could not find a reference to this problem. The problem was not introduced in 20011222 because 20010924 shows the same problem.
You use `Return' without parameters which returns an arbitrary (undefined) value. If you compile with `-Wall' (always recommended), you'll see this problem immediately.
You can use `Exit' instead of `Return' to avoid this problem. Both are non-standard, BTW (`Exit' comaptible to BP, `Return' to C-; and perhaps some other Pascal compilers). The "clean" solution, and very easy to do here, would be to use an `else' branch.
Frank
-----Original Message----- From: gpc-owner@gnu.de [mailto:gpc-owner@gnu.de]On Behalf Of Frank Heckenbach Sent: 11 January 2002 16:37 To: gpc@gnu.de Subject: Re: boolean function return value wrong
<snip>
You use `Return' without parameters which returns an arbitrary (undefined) value.
Ah. A hangover from Pascals from DEC/VS/Sparc which don't want a value (last value assigned to the function name is used).
I can't see any way an "undefined" return value is useful so why is it just a warning since it's almost bound to produce bad code?
If you compile with `-Wall' (always recommended), you'll see this problem immediately.
Sadly I get thousands of warnings like that (case statements where not all values checked mostly).
You can use `Exit' instead of `Return' to avoid this problem. Both are non-standard, BTW (`Exit' compatible to BP, `Return' to C-; and perhaps some other Pascal compilers).
Just what I need. I looked at the return man page but it's a "to be supplied" page and didn't reference the exit command.
The "clean" solution, and very easy to do here, would be to use an `else' branch.
Too much existing code.
Thanks for the excellent help again Frank.
Cheers, Martin.
Martin G C Davies wrote:
I can't see any way an "undefined" return value is useful so why is it just a warning since it's almost bound to produce bad code?
I'll turn it into an error.
If you compile with `-Wall' (always recommended), you'll see this problem immediately.
Sadly I get thousands of warnings like that (case statements where not all values checked mostly).
Try `-Wno-switch' (after `-Wall').
Frank
Frank Heckenbach wrote:
Martin G C Davies wrote:
gpc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/specs gpc version 20011222, based on gcc-2.8.1
I have to use gcc-2.8.1 because later versions have a bug with goto out multiple levels (as discussed in this forum before).
My new problem is with source:-
program Hello ;
function covers( var p1 , p2 : integer ) : boolean ; begin if ( p1 = p2 ) then begin covers := false ; writeln( 'mgcd covers returns false 1' ) ; return ; end ;
covers := true ; writeln( 'mgcd covers returns true' ) ;
end ;
var i1, i2 : integer ; begin i1 := 1 ; i2 := 1 ; writeln( 'covers=' , covers( i1 , i2 ) ) ; end.
When I run it I get:-
mgcd covers returns false 1 covers=True
I could not find a reference to this problem. The problem was not introduced in 20011222 because 20010924 shows the same problem.
You use `Return' without parameters which returns an arbitrary (undefined) value. If you compile with `-Wall' (always recommended), you'll see this problem immediately.
You can use `Exit' instead of `Return' to avoid this problem. Both are non-standard, BTW (`Exit' comaptible to BP, `Return' to C-; and perhaps some other Pascal compilers). The "clean" solution, and very easy to do here, would be to use an `else' branch.
AFAIK this usage is not condoned by any Pascal standard, and should be discouraged for just this sort of reason. Would it be possible to collect such things and emit warnings for them unless they have been specifically enabled, for example by the --BP?? flag for exit. This would discourage people from using such unsafe mechanisms, and still meet your objectives of compiling anything from anywhere :-)
I would imagine a "warn(errno, otherparms)" procedure in the compiler, called for all suspicious constructs. It would be up to warn to suppress in specific configurations.
Incidentally, shouldn't the return have used the value of covers in the first place? It is not possible to write "return (covers);" because that would be a recursive call. Maybe it should have been compiled as a jump to the exit code, which is probably what you are doing for 'exit' anyway.
CBFalconer wrote:
Frank Heckenbach wrote:
Martin G C Davies wrote:
gpc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.6/2.8.1/specs gpc version 20011222, based on gcc-2.8.1
I have to use gcc-2.8.1 because later versions have a bug with goto out multiple levels (as discussed in this forum before).
My new problem is with source:-
program Hello ;
function covers( var p1 , p2 : integer ) : boolean ; begin if ( p1 = p2 ) then begin covers := false ; writeln( 'mgcd covers returns false 1' ) ; return ; end ;
covers := true ; writeln( 'mgcd covers returns true' ) ;
end ;
var i1, i2 : integer ; begin i1 := 1 ; i2 := 1 ; writeln( 'covers=' , covers( i1 , i2 ) ) ; end.
When I run it I get:-
mgcd covers returns false 1 covers=True
I could not find a reference to this problem. The problem was not introduced in 20011222 because 20010924 shows the same problem.
You use `Return' without parameters which returns an arbitrary (undefined) value. If you compile with `-Wall' (always recommended), you'll see this problem immediately.
You can use `Exit' instead of `Return' to avoid this problem. Both are non-standard, BTW (`Exit' comaptible to BP, `Return' to C-; and perhaps some other Pascal compilers). The "clean" solution, and very easy to do here, would be to use an `else' branch.
AFAIK this usage is not condoned by any Pascal standard, and should be discouraged for just this sort of reason. Would it be possible to collect such things and emit warnings for them unless they have been specifically enabled, for example by the --BP?? flag for exit. This would discourage people from using such unsafe mechanisms, and still meet your objectives of compiling anything from anywhere :-)
I think there are two different issues:
- This particular usage of `Return' without a parameter in a function. That's simply wrong, and GPC will flag it as an error in the future.
- Use of `Return' and `Exit' in general. Indeed, Pascal standards don't condone them -- not because of this particular problem but because of the "single exit" philosophy. -- OTOH, they have `goto', and (EP only, IIRC) `Halt' which also contradict this philosophy. A main reason often given for the existence of `goto' is the nonlocal usage to return from nested routines, and in my experience those situations can often be adequately dealt with using `Exit'; and `Exit' is still "cleaner" in a way than `goto' which can jump in the middle of an outer procedure, whereas `Exit' at least returns to where it was called from. Well, we could keep arguing about these points, but the bottom line is: GPC's design principle is, allow for all reasonable extensions (and some not so reasonable ones ;-) by default, and forbid them with `--extended-pascal' etc. That's exactly what it does here, i.e. in EP mode, `Exit' and `Return' are simply unknown identifiers.
Frank