This is the mail archive of the gcc@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]

Re: size_t printf warnings and preprocessor


On Thu, 12 Jul 2001, Marc Espie wrote:

> I would like to have a switch such that 
> size_t var;
> printf(format, var);
> 
> invariably ends in an error, as there can't be a single format that works
> with size_t.

With C99, people should just use %zu, which will work.  C89 programmers,
if conscientious, will have been casting size_t to (unsigned long) and
using %lu.

If you want warnings for other formats, arrange for size_t to be the
magical sizetype node everywhere, and for this to be a proper extended
integer type - but that is the opposite direction to the way things have
been going (ensuring that sizetype doesn't appear as a type in user code,
only c_size_type_node) and non-conforming to C89, in which size_t must be
one of the standard integer types (DR#067).

I think attempting to track where the format strings came from and whether
there might be problems on other systems is too much complexity for too
little gain.  You can analyse programs for bugs with a compiler, and this
will only examine the code that applies on the system you use; or you can
analyse them with separate analysis tools, which may handle #ifdef-ed out
code and portability issues, but can't leverage compiler internals in the
same way.

Attributes on string literals could be useful, but for such purposes as
putting them in special sections rather than marking their format
properties.

-- 
Joseph S. Myers
jsm28@cam.ac.uk


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