[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gcc warnings

From: David Chisnall
Subject: Re: gcc warnings
Date: Wed, 17 Aug 2011 14:51:14 +0100

On 17 Aug 2011, at 14:41, Andreas Höschler wrote:

> Hi all,
>>> I am cross-building on GNUstep/Solaris and MacOSX. When I build code like 
>>>   NSString *message = @"some string"
>>>   [NSException raise:NSInternalInconsistencyException format:message];
>>> on MacOSX 10.6 using GNUstep make, gcc gives the following warning
>>>     SOEditingContext.m:3574: warning: format not a string literal and no 
>>> format arguments
>>> Any idea how to get rid of that one?
>>  [NSException raise:NSInternalInconsistencyException format:  @"%@", 
>> message];
>> The warning is because it can't do a compile time check of message to see if 
>> it's a valid format string ... you need to use a string literal as the 
>> format string.
> Thanks for your feedback! I know that this prevents the warning, but I can't 
> do that. The message is calculated by some method and then returned as a 
> string. I then need to raise the exception with the readily build string. If 
> NSException had a +[raise:string:] method I would use that, but it hasn't! :-(
> I have the same problem with NSLog(). I am again building the message 
> somewhere and get it as a NSString. I then simply want to log it out and get 
> the above warning. 
>   NSLog([localException description]);   

This is just plain wrong.   Do that, and you can get random stack corruption.  
You should always do this instead:

NSLog(@"%@", [localException description]);   

> That's simply annoying! :-( Isn't there some compiler switch that could be 
> used to suppress these kind of warnings?

Well, the best way of getting rid of compiler warnings is to write correct 
code.  If you have a string that is generated programatically, then you can't 
generally guarantee that it doesn't contain format escape sequences, so if you 
pass it to something that expects a format string then you may have things 

If you have a function that returns output that is usable in format strings in 
the same context as its input, then there is an __attribute__() that you can 
add to let the compiler know.  See the localisation stuff in NSBundle.h for 
some examples of this.


-- Sent from my Apple II

reply via email to

[Prev in Thread] Current Thread [Next in Thread]