ShayDavidson
ShayDavidson

Reputation: 781

Is there a way to show custom warnings when building a project in XCode?

Let's say I want to detect something in my code which I cannot check by tests.

e.g.: - Go over my entire code base to detect there I have a space between brackets and curly brackets (using a regex). - Go over my entire code base and detect style smells.

etc...

If one of these checks fails, I'd like to show a custom warning I define myself.

Is there anything like this in XCode?

Thanks.

Upvotes: 3

Views: 777

Answers (2)

gardenofwine
gardenofwine

Reputation: 1362

I wrote a run script for Xcode that does exactly what you requested. It searches by regex for predefined "code smells" or "style smells" and warns you at compile time

Read the blog post, but the final script is here

#test if git exists.
command -v git > /dev/null 2>$1 || {exit 0}
#separate code smells with | i.e. (code_smell_1|code_smell_2)
KEYWORDS="(didDeselectRowAtIndexPath)"
git diff --name-only HEAD | grep "\.m" | xargs egrep -s --with-filename --line-number --only-matching "$KEYWORDS.*[^\/]*$" | perl -p -e "s/($KEYWORDS)/ warning: Are you sure you want to use \$1?\nTo remove this warning, append a comment at the end of this line \$1/"

This particular script searches for appearances of didDeselectRowAtIndexPath in the code, but you can change that to any string

Upvotes: 3

trojanfoe
trojanfoe

Reputation: 122381

You would need to create a project target that runs a script, which searches through your source files and does your checks (this is not trivial) and returns non-zero to indicate error.

Then make your app target dependent on this "check target" and the build will fail if the check fails.

If you want some indication of how you write this "check script" then I would be inclined to employ the clang static analyzer. However, unfortunately, scan-build is not installed as part of the Xcode Command Line tools, so you would need to use the LLVM-binaries in some way, or perhaps the macports version contains it.

For trivial checks, relating to spaces and other stylistic issues rather than real, live, code problems, then you will have to employ some other mechanism, and I have no idea how to start with that, sorry.

Upvotes: 3

Related Questions