A foundation for refactoring C with macros

This paper establishes the concept of "preprocessor dependences" as a foundation for building automated refactoring tools that transform source code containing lexical macros and conditional compilation directives, such as those provided by the C preprocessor. We define a preprocessor dependence graph (PPDG) that models the relationships among macro definitions, macro invocations, and conditional compilation directives in a file--the relationships that must be maintained for the semantics of the C preprocessor to be preserved. For many refactorings, a tool can construct a PPDG from the code before and after it is transformed, then perform a linear-time comparison of the two graphs to determine whether the refactoring will operate correctly in the presence of macros and conditional compilation directives. The proposed technique was implemented in OpenRefactory/C and tested by applying refactorings to GNU Coreutils version 8.21. Empirical results indicate that the technique is effective; it successfully handled refactoring scenarios in which Eclipse CDT, Visual Assist X, and XRefactory all refactored code incorrectly.

[1]  Ralph E. Johnson,et al.  Embracing the C preprocessor during refactoring , 2013, J. Softw. Evol. Process..

[2]  Sebastian Erdweg,et al.  A variability-aware module system , 2012, OOPSLA '12.

[3]  Wolfgang De Meuter,et al.  Can we refactor conditional compilation into aspects? , 2009, AOSD '09.

[4]  Steven S. Muchnick,et al.  Advanced Compiler Design and Implementation , 1997 .

[5]  David A. Padua,et al.  Dependence graphs and compiler optimizations , 1981, POPL '81.

[6]  Ralph E. Johnson,et al.  Analyzing multiple configurations of a C program , 2005, 21st IEEE International Conference on Software Maintenance (ICSM'05).

[7]  Yoann Padioleau,et al.  Parsing C/C++ Code without Pre-processing , 2009, CC.

[8]  Jeffrey Overbey,et al.  OpenRefactory/C: an infrastructure for building correct and complex C transformations , 2013, WRT '13.

[9]  Torbjörn Ekman,et al.  Stepping Stones over the Refactoring Rubicon , 2009, ECOOP.

[10]  Max Schaefer,et al.  Stepping Stones over the Refactoring Rubicon – Lightweight Language Extensions to Easily Realise Refactorings , 2009 .

[11]  Henry Spencer,et al.  #ifdef Considered Harmful, or Portability Experience with C News , 1992, USENIX Summer.

[12]  Andrew P. Black,et al.  How we refactor, and how we know it , 2009, 2009 IEEE 31st International Conference on Software Engineering.

[13]  Robert Grimm,et al.  SuperC: parsing all of C by taming the preprocessor , 2012, PLDI.

[14]  Martin Erwig,et al.  The Choice Calculus: A Representation for Software Variation , 2011, TSEM.

[15]  William G. Griswold Program restructuring as an aid to software maintenance , 1992 .

[16]  M. Platoff,et al.  An integrated program representation and toolkit for the maintenance of C programs , 1991, Proceedings. Conference on Software Maintenance 1991.

[17]  Joe D. Warren,et al.  The program dependence graph and its use in optimization , 1987, TOPL.

[18]  David Notkin,et al.  A framework for preprocessor-aware C source code analyses , 2000, Softw. Pract. Exp..

[19]  Jeffrey Overbey,et al.  Differential precondition checking: A lightweight, reusable analysis for refactoring tools , 2011, 2011 26th IEEE/ACM International Conference on Automated Software Engineering (ASE 2011).

[20]  Sven Apel,et al.  An analysis of the variability in forty preprocessor-based software product lines , 2010, 2010 ACM/IEEE 32nd International Conference on Software Engineering.

[21]  Oege de Moor,et al.  Specifying and implementing refactorings , 2010, OOPSLA.

[22]  Jeffrey Overbey,et al.  Systematic Testing of Refactoring Engines on Real Software Projects , 2013, ECOOP.

[23]  Ken Kennedy,et al.  Optimizing Compilers for Modern Architectures: A Dependence-based Approach , 2001 .

[24]  Michael D. Ernst,et al.  An Empirical Analysis of C Preprocessor Use , 2002, IEEE Trans. Software Eng..

[25]  Frank Tip,et al.  Correct Refactoring of Concurrent Java Code , 2010, ECOOP.

[26]  Alejandra Garrido,et al.  Program refactoring in the presence of preprocessor directives , 2005 .

[27]  Sven Apel,et al.  Scalable analysis of variable software , 2013, ESEC/FSE 2013.