Suppose I have a C source file (or a language close enough to C).
- It may have
#include
directives. - It may have other preprocessor directives.
- It may have zero, one or multiple function declarations with a definition.
- It may also have declarations without definitions
- The syntax is valid and the file would compile if its includes were made (But you can't really rely on that without applying the preprocessor and making the includes, which you won't).
Now, what is an efficient, hopefully established, way of programattically locating function signatures in this file, preferably with some kind of parse tree or syntax tree for the signature - without recourse to any other files?
Obviously this cannot be 100% fool-proof: One could use weird preprocessor tricks to "hide" or obfuscate declarations; one could use crazy spacing and indentation etc. But - I only need to catch "plain vanilla" function definitions with no funny business.
Notes:
- The program which needs to locate C headers can be C++ or C. This question is "cleaner" if I said I want to do it in C, but in reality I'm writing C++, so...
- I'm trying to avoid something as heavy as cppast which would also probably need to be able to find those include files.
- I think I might have had a related question at some point in time, but I can't find it...
- You may assume the function does not have any function pointer parameters, nor does it return a function pointer.
- The richer the AST the better, but I won't be too picky.
- I was thinking maybe something like the parsers which IDEs use, which have to be somewhat robust against syntax errors and missing files, and have to produce something useful even for broken files. But that's just a flight of fancy.