Pennington— The Name in Software Engineering Automation

XTRAN Demonstration:  Protect Included Files from Multiple Inclusion

It is convenient to be able to include a file multiple times without penalty, e.g. if multiple layers of includes need to include it without knowing whether it has already been included.  We provide for this by inserting preprocessor statements at the start and end of every included file before rendering the code for output.

This code re-engineering automation example used an XTRAN rules file comprising about 135 non-comment lines of "meta-code" (XTRAN's rules language).  The rules are not particularly specific to C; they can easily be adapted to handle any 3GL language.

The following is an English paraphrase of the meta-code used:

    For each statement
        If it is an "include" statement that hasn't already been protected
            Get included file's name from first included statement
            Remap special characters to "_" to make legal preprocessor name
            Insert "if not defined" preprocessor statement before first included statement
            Insert following "define" preprocessor statement 
            Insert "end" preprocessor statement as new last included statement

The input to and output from XTRAN are untouched.



* Input to XTRAN:

incl_1.h

#define I1A 1
#define I1B 1

incl_2.h

#define I2A 2
#define I2B 2
#define I2C 2

main.c

#include "incl_1.h"
#include "incl_2.h"


* Output from XTRAN:

incl_1.h

#ifndef INCL_1_INCLUDED    /*if not included yet*/
#define INCL_1_INCLUDED    /*include only once*/
#define I1A 1
#define I1B 1
#endif                     /*"not included yet"*/

incl_2.h

#ifndef INCL_2_INCLUDED    /*if not included yet*/
#define INCL_2_INCLUDED    /*include only once*/
#define I2A 2
#define I2B 2
#define I2C 2
#endif                     /*"not included yet"*/


COPYRIGHT 2012; reproduction prohibited without permission.  Revised 2009-09-11

XTRAN is a trademark of Pennington Systems Incorporated.

  Pennington Systems Incorporated
2820 N. Pinal Avenue, Suite 12
Casa Grande, Arizona 85122-7927
USA


  Phone:  +1(480)626-5503
Fax:  +1(480)626-7618
Email:  Info@Pennington.com
Web:  http://WWW.Pennington.com