4

I need to parse function headers from a .i file used by SWIG which contains all sorts of garbage beside the function headers. (final output would be a list of function declarations)
The best option for me would be using the GNU toolchain (GCC, Binutils, etc..) to do so, but i might be missing an easy way of doing it with SWIG. If I am please tell me!
Thanks :]

edit: I also don't know how to do that with GCC toolchain, if you have an idea it will be great.

3 Answers 3

2

I would try getting an XML dump of the abstract syntax tree either from clang or from gccxml. From there you can easily extract the function declarations you are interested in.

Sign up to request clarification or add additional context in comments.

Comments

1

Our DMS Software Reengineering Toolkit provides general purpose program parsing, analysis, and transformation capability. It has front ends for a wide variety of languages, including C++.

It has been used to analyze and transforms very complex C++ programs and their header files.

You aren't clear as to what you will do after you "parse the function headers"; normally people want to extract some information or produce another artifact. DMS with its C++ front end can do the parsing; you can configure DMS to do the custom stuff.

As a practical matter, this isn't usually an afternoon's exercise; DMS is a complex beast, because it has to deal with complex beasts such as C++. And I'd expect you to face the same kind of complexity for any tool that can handle C++. The GCC toolchain can clearly handle C++, so you might be able to do it with that (at that same level of complexity) but GCC is designed to be a compiler, and IMHO you will find it a fight to get it do what you want.

Your "output function declarations" goal isn't clear. You want just the function names? You want a function signature? You want all the type declarations on which the function depends? You want all the type declarations on which the function depends, if they are not already present in an existing include file you intend to use?

3 Comments

I need to get functions signatures. It does seem like a great piece of software though unfortunately I need to settle on open source\free stuff.
Why don't you need the type declarations for the types that support the function signatures? In most applications that we do with DMS, in which you want to generate code to instantiate/call such functions, you need that information or you don't get compilable code.
I can get compilable code with the functions signatures alone since i can just make a null pointer to the types the functions are using and use *ptr as an object. It is kind of cheating but it will definitely compile and that's all I need for now. Thanks for the great answer though!
0

The best way to extract function decls from the garbage which is C header files is to substitute out what constitutes the most smelly garbage: macros. You can do that with:

   cpp - The C Preprocessor

1 Comment

it doesn't help me. Macros et al. are maybe 1% of the file, I need something much stronger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.