Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • driver/cc1.cc

    r6dc19544 r7c8246d  
    1010// Created On       : Fri Aug 26 14:23:51 2005
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Sep  3 16:57:05 2018
    13 // Update Count     : 125
     12// Last Modified On : Fri Aug 30 15:36:42 2019
     13// Update Count     : 377
    1414//
    1515
     
    1919#include <string>
    2020using std::string;
     21#include <algorithm>                                                                    // find
    2122#include <cstdio>                                                                               // stderr, stdout, perror, fprintf
    2223#include <cstdlib>                                                                              // getenv, exit, mkstemp
    2324#include <unistd.h>                                                                             // execvp, fork, unlink
    2425#include <sys/wait.h>                                                                   // wait
     26#include <fcntl.h>
     27
    2528
    2629#include "config.h"                                                                             // configure info
     
    3033
    3134
    32 string compiler_name( CFA_BACKEND_CC );                                 // path/name of C compiler
    33 
    34 string D__GCC_BPREFIX__( "-D__GCC_BPREFIX__=" );
    35 string D__CFA_FLAGPREFIX__( "-D__CFA_FLAG__=" );
    36 
    37 char tmpname[] = P_tmpdir "/CFAXXXXXX";
    38 int tmpfilefd = -1;
    39 
    40 
    41 bool prefix( string arg, string pre ) {
     35static string compiler_path( CFA_BACKEND_CC );                  // C compiler path/name
     36static bool CFA_flag = false;                                                   // -CFA flag
     37static bool save_temps = false;                                                 // -save-temps flag
     38static string o_file;
     39static string bprefix;
     40
     41
     42static bool prefix( const string & arg, const string & pre ) {
    4243        return arg.substr( 0, pre.size() ) == pre;
    4344} // prefix
    4445
    45 enum { NumSuffixes = 2 };
    46 const string suffixes[NumSuffixes] = { "cfa", "hfa", };
    47 
    48 void suffix( string arg, const char * args[], int & nargs ) {
    49         //std::cerr << arg << std::endl;
     46static void suffix( const string & arg, const char * args[], int & nargs ) {
     47        enum { NumSuffixes = 3 };
     48        static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
     49
    5050        size_t dot = arg.find_last_of( "." );
    51         //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
    5251        if ( dot == string::npos ) return;
    53         string sx = arg.substr( dot + 1 );
    54         for ( int i = 0; i < NumSuffixes; i += 1 ) {
    55                 if ( sx == suffixes[i] ) {
    56                         args[nargs] = "-x";
    57                         nargs += 1;
    58                         args[nargs] = "c";
    59                         nargs += 1;
    60                         return;
    61                 } // if
    62         } // for
     52        const string * end = suffixes + NumSuffixes;
     53        if ( std::find( suffixes, end, arg.substr( dot + 1 ) ) != end ) {
     54                args[nargs++] = "-x";
     55                args[nargs++] = "c";
     56        } // if
    6357} // suffix
    6458
    6559
    66 void checkEnv( const char * args[], int & nargs ) {
    67         char *value;
    68 
    69         value = getenv( "__CFA_COMPILER__" );
    70         if ( value != NULL ) {
    71                 compiler_name = value;
    72                 #ifdef __DEBUG_H__
    73                 cerr << "env arg:\"" << compiler_name << "\"" << endl;
    74                 #endif // __DEBUG_H__
    75         } // if
    76 
    77         value = getenv( "__GCC_MACHINE__" );
    78         if ( value != NULL ) {
    79                 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
    80                 #ifdef __DEBUG_H__
    81                 cerr << "env arg:\"" << args[nargs] << "\"" << endl;
    82                 #endif // __DEBUG_H__
    83                 nargs += 1;
    84         } // if
    85 
    86         value = getenv( "__GCC_VERSION__" );
    87         if ( value != NULL ) {
    88                 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
    89                 #ifdef __DEBUG_H__
    90                 cerr << "env arg:\"" << args[nargs] << "\"" << endl;
    91                 #endif // __DEBUG_H__
    92                 nargs += 1;
    93         } // if
    94 } // checkEnv
    95 
    96 
    97 void rmtmpfile() {
     60static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );
     61
     62static void checkEnv1( const char * args[], int & nargs ) { // stage 1
     63        extern char ** environ;
     64
     65        for ( int i = 0; environ[i]; i += 1 ) {
     66                string arg( environ[i] );
     67                #ifdef __DEBUG_H__
     68                cerr << "env arg:\"" << arg << "\"" << endl;
     69                #endif // __DEBUG_H__
     70
     71                if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
     72                        string val( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) );
     73                        if ( prefix( val, "-compiler=" ) ) {
     74                                compiler_path = val.substr( 10 );
     75                        } // if
     76                } // if
     77        } // for
     78} // checkEnv1
     79
     80
     81static void checkEnv2( const char * args[], int & nargs ) { // stage 2
     82        extern char ** environ;
     83
     84        for ( int i = 0; environ[i]; i += 1 ) {
     85                string arg( environ[i] );
     86                #ifdef __DEBUG_H__
     87                cerr << "env arg:\"" << arg << "\"" << endl;
     88                #endif // __DEBUG_H__
     89
     90                if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
     91                        string val( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) );
     92                        if ( prefix( val, "-compiler=" ) ) {
     93                                compiler_path = val.substr( 10 );
     94                        } else if ( val == "-CFA" ) {
     95                                CFA_flag = true;
     96                        } else if ( val == "-save-temps" ) {
     97                                save_temps = true;
     98                        } else if ( prefix( val, "-o=" ) ) {            // output file for -CFA
     99                                o_file = val.substr( 3 );
     100                        } else if ( prefix( val, "-B=" ) ) {            // location of cfa-cpp
     101                                bprefix = val.substr( 3 );
     102                        } else {                                                                        // normal flag for cfa-cpp
     103                                args[nargs++] = ( *new string( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) ) ).c_str();
     104                        } // if
     105                } // if
     106        } // for
     107} // checkEnv2
     108
     109
     110static char tmpname[] = P_tmpdir "/CFAXXXXXX.i";
     111static int tmpfilefd = -1;
     112static bool startrm = false;
     113
     114static void rmtmpfile() {
     115        if ( tmpfilefd == -1 ) return;                                          // RACE, file created ?
     116
     117        startrm = true;                                                                         // RACE with C-c C-c
    98118        if ( unlink( tmpname ) == -1 ) {                                        // remove tmpname
    99                 perror ( "CFA Translator error: cpp failed" );
    100                 exit( EXIT_FAILURE );
    101         } // if
    102         tmpfilefd = -1;                                                                         // mark closed
     119                perror ( "CC1 Translator error: failed, unlink" );
     120                exit( EXIT_FAILURE );
     121        } // if
     122        tmpfilefd = -1;                                                                         // mark removed
    103123} // rmtmpfile
    104124
    105125
    106 void sigTermHandler( __attribute__((unused)) int signal ) {
     126static void sigTermHandler( int ) {                                             // C-c C-c
     127        if ( startrm ) return;                                                          // return and let rmtmpfile finish, and then program finishes
     128
    107129        if ( tmpfilefd != -1 ) {                                                        // RACE, file created ?
    108                 rmtmpfile();                                                                    // remove
    109                 exit( EXIT_FAILURE );                                                   // terminate
    110         } // if
     130                rmtmpfile();                                                                    // remove tmpname
     131        } // if
     132        exit( EXIT_FAILURE );                                                           // terminate
    111133} // sigTermHandler
    112134
    113135
    114 void Stage1( const int argc, const char * const argv[] ) {
     136static void Stage1( const int argc, const char * const argv[] ) {
    115137        int code;
    116 
    117138        string arg;
    118         string bprefix;
    119 
    120         const char *cpp_in = NULL;
    121         const char *cpp_out = NULL;
    122 
    123         bool CFA_flag = false;
     139
     140        const char * cpp_in = nullptr;
     141        const char * cpp_out = nullptr;
     142
    124143        bool cpp_flag = false;
    125         const char *o_name = NULL;
    126 
    127         const char *args[argc + 100];                                           // leave space for 100 additional cpp command line values
     144        bool o_flag = false;
     145
     146        const char * args[argc + 100];                                          // leave space for 100 additional cpp command line values
    128147        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
    129         const char *cargs[20];                                                          // leave space for 20 additional cfa-cpp command line values
    130         int ncargs = 1;                                                                         // 0 => command name
    131 
    132         signal( SIGINT,  sigTermHandler );
    133         signal( SIGTERM, sigTermHandler );
    134148
    135149        #ifdef __DEBUG_H__
    136150        cerr << "Stage1" << endl;
    137151        #endif // __DEBUG_H__
    138         checkEnv( args, nargs );                                                        // arguments passed via environment variables
     152        checkEnv1( args, nargs );                                                       // arguments passed via environment variables
    139153        #ifdef __DEBUG_H__
    140154        for ( int i = 1; i < argc; i += 1 ) {
     
    168182                                i += 1;                                                                 // and the argument
    169183                                cpp_flag = true;
    170                         } else if ( arg == "-D__CFA_PREPROCESS__" ) {
    171                                 CFA_flag = true;
    172                         } else if ( arg == "-D" && string( argv[i + 1] ) == "__CFA_PREPROCESS__" ) {
    173                                 i += 1;                                                                 // and the argument
    174                                 CFA_flag = true;
    175                         } else if ( prefix( arg, D__CFA_FLAGPREFIX__ ) ) {
    176                                 cargs[ncargs] = ( *new string( arg.substr( D__CFA_FLAGPREFIX__.size() ) ) ).c_str();
    177                                 ncargs += 1;
    178                         } else if ( arg == "-D" && prefix( argv[i + 1], D__CFA_FLAGPREFIX__.substr(2) ) ) {
    179                                 cargs[ncargs] = ( *new string( string( argv[i + 1] ).substr( D__CFA_FLAGPREFIX__.size() - 2 ) ) ).c_str();
    180                                 ncargs += 1;
    181                                 i += 1;                                                                 // and the argument
    182                         } else if ( prefix( arg, D__GCC_BPREFIX__ ) ) {
    183                                 bprefix = arg.substr( D__GCC_BPREFIX__.size() );
    184                         } else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_BPREFIX__.substr(2) ) ) {
    185                                 bprefix = string( argv[i + 1] ).substr( D__GCC_BPREFIX__.size() - 2 );
    186                                 i += 1;                                                                 // and the argument
    187 
    188                         // all other flags
     184
     185                                // all other flags
    189186
    190187                        } else if ( arg == "-o" ) {
    191188                                i += 1;
    192                                 o_name = argv[i];
     189                                o_flag = true;
     190                                cpp_out = argv[i];
    193191                        } else {
    194                                 args[nargs] = argv[i];                                  // pass the flag along
    195                                 nargs += 1;
     192                                args[nargs++] = argv[i];                                // pass the flag along
    196193                                // CPP flags with an argument
    197194                                if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
     
    199196                                         arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
    200197                                        i += 1;
    201                                         args[nargs] = argv[i];                          // pass the argument along
    202                                         nargs += 1;
     198                                        args[nargs++] = argv[i];                        // pass the argument along
    203199                                        #ifdef __DEBUG_H__
    204200                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
    205201                                        #endif // __DEBUG_H__
    206202                                } else if ( arg == "-MD" || arg == "-MMD" ) {
    207                                         args[nargs] = "-MF";                            // insert before file
    208                                         nargs += 1;
     203                                        args[nargs++] = "-MF";                          // insert before file
    209204                                        i += 1;
    210                                         args[nargs] = argv[i];                          // pass the argument along
    211                                         nargs += 1;
     205                                        args[nargs++] = argv[i];                        // pass the argument along
    212206                                        #ifdef __DEBUG_H__
    213207                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
     
    216210                        } // if
    217211                } else {                                                                                // obtain input and possibly output files
    218                         if ( cpp_in == NULL ) {
     212                        if ( cpp_in == nullptr ) {
    219213                                cpp_in = argv[i];
    220214                                #ifdef __DEBUG_H__
    221215                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
    222216                                #endif // __DEBUG_H__
    223                         } else if ( cpp_out == NULL ) {
     217                        } else if ( cpp_out == nullptr ) {
    224218                                cpp_out = argv[i];
    225219                                #ifdef __DEBUG_H__
     
    238232                cerr << " " << args[i];
    239233        } // for
    240         if ( cpp_in != NULL ) cerr << " " << cpp_in;
    241         if ( cpp_out != NULL ) cerr << " " << cpp_out;
     234        if ( cpp_in != nullptr ) cerr << " " << cpp_in;
     235        if ( cpp_out != nullptr ) cerr << " " << cpp_out;
    242236        cerr << endl;
    243237        #endif // __DEBUG_H__
    244238
    245         if ( cpp_in == NULL ) {
     239        if ( cpp_in == nullptr ) {
    246240                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
    247241                exit( EXIT_FAILURE );
     
    252246                // output or -o. The call to cfa has a -E so it does not have to be added to the argument list.
    253247
    254                 args[0] = compiler_name.c_str();
     248                args[0] = compiler_path.c_str();
    255249                suffix( cpp_in, args, nargs );                                  // check suffix
    256                 args[nargs] = cpp_in;
    257                 nargs += 1;
    258                 if ( o_name != NULL ) {                                                 // location for output
    259                         args[nargs] = "-o";
    260                         nargs += 1;
    261                         args[nargs] = o_name;
    262                         nargs += 1;
    263                 } // if
    264                 args[nargs] = NULL;                                                             // terminate argument list
     250                args[nargs++] = cpp_in;
     251                if ( o_flag ) {                                                                 // location for output
     252                        args[nargs++] = "-o";
     253                } // if
     254                args[nargs++] = cpp_out;
     255                args[nargs] = nullptr;                                                  // terminate argument list
    265256
    266257                #ifdef __DEBUG_H__
    267258                cerr << "nargs: " << nargs << endl;
    268                 for ( int i = 0; args[i] != NULL; i += 1 ) {
     259                for ( int i = 0; args[i] != nullptr; i += 1 ) {
    269260                        cerr << args[i] << " ";
    270261                } // for
     
    272263                #endif // __DEBUG_H__
    273264
    274                 execvp( args[0], (char *const *)args );                 // should not return
    275                 perror( "CFA Translator error: cpp level, execvp" );
    276                 exit( EXIT_FAILURE );
    277         } // if
    278 
    279         // Create a temporary file to store output of the C preprocessor.
    280 
    281         tmpfilefd = mkstemp( tmpname );
    282         if ( tmpfilefd == -1 ) {
    283                 perror( "CFA Translator error: cpp level, mkstemp" );
    284                 exit( EXIT_FAILURE );
    285         } // if
    286 
    287         #ifdef __DEBUG_H__
    288         cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl;
    289         #endif // __DEBUG_H__
    290 
    291         // Run the C preprocessor and save the output in tmpfile.
     265                execvp( args[0], (char * const *)args );                // should not return
     266                perror( "CC1 Translator error: stage 1, execvp" );
     267                exit( EXIT_FAILURE );
     268        } // if
     269
     270        // Run the C preprocessor and save the output in the given file.
    292271
    293272        if ( fork() == 0 ) {                                                             // child process ?
     
    295274                // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error,
    296275                // when cpp writes to stdout. Hence, stdout is redirected into the temporary file.
    297                 if ( freopen( tmpname, "w", stdout ) == NULL ) { // redirect stdout to tmpname
    298                         perror( "CFA Translator error: cpp level, freopen" );
     276                if ( freopen( cpp_out, "w", stdout ) == nullptr ) { // redirect stdout to output file
     277                        perror( "CC1 Translator error: stage 1, freopen" );
    299278                        exit( EXIT_FAILURE );
    300279                } // if
    301280
    302                 args[0] = compiler_name.c_str();
     281                args[0] = compiler_path.c_str();
    303282                suffix( cpp_in, args, nargs );                                  // check suffix
    304                 args[nargs] = cpp_in;                                                   // input to cpp
    305                 nargs += 1;
    306                 args[nargs] = NULL;                                                             // terminate argument list
     283                args[nargs++] = cpp_in;                                                 // input to cpp
     284                args[nargs] = nullptr;                                                  // terminate argument list
    307285
    308286                #ifdef __DEBUG_H__
    309287                cerr << "cpp nargs: " << nargs << endl;
    310                 for ( int i = 0; args[i] != NULL; i += 1 ) {
     288                for ( int i = 0; args[i] != nullptr; i += 1 ) {
    311289                        cerr << args[i] << " ";
    312290                } // for
     
    314292                #endif // __DEBUG_H__
    315293
    316                 execvp( args[0], (char *const *)args );                 // should not return
    317                 perror( "CFA Translator error: cpp level, execvp" );
     294                execvp( args[0], (char * const *)args );                // should not return
     295                perror( "CC1 Translator error: stage 1 cpp, execvp" );
     296                cerr << " invoked " << args[0] << endl;
    318297                exit( EXIT_FAILURE );
    319298        } // if
     
    325304        #endif // __DEBUG_H__
    326305
    327         if ( WIFSIGNALED(code) != 0 ) {                                         // child failed ?
    328                 rmtmpfile();                                                                    // remove tmpname
    329                 cerr << "CFA Translator error: cpp failed with signal " << WTERMSIG(code) << endl;
    330                 exit( EXIT_FAILURE );
    331         } // if
    332 
    333         if ( WEXITSTATUS(code) != 0 ) {                                         // child error ?
    334                 rmtmpfile();                                                                    // remove tmpname
    335                 exit( WEXITSTATUS( code ) );                                    // do not continue
    336         } // if
    337 
    338         // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard
    339         // output.  Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.
    340 
    341         if ( fork() == 0 ) {                                                            // child runs CFA
    342                 cargs[0] = ( *new string( bprefix + "cfa-cpp" ) ).c_str();
    343 
    344                 // Source file-name used to generate routine names containing global initializations for TU.
    345                 cargs[ncargs] = ( *new string( "-F" ) ).c_str();
    346                 ncargs += 1;
    347                 cargs[ncargs] = ( *new string( string( cpp_in ) ) ).c_str();
    348                 ncargs += 1;
    349 
    350                 cargs[ncargs] = tmpname;
    351                 ncargs += 1;
    352                 if ( o_name != NULL ) {
    353                         cargs[ncargs] = o_name;
    354                         ncargs += 1;
    355                 } else if ( ! CFA_flag ) {                                              // run cfa-cpp ?
    356                         cargs[ncargs] = cpp_out;
    357                         ncargs += 1;
    358                 } // if
    359                 cargs[ncargs] = NULL;                                                   // terminate argument list
    360 
    361                 #ifdef __DEBUG_H__
    362                 cerr << "cfa-cpp ncargs: " << (o_name ? o_name : "No -o") << " " << CFA_flag << " " << ncargs << endl;
    363                 for ( int i = 0; cargs[i] != NULL; i += 1 ) {
    364                         cerr << cargs[i] << " ";
    365                 } // for
    366                 cerr << endl;
    367                 #endif // __DEBUG_H__
    368 
    369                 execvp( cargs[0], (char * const *)cargs );              // should not return
    370                 perror( "CFA Translator error: cpp level, execvp" );
    371                 exit( EXIT_FAILURE );
    372         } // if
    373 
    374         wait( &code );                                                                          // wait for child to finish
    375 
    376         #ifdef __DEBUG_H__
    377         cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
    378         #endif // __DEBUG_H__
    379 
    380         // Must unlink here because file must exist across execvp.
    381         rmtmpfile();                                                                            // remove tmpname
    382 
    383306        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
    384                 cerr << "CFA Translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl;
    385                 exit( EXIT_FAILURE );
    386         } // if
    387 
    388         exit( WEXITSTATUS(code) );
     307                cerr << "CC1 Translator error: stage 1, child failed " << WTERMSIG(code) << endl;
     308                exit( EXIT_FAILURE );
     309        } // if
     310
     311        exit( WEXITSTATUS(code) );                                                      // bad cpp result stops top-level gcc
    389312} // Stage1
    390313
    391314
    392 void Stage2( const int argc, const char * const * argv ) {
     315static void Stage2( const int argc, const char * const * argv ) {
     316        int code;
    393317        string arg;
    394318
    395         const char *cpp_in = NULL;
    396 
    397         const char *args[argc + 100];                                           // leave space for 100 additional cfa command line values
     319        const char * cpp_in = nullptr;
     320        const char * cpp_out = nullptr;
     321
     322        const char * args[argc + 100];                                          // leave space for 100 additional cfa command line values
    398323        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
     324        const char * cargs[20];                                                         // leave space for 20 additional cfa-cpp command line values
     325        int ncargs = 1;                                                                         // 0 => command name
    399326
    400327        #ifdef __DEBUG_H__
    401328        cerr << "Stage2" << endl;
    402329        #endif // __DEBUG_H__
    403         checkEnv( args, nargs );                                                        // arguments passed via environment variables
     330        checkEnv2( cargs, ncargs );                                                     // arguments passed via environment variables
    404331        #ifdef __DEBUG_H__
    405332        for ( int i = 1; i < argc; i += 1 ) {
     
    430357
    431358                        } else {
    432                                 args[nargs] = argv[i];                                  // pass the flag along
    433                                 nargs += 1;
     359                                args[nargs++] = argv[i];                                // pass the flag along
    434360                                if ( arg == "-o" ) {
    435361                                        i += 1;
    436                                         args[nargs] = argv[i];                          // pass the argument along
    437                                         nargs += 1;
     362                                        cpp_out = argv[i];
     363                                        args[nargs++] = argv[i];                        // pass the argument along
    438364                                        #ifdef __DEBUG_H__
    439365                                        cerr << "arg:\"" << argv[i] << "\"" << endl;
     
    442368                        } // if
    443369                } else {                                                                                // obtain input and possibly output files
    444                         if ( cpp_in == NULL ) {
     370                        if ( cpp_in == nullptr ) {
    445371                                cpp_in = argv[i];
    446372                                #ifdef __DEBUG_H__
    447373                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
    448374                                #endif // __DEBUG_H__
     375                        } else if ( cpp_out == nullptr ) {
     376                                cpp_out = argv[i];
     377                                #ifdef __DEBUG_H__
     378                                cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
     379                                #endif // __DEBUG_H__
    449380                        } else {
    450                                 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
     381                                cerr << "Usage: " << argv[0] << " more than two files specified" << endl;
    451382                                exit( EXIT_FAILURE );
    452383                        } // if
    453384                } // if
    454385        } // for
     386
     387        if ( cpp_in == nullptr ) {
     388                cerr << "Usage: " << argv[0] << " missing input file" << endl;
     389                exit( EXIT_FAILURE );
     390        } // if
     391        if ( cpp_out == nullptr ) {
     392                cerr << "Usage: " << argv[0] << " missing output file" << endl;
     393                exit( EXIT_FAILURE );
     394        } // if
     395
     396        // Create a temporary file, if needed, to store output of the cfa-cpp preprocessor. Cannot be created in forked
     397        // process because variables tmpname and tmpfilefd are cloned.
     398
     399        string cfa_cpp_out;
     400
     401        if ( ! CFA_flag ) {                                                                     // run compiler ?
     402                if ( save_temps ) {
     403                        cfa_cpp_out = cpp_in;
     404                        size_t dot = cfa_cpp_out.find_last_of( "." );
     405                        if ( dot == string::npos ) {
     406                                cerr << "CC1 Translator error: stage 2, bad file name " << endl;
     407                                exit( EXIT_FAILURE );
     408                        } // if
     409
     410                        cfa_cpp_out = cfa_cpp_out.substr( 0, dot ) + ".ifa";
     411                        if ( creat( cfa_cpp_out.c_str(), 0666 ) == -1 ) {
     412                                perror( "CC1 Translator error: stage 2, creat" );
     413                                exit( EXIT_FAILURE );
     414                        } // if
     415                } else {
     416                        tmpfilefd = mkstemps( tmpname, 2 );
     417                        if ( tmpfilefd == -1 ) {
     418                                perror( "CC1 Translator error: stage 2, mkstemp" );
     419                                exit( EXIT_FAILURE );
     420                        } // if
     421                        cfa_cpp_out = tmpname;
     422                } // if
     423                #ifdef __DEBUG_H__
     424                cerr << "cfa_cpp_out: " << cfa_cpp_out << endl;
     425                #endif // __DEBUG_H__
     426        } // if
     427
     428        // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard
     429        // output.  Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.
     430
     431        if ( fork() == 0 ) {                                                            // child runs CFA
     432                cargs[0] = ( *new string( bprefix + "cfa-cpp" ) ).c_str();
     433                cargs[ncargs++] = cpp_in;
     434
     435                if ( CFA_flag ) {                                                               // run cfa-cpp ?
     436                        if ( o_file.size() != 0 ) {                                     // location for output
     437                                cargs[ncargs++] = ( *new string( o_file.c_str() ) ).c_str();
     438                        } // if
     439                } else {
     440                        cargs[ncargs++] = cfa_cpp_out.c_str();
     441                } // if
     442                cargs[ncargs] = nullptr;                                                // terminate argument list
     443
     444                #ifdef __DEBUG_H__
     445                for ( int i = 0; cargs[i] != nullptr; i += 1 ) {
     446                        cerr << cargs[i] << " ";
     447                } // for
     448                cerr << endl;
     449                #endif // __DEBUG_H__
     450
     451                execvp( cargs[0], (char * const *)cargs );              // should not return
     452                perror( "CC1 Translator error: stage 2 cfa-cpp, execvp" );
     453                cerr << " invoked " << cargs[0] << endl;
     454                exit( EXIT_FAILURE );
     455        } // if
     456
     457        wait( &code );                                                                          // wait for child to finish
     458
     459        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
     460                rmtmpfile();                                                                    // remove tmpname
     461                cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
     462                exit( EXIT_FAILURE );
     463        } // if
     464
     465        if ( CFA_flag ) {                                                                       // no tmpfile created
     466                exit( WEXITSTATUS( code ) );                                    // stop regardless of success or failure
     467        } // if
     468
     469        #ifdef __DEBUG_H__
     470        cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
     471        #endif // __DEBUG_H__
     472
     473        if ( WEXITSTATUS(code) ) {                                                      // child error ?
     474                rmtmpfile();                                                                    // remove tmpname
     475                exit( WEXITSTATUS( code ) );                                    // do not continue
     476        } // if
    455477
    456478        #ifdef __DEBUG_H__
     
    459481                cerr << " " << args[i];
    460482        } // for
    461         cerr << endl;
    462         if ( cpp_in != NULL ) cerr << " " << cpp_in;
    463         #endif // __DEBUG_H__
    464 
    465         args[0] = compiler_name.c_str();
    466         args[nargs] = "-S";                                                                     // only compile and put assembler output in specified file
    467         nargs += 1;
    468         args[nargs] = cpp_in;
    469         nargs += 1;
    470         args[nargs] = NULL;                                                                     // terminate argument list
    471 
    472         #ifdef __DEBUG_H__
    473         cerr << "stage2 nargs: " << nargs << endl;
    474         for ( int i = 0; args[i] != NULL; i += 1 ) {
    475                 cerr << args[i] << " ";
    476         } // for
    477         cerr << endl;
    478         #endif // __DEBUG_H__
    479 
    480         execvp( args[0], (char * const *)args );                        // should not return
    481         perror( "CFA Translator error: cpp level, execvp" );
    482         exit( EXIT_FAILURE );                                                           // tell gcc not to go any further
     483        cerr << " " << cpp_in << endl;
     484        #endif // __DEBUG_H__
     485
     486        if ( fork() == 0 ) {                                                            // child runs CFA
     487                args[0] = compiler_path.c_str();
     488                args[nargs++] = "-S";                                                   // only compile and put assembler output in specified file
     489                if ( save_temps ) {                                                             // make gcc accept .ifa suffix
     490                        args[nargs++] = "-x";
     491                        args[nargs++] = "cpp-output";
     492                } // if
     493                args[nargs++] = cfa_cpp_out.c_str();
     494                args[nargs] = nullptr;                                                  // terminate argument list
     495
     496                #ifdef __DEBUG_H__
     497                cerr << "stage2 nargs: " << nargs << endl;
     498                for ( int i = 0; args[i] != nullptr; i += 1 ) {
     499                        cerr << args[i] << " ";
     500                } // for
     501                cerr << endl;
     502                #endif // __DEBUG_H__
     503
     504                execvp( args[0], (char * const *)args );                // should not return
     505                perror( "CC1 Translator error: stage 2 cc1, execvp" );
     506                cerr << " invoked " << cargs[0] << endl;
     507                exit( EXIT_FAILURE );                                                   // tell gcc not to go any further
     508        } // if
     509
     510        wait( &code );                                                                          // wait for child to finish
     511
     512        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
     513                rmtmpfile();                                                                    // remove tmpname
     514                cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
     515                exit( EXIT_FAILURE );
     516        } // if
     517
     518        #ifdef __DEBUG_H__
     519        cerr << "return code from gcc cc1:" << WEXITSTATUS(code) << endl;
     520        #endif // __DEBUG_H__
     521
     522        rmtmpfile();                                                                            // remove tmpname
     523        exit( WEXITSTATUS( code ) );                                            // stop regardless of success or failure
    483524} // Stage2
    484525
    485526
     527// This program is called twice because of the -no-integrated-cpp. The calls are differentiated by the first
     528// command-line argument. The first call replaces the traditional cpp pass to preprocess the C program. The second call
     529// is to the compiler, which is broken into two steps: preprocess again with cfa-cpp and then call gcc to compile the
     530// doubly preprocessed program.
     531
    486532int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
    487533        #ifdef __DEBUG_H__
    488         for ( int i = 0; env[i] != NULL; i += 1 ) {
     534        for ( int i = 0; env[i] != nullptr; i += 1 ) {
    489535                cerr << env[i] << endl;
    490536        } // for
    491537        #endif // __DEBUG_H__
    492538
    493         string arg = argv[1];
     539        signal( SIGINT,  sigTermHandler );
     540        signal( SIGTERM, sigTermHandler );
     541
     542        string arg( argv[1] );
    494543
    495544        // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
Note: See TracChangeset for help on using the changeset viewer.