source: driver/cc1.cc @ 4ed7946e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 4ed7946e was 6da81c7, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

Fix cc1 -E flag for use with gcc11

  • Property mode set to 100644
File size: 18.4 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[6dc19544]7// cc1.cc --
[b87a5ed]8//
9// Author           : Peter A. Buhr
[51b7345]10// Created On       : Fri Aug 26 14:23:51 2005
11// Last Modified By : Peter A. Buhr
[6da81c7]12// Last Modified On : Tue Jun  1 23:07:21 2021
13// Update Count     : 415
[51b7345]14//
15
16#include <iostream>
17using std::cerr;
18using std::endl;
19#include <string>
20using std::string;
[2c60af75]21#include <algorithm>                                                                    // find
[b87a5ed]22#include <cstdio>                                                                               // stderr, stdout, perror, fprintf
23#include <cstdlib>                                                                              // getenv, exit, mkstemp
24#include <unistd.h>                                                                             // execvp, fork, unlink
25#include <sys/wait.h>                                                                   // wait
[36de20d]26#include <fcntl.h>                                                                              // creat
[bbb1b35]27
[51b7345]28
[b87a5ed]29#include "config.h"                                                                             // configure info
[51b7345]30
31
32//#define __DEBUG_H__
33
34
[33c849e]35static string compiler_path( CFA_BACKEND_CC );                  // C compiler path/name
[2c60af75]36static bool CFA_flag = false;                                                   // -CFA flag
[bbb1b35]37static bool save_temps = false;                                                 // -save-temps flag
[2c60af75]38static string o_file;
[417a630]39static string bprefix;
[0163d3e]40static string lang;                                                                             // -x flag
[51b7345]41
[bbb1b35]42
[2c60af75]43static bool prefix( const string & arg, const string & pre ) {
[b87a5ed]44        return arg.substr( 0, pre.size() ) == pre;
[51b7345]45} // prefix
46
[2c60af75]47static void suffix( const string & arg, const char * args[], int & nargs ) {
48        enum { NumSuffixes = 3 };
49        static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
[dffaeac]50
51        size_t dot = arg.find_last_of( "." );
[b740f0b]52        if ( dot == string::npos ) return;
[2c60af75]53        const string * end = suffixes + NumSuffixes;
54        if ( std::find( suffixes, end, arg.substr( dot + 1 ) ) != end ) {
55                args[nargs++] = "-x";
56                args[nargs++] = "c";
57        } // if
[dffaeac]58} // suffix
59
[51b7345]60
[36de20d]61static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );               // "__CFA_FLAG__=" suffix
[51b7345]62
[2c60af75]63static void checkEnv1( const char * args[], int & nargs ) { // stage 1
64        extern char ** environ;
[51b7345]65
[2c60af75]66        for ( int i = 0; environ[i]; i += 1 ) {
[bbb1b35]67                string arg( environ[i] );
[dffaeac]68                #ifdef __DEBUG_H__
[2c60af75]69                cerr << "env arg:\"" << arg << "\"" << endl;
[dffaeac]70                #endif // __DEBUG_H__
[51b7345]71
[2c60af75]72                if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
[0bf5340]73                        string val( arg.substr( arg.find_first_of( "=" ) + 1 ) );
[2c60af75]74                        if ( prefix( val, "-compiler=" ) ) {
75                                compiler_path = val.substr( 10 );
[0163d3e]76                        } else if ( prefix( val, "-x=" ) ) {
77                                lang = val.substr( 3 );
[2c60af75]78                        } // if
79                } // if
80        } // for
81} // checkEnv1
82
83
84static void checkEnv2( const char * args[], int & nargs ) { // stage 2
85        extern char ** environ;
86
87        for ( int i = 0; environ[i]; i += 1 ) {
[bbb1b35]88                string arg( environ[i] );
[dffaeac]89                #ifdef __DEBUG_H__
[2c60af75]90                cerr << "env arg:\"" << arg << "\"" << endl;
[dffaeac]91                #endif // __DEBUG_H__
[2c60af75]92
93                if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
[0bf5340]94                        string val( arg.substr( arg.find_first_of( "=" ) + 1 ) );
[2c60af75]95                        if ( prefix( val, "-compiler=" ) ) {
96                                compiler_path = val.substr( 10 );
[bbb1b35]97                        } else if ( val == "-CFA" ) {
[2c60af75]98                                CFA_flag = true;
[b4130f9]99                        } else if ( val == "-save-temps" || val == "--save-temps" ) {
[bbb1b35]100                                save_temps = true;
[2c60af75]101                        } else if ( prefix( val, "-o=" ) ) {            // output file for -CFA
102                                o_file = val.substr( 3 );
[417a630]103                        } else if ( prefix( val, "-B=" ) ) {            // location of cfa-cpp
104                                bprefix = val.substr( 3 );
[0163d3e]105                        } else if ( prefix( val, "-x=" ) ) {            // ignore
[33c849e]106                        } else {                                                                        // normal flag for cfa-cpp
[0bf5340]107                                args[nargs++] = ( *new string( arg.substr( arg.find_first_of( "=" ) + 1 ) ) ).c_str();
[2c60af75]108                        } // if
109                } // if
110        } // for
111} // checkEnv2
[51b7345]112
[36de20d]113#define CFA_SUFFIX ".ifa"
[51b7345]114
[36de20d]115static char tmpname[] = P_tmpdir "/CFAXXXXXX" CFA_SUFFIX;
[2c60af75]116static int tmpfilefd = -1;
117static bool startrm = false;
118
119static void rmtmpfile() {
[bbb1b35]120        if ( tmpfilefd == -1 ) return;                                          // RACE, file created ?
121
[2c60af75]122        startrm = true;                                                                         // RACE with C-c C-c
[b87a5ed]123        if ( unlink( tmpname ) == -1 ) {                                        // remove tmpname
[2c60af75]124                perror ( "CC1 Translator error: failed, unlink" );
[b87a5ed]125                exit( EXIT_FAILURE );
126        } // if
[2c60af75]127        tmpfilefd = -1;                                                                         // mark removed
[bdd516a]128} // rmtmpfile
129
130
[2c60af75]131static void sigTermHandler( int ) {                                             // C-c C-c
132        if ( startrm ) return;                                                          // return and let rmtmpfile finish, and then program finishes
133
[b87a5ed]134        if ( tmpfilefd != -1 ) {                                                        // RACE, file created ?
[2c60af75]135                rmtmpfile();                                                                    // remove tmpname
[b87a5ed]136        } // if
[2c60af75]137        exit( EXIT_FAILURE );                                                           // terminate
[bdd516a]138} // sigTermHandler
139
140
[2c60af75]141static void Stage1( const int argc, const char * const argv[] ) {
[b87a5ed]142        int code;
143        string arg;
[51b7345]144
[bbb1b35]145        const char * cpp_in = nullptr;
146        const char * cpp_out = nullptr;
[51b7345]147
[b87a5ed]148        bool cpp_flag = false;
[2c60af75]149        bool o_flag = false;
[51b7345]150
[bbb1b35]151        const char * args[argc + 100];                                          // leave space for 100 additional cpp command line values
[b87a5ed]152        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
[bdd516a]153
[dffaeac]154        #ifdef __DEBUG_H__
[7b937575]155        cerr << "Stage1" << endl;
[dffaeac]156        #endif // __DEBUG_H__
[2c60af75]157        checkEnv1( args, nargs );                                                       // arguments passed via environment variables
[bec4d24]158        #ifdef __DEBUG_H__
159        for ( int i = 1; i < argc; i += 1 ) {
160                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
161        } // for
162        #endif // __DEBUG_H__
[7b937575]163
[b87a5ed]164        // process all the arguments
[51b7345]165
[bec4d24]166        for ( int i = 1; i < argc; i += 1 ) {
[b87a5ed]167                arg = argv[i];
168                if ( prefix( arg, "-" ) ) {
169                        // strip g++ flags that are inappropriate or cause duplicates in subsequent passes
170
171                        if ( arg == "-quiet" ) {
172                        } else if ( arg == "-imultilib" || arg == "-imultiarch" ) {
[db62eef]173                                i += 1;                                                                 // and argument
[b87a5ed]174                        } else if ( prefix( arg, "-A" ) ) {
175                        } else if ( prefix( arg, "-D__GNU" ) ) {
176                                //********
177                                // GCC 5.6.0 SEPARATED THE -D FROM THE ARGUMENT!
178                                //********
179                        } else if ( arg == "-D" && prefix( argv[i + 1], "__GNU" ) ) {
[db62eef]180                                i += 1;                                                                 // and argument
[b87a5ed]181
182                                // strip flags controlling cpp step
183
184                        } else if ( arg == "-D__CPP__" ) {
185                                cpp_flag = true;
186                        } else if ( arg == "-D" && string( argv[i + 1] ) == "__CPP__" ) {
[db62eef]187                                i += 1;                                                                 // and argument
[b87a5ed]188                                cpp_flag = true;
189
[2c60af75]190                                // all other flags
[b87a5ed]191
192                        } else if ( arg == "-o" ) {
193                                i += 1;
[2c60af75]194                                o_flag = true;
195                                cpp_out = argv[i];
[b87a5ed]196                        } else {
[db62eef]197                                args[nargs++] = argv[i];                                // pass flag along
[b87a5ed]198                                // CPP flags with an argument
[f8b6d921]199                                if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
[b87a5ed]200                                         arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" ||
[6da81c7]201                                         arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ||
202                                         arg == "-dumpbase-ext" || arg == "-dumpbase"
203                                        ) {
[b87a5ed]204                                        i += 1;
[db62eef]205                                        args[nargs++] = argv[i];                        // pass argument along
[dffaeac]206                                        #ifdef __DEBUG_H__
[b87a5ed]207                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[dffaeac]208                                        #endif // __DEBUG_H__
[b87a5ed]209                                } else if ( arg == "-MD" || arg == "-MMD" ) {
[db62eef]210                                        // gcc frontend generates the dependency file-name after the -MD/-MMD flag, but it is necessary to
211                                        // prefix that file name with -MF.
[2c60af75]212                                        args[nargs++] = "-MF";                          // insert before file
[b87a5ed]213                                        i += 1;
[db62eef]214                                        args[nargs++] = argv[i];                        // pass argument along
[dffaeac]215                                        #ifdef __DEBUG_H__
[b87a5ed]216                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[dffaeac]217                                        #endif // __DEBUG_H__
[b87a5ed]218                                } // if
219                        } // if
220                } else {                                                                                // obtain input and possibly output files
[bbb1b35]221                        if ( cpp_in == nullptr ) {
[b87a5ed]222                                cpp_in = argv[i];
[dffaeac]223                                #ifdef __DEBUG_H__
[b87a5ed]224                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
[dffaeac]225                                #endif // __DEBUG_H__
[bbb1b35]226                        } else if ( cpp_out == nullptr ) {
[b87a5ed]227                                cpp_out = argv[i];
[dffaeac]228                                #ifdef __DEBUG_H__
[b87a5ed]229                                cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
[dffaeac]230                                #endif // __DEBUG_H__
[b87a5ed]231                        } else {
232                                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
233                                exit( EXIT_FAILURE );
234                        } // if
235                } // if
236        } // for
237
[dffaeac]238        #ifdef __DEBUG_H__
[b87a5ed]239        cerr << "args:";
[bec4d24]240        for ( int i = 1; i < nargs; i += 1 ) {
[b87a5ed]241                cerr << " " << args[i];
242        } // for
[bbb1b35]243        if ( cpp_in != nullptr ) cerr << " " << cpp_in;
244        if ( cpp_out != nullptr ) cerr << " " << cpp_out;
[b87a5ed]245        cerr << endl;
[dffaeac]246        #endif // __DEBUG_H__
[b87a5ed]247
[bbb1b35]248        if ( cpp_in == nullptr ) {
[51b7345]249                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
250                exit( EXIT_FAILURE );
251        } // if
[b87a5ed]252
253        if ( cpp_flag ) {
254                // The -E flag is specified on the cfa command so only run the preprocessor and output is written to standard
255                // output or -o. The call to cfa has a -E so it does not have to be added to the argument list.
256
[2c60af75]257                args[0] = compiler_path.c_str();
[0163d3e]258                if ( lang.size() == 0 ) {
259                        suffix( cpp_in, args, nargs );                          // check suffix
260                } else {
261                        args[nargs++] = "-x";
262                        args[nargs++] = ( *new string( lang.c_str() ) ).c_str();
263                } // if
[2c60af75]264                args[nargs++] = cpp_in;
265                if ( o_flag ) {                                                                 // location for output
266                        args[nargs++] = "-o";
[b87a5ed]267                } // if
[2c60af75]268                args[nargs++] = cpp_out;
[bbb1b35]269                args[nargs] = nullptr;                                                  // terminate argument list
[51b7345]270
[dffaeac]271                #ifdef __DEBUG_H__
[b87a5ed]272                cerr << "nargs: " << nargs << endl;
[bbb1b35]273                for ( int i = 0; args[i] != nullptr; i += 1 ) {
[b87a5ed]274                        cerr << args[i] << " ";
275                } // for
276                cerr << endl;
[dffaeac]277                #endif // __DEBUG_H__
[51b7345]278
[bbb1b35]279                execvp( args[0], (char * const *)args );                // should not return
[2c60af75]280                perror( "CC1 Translator error: stage 1, execvp" );
[b87a5ed]281                exit( EXIT_FAILURE );
[51b7345]282        } // if
283
[2c60af75]284        // Run the C preprocessor and save the output in the given file.
[51b7345]285
[db62eef]286        if ( fork() == 0 ) {                                                            // child process ?
[b87a5ed]287                // -o xxx.ii cannot be used to write the output file from cpp because no output file is created if cpp detects
288                // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error,
289                // when cpp writes to stdout. Hence, stdout is redirected into the temporary file.
[bbb1b35]290                if ( freopen( cpp_out, "w", stdout ) == nullptr ) { // redirect stdout to output file
[2c60af75]291                        perror( "CC1 Translator error: stage 1, freopen" );
[b87a5ed]292                        exit( EXIT_FAILURE );
293                } // if
[51b7345]294
[2c60af75]295                args[0] = compiler_path.c_str();
[0163d3e]296                if ( lang.size() == 0 ) {
297                        suffix( cpp_in, args, nargs );                          // check suffix
298                } else {
299                        args[nargs++] = "-x";
300                        args[nargs++] = ( *new string( lang.c_str() ) ).c_str();
301                } // if
[2c60af75]302                args[nargs++] = cpp_in;                                                 // input to cpp
[bbb1b35]303                args[nargs] = nullptr;                                                  // terminate argument list
[51b7345]304
[dffaeac]305                #ifdef __DEBUG_H__
[b87a5ed]306                cerr << "cpp nargs: " << nargs << endl;
[bbb1b35]307                for ( int i = 0; args[i] != nullptr; i += 1 ) {
[b87a5ed]308                        cerr << args[i] << " ";
309                } // for
310                cerr << endl;
[dffaeac]311                #endif // __DEBUG_H__
[51b7345]312
[bbb1b35]313                execvp( args[0], (char * const *)args );                // should not return
[5a43ab8]314                perror( "CC1 Translator error: stage 1 cpp, execvp" );
315                cerr << " invoked " << args[0] << endl;
[b87a5ed]316                exit( EXIT_FAILURE );
[51b7345]317        } // if
318
[b87a5ed]319        wait( &code );                                                                          // wait for child to finish
[51b7345]320
[dffaeac]321        #ifdef __DEBUG_H__
[b87a5ed]322        cerr << "return code from cpp:" << WEXITSTATUS(code) << endl;
[dffaeac]323        #endif // __DEBUG_H__
[51b7345]324
[b87a5ed]325        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
[36de20d]326                rmtmpfile();                                                                    // remove tmpname
[2c60af75]327                cerr << "CC1 Translator error: stage 1, child failed " << WTERMSIG(code) << endl;
[b87a5ed]328                exit( EXIT_FAILURE );
329        } // if
[51b7345]330
[36de20d]331        exit( WEXITSTATUS( code ) );                                            // bad cpp result stops top-level gcc
[51b7345]332} // Stage1
333
334
[2c60af75]335static void Stage2( const int argc, const char * const * argv ) {
336        int code;
[b87a5ed]337        string arg;
[51b7345]338
[bbb1b35]339        const char * cpp_in = nullptr;
340        const char * cpp_out = nullptr;
[51b7345]341
[bbb1b35]342        const char * args[argc + 100];                                          // leave space for 100 additional cfa command line values
[b87a5ed]343        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
[bbb1b35]344        const char * cargs[20];                                                         // leave space for 20 additional cfa-cpp command line values
[2c60af75]345        int ncargs = 1;                                                                         // 0 => command name
[51b7345]346
[dffaeac]347        #ifdef __DEBUG_H__
[7b937575]348        cerr << "Stage2" << endl;
[dffaeac]349        #endif // __DEBUG_H__
[2c60af75]350        checkEnv2( cargs, ncargs );                                                     // arguments passed via environment variables
[bec4d24]351        #ifdef __DEBUG_H__
352        for ( int i = 1; i < argc; i += 1 ) {
353                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
354        } // for
355        #endif // __DEBUG_H__
[7b937575]356
[49d3128]357        enum {
358                Color_Auto   = 0,
359                Color_Always = 1,
360                Color_Never  = 2,
361        } color_arg = Color_Auto;
362
363        const char * color_names[3] = { "--colors=auto", "--colors=always", "--colors=never" };
364
[b87a5ed]365        // process all the arguments
[51b7345]366
[bec4d24]367        for ( int i = 1; i < argc; i += 1 ) {
[b87a5ed]368                arg = argv[i];
369                if ( prefix( arg, "-" ) ) {
370                        // strip inappropriate flags
[51b7345]371
[49d3128]372                        if ( prefix( arg, "-fdiagnostics-color=" ) ) {
373                                string choice = arg.substr(20);
374                                     if(choice == "always") color_arg = Color_Always;
375                                else if(choice == "never" ) color_arg = Color_Never;
376                                else if(choice == "auto"  ) color_arg = Color_Auto;
377                        } else if ( arg == "-fno-diagnostics-color" ) {
378                                color_arg = Color_Auto;
[36de20d]379                        } // if
[49d3128]380
[b87a5ed]381                        if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" ||
[36de20d]382                                 // Currently CFA does not suppose precompiled .h files.
383                                 prefix( arg, "--output-pch" ) ) {
[51b7345]384
[b87a5ed]385                                // strip inappropriate flags with an argument
[51b7345]386
[a804f81]387                        } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" || arg == "-dumpdir" ) {
[b87a5ed]388                                i += 1;
[dffaeac]389                                #ifdef __DEBUG_H__
[b87a5ed]390                                cerr << "arg:\"" << argv[i] << "\"" << endl;
[dffaeac]391                                #endif // __DEBUG_H__
[51b7345]392
[b87a5ed]393                                // all other flags
[51b7345]394
[b87a5ed]395                        } else {
[db62eef]396                                args[nargs++] = argv[i];                                // pass flag along
[b87a5ed]397                                if ( arg == "-o" ) {
398                                        i += 1;
[2c60af75]399                                        cpp_out = argv[i];
[db62eef]400                                        args[nargs++] = argv[i];                        // pass argument along
[68bceeb]401                                        #ifdef __DEBUG_H__
[b87a5ed]402                                        cerr << "arg:\"" << argv[i] << "\"" << endl;
[68bceeb]403                                        #endif // __DEBUG_H__
[b87a5ed]404                                } // if
405                        } // if
406                } else {                                                                                // obtain input and possibly output files
[bbb1b35]407                        if ( cpp_in == nullptr ) {
[b87a5ed]408                                cpp_in = argv[i];
[dffaeac]409                                #ifdef __DEBUG_H__
[b87a5ed]410                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
[dffaeac]411                                #endif // __DEBUG_H__
[bbb1b35]412                        } else if ( cpp_out == nullptr ) {
[2c60af75]413                                cpp_out = argv[i];
414                                #ifdef __DEBUG_H__
415                                cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
416                                #endif // __DEBUG_H__
[b87a5ed]417                        } else {
[bbb1b35]418                                cerr << "Usage: " << argv[0] << " more than two files specified" << endl;
[b87a5ed]419                                exit( EXIT_FAILURE );
420                        } // if
421                } // if
422        } // for
[51b7345]423
[bbb1b35]424        if ( cpp_in == nullptr ) {
425                cerr << "Usage: " << argv[0] << " missing input file" << endl;
426                exit( EXIT_FAILURE );
427        } // if
428        if ( cpp_out == nullptr ) {
429                cerr << "Usage: " << argv[0] << " missing output file" << endl;
430                exit( EXIT_FAILURE );
431        } // if
432
[2c60af75]433        // Create a temporary file, if needed, to store output of the cfa-cpp preprocessor. Cannot be created in forked
434        // process because variables tmpname and tmpfilefd are cloned.
435
[bbb1b35]436        string cfa_cpp_out;
437
438        if ( ! CFA_flag ) {                                                                     // run compiler ?
439                if ( save_temps ) {
440                        cfa_cpp_out = cpp_in;
441                        size_t dot = cfa_cpp_out.find_last_of( "." );
442                        if ( dot == string::npos ) {
443                                cerr << "CC1 Translator error: stage 2, bad file name " << endl;
444                                exit( EXIT_FAILURE );
445                        } // if
[2c60af75]446
[36de20d]447                        cfa_cpp_out = cfa_cpp_out.substr( 0, dot ) + CFA_SUFFIX;
[bbb1b35]448                        if ( creat( cfa_cpp_out.c_str(), 0666 ) == -1 ) {
449                                perror( "CC1 Translator error: stage 2, creat" );
450                                exit( EXIT_FAILURE );
451                        } // if
452                } else {
[0bf5340]453                        tmpfilefd = mkstemps( tmpname, 4 );
[bbb1b35]454                        if ( tmpfilefd == -1 ) {
455                                perror( "CC1 Translator error: stage 2, mkstemp" );
456                                exit( EXIT_FAILURE );
457                        } // if
458                        cfa_cpp_out = tmpname;
459                } // if
[2c60af75]460                #ifdef __DEBUG_H__
[bbb1b35]461                cerr << "cfa_cpp_out: " << cfa_cpp_out << endl;
[2c60af75]462                #endif // __DEBUG_H__
463        } // if
464
465        // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard
466        // output.  Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.
467
[36de20d]468        if ( fork() == 0 ) {                                                            // child runs CFA preprocessor
[7c8246d]469                cargs[0] = ( *new string( bprefix + "cfa-cpp" ) ).c_str();
[2c60af75]470                cargs[ncargs++] = cpp_in;
471
472                if ( CFA_flag ) {                                                               // run cfa-cpp ?
473                        if ( o_file.size() != 0 ) {                                     // location for output
474                                cargs[ncargs++] = ( *new string( o_file.c_str() ) ).c_str();
475                        } // if
476                } else {
[bbb1b35]477                        cargs[ncargs++] = cfa_cpp_out.c_str();
[2c60af75]478                } // if
[49d3128]479
480                cargs[ncargs++] = color_names[color_arg];
481
[5a43ab8]482                cargs[ncargs] = nullptr;                                                // terminate argument list
[2c60af75]483
484                #ifdef __DEBUG_H__
[bbb1b35]485                for ( int i = 0; cargs[i] != nullptr; i += 1 ) {
[2c60af75]486                        cerr << cargs[i] << " ";
487                } // for
488                cerr << endl;
489                #endif // __DEBUG_H__
490
491                execvp( cargs[0], (char * const *)cargs );              // should not return
[5a43ab8]492                perror( "CC1 Translator error: stage 2 cfa-cpp, execvp" );
493                cerr << " invoked " << cargs[0] << endl;
[2c60af75]494                exit( EXIT_FAILURE );
495        } // if
496
497        wait( &code );                                                                          // wait for child to finish
498
499        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
500                rmtmpfile();                                                                    // remove tmpname
501                cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
502                exit( EXIT_FAILURE );
503        } // if
504
505        if ( CFA_flag ) {                                                                       // no tmpfile created
506                exit( WEXITSTATUS( code ) );                                    // stop regardless of success or failure
507        } // if
508
509        #ifdef __DEBUG_H__
510        cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
511        #endif // __DEBUG_H__
512
513        if ( WEXITSTATUS(code) ) {                                                      // child error ?
514                rmtmpfile();                                                                    // remove tmpname
515                exit( WEXITSTATUS( code ) );                                    // do not continue
516        } // if
517
[dffaeac]518        #ifdef __DEBUG_H__
[b87a5ed]519        cerr << "args:";
[bec4d24]520        for ( int i = 1; i < nargs; i += 1 ) {
[b87a5ed]521                cerr << " " << args[i];
522        } // for
[bbb1b35]523        cerr << " " << cpp_in << endl;
[dffaeac]524        #endif // __DEBUG_H__
[51b7345]525
[36de20d]526        if ( fork() == 0 ) {                                                            // child runs gcc
[2c60af75]527                args[0] = compiler_path.c_str();
528                args[nargs++] = "-S";                                                   // only compile and put assembler output in specified file
[0bf5340]529                args[nargs++] = "-x";
530                args[nargs++] = "cpp-output";
531
[bbb1b35]532                args[nargs++] = cfa_cpp_out.c_str();
533                args[nargs] = nullptr;                                                  // terminate argument list
[2c60af75]534
535                #ifdef __DEBUG_H__
536                cerr << "stage2 nargs: " << nargs << endl;
[bbb1b35]537                for ( int i = 0; args[i] != nullptr; i += 1 ) {
[2c60af75]538                        cerr << args[i] << " ";
539                } // for
540                cerr << endl;
541                #endif // __DEBUG_H__
542
543                execvp( args[0], (char * const *)args );                // should not return
[5a43ab8]544                perror( "CC1 Translator error: stage 2 cc1, execvp" );
[c5c0148]545                cerr << " invoked " << args[0] << endl;
[2c60af75]546                exit( EXIT_FAILURE );                                                   // tell gcc not to go any further
547        } // if
548
549        wait( &code );                                                                          // wait for child to finish
[0bf5340]550        rmtmpfile();                                                                            // remove tmpname
[2c60af75]551
552        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
553                cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
554                exit( EXIT_FAILURE );
555        } // if
[51b7345]556
[dffaeac]557        #ifdef __DEBUG_H__
[2c60af75]558        cerr << "return code from gcc cc1:" << WEXITSTATUS(code) << endl;
[dffaeac]559        #endif // __DEBUG_H__
[51b7345]560
[2c60af75]561        exit( WEXITSTATUS( code ) );                                            // stop regardless of success or failure
[51b7345]562} // Stage2
563
564
[2c60af75]565// This program is called twice because of the -no-integrated-cpp. The calls are differentiated by the first
566// command-line argument. The first call replaces the traditional cpp pass to preprocess the C program. The second call
567// is to the compiler, which is broken into two steps: preprocess again with cfa-cpp and then call gcc to compile the
568// doubly preprocessed program.
569
[b3c36f4]570int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
[dffaeac]571        #ifdef __DEBUG_H__
[bbb1b35]572        for ( int i = 0; env[i] != nullptr; i += 1 ) {
[b87a5ed]573                cerr << env[i] << endl;
574        } // for
[dffaeac]575        #endif // __DEBUG_H__
[51b7345]576
[2c60af75]577        signal( SIGINT,  sigTermHandler );
578        signal( SIGTERM, sigTermHandler );
579
[bbb1b35]580        string arg( argv[1] );
[51b7345]581
[b87a5ed]582        // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
[51b7345]583
[b87a5ed]584        if ( arg == "-E" ) {
585                Stage1( argc, argv );
586        } else if ( arg == "-fpreprocessed" ) {
587                Stage2( argc, argv );
588        } else {
589                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
590                exit( EXIT_FAILURE );
591        } // if
[51b7345]592} // main
593
594// Local Variables: //
[b87a5ed]595// tab-width: 4 //
596// mode: c++ //
[51b7345]597// compile-command: "make install" //
598// End: //
Note: See TracBrowser for help on using the repository browser.