[cfe-users] Accepting "%@" in format string with __attribute__ format

David Maclachlan dmaclach at google.com
Thu Sep 19 08:51:26 PDT 2013


Hey all,

I have the exact same problem  as this old message on the gcc-help mailing
list.

http://gcc.gnu.org/ml/gcc-help/2010-03/msg00114.html

Since the original author did such a great job explaining it, I'm just
going to paste it here:

------

I have a function like

void myLog(const char *format, ...);

Now I want to let the compiler check the passed parameters if they fit to
the format string like printf.
I know this can be done easily with the following:

void myLog(const char *format, ...) __attribute__((format(printf, 1, 2)));

Now my problem is: I also want to accept the "%@" specifier that is
accepted in Obj-C NSStrings or NSLog. The "%@" specifier accepts Obj-C
objects (NSObject or id) or Core Foundation Object References (CFTypeRef).
I saw that NSLog is declared this way:

void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1,
2)));

Problem is that I don't have an NSString as format string parameter in my
function. I have a plain old C-string (it should also work in C++ code, not
only in Obj-C or Obj-C++ code). But it accepts the "%@" specifier (I am
using Core Foundation in the Implementation under-the-hood).
When I use the __NSString__ declaration like NSLog, gcc gives me an error
message that my format parameter is not an NSString. Sure, it is not. But
it doesn't matter. I just want gcc to do the format parameter checking like
that.

Is there any chance to get gcc check my passed parameters the "Objective-C
way" with "%@" included but still having the format string passed as plain
old C-string? It there any other types than __NSString__ I could try?

--------

Any suggestions?

Cheers,
Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20130919/f8bc61c7/attachment.html>


More information about the cfe-users mailing list