Re: C code refactoring or optimizing ?

Michael Beck <mm.beck@gmx.net>
30 Mar 2007 08:30:44 -0400

          From comp.compilers

Related articles
C code refactoring or optimizing ? Dennis.Yurichev@gmail.com (Dennis Yurichev) (2007-03-29)
Re: C code refactoring or optimizing ? idbaxter@semdesigns.com (Ira Baxter) (2007-03-29)
Re: C code refactoring or optimizing ? mm.beck@gmx.net (Michael Beck) (2007-03-30)
Re: C code refactoring or optimizing ? jeffrey.kenton@comcast.net (Jeff Kenton) (2007-04-01)
| List of all articles for this month |

From: Michael Beck <mm.beck@gmx.net>
Newsgroups: comp.compilers
Date: 30 Mar 2007 08:30:44 -0400
Organization: University of Karlsruhe, Germany
References: 07-03-102
Keywords: C, tools
Posted-Date: 30 Mar 2007 08:30:44 EDT

Dennis Yurichev wrote:


> Hi.
> How can I find some ready tool or how can I find a proper way to
> develop a tool which will convert such code:
[..]


C and C++ add some additional burden for such a task: the preprocessor.
What about that:


#ifdef X
void f1() {
#ifdef Y
...
#endif
}
#else
void f1() { ... };
#endif


...
f1()
...


should be refactored to


#if !defined(X) || defined(Y)
f1();
#endif


???


Refactoring tools normally operate on an abstract syntax tree which is hard
to be constructed WITH a preprocessor.
The solution would be to select ONE configuration (and build the tree for a
preprocessed input) or live with the sometimes wrong results :-)


Another problem with refactoring is that normally you would expect that
comments are still on the SAME place after a transformation and the
indentation is still the same for unchanged parts.
This means that your tool must include all comments/positions into its
syntax tree.


Recoder is a free tool for such a task, however the C frontend is not (yet?)
released to my knowledge, just a Java one.


Last but not least the AST is not the best representation to do
optimizations, so some optimizations are hard to implement.


If your task is source-to-source transformation (and you can live with one
configuration/without comments/indentation), any compiler than build an AST
and can convert it back to source can be used for this task. Look for EDG
frontend (you can get a academic license for it) or LLVM (free) for
instance.


best regards,


--
Michael Beck



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.