Changeset bbb1b35 for driver/cc1.cc


Ignore:
Timestamp:
Aug 23, 2019, 5:39:48 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
330d933
Parents:
ef22ad6
Message:

first attempt at -save-temp for cfa-cpp output, formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • driver/cc1.cc

    ref22ad6 rbbb1b35  
    1010// Created On       : Fri Aug 26 14:23:51 2005
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 22 22:29:52 2019
    13 // Update Count     : 328
     12// Last Modified On : Fri Aug 23 15:06:27 2019
     13// Update Count     : 371
    1414//
    1515
     
    2424#include <unistd.h>                                                                             // execvp, fork, unlink
    2525#include <sys/wait.h>                                                                   // wait
     26#include <fcntl.h>
     27
    2628
    2729#include "config.h"                                                                             // configure info
     
    3436static string compiler_path( CFA_BACKEND_CC );                  // path/name of C compiler
    3537static bool CFA_flag = false;                                                   // -CFA flag
     38static bool save_temps = false;                                                 // -save-temps flag
    3639static string o_file;
     40
    3741
    3842static bool prefix( const string & arg, const string & pre ) {
     
    4448        static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
    4549
    46         //std::cerr << arg << std::endl;
    4750        size_t dot = arg.find_last_of( "." );
    48         //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
    4951        if ( dot == string::npos ) return;
    5052        const string * end = suffixes + NumSuffixes;
     
    6264
    6365        for ( int i = 0; environ[i]; i += 1 ) {
    64                 string arg = environ[i];
     66                string arg( environ[i] );
    6567                #ifdef __DEBUG_H__
    6668                cerr << "env arg:\"" << arg << "\"" << endl;
     
    6870
    6971                if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
    70                         string val = arg.substr( __CFA_FLAGPREFIX__.size() + 4 );
     72                        string val( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) );
    7173                        if ( prefix( val, "-compiler=" ) ) {
    7274                                compiler_path = val.substr( 10 );
     
    8183
    8284        for ( int i = 0; environ[i]; i += 1 ) {
    83                 string arg = environ[i];
     85                string arg( environ[i] );
    8486                #ifdef __DEBUG_H__
    8587                cerr << "env arg:\"" << arg << "\"" << endl;
     
    8789
    8890                if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
    89                         string val = arg.substr( __CFA_FLAGPREFIX__.size() + 4 );
     91                        string val( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) );
    9092                        if ( prefix( val, "-compiler=" ) ) {
    9193                                compiler_path = val.substr( 10 );
    92                         } else if ( prefix( val, "-CFA" ) ) {
     94                        } else if ( val == "-CFA" ) {
    9395                                CFA_flag = true;
     96                        } else if ( val == "-save-temps" ) {
     97                                save_temps = true;
    9498                        } else if ( prefix( val, "-o=" ) ) {            // output file for -CFA
    9599                                o_file = val.substr( 3 );
     
    107111
    108112static void rmtmpfile() {
     113        if ( tmpfilefd == -1 ) return;                                          // RACE, file created ?
     114
    109115        startrm = true;                                                                         // RACE with C-c C-c
    110116        if ( unlink( tmpname ) == -1 ) {                                        // remove tmpname
     
    130136        string arg;
    131137
    132         const char *cpp_in = NULL;
    133         const char *cpp_out = NULL;
     138        const char * cpp_in = nullptr;
     139        const char * cpp_out = nullptr;
    134140
    135141        bool cpp_flag = false;
    136142        bool o_flag = false;
    137143
    138         const char *args[argc + 100];                                           // leave space for 100 additional cpp command line values
     144        const char * args[argc + 100];                                          // leave space for 100 additional cpp command line values
    139145        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
    140146
     
    202208                        } // if
    203209                } else {                                                                                // obtain input and possibly output files
    204                         if ( cpp_in == NULL ) {
     210                        if ( cpp_in == nullptr ) {
    205211                                cpp_in = argv[i];
    206212                                #ifdef __DEBUG_H__
    207213                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
    208214                                #endif // __DEBUG_H__
    209                         } else if ( cpp_out == NULL ) {
     215                        } else if ( cpp_out == nullptr ) {
    210216                                cpp_out = argv[i];
    211217                                #ifdef __DEBUG_H__
     
    224230                cerr << " " << args[i];
    225231        } // for
    226         if ( cpp_in != NULL ) cerr << " " << cpp_in;
    227         if ( cpp_out != NULL ) cerr << " " << cpp_out;
     232        if ( cpp_in != nullptr ) cerr << " " << cpp_in;
     233        if ( cpp_out != nullptr ) cerr << " " << cpp_out;
    228234        cerr << endl;
    229235        #endif // __DEBUG_H__
    230236
    231         if ( cpp_in == NULL ) {
     237        if ( cpp_in == nullptr ) {
    232238                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
    233239                exit( EXIT_FAILURE );
     
    245251                } // if
    246252                args[nargs++] = cpp_out;
    247                 args[nargs] = NULL;                                                             // terminate argument list
     253                args[nargs] = nullptr;                                                  // terminate argument list
    248254
    249255                #ifdef __DEBUG_H__
    250256                cerr << "nargs: " << nargs << endl;
    251                 for ( int i = 0; args[i] != NULL; i += 1 ) {
     257                for ( int i = 0; args[i] != nullptr; i += 1 ) {
    252258                        cerr << args[i] << " ";
    253259                } // for
     
    255261                #endif // __DEBUG_H__
    256262
    257                 execvp( args[0], (char *const *)args );                 // should not return
     263                execvp( args[0], (char * const *)args );                // should not return
    258264                perror( "CC1 Translator error: stage 1, execvp" );
    259265                exit( EXIT_FAILURE );
     
    266272                // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error,
    267273                // when cpp writes to stdout. Hence, stdout is redirected into the temporary file.
    268                 if ( freopen( cpp_out, "w", stdout ) == NULL ) { // redirect stdout to output file
     274                if ( freopen( cpp_out, "w", stdout ) == nullptr ) { // redirect stdout to output file
    269275                        perror( "CC1 Translator error: stage 1, freopen" );
    270276                        exit( EXIT_FAILURE );
     
    274280                suffix( cpp_in, args, nargs );                                  // check suffix
    275281                args[nargs++] = cpp_in;                                                 // input to cpp
    276                 args[nargs] = NULL;                                                             // terminate argument list
     282                args[nargs] = nullptr;                                                  // terminate argument list
    277283
    278284                #ifdef __DEBUG_H__
    279285                cerr << "cpp nargs: " << nargs << endl;
    280                 for ( int i = 0; args[i] != NULL; i += 1 ) {
     286                for ( int i = 0; args[i] != nullptr; i += 1 ) {
    281287                        cerr << args[i] << " ";
    282288                } // for
     
    284290                #endif // __DEBUG_H__
    285291
    286                 execvp( args[0], (char *const *)args );                 // should not return
     292                execvp( args[0], (char * const *)args );                // should not return
    287293                perror( "CC1 Translator error: stage 1, execvp" );
    288294                exit( EXIT_FAILURE );
     
    308314        string arg;
    309315
    310         const char * cpp_in = NULL;
    311         const char * cpp_out = NULL;
    312 
    313         const char *args[argc + 100];                                           // leave space for 100 additional cfa command line values
     316        const char * cpp_in = nullptr;
     317        const char * cpp_out = nullptr;
     318
     319        const char * args[argc + 100];                                          // leave space for 100 additional cfa command line values
    314320        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
    315         const char *cargs[20];                                                          // leave space for 20 additional cfa-cpp command line values
     321        const char * cargs[20];                                                         // leave space for 20 additional cfa-cpp command line values
    316322        int ncargs = 1;                                                                         // 0 => command name
    317323
     
    359365                        } // if
    360366                } else {                                                                                // obtain input and possibly output files
    361                         if ( cpp_in == NULL ) {
     367                        if ( cpp_in == nullptr ) {
    362368                                cpp_in = argv[i];
    363369                                #ifdef __DEBUG_H__
    364370                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
    365371                                #endif // __DEBUG_H__
    366                         } else if ( cpp_out == NULL ) {
     372                        } else if ( cpp_out == nullptr ) {
    367373                                cpp_out = argv[i];
    368374                                #ifdef __DEBUG_H__
     
    370376                                #endif // __DEBUG_H__
    371377                        } else {
    372                                 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
     378                                cerr << "Usage: " << argv[0] << " more than two files specified" << endl;
    373379                                exit( EXIT_FAILURE );
    374380                        } // if
    375381                } // if
    376382        } // for
     383
     384        if ( cpp_in == nullptr ) {
     385                cerr << "Usage: " << argv[0] << " missing input file" << endl;
     386                exit( EXIT_FAILURE );
     387        } // if
     388        if ( cpp_out == nullptr ) {
     389                cerr << "Usage: " << argv[0] << " missing output file" << endl;
     390                exit( EXIT_FAILURE );
     391        } // if
    377392
    378393        // Create a temporary file, if needed, to store output of the cfa-cpp preprocessor. Cannot be created in forked
    379394        // process because variables tmpname and tmpfilefd are cloned.
    380395
    381         if ( ! CFA_flag ) {                                                             // run cfa-cpp ?
    382                 tmpfilefd = mkstemps( tmpname, 2 );
    383                 if ( tmpfilefd == -1 ) {
    384                         perror( "CC1 Translator error: stage 2, mkstemp" );
    385                         exit( EXIT_FAILURE );
    386                 } // if
    387 
    388                 #ifdef __DEBUG_H__
    389                 cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl;
     396        string cfa_cpp_out;
     397
     398        if ( ! CFA_flag ) {                                                                     // run compiler ?
     399                if ( save_temps ) {
     400                        cfa_cpp_out = cpp_in;
     401                        size_t dot = cfa_cpp_out.find_last_of( "." );
     402                        if ( dot == string::npos ) {
     403                                cerr << "CC1 Translator error: stage 2, bad file name " << endl;
     404                                exit( EXIT_FAILURE );
     405                        } // if
     406
     407                        cfa_cpp_out = cfa_cpp_out.substr( 0, dot ) + ".ifa";
     408                        if ( creat( cfa_cpp_out.c_str(), 0666 ) == -1 ) {
     409                                perror( "CC1 Translator error: stage 2, creat" );
     410                                exit( EXIT_FAILURE );
     411                        } // if
     412                } else {
     413                        tmpfilefd = mkstemps( tmpname, 2 );
     414                        if ( tmpfilefd == -1 ) {
     415                                perror( "CC1 Translator error: stage 2, mkstemp" );
     416                                exit( EXIT_FAILURE );
     417                        } // if
     418                        cfa_cpp_out = tmpname;
     419                } // if
     420                #ifdef __DEBUG_H__
     421                cerr << "cfa_cpp_out: " << cfa_cpp_out << endl;
    390422                #endif // __DEBUG_H__
    391423        } // if
     
    404436                        } // if
    405437                } else {
    406                         cargs[ncargs++] = tmpname;
    407                 } // if
    408                 cargs[ncargs] = NULL;                                                   // terminate argument list
    409 
    410                 #ifdef __DEBUG_H__
    411                 for ( int i = 0; cargs[i] != NULL; i += 1 ) {
     438                        cargs[ncargs++] = cfa_cpp_out.c_str();
     439                } // if
     440                cargs[ncargs] = nullptr;                                                        // terminate argument list
     441
     442                #ifdef __DEBUG_H__
     443                for ( int i = 0; cargs[i] != nullptr; i += 1 ) {
    412444                        cerr << cargs[i] << " ";
    413445                } // for
     
    446478                cerr << " " << args[i];
    447479        } // for
    448         cerr << endl;
    449         if ( cpp_in != NULL ) cerr << " " << cpp_in;
     480        cerr << " " << cpp_in << endl;
    450481        #endif // __DEBUG_H__
    451482
     
    453484                args[0] = compiler_path.c_str();
    454485                args[nargs++] = "-S";                                                   // only compile and put assembler output in specified file
    455                 args[nargs++] = tmpname;
    456                 args[nargs] = NULL;                                                             // terminate argument list
     486                if ( save_temps ) {                                                             // make gcc accept .ifa suffix
     487                        args[nargs++] = "-x";
     488                        args[nargs++] = "cpp-output";
     489                } // if
     490                args[nargs++] = cfa_cpp_out.c_str();
     491                args[nargs] = nullptr;                                                  // terminate argument list
    457492
    458493                #ifdef __DEBUG_H__
    459494                cerr << "stage2 nargs: " << nargs << endl;
    460                 for ( int i = 0; args[i] != NULL; i += 1 ) {
     495                for ( int i = 0; args[i] != nullptr; i += 1 ) {
    461496                        cerr << args[i] << " ";
    462497                } // for
     
    493528int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
    494529        #ifdef __DEBUG_H__
    495         for ( int i = 0; env[i] != NULL; i += 1 ) {
     530        for ( int i = 0; env[i] != nullptr; i += 1 ) {
    496531                cerr << env[i] << endl;
    497532        } // for
     
    501536        signal( SIGTERM, sigTermHandler );
    502537
    503         string arg = argv[1];
     538        string arg( argv[1] );
    504539
    505540        // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
Note: See TracChangeset for help on using the changeset viewer.