source: driver/cc1.cc@ 8fd1b7c

ADT ast-experimental
Last change on this file since 8fd1b7c was d43a1fe, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

remove unused parameters from checkEnv1 to remove warnings

  • Property mode set to 100644
File size: 18.4 KB
Line 
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//
7// cc1.cc --
8//
9// Author : Peter A. Buhr
10// Created On : Fri Aug 26 14:23:51 2005
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Feb 17 18:04:23 2022
13// Update Count : 422
14//
15
16#include <iostream>
17using std::cerr;
18using std::endl;
19#include <string>
20using std::string;
21#include <algorithm> // find
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
26#include <fcntl.h> // creat
27
28
29#include "config.h" // configure info
30
31
32//#define __DEBUG_H__
33
34
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;
40static string lang; // -x flag
41
42
43static bool prefix( const string & arg, const string & pre ) {
44 return arg.substr( 0, pre.size() ) == pre;
45} // prefix
46
47static void suffix( const string & arg, const char * args[], int & nargs ) {
48 enum { NumSuffixes = 3 };
49 static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
50
51 size_t dot = arg.find_last_of( "." );
52 if ( dot == string::npos ) return;
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
58} // suffix
59
60
61static string __CFA_FLAGPREFIX__( "__CFA_FLAG" ); // "__CFA_FLAG__=" suffix
62
63static void checkEnv1() { // stage 1
64 extern char ** environ;
65
66 for ( int i = 0; environ[i]; i += 1 ) {
67 string arg( environ[i] );
68 #ifdef __DEBUG_H__
69 cerr << "env arg:\"" << arg << "\"" << endl;
70 #endif // __DEBUG_H__
71
72 if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
73 string val( arg.substr( arg.find_first_of( "=" ) + 1 ) );
74 if ( prefix( val, "-compiler=" ) ) {
75 compiler_path = val.substr( 10 );
76 } else if ( prefix( val, "-x=" ) ) {
77 lang = val.substr( 3 );
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 ) {
88 string arg( environ[i] );
89 #ifdef __DEBUG_H__
90 cerr << "env arg:\"" << arg << "\"" << endl;
91 #endif // __DEBUG_H__
92
93 if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
94 string val( arg.substr( arg.find_first_of( "=" ) + 1 ) );
95 if ( prefix( val, "-compiler=" ) ) {
96 compiler_path = val.substr( 10 );
97 } else if ( val == "-CFA" ) {
98 CFA_flag = true;
99 } else if ( val == "-save-temps" || val == "--save-temps" ) {
100 save_temps = true;
101 } else if ( prefix( val, "-o=" ) ) { // output file for -CFA
102 o_file = val.substr( 3 );
103 } else if ( prefix( val, "-B=" ) ) { // location of cfa-cpp
104 bprefix = val.substr( 3 );
105 } else if ( prefix( val, "-x=" ) ) { // ignore
106 } else { // normal flag for cfa-cpp
107 args[nargs++] = ( *new string( arg.substr( arg.find_first_of( "=" ) + 1 ) ) ).c_str();
108 } // if
109 } // if
110 } // for
111} // checkEnv2
112
113#define CFA_SUFFIX ".ifa"
114
115static char tmpname[] = P_tmpdir "/CFAXXXXXX" CFA_SUFFIX;
116static int tmpfilefd = -1;
117static bool startrm = false;
118
119static void rmtmpfile() {
120 if ( tmpfilefd == -1 ) return; // RACE, file created ?
121
122 startrm = true; // RACE with C-c C-c
123 if ( unlink( tmpname ) == -1 ) { // remove tmpname
124 perror ( "CC1 Translator error: failed, unlink" );
125 exit( EXIT_FAILURE );
126 } // if
127 tmpfilefd = -1; // mark removed
128} // rmtmpfile
129
130
131static void sigTermHandler( int ) { // C-c C-c
132 if ( startrm ) return; // return and let rmtmpfile finish, and then program finishes
133
134 if ( tmpfilefd != -1 ) { // RACE, file created ?
135 rmtmpfile(); // remove tmpname
136 } // if
137 exit( EXIT_FAILURE ); // terminate
138} // sigTermHandler
139
140
141static void Stage1( const int argc, const char * const argv[] ) {
142 int code;
143 string arg;
144
145 const char * cpp_in = nullptr;
146 const char * cpp_out = nullptr;
147
148 bool cpp_flag = false;
149 bool o_flag = false;
150
151 const char * args[argc + 100]; // leave space for 100 additional cpp command line values
152 int nargs = 1; // number of arguments in args list; 0 => command name
153
154 #ifdef __DEBUG_H__
155 cerr << "Stage1" << endl;
156 #endif // __DEBUG_H__
157 checkEnv1(); // arguments passed via environment variables
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__
163
164 // process all the arguments
165
166 for ( int i = 1; i < argc; i += 1 ) {
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" ) {
173 i += 1; // and argument
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" ) ) {
180 i += 1; // and argument
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__" ) {
187 i += 1; // and argument
188 cpp_flag = true;
189
190 // all other flags
191
192 } else if ( arg == "-o" ) {
193 i += 1;
194 o_flag = true;
195 cpp_out = argv[i];
196 } else {
197 args[nargs++] = argv[i]; // pass flag along
198 // CPP flags with an argument
199 if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
200 arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" ||
201 arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ||
202 arg == "-dumpbase-ext" || arg == "-dumpbase"
203 ) {
204 i += 1;
205 args[nargs++] = argv[i]; // pass argument along
206 #ifdef __DEBUG_H__
207 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
208 #endif // __DEBUG_H__
209 } else if ( arg == "-MD" || arg == "-MMD" ) {
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.
212 args[nargs++] = "-MF"; // insert before file
213 i += 1;
214 args[nargs++] = argv[i]; // pass argument along
215 #ifdef __DEBUG_H__
216 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
217 #endif // __DEBUG_H__
218 } // if
219 } // if
220 } else { // obtain input and possibly output files
221 if ( cpp_in == nullptr ) {
222 cpp_in = argv[i];
223 #ifdef __DEBUG_H__
224 cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
225 #endif // __DEBUG_H__
226 } else if ( cpp_out == nullptr ) {
227 cpp_out = argv[i];
228 #ifdef __DEBUG_H__
229 cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
230 #endif // __DEBUG_H__
231 } else {
232 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
233 exit( EXIT_FAILURE );
234 } // if
235 } // if
236 } // for
237
238 #ifdef __DEBUG_H__
239 cerr << "args:";
240 for ( int i = 1; i < nargs; i += 1 ) {
241 cerr << " " << args[i];
242 } // for
243 if ( cpp_in != nullptr ) cerr << " " << cpp_in;
244 if ( cpp_out != nullptr ) cerr << " " << cpp_out;
245 cerr << endl;
246 #endif // __DEBUG_H__
247
248 if ( cpp_in == nullptr ) {
249 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
250 exit( EXIT_FAILURE );
251 } // if
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
257 args[0] = compiler_path.c_str();
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
264 args[nargs++] = cpp_in;
265 if ( o_flag ) { // location for output
266 args[nargs++] = "-o";
267 } // if
268 args[nargs++] = cpp_out;
269 args[nargs] = nullptr; // terminate argument list
270
271 #ifdef __DEBUG_H__
272 cerr << "nargs: " << nargs << endl;
273 for ( int i = 0; args[i] != nullptr; i += 1 ) {
274 cerr << args[i] << " ";
275 } // for
276 cerr << endl;
277 #endif // __DEBUG_H__
278
279 execvp( args[0], (char * const *)args ); // should not return
280 perror( "CC1 Translator error: stage 1, execvp" );
281 exit( EXIT_FAILURE );
282 } // if
283
284 // Run the C preprocessor and save the output in the given file.
285
286 if ( fork() == 0 ) { // child process ?
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.
290 if ( freopen( cpp_out, "w", stdout ) == nullptr ) { // redirect stdout to output file
291 perror( "CC1 Translator error: stage 1, freopen" );
292 exit( EXIT_FAILURE );
293 } // if
294
295 args[0] = compiler_path.c_str();
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
302 args[nargs++] = cpp_in; // input to cpp
303 args[nargs] = nullptr; // terminate argument list
304
305 #ifdef __DEBUG_H__
306 cerr << "cpp nargs: " << nargs << endl;
307 for ( int i = 0; args[i] != nullptr; i += 1 ) {
308 cerr << args[i] << " ";
309 } // for
310 cerr << endl;
311 #endif // __DEBUG_H__
312
313 execvp( args[0], (char * const *)args ); // should not return
314 perror( "CC1 Translator error: stage 1 cpp, execvp" );
315 cerr << " invoked " << args[0] << endl;
316 exit( EXIT_FAILURE );
317 } // if
318
319 wait( &code ); // wait for child to finish
320
321 #ifdef __DEBUG_H__
322 cerr << "return code from cpp:" << WEXITSTATUS(code) << endl;
323 #endif // __DEBUG_H__
324
325 if ( WIFSIGNALED(code) ) { // child failed ?
326 rmtmpfile(); // remove tmpname
327 cerr << "CC1 Translator error: stage 1, child failed " << WTERMSIG(code) << endl;
328 exit( EXIT_FAILURE );
329 } // if
330
331 exit( WEXITSTATUS( code ) ); // bad cpp result stops top-level gcc
332} // Stage1
333
334
335static void Stage2( const int argc, const char * const * argv ) {
336 int code;
337 string arg;
338
339 const char * cpp_in = nullptr;
340 const char * cpp_out = nullptr;
341
342 const char * args[argc + 100]; // leave space for 100 additional cfa command line values
343 int nargs = 1; // number of arguments in args list; 0 => command name
344 const char * cargs[20]; // leave space for 20 additional cfa-cpp command line values
345 int ncargs = 1; // 0 => command name
346
347 #ifdef __DEBUG_H__
348 cerr << "Stage2" << endl;
349 #endif // __DEBUG_H__
350 checkEnv2( cargs, ncargs ); // arguments passed via environment variables
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__
356
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
365 // process all the arguments
366
367 for ( int i = 1; i < argc; i += 1 ) {
368 arg = argv[i];
369 if ( prefix( arg, "-" ) ) {
370 // strip inappropriate flags
371
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;
379 } // if
380
381 if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" ||
382 // Currently CFA does not suppose precompiled .h files.
383 prefix( arg, "--output-pch" ) ) {
384
385 // strip inappropriate flags with an argument
386
387 } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" || arg == "-dumpdir" ) {
388 i += 1;
389 #ifdef __DEBUG_H__
390 cerr << "arg:\"" << argv[i] << "\"" << endl;
391 #endif // __DEBUG_H__
392
393 // all other flags
394
395 } else {
396 args[nargs++] = argv[i]; // pass flag along
397 if ( arg == "-o" ) {
398 i += 1;
399 cpp_out = argv[i];
400 args[nargs++] = argv[i]; // pass argument along
401 #ifdef __DEBUG_H__
402 cerr << "arg:\"" << argv[i] << "\"" << endl;
403 #endif // __DEBUG_H__
404 } // if
405 } // if
406 } else { // obtain input and possibly output files
407 if ( cpp_in == nullptr ) {
408 cpp_in = argv[i];
409 #ifdef __DEBUG_H__
410 cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
411 #endif // __DEBUG_H__
412 } else if ( cpp_out == nullptr ) {
413 cpp_out = argv[i];
414 #ifdef __DEBUG_H__
415 cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
416 #endif // __DEBUG_H__
417 } else {
418 cerr << "Usage: " << argv[0] << " more than two files specified" << endl;
419 exit( EXIT_FAILURE );
420 } // if
421 } // if
422 } // for
423
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
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
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
446
447 cfa_cpp_out = cfa_cpp_out.substr( 0, dot ) + CFA_SUFFIX;
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 {
453 tmpfilefd = mkstemps( tmpname, 4 );
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
460 #ifdef __DEBUG_H__
461 cerr << "cfa_cpp_out: " << cfa_cpp_out << endl;
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
468 if ( fork() == 0 ) { // child runs CFA preprocessor
469 cargs[0] = ( *new string( bprefix + "cfa-cpp" ) ).c_str();
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 {
477 cargs[ncargs++] = cfa_cpp_out.c_str();
478 } // if
479
480 cargs[ncargs++] = color_names[color_arg];
481
482 cargs[ncargs] = nullptr; // terminate argument list
483
484 #ifdef __DEBUG_H__
485 for ( int i = 0; cargs[i] != nullptr; i += 1 ) {
486 cerr << cargs[i] << " ";
487 } // for
488 cerr << endl;
489 #endif // __DEBUG_H__
490
491 execvp( cargs[0], (char * const *)cargs ); // should not return
492 perror( "CC1 Translator error: stage 2 cfa-cpp, execvp" );
493 cerr << " invoked " << cargs[0] << endl;
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
518 #ifdef __DEBUG_H__
519 cerr << "args:";
520 for ( int i = 1; i < nargs; i += 1 ) {
521 cerr << " " << args[i];
522 } // for
523 cerr << " " << cpp_in << endl;
524 #endif // __DEBUG_H__
525
526 if ( fork() == 0 ) { // child runs gcc
527 args[0] = compiler_path.c_str();
528 args[nargs++] = "-S"; // only compile and put assembler output in specified file
529 args[nargs++] = "-x";
530 args[nargs++] = "cpp-output";
531
532 args[nargs++] = cfa_cpp_out.c_str();
533 args[nargs] = nullptr; // terminate argument list
534
535 #ifdef __DEBUG_H__
536 cerr << "stage2 nargs: " << nargs << endl;
537 for ( int i = 0; args[i] != nullptr; i += 1 ) {
538 cerr << args[i] << " ";
539 } // for
540 cerr << endl;
541 #endif // __DEBUG_H__
542
543 execvp( args[0], (char * const *)args ); // should not return
544 perror( "CC1 Translator error: stage 2 cc1, execvp" );
545 cerr << " invoked " << args[0] << endl;
546 exit( EXIT_FAILURE ); // tell gcc not to go any further
547 } // if
548
549 wait( &code ); // wait for child to finish
550 rmtmpfile(); // remove tmpname
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
556
557 #ifdef __DEBUG_H__
558 cerr << "return code from gcc cc1:" << WEXITSTATUS(code) << endl;
559 #endif // __DEBUG_H__
560
561 exit( WEXITSTATUS( code ) ); // stop regardless of success or failure
562} // Stage2
563
564
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
570int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
571 #ifdef __DEBUG_H__
572 for ( int i = 0; env[i] != nullptr; i += 1 ) {
573 cerr << env[i] << endl;
574 } // for
575 #endif // __DEBUG_H__
576
577 signal( SIGINT, sigTermHandler );
578 signal( SIGTERM, sigTermHandler );
579
580 string arg( argv[1] );
581
582 // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
583
584 if ( arg == "-E" ) {
585 Stage1( argc, argv );
586 } else if ( arg == "-fpreprocessed" ) {
587 Stage2( argc, argv );
588 } else {
589 cerr << "Usage: " << argv[0] << " [-E input-file [output-file] ] | [-fpreprocessed input-file output-file] [options]" << endl;
590 exit( EXIT_FAILURE );
591 } // if
592} // main
593
594// Local Variables: //
595// tab-width: 4 //
596// mode: c++ //
597// compile-command: "make install" //
598// End: //
Note: See TracBrowser for help on using the repository browser.