Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Q: stdint format specifiers Date: Thu, 04 Aug 2016 12:44:43 -0700 Organization: None to speak of Lines: 39 Message-ID: References: <282c43f4-9e51-4c3c-9552-6703c60df230@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="14607"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18iZEJHwITRkanrAsBILRXz" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:lquGY0wF1hlOIHMl4ZAAYytaAQk= sha1:jy6+rbYbfduLixgb9TOkW5HFvfA= Xref: csiph.com comp.lang.c:91672 supercat@casperkitty.com writes: > On Thursday, August 4, 2016 at 11:11:29 AM UTC-5, Johann Klammer wrote: >> Does anyone have a coccinelle script to fixup *printf format specifiers to >> those pesky PRI*** ones? I could not find one on coccinellery . > > Such a thing will likely be difficult unless one can keep track of types > in more detail than most compilers do. If a particular compiler defines > "int32_t" as an alias for "long", then "%ld" would likely get used on that > compiler as the formatting code for both "int32_t" and "long". Unless > a conversion tool can keep track of which 32-bit variables are actually > of type "uint32_t", it would have no way of knowing which ones should be > printed out using a "%ld" specifier and which ones require the PRI_* macros. To be clear, assuming int32_t is a typedef for long, there is no distinction between a variable of type int32_t and a variable of type long. int32_t is not a distinct type with the same characteristics as long; it's a different name for exactly the same type. Certainly a tool *could* keep track of which typedef is used to define an object, and choose a printf format based on that. (It would be tracking additional information that's not defined by the C language; nothing wrong with that.) Without that ability, it could use "%ld" for any argument of type long, whether it was defined using the name "long" or the name "int32_t". But then the resulting code (assuming you're trying to generate C source code) wouldn't be portable; it could fail when compiled for a system where int32_t is an alias for int, and long is 64 bits. (Unless you regenerate the source code for each target system; is that an option?) It's an interesting problem, and not one that the C language gives you a lot of help with. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"