Pascal Viandier wrote:
Hello,
When compiling a Pascal source with gpc, I receive the following warning:
gpc -g -O0 -D_DEBUG --setlimit=2000 --unit-path=gpi:gpc --gpi-destination-path=gpi --implementation-only -D_DBG -Icommon/include -Imain/include -I/data2/informix-7.31.UD6/include -I/data2/informix-7.31.UD6/incl/esql -I/usr/local/devapps/ISAM/include -c common/src/fdb.pas -o obj/dbg/fdb.o common/src/fdb.pas: In procedure `CreeFDB': common/src/fdb.pas:1041: warning: `for' loop counter is threatened in a subroutine
<snip>
The code inside the 'for' loop does not call any subroutine, except WriteLn and Return (conditionally) and they don't make any use of the counter. I wonder how the loop counter may be threatened in any way... My question is: did I missed something, or is gpc so delicate that one cannot use a 'for' loop counter outside of the loop for other purpose (that's the case here, the 'for' loop in the middle of a procedure)? How gpc determines that a 'for' loop counter may be altered in the body of the loop?
The name "threatened" came from Extended Pascal standard (ISO-10206). The name formalizes concept that "a statement may (possibly) modify a variable". This is a simple condition, so it gives many false positves. In EP it is an error if the loop control variable is treathened in a subroutine. EP do not consider if the subroutine may be called from the loop or not. In general finding out if a subroutine may be called from the loop is a variant of halting problem, and if one would like an approximation to "may be called" the standard would be forced to formulate exact rules which in turn would place extra burden on all EP-compilant compiler.
GPC when given `-fextended-pascal' option tries to be EP compilant and changes the above warning to an error.
GPC allows (without warnings) assignments to the loop counter, but only outside the loop. Usually it is easy to avoid assignments in subroutines by introducing a new variable.