Is it possible to create a custom compiler directive in Swift? - swift

Imagine you don't want to enable GCC_TREAT_WARNINGS_AS_ERRORS. Because you are working on an epic legacy code and you don't want to make all 999+ warnings to be errors! But we want it for some cases:
#if DEBUG
#warning("Change this on production")
#else
#error("Change this on production")
#endif
Is there a way to create a custom one? For example something like:
#criticalWarning("Show correct message for errors")

Related

Using compiler variables in Swift

In Objective-C I had a bunch of compiler flags set in Build Settings -> Other C Flags that were being used in the code. For instance:
Flag => -DPortNumber = 1
And in code I was able to access it by #(PortNumber)
This doesn't work in Swift, and I'm not able to find an answer.
The -D flag to C compilers defines a preprocessor macro. There are no preprocessor macros in Swift. So if you're looking to do something like:
// compile with -DPORT_NUMBER 31337
var port = PORT_NUMBER // error
... you can't. Swift is designed for source code to be syntactically complete before compilation. If you could switch out blocks of it at build time, you'd break the ability of the toolchain to help verify that your code is correct. (Partly this is because preprocessor macros in C are textual replacement: you can use them to rewrite any part of the language, not just fill in values for variables.)
The Swift compiler does have a -D flag, but its use is more limited: you can use it for build configurations only. So, if you wanted to do something like the following, you'd be cool:
// compile with -DUSE_STAGING_SERVER
#if USE_STAGING_SERVER
var port = 31337
#else
var port = 80
#endif
Note that unlike C, everything inside an #if block needs to be syntactically complete. (For example, you can't put just the declaration line of a func in an #if block and leave the function body outside the conditional.)
Of course, this doesn't help you if you want to have a configuration value set at compile time be used in your code. For that, I'd recommend alternate approaches. Xcode can still do textual substitution in resource files, like property lists. (Note that the Info.plist that comes with your app is full of things like $(TARGET_NAME), for example.) So, you could include a bundle resource with your app whose contents are populated at compile time according to your project settings, then read your port number from that.

How to include code into the build only when a flag is set?

I have added some debugging code to my app which I want to call only when needed. I remember there is some kind of IFDEF that can be used to conditionally include code into a source file.
For example I might have something like this:
IFDEF kDebugEnabled == YES {
// some debugging code here
}
And then this piece of code is only compiled into the binary if that kDebugEnabled is YES.
How can I do something like this?
Please note: I don't want to use the project compiler flag settings. I just want to define a BOOL (or something that serves the purpose just as well) which is true or false and then just easily set it in my App Delegate for example. I find it hard to navigate to the project compiler settings, searching for a flag and then setting it. I know there is a Debug flag which might be of use.
What you are looking for is:
#ifdef __YOURSYMBOL__
<conditional code>
#endif
You can programmatically define __YOURSYMBOL__ like this:
#define __YOURSYMBOL__
__YOURSYMBOL__ can be any string that makes sense to you to remember why you are including/excluding that code snippet.
The DEBUG constant is a special preprocessor constant that the compiler defines specifically for you when the code is built for debugging, so you can simply use it:
#ifdef DEBUG
<conditional code>
#endif
Take into account that this is the C-preprocessor, not C, nor Objective-C that you are using, so a test like kDebugEnabled == YES (where kDebugEnabled is an Objective-C variable) is simply not possible. You can define integer values for your constants, like this:
#define __LOG_LEVEL__ 3
and then test for it:
#if __LOG_LEVEL__ == 3
...
Endif
As far as I know, you can't have code in your classes that is not compiled into the final product without using compiler flags. However, using the DEBUG flag is a lot easier than you think. If you are using Xcode 4, it's set up for you by default.
#ifdef DEBUG
// Your debug-only code goes here
#endif // DEBUG
Xcode has, by default, two configurations, Debug and Release. When you use the debug build configuration, among other things, it sets the DEBUG compiler flag, which you can then use to conditionally compile code. No need to mess with compilation settings at all.

How to customize eclipse CDT code templates

I need the code I am writing for a project to match some style guidelines. However the standard templates included with CDT don't match this style. Especially the layout of the header guards is not the way it should be. I had a look at the template and for my Eclipse it looks like this:
${filecomment}
#ifndef ${include_guard_symbol}
#define ${include_guard_symbol}
${typecomment}
${declarations}
#endif /* ${include_guard_symbol} */
So I am guessing the variable ${include_guard_symbol} is set somewhere in the CDT, but is it possible to change this setting without needing to modify the CDT itself?
On a slightly different, but related note:
Is it possible to add your own templates, so you just could add new files of other types (test-cases, specialized classes etc) using the normal new dialog for the project?
We've had a similar struggle on our project. One solution is to throw out ${include_guard_symbol} in the template all together, and define it yourself, possibly using some of the other predefined variables. Something like this:
${filecomment}
#ifndef MyProject_${file_base}_h
#define MyProject_${file_base}_h
${typecomment}
${declarations}
#endif /* MyProject_${file_base}_h */
So for a header file named inc/Foo.h, the include guard would be inserted like this:
#ifndef MyProject_Foo_h
#define MyProject_Foo_h
Unfortunately, there doesn't seem to be a way to customize much beyond that. For example, if I defined a class nested in a namespace, I might want to put the namespace as part of the include guard. I can't find a way to do that in eclipse, currently.
So in the Preferences dialog under C/C++ -> Code Style -> Code Templates you can modify the template to be closer to what you need, for example if you need the namespace in the guard, you can do something like.
${filecomment}
#ifndef ${namespace_name}_${include_guard_symbol}
#define ${namespace_name}_${include_guard_symbol}
${includes}
${namespace_begin}
${declarations}
${namespace_end}
#endif /* ${namespace_name}_${include_guard_symbol} */

