Debugging: GCC/Clang __attribute__((format)) Specifier

Discussion of chess software programming and technical issues.

Moderator: Ras

Rein Halbersma
Posts: 771
Joined: Tue May 22, 2007 11:13 am

Re: Debugging: GCC/Clang __attribute__((format)) Specifier

Post by Rein Halbersma »

Tom Likens wrote: And yes, before you start throwing stones I know that C++'s "cout << blah blah" syntax would have prevented all this, but frankly I viserally hate that syntax, so to each their own.
I don't know if you can compile your program in C++ mode, or if you would like to depend on Boost, but the Boost.Format library combines type safety with convenience.
mvk
Posts: 589
Joined: Tue Jun 04, 2013 10:15 pm

Re: Debugging: GCC/Clang __attribute__((format)) Specifier

Post by mvk »

Tom Likens wrote:Yes, it's amazing how many of these creep in--especially, when compiling for 32-bit vs. 64-bit. I even saw differences between 64-bit Linux and 64-bit Windows. It's was rather humbling. And they can be damaging, when I cleaned this up the 32-bit Windows compile was no longer unstable (i.e. no more crashing).

--tom
I doubt these five will be fixed. Anyway, thanks for the "heads up".
[Account deleted]
lucasart
Posts: 3243
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Debugging: GCC/Clang __attribute__((format)) Specifier

Post by lucasart »

Rein Halbersma wrote:
Tom Likens wrote: And yes, before you start throwing stones I know that C++'s "cout << blah blah" syntax would have prevented all this, but frankly I viserally hate that syntax, so to each their own.
I don't know if you can compile your program in C++ mode, or if you would like to depend on Boost, but the Boost.Format library combines type safety with convenience.
You're kidding right? There's enough uglyness in C++ iostream not to add more with Boost. I thought Tom's initial post was clear about not wanting C++ solutions.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
Rein Halbersma
Posts: 771
Joined: Tue May 22, 2007 11:13 am

Re: Debugging: GCC/Clang __attribute__((format)) Specifier

Post by Rein Halbersma »

lucasart wrote:
Rein Halbersma wrote:
Tom Likens wrote: And yes, before you start throwing stones I know that C++'s "cout << blah blah" syntax would have prevented all this, but frankly I viserally hate that syntax, so to each their own.
I don't know if you can compile your program in C++ mode, or if you would like to depend on Boost, but the Boost.Format library combines type safety with convenience.
You're kidding right? There's enough uglyness in C++ iostream not to add more with Boost. I thought Tom's initial post was clear about not wanting C++ solutions.
It sounds like you didn't even bother to read the posted link, and only expressed your general feelings about C++... :roll:

What most people hate about iostreams is its extreme verbosity of the various setXXX manipulators, and the fact that formatting options and variables are intertwined. Boost.Format is similar to printf style formatting. Given a formatting string s, the following two lines should do the same

Code: Select all

printf(s, x1, x2);
cout << format(s) ℅ x1 % x2;
It still uses cout, but with a lot less verbosity than traditional iostreams and added type safety as a bonus.

Only if you are hung up about overloaded operator<< and operator% as a matter of principle, then yes, by all means stick to printf().
hMx
Posts: 61
Joined: Wed Mar 08, 2006 9:40 pm
Location: Germany, Berlin

Re: Debugging: GCC/Clang __attribute__((format)) Specifier

Post by hMx »

Tom Likens wrote: Now that I've added the attribute stuff both Clang and gcc catch format vs. argument errors inside all these custom functions. The immediate downside though is that this ties you to those compilers. That's not too tremendous a handicap though, because the prototypes could be #ifdef in and out depending on the compiler.

--tom
To retain some readability in spite of conditional expansion the usual idiom is to use conditionally defined manifests, like this:

Code: Select all

#if defined(__GNUC__)
# define Wprintf12 __attribute__((format (printf, 1, 2)))
#else
# define Wprintf12 /*nothing*/
#endif

void print(   const char *format, ...) Wprintf12;
void printL(  const char *format, ...) Wprintf12;
IMO that is much better than using #if or #ifdef at each usage point. And you get the additional checking wherever you have support for it. On all other platforms you just have no further checking, but still the same code.
Tom Likens
Posts: 303
Joined: Sat Apr 28, 2012 6:18 pm
Location: Austin, TX

Re: Debugging: GCC/Clang __attribute__((format)) Specifier

Post by Tom Likens »

hMx wrote:
Tom Likens wrote: Now that I've added the attribute stuff both Clang and gcc catch format vs. argument errors inside all these custom functions. The immediate downside though is that this ties you to those compilers. That's not too tremendous a handicap though, because the prototypes could be #ifdef in and out depending on the compiler.

--tom
To retain some readability in spite of conditional expansion the usual idiom is to use conditionally defined manifests, like this:

Code: Select all

#if defined(__GNUC__)
# define Wprintf12 __attribute__((format (printf, 1, 2)))
#else
# define Wprintf12 /*nothing*/
#endif

void print(   const char *format, ...) Wprintf12;
void printL(  const char *format, ...) Wprintf12;
IMO that is much better than using #if or #ifdef at each usage point. And you get the additional checking wherever you have support for it. On all other platforms you just have no further checking, but still the same code.
Yes, that was actually my intent. I use this trick all over the place when compiling the release vs. debug version of the engine. As you mention it's much cleaner. Thanks for the tip.

--tom