26

(I found the answer to this elsewhere while writing the question, but I thought it might be helpful to others if I posted it since I couldn't find anything here.)

I want to mark methods that need better error handling. I'd like them to show up as compiler warnings so other developers (who may be responsible for that area) will notice, and hopefully fix at their leisure.

(Other approaches welcome, I looked at __attribute__((warning)) but couldn't get it to work.)

zekel
  • 9,227
  • 10
  • 65
  • 96

2 Answers2

51

It's very easy to do:

#warning Needs better error handling, please.
zekel
  • 9,227
  • 10
  • 65
  • 96
  • 2
    This isn't really a good idea, as it makes warnings a regular part of the build rather than indications of possible errors. You'd be better off using //FIXME or //TODO, which many IDEs automatically pick up, to mark sections of code that need work. – Jon Shier Jun 02 '11 at 23:50
  • 3
    @jshier I consider it developer error (or at least I would only mark ones that I consider developer error.) The idea is to help others (and myself) get better at error handling so as do the right thing going forward. The warnings will disappear when they're addressed, and I think they need to be addressed. I don't think //FIXME and //TODO are strong enough. – zekel Jun 08 '11 at 18:10
  • (Also, when you're working on a small enough team it makes more sense and isn't obnoxious because it's only seen by your target audience.) – zekel Jun 30 '11 at 14:38
  • 5
    Just be careful when you use //FIXME or //TODO. You need to add a colon at the end. The right syntax is: //FIXME: fix me comment and // TODO: to do comment – Designerd Oct 04 '11 at 19:20
  • @theReverend They show up in the method list in the [jump bar](http://stackoverflow.com/questions/20069016/xcode-5-how-to-enable-jump-bar) that's right above the code window. – zekel Mar 14 '14 at 18:20
4

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 & FIXME comments, use /bin/sh as the shell and paste in this script:

TAGS="TODO:|FIXME:"
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/"

Source : Generate Xcode Warnings from TODO Comments

Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62