Bug 64862 - printf attribute should accept other string types
Summary: printf attribute should accept other string types
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 5.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on: 38308
Blocks:
  Show dependency treegraph
 
Reported: 2015-01-29 16:54 UTC by Tom Tromey
Modified: 2020-08-30 20:21 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-01-24 00:00:00


Attachments
ancient patch (7.21 KB, patch)
2019-01-11 18:42 UTC, Tom Tromey
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Tromey 2015-01-29 16:54:16 UTC
Consider this source:

#include <stdarg.h>
#include <uchar.h>

extern void p (const char16_t *fmt, ...)
  __attribute__((__printf__ (1, 2)));

void f()
{
  p(u"%d\n", 23);
}


Currently gcc rejects this attribute:

pokyo. gcc --syntax-only -std=c11 -Wall r.c
r.c:5:3: warning: ‘__printf__’ attribute directive ignored [-Wattributes]
   __attribute__((__printf__ (1, 2)));
   ^


I don't see why it should, though.
It seems to me that it could accept wchar_t, char16_t, or char32_t
strings as the format argument.
Comment 1 Andreas Schwab 2015-01-29 17:40:39 UTC
glibc has this in <wchar.h>:

extern int wprintf (const wchar_t *__restrict __format, ...)
     /* __attribute__ ((__format__ (__wprintf__, 1, 2))) */;
Comment 2 Tom Tromey 2015-01-29 18:09:23 UTC
Naturally my example was wrong.
Sorry about that.  But gcc still doesn't handle it:

#include <stdarg.h>
#include <uchar.h>

extern void p (const char16_t *fmt, ...)
  __attribute__((format (__printf__, 1, 2)));

void f()
{
  p (u"%s %d", 23, "hi");
}



... with gcc saying:

r.cc:5:44: error: format string argument is not a string type
   __attribute__((format (__printf__, 1, 2)));
                                            ^
Comment 3 Andrew Pinski 2015-01-29 18:16:26 UTC
wchar_t part was filed as bug 38308 a long time ago.
Comment 4 jsm-csl@polyomino.org.uk 2015-01-29 22:00:12 UTC
Note also that gcc.dg/format tests are run both with and without -DWIDE - 
the intent there is that wide string formats should be tested, when 
supported, with essentially the same tests as narrow string formats (via 
appropriate macros to make the tests generic, e.g. wrapping format strings 
in a macro L()).  That could be applied to other string types as well by 
running the tests more than twice.
Comment 5 Tom Tromey 2015-02-21 04:48:40 UTC
I have a patch for this.  I think it fixes bug 38308 and bug 20110
as well.  It works by adding a new function to libcpp to convert
a string back to the source charset.

I still have to get a new copyright assignment.
And the test cases are being a pain to update.
Comment 6 Andrew Pinski 2016-01-24 07:28:53 UTC
(In reply to Tom Tromey from comment #5)
> I still have to get a new copyright assignment.
> And the test cases are being a pain to update.

How is the copyright assignment coming?
Comment 7 Martin Sebor 2018-09-14 17:33:59 UTC
Any progress?
Comment 8 Tom Tromey 2019-01-11 18:42:08 UTC
Sorry about the extreme delay on this.
I think my patch has long since bit-rotted, but I can attach it for
reference.  I believe my assignment situation got sorted out so this
should be fine to read and/or copy from.
Comment 9 Tom Tromey 2019-01-11 18:42:33 UTC
Created attachment 45413 [details]
ancient patch
Comment 10 Tom Tromey 2019-01-11 18:44:17 UTC
Also I think all the test suite changes never really worked.