Can a macro also generate a method name?

Is it generally valid that a macro is used to create a method name? I mean...actually it's just simple text replacement before the compiler actually runs, right?
Yes, it is valid; macro expansion occurs before the compiler even reads the code. The main limitation is that one cannot embed a preprocessor directive within a preprocessor directive. So, for example:
// This is ok:
#define PREFIX(X) this_name_is_prefixed_ ## X
// ...
- (void) PREFIX(doSomething):id;
// ...
// But this isn't:
#define IMPORT(X) #import X
IMPORT(<Foundation/Foundation.h>) // <= Don't expect this to work
With the exception of the "#import" directive, Objective-C's preprocessor is basically the same as the C preprocessor. (The "#import" is like "#include", except that #import implies include only once, so preprocessor guards are not required for headers that are included only with #import).
There is nothing that prevents that. It is even commonly used (though I don't know for the iphone), for instance, in device drivers implementation. In that case, macros are used to generate boilerplate code, and for this boilerplate to communicate with your code, you have to either guess the correct function names (not advised), or use generating macros, for example USB_ATTACH(uthum) to generate the signature of the attach method for the uthum driver.

How to use #if directives in C#(3.0)

I just found two piece of code
#if CONSOLE // defined by the console version using
ournamespace.FactoryInitializer;
#endif
and
#if _NET_1_1
log4net.Config.DOMConfigurator.ConfigureAndWatch(new System.IO.FileInfo(s) );
#else
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(s) );
#endif
Can any one please tell me with a running sample( please provide a simple one) what is the significance of those code snippets and when and how to use those?
Thanks.
Sure. These refer to conditional compilation symbols which can be defined at compile-time and which control what code is actually built. Here's an example:
using System;
class Test
{
static void Main()
{
#if FOO
Console.WriteLine("FOO was defined");
#endif
#if BAR
Console.WriteLine("BAR was defined");
#endif
}
}
If you compile this with
csc Test.cs
It won't print anything. If you compile it with
csc Test.cs /D:FOO
then it will print "FOO was defined" - and obviously the same is true for BAR.
Note that these aren't the same as C++ macros - a symbol is either defined or not; it doesn't have a "replacement value" as such.
In Visual Studio, you specify which symbols should be defined in the Build tab of the project properties. Additionally, at the very start of the file you can explicitly define and undefine symbols:
#define FOO
#undef BAR
This can be important when calling methods decorated with ConditionalAttribute - such calls are ignored by the compiler if the appropriate symbol isn't defined. So if you wanted to make sure that all your Debug.Print calls came through even if you hadn't defined the DEBUG symbol for the rest of the project, you could use:
#define DEBUG
...
Debug.Print("Foo");
Personally, I don't use all this very much. Aside from anything else, it makes it easier to understand the code if you know that it will all be compiled and run at execution time.
EDIT: Just to clarify a little on terminology - #if, #line, #pragma etc are all preprocessor directives; FOO and BAR (in this case) are the conditional compilation symbols.
They're used for conditional compilation.
If CONSOLE (known as a conditional compilation symbol) is defined for the first example with #define CONSOLE, the code within #if CONSOLE and #endif will be compiled and built into the assembly, otherwise the compiler ignores the code within them.
Undefining a conditional compile symbol is via #undef e.g #undef CONSOLE. The language specification also states :
There is no requirement that
conditional compilation symbols be
explicitly declared before they are
referenced in pre-processing
expressions. Instead, undeclared
symbols are simply undefined and thus
have the value false.
Those are called preprocessor directives. Quote from the docs:
'#if' lets you begin a conditional directive,
testing a symbol or symbols
to see if they evaluate to true. If
they do evaluate to true, the compiler
evaluates all the code between the #if
and the next directive.
So basically when you compile your program with /define:symbol switch it will either evaluate the if statement or not. For example:
csc foo.cs /define:DEBUG
allows you to define the DEBUG directive and enter the #if DEBUG branch. Remember that contrary to the if statement those are purely compile time and the body of the else statement won't even be included in your compiled assembly.
Your project can have multiple configurations, the most common are Debug and Release.
In Debug mode you can output debug strings, do additional checking etc.
For example:
void a(int x){
#if DEBUG
System.Diagnostics.Debug.WriteLine("a("+x+")");
#endif
//Do stuff.
}
You can define directives project-wide in the project's properties and make debug/release builds, or you could make an application that uses different libraries for some output (OpenGL/XNA). Or as you have, #if _NET_1_1 checks if a symbol _NET_1_1 is defined, assuming that .NET FX 1.1 is used, and uses proper classes, so you can target multiple framework versions in multiple project configurations.