Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 899
Luca Avatar asked May 26 '26 05:05

Luca


1 Answers

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


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!