Xcode: macros breaking completion/highlighting
-
16-02-2021 - |
Вопрос
From the Xcode Docs:
Syntax highlighting, code completion, and every other index-driven feature is handled by the LLVM parser. If the compiler knows about a symbol, so does the Xcode IDE.
but none of these "index-driven features" are working for me when writing code inside of a preprocessor directive. Does anyone have a solution for this?
Examples:
When building with Unused Parameter warnings turned on, tons of warnings are generated even when said parameter is used. The code in this specific screenshot is from Apples Reachability.m
and is unmodified. Notice that syntax highlighting is also nonexistent here:
Correct completion inside #if
:
But, incorrect completion inside #else
:
It half works with local variables:
But breaks again when calling something declared outside of the current method scope:
Another example
Another example
Can anyone please tell me how (or even if) this can be fixed?
Решение 3
Turns out this was a bug and is now fixed in Xcode Version 4.3.1 (4E1019).
Другие советы
XCode actually evaluates the conditions on preprocessor directives and only does highlighting/completing for the code inside the currently true condition. For example
Syntax highlighting, autocompletion, and warnings (unsused variable warning in this case) don't work in the untrue condition.
I opened a question asking how to make XCode do all these things on both sides of the condition, but no luck so far.
XCode syntax highlighting in both conditions of preprocessor #if #else
For the record I do not think this behavior is a bug. I can imagine cases where it would be extremely frustrating to be getting errors on code that won't be compiled. It would however be nice to be able to edit both sides of these conditions without having to manipulate your macro definitions.
@chown, I think "If the compiler knows about a symbol...exactly as they are when building." indicate that compiler parses the code and follow the all preprocessor conditions. In such case, compiler will not notice variable usage inside of such #ifdef
where condition has failed.
This also explains why it is "works fine in the #else
.".
You can try to use UNUSED macro, but you will have to disable warning for "Unused values":
#define UNUSED(a) a
...
-(void)test:(id)argument
{
UNUSED(argument);
#ifdef AAA
NSLog(@"arg:%@", argument);
#endif
}