5

I was going through AFNetworking implementation and I found this

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wassign-enum"
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]];
#pragma clang diagnostic pop

(AFHTTPClient:489-492)

The assign-enum warning is obviously being turned off, but I wonder what does it mean.

What is the warning thrown by clang in that case?

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235

2 Answers2

8

The warning emitted in the absence of the clang pragmas is:

Integer constant not in range of enumerated type 'NSJSONWritingOptions' (aka 'enum NSJSONWritingOptions')

Looking at the declaration of NSJSONWritingOptions, we see that there is no defined value for 0:

enum {     NSJSONWritingPrettyPrinted = (1UL << 0) }; typedef NSUInteger NSJSONWritingOptions;

The docs do suggest passing 0, but there is no option defined like NSJSONWritingNoOption = 0, and thus we are assigning a constant (0) to an enum type that doesn't define 0 as a possible value.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
  • 1
    Thank you, it makes sense now. I still don't get why there's no something like `NSJSONWritingOptionsNone = 0` defined, which would probably be more elegant. That's a different question anyway. +1 – Gabriele Petronella Aug 28 '13 at 09:43
  • Ah I stumbled on this on the way. On iPhone. – uchuugaka Aug 28 '13 at 09:47
  • On the one hand, having it would be pointless from the perspective of implementing the API: no one would check something like `if (passedInOptions & NSJSONWritingOptionsNone) {//purely default case }` as that's not going to work and is kind of absurd anyway. It's always going to be `if (passedInOptions & NSJSONWritingOptionsCustomStuff) {// do custom stuff first} ... //default stuff here`. But it is definitely API design laziness to leave it out, for this very reason. – Carl Veazey Aug 28 '13 at 09:49
  • Agreed. I'll consider forwarding this to github. Not very important, but that library is such a gem that's it's a pity to see it polluted with compiler pragmas. – Gabriele Petronella Aug 28 '13 at 09:53
  • `NSJSONWritingOptions` is defined by Apple as part of the Foundation framework - not sure AFNetworking could do anything about it, unless there's some clever way of adding values to previously defined enums that I don't know about. – Carl Veazey Aug 28 '13 at 10:02
  • 2
    @GabrielePetronella There's not much Mattt Thompson can do about NSJSONWritingOptions since it's part of Foundation. It would be much nicer to cast the offending literal to the correct type, though: `options:(NSJSONWritingOptions)0` – Nikolai Ruhe Aug 28 '13 at 10:03
  • Regardless, the pull request that introduced these also fixes the warning for using `?:`, I'm not sure how that could be worked around otherwise. https://github.com/AFNetworking/AFNetworking/pull/957/files – Carl Veazey Aug 28 '13 at 10:07
  • Speaking of that, what's `-Wgnu`? – Gabriele Petronella Aug 28 '13 at 10:12
  • @CarlVeazey Using the `?:` operator in the gnu extended way (leaving out the second operand) is a minor issue for code that is supposed to be compiled as part of arbitrary projects that might use stricter compiler settings. Ignoring `-Wgnu` only hides these cases so I'd recommend to get rid of this. – Nikolai Ruhe Aug 28 '13 at 10:13
  • @Nikolai, yeah I got it by reading the merged pull request that introduced them. I agree that it's for corner cases, but I understand that if you use strict compiler settings you probably don't want an external library to generate warnings. – Gabriele Petronella Aug 28 '13 at 10:16
0

If you remove the pragmas an build it you will see the warning. Beyond that you would need to check the clang manual.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
  • I googled a while and I couldn't find a description of such warning. Any pointers to relevant documentation would be appreciated. – Gabriele Petronella Aug 28 '13 at 09:15
  • I see a lot of google results with this flag. Clang aims for a lot of GCC compatibility and flags are many with many arguments. See this SO post http://stackoverflow.com/questions/7880812/complete-list-of-clang-flags – uchuugaka Aug 28 '13 at 09:32
  • I've seen that post already. It points to a list of clang flags, but with no description. Am I missing something? – Gabriele Petronella Aug 28 '13 at 09:35
  • I'm stupid, I missed the actual answer. Anyway `clang -cc1 --help | grep assign-enum` doesn't return any result. – Gabriele Petronella Aug 28 '13 at 09:37
  • Appears indeed to be poorly documented but maybe this thread also helps... http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20130603/081154.html You might actually need to dig into clang source but certainly appears to be a warning about asginment with enum values. And they are using more warnings but cherry-picking this one to ignore here. – uchuugaka Aug 28 '13 at 09:39
  • Carl finally nailed it, thank you for the effort anyway. I appreciate that. – Gabriele Petronella Aug 28 '13 at 09:44
  • Seems maybe a type checking if a value is expected to be from a named enum but is not found in that enum. If they do -Wall this might annoy them when passing 0 as the options arg in the method. Just laziness. – uchuugaka Aug 28 '13 at 09:46