在 C# 中解析 C 头文件 [英] Parsing C Header Files in C#

查看:112
本文介绍了在 C# 中解析 C 头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio C#,我需要解析 C 头文件以仅提取有关其中包含的函数声明的信息.对于每个函数,我都需要名称、返回类型及其参数.如果可能,我希望参数按照它们在函数声明中出现的顺序排列.我在网上看到过关于使用 Visual Studios 标签或 Exhuberant Ctags 等的内容.但从我收集到的内容来看,这些并不是让我使用 C# 代码从我的 C# 程序执行解析的真正选项(我可能错了?)我还查看了相关问题的所有其他答案,但它们似乎并不真正适用于我的情况(我可能只是愚蠢).如果我至少可以得到代表函数声明的所有代码行,我会有一个良好的开端,并且可以自己手动解析其余部分.提前致谢

I'm working with Visual Studio C#, and I need to parse C header files to extract information only about the function declarations contained within. For each function I need the name, return type, and its parameters. If possible, I'd like the parameters in the order in which they appear in the function declaration. I've seen stuff online about using visual studios tags, or Exhuberant Ctags, etc. But from what I gathered those aren't really options that let me perform the parse from my C# program with C# code (I may be mistaken?). I've also looked through all the other answers to related questions but they don't seem really apply to my situation (I may just be dumb). If I could at least get all the lines of code that represent function declarations I'd have a good start and could hand-parse the rest myself. Thanks in advance

推荐答案

要深入解析"C(头)文件并获取函数声明的类型信息,在实践中你需要:

To "parse" C (header) files in a deep sense and pick up the type information for function declarations, in practice you need:

  • 完整的预处理器(包括供应商添加的 pecaddillos,MS 的标头中有一些非常奇怪的东西),
  • 针对感兴趣的 C 方言的完整(语法)解析器/AST 构建器(没有C"这样的东西;供应商在此版本的编译器中提供了这些内容)
  • 完整的符号表构造(因为 typedef 是感兴趣的实际类型的别名)

许多人会建议编写自己的解析器(用于 C)".大多数人没有这样做;要做到这一点并把它做好,比他们理解的要多得多.如果您不从生产级机器开始,您将无法在不修复所有内容的情况下通过真正的 C 头文件.

Many people will suggest "write your own parser (for C)". Mostly those people haven't done this; its a lot more work to do this and get it right than they understand. If you don't start with a production-level machinery, you won't get through real C header files without fixing it all.

仅仅解析普通的 C 很困难;考虑歧义词组的解析问题

Just parsing plain C is hard; consider the problem of parsing the ambiguous phrase

 T*X;

经典解析器无法解析额外的黑客.

一般来说,您也无法单独解析 C 头文件.您需要拥有包含它的源代码上下文(通常包括编译器命令行),否则特定头文件中的 typedef、预处理器条件和宏将是未定义的,因此无法扩展到编译器通常看到的有效 C 中.

You will also not be able to parse a C header file by itself, in general. You need to have the source code context (often including the compiler command line) in which it is included, or typedefs, preprocessor conditionals and macros in a specific header file will be undefined and therefore unexpandable into the valid C that the compiler normally sees.

您最好使用预先存在的预先测试过的机器来为您执行此操作.Clang 作为一个选项出现在我的脑海中,尽管我不确定它是否处理 MS 头文件.GCC 是一种选择,但它真的非常想成为一个编译器,而不是您本地友好的 C 源代码分析工具,而且我再次不确定它是否支持 C 的 MS 方言.我们的 DMS 软件再工程工具包拥有所有以上为 C 的各种 MS 方言.

You are better off getting pre-existing pre-tested machinery that will do this for you. Clang comes to mind as an option, although I'm not sure it handles the MS header files. GCC is kind of an option, but it really, really wants to be a compiler, not your local friendly C source code analysis tool, and again I'm unsure of its support for MS dialects of C. Our DMS Software Reengineering Toolkit has all of the above for various MS dialects of C.

选择了可以实际解析此类标头的工具后,您可能希望对收集的标头信息一些事情.你对你想要完成的事情含糊其辞.在同时提到 C# 和 C 之后,有一个提示是您希望从 C# 代码调用 C 程序,因此需要为 C 代码生成 C# 等效 API.为此,您将需要机器来操作所提供的类型信息,并为 C# 声明构建文本".为此,您可能会发现您也需要其他支持工具来完成该部分.在这里 GCC 是一个完整的非启动器;它不会为您提供额外的帮助.Clang 和 DMS 都被设计为定制工具制造机械库.

Having chosen a tool that can actually parse such headers, you'll likely want to do something with the collected header information. You are vague about what you want to accomplish. Having mentioned C# and C in the same breath, there's a hint that you want to call C programs from C# code, and thus need to generate C# equivalent APIs for the C code. For this you will need machinery to manipulate the type information provided, and to build the "text" for the C# declarations. For this, you are likely to find that you need other supporting tooling to do that part, too. Here GCC is a complete non-starter; it will offer you no additional help. Clang and DMS are both designed to be libraries of custom-tool building machinery.

当然,这可能没有实际意义,这取决于您要处理多少头文件文本;如果它只是一个头文件,那么手动完成它可能是最简单的.您建议您愿意这样做(可以手动解析...").在这种情况下,您真正​​需要做的就是运行预处理器并解释输出.我相信您可以使用 GCC 和 Clang 甚至 MS 编译器的命令行开关;我知道 DMS 可以做到这一点.有关此处易于使用的选项,请参阅 在 Visual Studio 中预处理后如何查看 C/C++ 源文件?

Of course, this may all be moot depending on how much header file text you want to handle; it if is just one header file, doing it manually is probably easiest. You suggest you are willing to do that ("could hand-parse..."). In that case, all you really need to do is to run the preprocessor and interpret the output. I beleive you can do with command line switches for GCC and Clang and even the MS compilers; I know DMS can do this. For easily avialable options here, see How do I see a C/C++ source file after preprocessing in Visual Studio?

这篇关于在 C# 中解析 C 头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