Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert C99 code to C89

Tags:

c

visual-c++

c99

How can I convert c99 source code automatically to c89? I want to compile c99 libraries with Visual C++, but MSVC only supports c89. Many changes are only syntactic, such as struct initializers, and you could write a tool to "de-c99" code automatically. Does this preprocessor exist?

like image 974
Bruno Martinez Avatar asked May 29 '12 17:05

Bruno Martinez


3 Answers

Clang-based source-to-source translator:

https://github.com/libav/c99-to-c89/

like image 190
Kornel Avatar answered Oct 26 '22 11:10

Kornel


The commercial Comeau C/C++ compiler can do this.

Alternatively, use a proper C compiler (eg GCC or Clang via MinGW, Pelles C, Intel) and link the resulting object files. However, not all of these (in particular MinGW) support Microsoft's debug format.

like image 3
Christoph Avatar answered Oct 26 '22 12:10

Christoph


What you need is a program transformation system. Such a tool reads source code, builds compiler data structures (such as abstract syntax trees, allows you to apply (source-to-source) transformations on those structures, and then can regenerate source from the modified data structures.

You need one that can parse C99, and transform to C89. Our DMS Software Reengineering Toolkit can do that, using its C Front End (which can handle both dialects of C, including MSVC 89, as well as ObjectiveC). Having a mature parser is necessary if you want to do this.

Many folks may suggest that all you need is a C99 parser. As a practical matter, that isn't true; to do any interesting transformations on typical computer languages you need symbol table data, some data flow, etc. For more details, see my essay on Life After Parsing, and how DMS provides that life.

One such aspect is how you encode replacing struct initializers. You might do that by custom code that walks a C99 AST, finds such struct initializers, and procedurally hacks the tree. Yes, that works, but it isn't easy and you have to know a huge amount about the structure of the tree. DMS offers source-to-source rewrites, so you can write patterns that recognize the idioms you want modify, and patterns that produce the resulting idioms, all using C surface syntax.

like image 1
Ira Baxter Avatar answered Oct 26 '22 11:10

Ira Baxter