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: c/1027: slightly misleading printf format warning


"Joseph S. Myers" <jsm28@cam.ac.uk> writes:

> On Sun, 10 Dec 2000, Kaveh R. Ghazi wrote:
> 
> > IMHO, we should see if there is a solution which doesn't do this.  Or
> > better yet, implement the "extensible format checking" from the
> > projects page. :-)
> 
> Care to suggest a syntax for specifying format conversions :-)?  In
> particular, some change seems to be needed to both the C and C++ grammars
> to implement format specifications - at least if it is to be possible to
> pass types (such as "tree") in, which is fairly important.  Also, I don't
> want to end up with a syntax too closely bound to GCC's internal data
> structures at the time it was implemented.

Yes.  We have some work somewhat like this at Red Hat, but it's truly
evil: the file describing the format consists of colon-delimited
fields, and there's something like _23_ fields per line.

My thought is to simply have a regular expression to describe what can
go after % for each type.  For instance, a modified printf that
only supports `%c', `%d', and `%i' might be:

__attribute__((format (
	"-?[0-9]*c" : int;
	"-?[0-9]*lc" : wint_t;
	"-?\*c" : int, int;
	"-?\*lc" : int, wint_t;
	"[-+ 0]*([1-9][0-9]*)?(\.[0-9]*)?(hh|h|)[id]" : int;
	"[-+ 0]*([1-9][0-9]*)?(\.[0-9]*)?l[id]" : long;
	"[-+ 0]*([1-9][0-9]*)?(\.[0-9]*)?ll[id]" : long long;
	"[-+ 0]*([1-9][0-9]*)?(\.[0-9]*)?j[id]" : intmax_t;
	"[-+ 0]*([1-9][0-9]*)?(\.[0-9]*)?t[id]" : ptrdiff_t;
	"[-+ 0]*([1-9][0-9]*)?(\.\*)?(hh|h|)[id]" : int, int;
	"[-+ 0]*([1-9][0-9]*)?(\.\*)?l[id]" : int, long;
// more combinations...
	"[-+ 0]*\*(\.\*)?t[id]" : int, int, ptrdiff_t;
	)))

We might want to specify a keyword that means 'all the regular printf
formats', since 90% of the use of this will be to describe a function
that does printf with a few extra modifiers.

It is an error if there are multiple lines that match but have 
different numbers or types of arguments or that match different
format lengths.

This is needed to specify some of the bizzare formats that are seen in
the wild, for instance a format that takes two pointers of particular
types.  Unfortunately, it's not quite general enough; it doesn't allow
for the case of positional printf parameters.  This could be added
with just a little extra syntax...

-- 
- Geoffrey Keating <geoffk@geoffk.org>

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