Parsing Objective-C code for static analysis
Asked Answered
D

4

19

I love static analysis and compile-time checks, almost to a fault, but most of my day job is in Objective-C. To resolve this tension, I'd like to be able to write my own analysis tools that I can run on my Objective-C projects.

But googling around the Internet suggests that people are having a hard time putting together a complete Objective-C grammar. One site basically recommends giving up.

I did find a grammar on the ANTLR website, but when I fired it up, I couldn't get it to parse anything at all. For example, it responded to the line:

void x();

with src/main/resources/somecode.m line 1:0 no viable alternative at input 'void'

:(

I took a closer look at the grammar and found the following disheartening disclaimer:

it's a work in progress, most of the .h file can be parsed

But I need something that can parse both interface and implementation.

Is there a complete Objective-C 2.0 grammar out there somewhere? I'd prefer something that can work with Scala (so anything Java compatible, like ANTLR, would be perfect), but at this point I'd be willing to adapt something designed for another parser toolkit.

Dashiell answered 22/7, 2011 at 12:58 Comment(1)
gcc can compile Objective-C programs (assuming you aren't using Apple's API), so there should be a grammar somewhere in the gcc source, should there not? -- Oh, I see your first link talks about that.Platto
K
17

As others mentioned, Clang would be the right solution. You can provide your own AST consumers, i.e. classes that will be invoked when going over the AST, leaving you not having to worry about parsing or messing with grammar.

Clang supports Objective-C in its entirety, and there's a lot of classes already in the static analyzer that you can model your own checks after. (in clang/lib/StaticAnalyzer/Checkers).

That directory contains a lot of static analyzer checkers, but you can also just create a normal AST consumer. Refer to http://code.google.com/p/chromium/wiki/WritingClangPlugins for more information.

Kneepad answered 25/7, 2011 at 0:21 Comment(0)
H
6

Clang is a static analysis tool that has support for Objective-C. I've found it very useful in the past.

http://clang-analyzer.llvm.org/

Hogwash answered 22/7, 2011 at 15:8 Comment(4)
Yes, I know about clang. But I want something that's easily extensible and that will let me write my own programs to analyze the AST.Dashiell
@Bill: Is that meant to be ironic? One of clang's major selling points is its library-based architecture and easy extendability.Sliding
@Sedate: No, of course not. I thought clang was supposed to be easier for compiler writers to extend - I didn't know that you could write simple driver programs to walk the AST.Dashiell
@Bill: My apologies! It is indeed easy for AST consumers, see this (somewhat outdated) tutorial for example.Sliding
P
4

clang is extensible; you can extend their existing static analysis or create your own. llvm / clang is architected as a series of libraries you can link to (dynamically or statically). A great starting point is the ARC (automatic reference counting) migrator library, which is responsible for statically analysing and rewriting objective-c code.

arcmt-test is a small example program that consumes the ARC migrator library.

Priddy answered 22/7, 2011 at 21:16 Comment(0)
U
1

You can use OCDepend, it's a static analysis tool based on Clang that simplifies managing Objective-C code quality and provides a highly flexible code query framework.

Ung answered 15/3, 2013 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.