views:

62

answers:

4

I unintentionally compiled the following statement:

manager.groupName - lastGroupName;

Instead of:

manager.groupName = lastGroupName;

I am trying to understand why the compiler does not even provide a warning for the former statement I unintentionally provided. The statement has no effect, even it if is legal to subtract pointers from one another.

Both groupName and lastGroupName are of type (NSString *). The groupName property is declared as:

@property (nonatomic, retain) NSString *groupName;

Wondering if I should visit bugreporter or if there is a reason that XCode isn't providing a diagnostic.

A: 

XCode doesn't have the best syntax checking, although it's supposed to be much better in the version coming out soon (4.0 I believe, in beta now).

JakeVA
Xcode doesn't have any syntax checking. The syntax checking is done by the compiler, gcc by default. gcc's poor error reporting is one of the drivers behind clang.
JeremyP
A: 

Registered developers can download XCode 4 beta which improves syntax checking in the "clang" 2.0 front-end. You can get some of that now by selecting "llvm" with clang 1.5 as your compiler instead of GCC. Apple's current recommendation is the GCC front-end and LLVM 1.5 back-end, but then you'll still be stuck with GCC's awful error messages.

I'd recommend downloading XCode 4 and trying the code out on that. You might find some significant bugs that you can go back and fix in XCode 3 before release.

In addition, periodically doing a "Clean All" and "Compile and Analyze" will find a lot of common mistakes. Highly recommended, especially if you're stuck on something.

samkass
A: 

This is legal syntax, so no error should be expected. It does seem useless, and a high quality compiler might issue a warning.

At runtime the result is a different matter. Unless I'm misremembering this is undefined behavior unless the two pointers are the same or at least point into the same array.

Darron
It is not undefined behavior: a C compiler will perform the subtraction regardless of whether the result will provide a valid pointer.
ctpenrose
There used to be language in c89 to allow for segmentation faults on x86 if the two pointers had different segment values and the arithmetic was nonsense. I presume that language still exists in current versions of the standard.
Darron
I didn't know that. But I did compile and run this the other day: #include <stdio>
ctpenrose
+4  A: 

This is a legal statement in C and thus also in Objective-C, so the compiler doesn’t have to warn about it. You could add the warning flag -Wunused-value to the compiler settings. This warns about statements without effect like this.

Generally there are lots of flags to tell the compiler what exactly to warn about. Everybody has different ideas what is OK, and what should be warned about. If the compiler emits too many warnings they become useless.

Also note that clang does indeed produce better warning and error messages it doesn’t mean that it will automatically produce more warnings. It also has the same flags for enabling and disabling certain warnings that gcc has.

Sven
My mistake. I had long thought I had -Wall enabled in my project and alas I did not. Using -Wall (or presumably -Wunused-value) did in fact provide a warning for this statement.
ctpenrose