Generate Xcode Warnings from TODO Comments

Well written code doesn’t have compiler warnings or errors. I like to keep my code warning free so that I know that when one does appear, it means something important. Xcode already warns about a number of common problems but ones that are often overlooked are TODO and FIXME comments.

To help me keep my code running smooth, I like to have warnings in the build log if there are any TODO’s left in the code. This helps remind me about unfinished tasks or lets me know when another developer may not have finished something.

Originally I was using the compiler warnings for todo’s such as this:

#warning TODO: Finish this code 

but that will only warn on the TODO’s that have been properly prefixed with #warning, something that only I was doing, not my fellow developers. Instead, what I wanted to do was warn if there were any comments similar to:

// TODO: Finish this code

After a bit of experimentation I realized this is quite easy to do using the Build Phases in an Xcode target.

To set this up in your projects, select your target and then select the Build Phases tab. At the bottom of the window you’ll see an option to Add Build Phase at the bottom of the screen. You can use the Add Build Phase to add a Run Script build phase. The Run Script option allows you to select a shell and execute arbitrary code against the project.

To warn about TODO comments, use /bin/sh as the shell and paste in this script:

echo "searching ${SRCROOT} for ${TAGS}"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/"

Now, when the target builds it will run the bash script to locate any .h or .m files that contain TODO: or FIXME: and create a warning in the Xcode build log.

In some projects you’ll be surprised about how many buried TODOs may have been forgotten.