TIP #340 Version 1.3: Const Qualification of Tcl_SetResult's Argument and -Wwrite-strings

This is not necessarily the current version of this TIP.


TIP:340
Title:Const Qualification of Tcl_SetResult's Argument and -Wwrite-strings
Version:$Revision: 1.3 $
Author:Jan Nijtmans <nijtmans at users dot sourceforge dot net>
State:Draft
Type:Project
Tcl-Version:8.6
Vote:Pending
Created:Friday, 14 November 2008
Keywords:Tcl_SetResult

Abstract

As a follow-up of TIP #27, in Tcl 8.6 and Tk 8.6 much work has been done to clean up the remaining places where pointers were not const qualified. The question is, how can we prevent that in the future similar "mistakes" are made. The gcc compiler warning -Wwrite-strings helps in that, therefore this TIP proposes to add that to the CFLAGS_WARNING flag in Tcl and Tk when using gcc. But for this flag to be introduced, all warnings will have to be eliminated. In the HEAD, this is done already, except for one function: Tcl_SetResult. This function is explicitely mentioned in TIP #27 not to be modified, because it cannot be handled without unsafe casting. This TIP proposes to modify it, as a special exception of TIP #27, and use a single type cast (in tclResult.c, line 423) for that.

Rationale

The gcc manual mentions for the flag -Wwrite-strings:

When compiling C, give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will get a warning .... These warnings will help you find at compile time code that can try to write into a string constant, but only if you have been very careful about using const in declarations and prototypes. Otherwise, it will just be a nuisance; this is why we did not make -Wall request these warnings.

Now that all Tcl and Tk API's are modified to be very careful about using const, this opens the way to add -Wwrite-strings to CFLAGS_WARNING when building with gcc. Other extensions can start to do the same, if they want to find out about this type of potential problem.

When considering the elimination of the warning when using 'Tcl_SetResult, I see two alternatives:

There has been a discussion stating that changing the Tcl_SetResult signature is wrong, because Tcl_SetResult() cannot be made const-correct. By its very design, this is impossible. The only solution is to deprecate it. But deprecating it means that 129 Tcl_SetResult calls in Tcl and 258 calls in Tk need to be modified. Ough. But there is a simpler solution.

Most Tcl_SetResult calls use TCL_STATIC or TCL_VOLATILE as last argument. In this case, the second argument is expected to be a const.

Very few Tcl_SetResult calls have some other value as last argument, most likely TCL_DYNAMIC. This TIP proposes to deprecate Tcl_SetResult for other freeProc values than TCL_STATIC and TCL_VOLATILE.

It turns out that Tcl had only 4 deprecated (as defined by this TIP) Tcl_SetResult calls, one of them was wrong [Bug 2308236], two of them were in tclTest.c meant to test the Tcl_SetResult function itself. Tk had only 5 such calls. All those calls have been modified now. Tcl and Tk are free from depreceded Tcl_SetResult calls now.

Reference Implementation

The patch is available in: http://sourceforge.net/tracker/?func=detail&group_id=10894&atid=310894&aid=2315890

Copyright

This document has been placed in the public domain.


Powered by TclThis is not necessarily the current version of this TIP.

TIP AutoGenerator - written by Donal K. Fellows