Treat using NSLog as a build error - ios

I want to treat any NSLog in my code as an error. The line should be a compile time error in Xcode. Just like how usage of release in an ARC enabled project is treated as an error.

Try this,
#define NSLog_UNAVAILABLE __attribute__((unavailable("NSLog is treated as Error.")))
FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) NSLog_UNAVAILABLE;

You can redeclare prototype of NSLog in your code with __deprecated, so any use of it will produce warning:
FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) __deprecated;
Then maybe there is some way to turn warnign into error.

You can use this if you use Clang as your compiler (actually just realised GCC also supports C11's _Static_assert too from version 4.6):
#define NSLog(...) _Static_assert(0, "Not allowed to use NSLog!")
Compilation will fail if a _Static_assert fails.

Related

Xcode compiler errors when using ##__VA_ARGS__

Here is my customized NSLog.
#define NSLog(fmt, ...) printf("🌳🌳🌳%s,%d\n %s\n\n", __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:fmt, ##__VA_ARGS__].UTF8String)
This log is work good on another project, but it occurred 4 error when compiler time in current project. If using __VA_ARGS__, like #define NSLog(...) NSLog(__VA_ARGS__), it's going to compile successful. I think that maybe caused in Building Setting.
It's anyone has any idea to help me to solve this.
Check that the header file declaring this imports Foundation. Check that you don't have NSLog defined somewhere else as well and finally, really not a good idea to redefine NSLog. You're probably better off adopting a logging framework or defining your own like this:
#define MELog(fmt, ...) NSLog("🌳🌳🌳%s,%d\n %s\ ....

XCode use of undeclared identifier, only appears if the file is opened

When I clean, build and run my XCode project all goes well, but if I open a file that calls the function, an Use of undeclared identifier 'func()' appears. This is the whole implementation:
The function is called:
func();
And 'func()' is decleared in a .h file like so:
#if __cplusplus
extern "C" {
#endif
extern void func();
#if __cplusplus
}
#endif
and func() is implemented is a cplusplus library.
Why would the error ONLY appears if the file is open, but if I don't open it, it runs and works just fine?
Xcode has two ways of generating error messages. Usually both of these generate the same messages, so you do not detect that there are two systems.
The first system is the editor which does syntax coloring and auto-completion. It will also show error messages almost instantly after you write an error.
The second system is errors shown in the build log.
I suspect that your project has a complicated include setting. This prevents the first system from finding the correct file to include and therefore it cannot find the definition of func().

Operator 'defined' requires an identifier ios

I've below code in my project dlog should print the values in console if isConsoleLogActive is YES.
It gives error like Operator 'defined' requires an identifier
#if defined ([Util isConsoleLogActive])// Operator 'defined' requires an identifier in this line
#define DLog(...) NSLog(__VA_ARGS__)
#define DTrace() NSLog(#"%s", __PRETTY_FUNCTION__)
#else
#define DLog(...) /* */
#define DTrace() /* */
#endif
if I use the same code([Util isConsoleLogActive]) in my .m it works perfectly fine. I face this issue only in #define
What could be the issue. Please give me some idea.
The various commands that start with # are preprocessor directives. These get executed before the compilation phase at build time, before your application actually executes. You should use the preprocessor directives to conditionally include different code in your application based on build configuration. The preprocessor, however, is the wrong way to handle conditional execution on a specific platform at runtime; for that, you want your standard "if...else" logic.
If your goal with that statement is to determine if the given selector exists, try respondsToSelector, instead.
Result of
[Util isConsoleLogActive]
is not known at compile-time. So you can not use it with '#if defined'.

CF_IMPLICIT_BRIDGING_ENABLED non-functional in xcode5?

The WWDC 2013 lecture slides contain a section that in some cases the explicit bridging casts can be omitted. I wrote this in Xcode 5 (using 10.8 though, not 10.9), and the compiler complains I need a bridging cast. Am I just totally not understanding the concept?
#import <Foundation/Foundation.h>
CF_IMPLICIT_BRIDGING_ENABLED
CFStringRef MyCreateStringFromNothing();
CF_IMPLICIT_BRIDGING_DISABLED
void SomeFunction() {
// compiler requires bridging cast here...
NSString* x = MyCreateStringFromNothing();
}
The reason I ask is I wanted to write:
NSString* s = CFUUIDCreateString(NULL, uuid);
and thought that the new work on implicit bridging should simply enable me to write this. But this also needs a bridging cast.
This appears to be sort of black magic, but from my experiments functions with "Create" in them don't work with implicit bridging.
When I compiled this:
CF_IMPLICIT_BRIDGING_ENABLED
CFStringRef MyCreateStringFromNothing();
CFStringRef MyGetStringFromNothing();
CF_IMPLICIT_BRIDGING_DISABLED
void SomeFunction() {
NSString *const fails1 = MyCreateStringFromNothing();
NSString *const fails2 = CFStringCreateCopy(kCFAllocatorDefault, CFSTR("FOO"));
NSString *const works = MyGetStringFromNothing();
}
The first two fail but the third one "works."
Note that implicit bridging apparently STILL does NOT actually keep the compiler from warning you about converting CFStringRef to NSString *, it just stops requiring you to insert a __bridge as well.
You'll notice that in slide at WWDC2013 in session 404 at 50:30, he uses CFDictionaryGetValue(), which returns a "void *", so it's one of the few functions that won't raise a warning. Any of the CF functions that return an actual type (e.g. CFStringRef, or CFDictionary) are still going to throw warnings, so, meh.
Check the compiler settings on the project and/or target. If this is an older project, you might have a stray option there causing you to use an older compiler. (This tripped me up a few times when I moved to Xcode 5 and wanted to use the latest Obj-C newness.)
These days, I always choose "Default compiler" in my build options.

Disallow NSLog to be used

Is it possible to disallow the use of NSLog, so that it will come up as an error if used at compile time?
Ideally some sort of compiler flag with the name of the method that is disallowed?
Thanks
If you re-declare NSLog (and perhaps also NSLogv) as
void NSLog(NSString *format, ...) UNAVAILABLE_ATTRIBUTE;
void NSLogv(NSString *format, va_list args) UNAVAILABLE_ATTRIBUTE;
in your precompiled header file, you get a nice error message:
main.m:199:3: error: 'NSLog' is unavailable
NSLog(#"%#", s1);
^
You can even provide a custom error message (found in Messages on deprecated and unavailable Attributes of the Clang documentation):
void NSLog(NSString *format, ...) __attribute__((unavailable("You should not do this!")));
main.m:202:3: error: 'NSLog' is unavailable: You should not do this!
NSLog(#"%#", s1);
^
In your prefix header:
#define NSLog(x, ...) (__please_dont_use_NSLog__)
Try this!
#ifdef DEBUG
# define NSLog(...) NSLog(__VA_ARGS__)
#else
# define NSLog(...)
#endif
The solution can be found here: Enable and Disable NSLog in DEBUG mode
Hope this helped!

Resources