Luca
Luca

Reputation: 11961

C source analysis

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.

Upvotes: 3

Views: 292

Answers (2)

Ira Baxter
Ira Baxter

Reputation: 95324

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.

Upvotes: 0

user97370
user97370

Reputation:

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

Upvotes: 3

Related Questions