Stop Dead Code Strip per function

So I'm using XCTest to make some tests for my iOS app. Most of my code is c++ and most of my objective-c files are objective-c++ including my test files. XCode doesn't really like this in that is breaks the code highlighting of the XCTest asserts etc, but it runs and works fine. I need to add extra data and functions to help with the tests. I make my data include an expected results stream as well that is read in for the test cases to check against, and I will have some extra print functions so when a test fails it can show me the final state of the test object that failed or out put extra info to help me see where it went wrong or help me understand exactly how it fails.

I have no use for these functions and data in the "normal" version of the app, and hence no code in my app will reference these functions or data structures and hence LLVM does the right thing and strips them from my code.


XCTest while being another Target and having its own build settings seems to compile the base target with their settings and then compile its own code with its settings, so if I set the Dead Code Stripping compile option to No in XCTest project it still strips the dead code in the base project. This then causes the XCTest compile to fail. As the Linker can't find the methods that were dead stripped.


At the moment I have all the test specific extra code made with a custom #define so it is compiled in when XCTest is built, but to ensure that the code is not deadstripped I need to find some random place in the code that I know won't be run under the test where I can add some code to use the functions and data structs in a way that won't optimised out by the compiler.


Is there a Pragma or other flag I can use that allows me to instruct the compiler not to remove this function/data struct that I can put inside the define as well? e.g

#ifdef TEST
#pragma DONT_STRIP
int* getSpecialTestData(int item) { ... } 
#endif

or a per file level don't strip, so I can pull all the test code in to a sperate file that is conditionally imported/included?

I have couple of suggestions for you:

1. move testing code from the main target to test target (for this you can use ObjC categories)

2. move testing code from the main target to a separate target and include that target in the main target, or, even better - only in the test target

3. you can add your own "User Defines" in the main target and wrap your test code in that define. When building test target you can set inside of the test build scheme additional flag that will be automatically passed through to the main target when test target is being built


About pt. 3 this is purely theoretical, I never tested this, but it should be possible

Stop Dead Code Strip per function
 
 
Q