C源码分析

发布于 2024-12-20 17:53:26 字数 860 浏览 1 评论 0原文

是时候问了:我需要一个 C 预处理器库。

对此有一个相同的问题,但我不认为依赖像cpp 将解决我的需求。

我正在尝试分析支持 C 预处理器的 C 风格语言。我需要生成的信息是底层源代码所依赖的预处理器符号列表。或者,我需要在给定一组定义的预处理器符号和包含路径的情况下解决条件预处理器指令。

我最初的解决方案是我自己的基于正则表达式的实现,但是(正如您可以想象的)它不能在所有情况下工作。宏替换、多个括号、字符串连接、宏参数是我将面临的正确示例。您可以在此处找到我的(部分)实现。

因此,我正在寻找一个库(最好在.NET上,但这不是必需的),它允许我获取有关源代码中声明(或应该声明)的所有预处理器符号及其定义的信息(因此它们之间存在相互依赖关系) )。

有什么解决办法吗?


主要目标是管理 OpenGL 着色语言源。用于管理这些源的技术之一是预处理器条件(使用标准 C 预处理器):无需使用运行时条件即可获取许多着色器程序的单一源(提高性能)。

预处理器信息用于源分析、源编辑(尤其是语法灰化功能)和(更重要的)编译的着色器对象缓存。

编译的着色器对象缓存通过缓存组成未来程序的对象来允许更快的程序链接(避免重复编译相同的源)。缓存基于源文本和编译参数(实际上是定义的预处理器符号)。实际上,应用程序应分析源代码以获取条件中使用的符号列表:该列表用于计算缓存哈希值。

Its time to ask: I need a C preprocessor library.

There's an identical question on this, but I don't think that relying on an external application like cpp will solve my needs.

I'm trying to analyse a C-style language which supports C preprocessor. The information I need to produce is the list of preprocessor symbols that the underlying source code depends on. Optionally, I need to solve conditional preprocessor directives, given a set of defined preprocessor symbols and include paths.

My initial solution was my own regex-based implementation, but (as you can imagine) it cannot work in all cases. Macro substitutions, multiple parenthesis, string concatenation, macro arguments are examples on what I shall face to get it right. You can find my (partial) implementation here.

So, I'm looking a library (preferably on .NET, but it is not required) which allow me to get information about all preprocessor symbols declared (or supposed to be declared) in source code and their definition (hence their inter-dependencies).

Are there any solution?


The main goal would be the management of OpenGL Shading Language sources. One of the used techiques to manage those source is the preprocessor conditional (using standard C preprocessor): one single source for getting many shader programs without using run-time conditionals (improve performances).

The preprocessor information is used for source analysis, source editing (especially syntax grayout functionality), and (more important) compiled shader object caching.

Compiled shader object caching allow faster program linkage by caching objects composing future programs (avoid repeated compilation of the same sources). Caching is based on the source text and the compilation parameters (indeed the defined preprocessor symbols). Indeed the application shall analyse the source code for getting a list of symbols used in the conditional: this list is used for computing cache hash values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短叹 2024-12-27 17:53:26

clang 项目提供了可以处理许多 C 变体的词法分析和预处理的 liblex。

The clang project provides liblex that can handle lexing and preprocessing of many C variants.

因为看清所以看轻 2024-12-27 17:53:26

我们的 DMS 软件重组工具包是通用程序分析和转换技术。

它可以配置为处理任意语言,并具有适用于许多语言(包括 C 和 C++)的强大前端,并配有完整的预处理器。这些预处理器可以配置为正常扩展,或者尽可能避免扩展,以允许解析器将尽可能多的代码结构收集到 AST 中,包括预处理器定义、条件和宏调用。虽然 DMS 本身以二进制库和一组工具的形式提供,但前端以源代码形式提供,以实现任何方面的完全定制,包括预处理器、词法分析器、解析器、符号表构造和流分析器。

它可能是着色器源代码分析器的一个很好的平台。您需要使用 C 前端来访问预处理器。不知道shader代码是不是像C一样;如果是这样,你可以改变 C 语法,并获得相当多的相关基础设施,包括控制和数据流分析。

Our DMS Software Reengineering Toolkit is general purpose program analysis and transformation technology.

It can be configured to process arbitrary languages, and has robust front ends for many lanauges, including C and C++, complete with full preprocessors. These preprocessors can be configured to expand normally, or to avoid expansion where possible to allow the parser to collect as much code structure as possible into the AST, including the preprocessor defines, conditionals, and macro calls. While DMS itself is offered as a binary library and a set of tools, the front ends are offered in source form to enable complete customization of any aspect, including the preprocessor, lexer, parser, symbol table construction and flow analyzers.

It might be a good platform for an analyzer for shader source code. You'd want to use the C front end to get at the preprocessor. I don't know if the shader code is like C; if so, you could bend the C grammer, and acquire quite a lot of related infrastructure, including control and data flow analysis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文