6

I need to do parsing of some Objective-C headers.

  • I've tried using Doxygen and parsing the XML output, but it doesn't fully support Objective C headers without comments (it chokes on macros defined in properties, check Doxygen not properly recognizing properties)
  • I've also tried using appledoc, but the XML output is not complete enough (for example, there is no information of inheritance for classes) and it has the same problem with macros on properties.
  • I've also tried parsing the output of the library Objective C metadata (using otool), but noticed that the metadata doesn't keep the types on methods (so you get method:(id)param:(id))

Does anyone know a good tool to do what I want? I'm suspecting clang will help me, but so far the -ast-dump and similar options just tries to generate an AST for a source I don't have (only headers).

Community
  • 1
  • 1
Edu Garcia
  • 453
  • 7
  • 21

2 Answers2

16

You may be able to use libclang. libclang is a programmatic interface designed for implementing tools like syntax highlighting and code completion.

clang -ast-dump works for me. (Note that -ast-dump is not supported by the driver, so you have to do some extra work to pass the flags that the driver usually handles. You can use clang -### ... to see exactly what the driver is doing.)

% clang -cc1 -ast-dump -fblocks -x objective-c /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
[...]
|-ObjCInterfaceDecl 0x1023727c0 <line:50:1, line:96:2> NSObject
| |-ObjCProtocol 0x102371350 'NSObject'
[...]
Greg Parker
  • 7,972
  • 2
  • 24
  • 21
  • 1
    This was it. I don't know why this wasn't working before, but I got it working now. One problem I have now is that some imports don't work (stuff like `#import `, but as you said, I'll have to pass parameters to the driver) – Edu Garcia Jul 11 '13 at 04:39
-6

I think using clang sounds way too hard. I would just use RegEx.

Instead I would write a simple shell script wrapper around Doxygen that comments out the problematic syntax.

It should be pretty simple to change:

@property(nonatomic, retain) BOOL myProperty NS_AVAILABLE_IOS(3_2);

To:

@property(nonatomic, retain) BOOL myProperty /*NS_AVAILABLE_IOS(3_2)*/;

You could even convert things like NS_DEPRECATED() to an @deprecated comment.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
  • 2
    No, regular expressions won't help for all the reasons cited in this answer: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – bbum Jul 11 '13 at 03:20
  • @bbum I wouldn't do regex alone, I'd do a hybrid of regex and some other scripting language. I have years of experience parsing programming languages with regex, it can be done and it's easy in this case. – Abhi Beckert Jul 11 '13 at 03:45
  • 2
    I guarantee for every combination of regex + scripting language you were to apply, I could find a perfectly valid bit of ObjC syntax that'd break it until you would end up re-inventing the entire standards compliant compiler... ;) – bbum Jul 11 '13 at 12:53
  • @bbum It doesn't need to cover all of ObjC's syntax, just the header files and just the specific part that doxygen can't handle. According to the question there are only 5 or 6 compiler macros that cause issues. That's not a big thing to cover. – Abhi Beckert Jul 11 '13 at 22:59
  • 3
    Not a big thing to cover until yet another thing trips it up. Been there, done that. Turned into Krazy Kode (I helped maintain the awful pile of regular expressions that parsed the system header files to produce the language bridge metadata used by PyObjC and other bridges). Now using clang, much -- MUCH -- better. – bbum Jul 11 '13 at 23:10