views:

322

answers:

4

Is there an Objective-C syntax checker?

I have tried gcc -fsyntax-only but it is not really 'syntax only'. It still produces errors if run on an individual implementation file which has references to external frameworks.

I am looking for something that can perform a syntax check on individual header or implementation files without attempting to link or produce object files.

Can gcc do this with additional flags I am unaware of, or is there another tool up to this task?

I want to do this from the command-line. Can xcodebuild do this for an individual file? Running xcodebuild for the entire project to check the syntax of one file is a bit much.

+1  A: 

You can compile a single file in Xcode[1] using Build->Compile (cmd-K) which is effectively a syntax check (there's no linking step).

[1] I assume you're using Xcode, as there's little point in using Objective-C without OS X (really the Cocoa frameworks).

Barry Wark
I want to do this from the command-line. Can xcodebuild do this for an individual file? Running xcodebuild for the entire project to check the syntax of one file is a bit much. I have amended my original question accordingly.
firstresponder
As Benno mentions, clang may be helpful, but I'm not sure. I don't know if the compilation step only can be invoked via xcodebuild.
Barry Wark
+1  A: 

You could try using clang -fsyntax-only instead, especially if you're using 10.6/Xcode 3.2. Clang/LLVM has much better separation between the parser and the other parts of the compiler chain. You can find clang in /Developer/usr/bin.

Benno
Clang reports the same errors I had with gcc. I just want it to check the syntax, I don't want it to complain because it can't find the framework I have included in my header file.
firstresponder
Your problem is that the framework contains headers you need and in any C-derived language you have to have the data from the header in order to fully check the syntax. Otherwise you'd be lacking macro definitions, type definitions and other things that contribute to the correctness of the syntax of a given compilation unit. I would try either adding -framework options or using -I to pull in the Headers directory of the framework, eg -I/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Foundation.framework/Headersfor the Foundation framework)
Benno
+2  A: 

There's no way for it to check the syntax without it knowing about the header files for the frameworks you are using. You need to use the -framework flag to include the relevant header files.

Stephen Canon
A: 

So after trawling through the gcc man page I discovered the -F flag which lets you add a framework directory to the list of directories gcc searches for header files.

This solves my issue.

Use it like this: gcc -fsyntax-only -ObjC -F/Path/To/A/Framework -F/Path/To/Another/Framework File.m

firstresponder