source: driver/cc1.cc@ 9bdb8b7

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 9bdb8b7 was c5c0148, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

change cargs to args in error message for CC1 Translator stage 2

  • Property mode set to 100644
File size: 17.1 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 : Sun Oct 20 08:14:33 2019
13// Update Count : 385
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>
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;
40
41
42static bool prefix( const string & arg, const string & pre ) {
43 return arg.substr( 0, pre.size() ) == pre;
44} // prefix
45
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
50 size_t dot = arg.find_last_of( "." );
51 if ( dot == string::npos ) return;
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
57} // suffix
58
59
60static string __CFA_FLAGPREFIX__( "__CFA_FLAG" ); // "N__=" suffix
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( arg.find_first_of( "=" ) + 1 ) );
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( arg.find_first_of( "=" ) + 1 ) );
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( arg.find_first_of( "=" ) + 1 ) ) ).c_str();
104 } // if
105 } // if
106 } // for
107} // checkEnv2
108
109
110static char tmpname[] = P_tmpdir "/CFAXXXXXX.ifa";
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
118 if ( unlink( tmpname ) == -1 ) { // remove tmpname
119 perror ( "CC1 Translator error: failed, unlink" );
120 exit( EXIT_FAILURE );
121 } // if
122 tmpfilefd = -1; // mark removed
123} // rmtmpfile
124
125
126static void sigTermHandler( int ) { // C-c C-c
127 if ( startrm ) return; // return and let rmtmpfile finish, and then program finishes
128
129 if ( tmpfilefd != -1 ) { // RACE, file created ?
130 rmtmpfile(); // remove tmpname
131 } // if
132 exit( EXIT_FAILURE ); // terminate
133} // sigTermHandler
134
135
136static void Stage1( const int argc, const char * const argv[] ) {
137 int code;
138 string arg;
139
140 const char * cpp_in = nullptr;
141 const char * cpp_out = nullptr;
142
143 bool cpp_flag = false;
144 bool o_flag = false;
145
146 const char * args[argc + 100]; // leave space for 100 additional cpp command line values
147 int nargs = 1; // number of arguments in args list; 0 => command name
148
149 #ifdef __DEBUG_H__
150 cerr << "Stage1" << endl;
151 #endif // __DEBUG_H__
152 checkEnv1( args, nargs ); // arguments passed via environment variables
153 #ifdef __DEBUG_H__
154 for ( int i = 1; i < argc; i += 1 ) {
155 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
156 } // for
157 #endif // __DEBUG_H__
158
159 // process all the arguments
160
161 for ( int i = 1; i < argc; i += 1 ) {
162 arg = argv[i];
163 if ( prefix( arg, "-" ) ) {
164 // strip g++ flags that are inappropriate or cause duplicates in subsequent passes
165
166 if ( arg == "-quiet" ) {
167 } else if ( arg == "-imultilib" || arg == "-imultiarch" ) {
168 i += 1; // and the argument
169 } else if ( prefix( arg, "-A" ) ) {
170 } else if ( prefix( arg, "-D__GNU" ) ) {
171 //********
172 // GCC 5.6.0 SEPARATED THE -D FROM THE ARGUMENT!
173 //********
174 } else if ( arg == "-D" && prefix( argv[i + 1], "__GNU" ) ) {
175 i += 1; // and the argument
176
177 // strip flags controlling cpp step
178
179 } else if ( arg == "-D__CPP__" ) {
180 cpp_flag = true;
181 } else if ( arg == "-D" && string( argv[i + 1] ) == "__CPP__" ) {
182 i += 1; // and the argument
183 cpp_flag = true;
184
185 // all other flags
186
187 } else if ( arg == "-o" ) {
188 i += 1;
189 o_flag = true;
190 cpp_out = argv[i];
191 } else {
192 args[nargs++] = argv[i]; // pass the flag along
193 // CPP flags with an argument
194 if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
195 arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" ||
196 arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
197 i += 1;
198 args[nargs++] = argv[i]; // pass the argument along
199 #ifdef __DEBUG_H__
200 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
201 #endif // __DEBUG_H__
202 } else if ( arg == "-MD" || arg == "-MMD" ) {
203 args[nargs++] = "-MF"; // insert before file
204 i += 1;
205 args[nargs++] = argv[i]; // pass the argument along
206 #ifdef __DEBUG_H__
207 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
208 #endif // __DEBUG_H__
209 } // if
210 } // if
211 } else { // obtain input and possibly output files
212 if ( cpp_in == nullptr ) {
213 cpp_in = argv[i];
214 #ifdef __DEBUG_H__
215 cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
216 #endif // __DEBUG_H__
217 } else if ( cpp_out == nullptr ) {
218 cpp_out = argv[i];
219 #ifdef __DEBUG_H__
220 cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
221 #endif // __DEBUG_H__
222 } else {
223 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
224 exit( EXIT_FAILURE );
225 } // if
226 } // if
227 } // for
228
229 #ifdef __DEBUG_H__
230 cerr << "args:";
231 for ( int i = 1; i < nargs; i += 1 ) {
232 cerr << " " << args[i];
233 } // for
234 if ( cpp_in != nullptr ) cerr << " " << cpp_in;
235 if ( cpp_out != nullptr ) cerr << " " << cpp_out;
236 cerr << endl;
237 #endif // __DEBUG_H__
238
239 if ( cpp_in == nullptr ) {
240 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
241 exit( EXIT_FAILURE );
242 } // if
243
244 if ( cpp_flag ) {
245 // The -E flag is specified on the cfa command so only run the preprocessor and output is written to standard
246 // output or -o. The call to cfa has a -E so it does not have to be added to the argument list.
247
248 args[0] = compiler_path.c_str();
249 suffix( cpp_in, args, nargs ); // check suffix
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
256
257 #ifdef __DEBUG_H__
258 cerr << "nargs: " << nargs << endl;
259 for ( int i = 0; args[i] != nullptr; i += 1 ) {
260 cerr << args[i] << " ";
261 } // for
262 cerr << endl;
263 #endif // __DEBUG_H__
264
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.
271
272 if ( fork() == 0 ) { // child process ?
273 // -o xxx.ii cannot be used to write the output file from cpp because no output file is created if cpp detects
274 // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error,
275 // when cpp writes to stdout. Hence, stdout is redirected into the temporary file.
276 if ( freopen( cpp_out, "w", stdout ) == nullptr ) { // redirect stdout to output file
277 perror( "CC1 Translator error: stage 1, freopen" );
278 exit( EXIT_FAILURE );
279 } // if
280
281 args[0] = compiler_path.c_str();
282 suffix( cpp_in, args, nargs ); // check suffix
283 args[nargs++] = cpp_in; // input to cpp
284 args[nargs] = nullptr; // terminate argument list
285
286 #ifdef __DEBUG_H__
287 cerr << "cpp nargs: " << nargs << endl;
288 for ( int i = 0; args[i] != nullptr; i += 1 ) {
289 cerr << args[i] << " ";
290 } // for
291 cerr << endl;
292 #endif // __DEBUG_H__
293
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;
297 exit( EXIT_FAILURE );
298 } // if
299
300 wait( &code ); // wait for child to finish
301
302 #ifdef __DEBUG_H__
303 cerr << "return code from cpp:" << WEXITSTATUS(code) << endl;
304 #endif // __DEBUG_H__
305
306 if ( WIFSIGNALED(code) ) { // child failed ?
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
312} // Stage1
313
314
315static void Stage2( const int argc, const char * const * argv ) {
316 int code;
317 string arg;
318
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
323 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
326
327 #ifdef __DEBUG_H__
328 cerr << "Stage2" << endl;
329 #endif // __DEBUG_H__
330 checkEnv2( cargs, ncargs ); // arguments passed via environment variables
331 #ifdef __DEBUG_H__
332 for ( int i = 1; i < argc; i += 1 ) {
333 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
334 } // for
335 #endif // __DEBUG_H__
336
337 // process all the arguments
338
339 for ( int i = 1; i < argc; i += 1 ) {
340 arg = argv[i];
341 if ( prefix( arg, "-" ) ) {
342 // strip inappropriate flags
343
344 if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" ||
345 // Currently CFA does not suppose precompiled .h files.
346 prefix( arg, "--output-pch" ) ) {
347
348 // strip inappropriate flags with an argument
349
350 } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" ) {
351 i += 1;
352 #ifdef __DEBUG_H__
353 cerr << "arg:\"" << argv[i] << "\"" << endl;
354 #endif // __DEBUG_H__
355
356 // all other flags
357
358 } else {
359 args[nargs++] = argv[i]; // pass the flag along
360 if ( arg == "-o" ) {
361 i += 1;
362 cpp_out = argv[i];
363 args[nargs++] = argv[i]; // pass the argument along
364 #ifdef __DEBUG_H__
365 cerr << "arg:\"" << argv[i] << "\"" << endl;
366 #endif // __DEBUG_H__
367 } // if
368 } // if
369 } else { // obtain input and possibly output files
370 if ( cpp_in == nullptr ) {
371 cpp_in = argv[i];
372 #ifdef __DEBUG_H__
373 cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
374 #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__
380 } else {
381 cerr << "Usage: " << argv[0] << " more than two files specified" << endl;
382 exit( EXIT_FAILURE );
383 } // if
384 } // if
385 } // 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, 4 );
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
477
478 #ifdef __DEBUG_H__
479 cerr << "args:";
480 for ( int i = 1; i < nargs; i += 1 ) {
481 cerr << " " << args[i];
482 } // for
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 args[nargs++] = "-x";
490 args[nargs++] = "cpp-output";
491
492 args[nargs++] = cfa_cpp_out.c_str();
493 args[nargs] = nullptr; // terminate argument list
494
495 #ifdef __DEBUG_H__
496 cerr << "stage2 nargs: " << nargs << endl;
497 for ( int i = 0; args[i] != nullptr; i += 1 ) {
498 cerr << args[i] << " ";
499 } // for
500 cerr << endl;
501 #endif // __DEBUG_H__
502
503 execvp( args[0], (char * const *)args ); // should not return
504 perror( "CC1 Translator error: stage 2 cc1, execvp" );
505 cerr << " invoked " << args[0] << endl;
506 exit( EXIT_FAILURE ); // tell gcc not to go any further
507 } // if
508
509 wait( &code ); // wait for child to finish
510 rmtmpfile(); // remove tmpname
511
512 if ( WIFSIGNALED(code) ) { // child failed ?
513 cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
514 exit( EXIT_FAILURE );
515 } // if
516
517 #ifdef __DEBUG_H__
518 cerr << "return code from gcc cc1:" << WEXITSTATUS(code) << endl;
519 #endif // __DEBUG_H__
520
521 exit( WEXITSTATUS( code ) ); // stop regardless of success or failure
522} // Stage2
523
524
525// This program is called twice because of the -no-integrated-cpp. The calls are differentiated by the first
526// command-line argument. The first call replaces the traditional cpp pass to preprocess the C program. The second call
527// is to the compiler, which is broken into two steps: preprocess again with cfa-cpp and then call gcc to compile the
528// doubly preprocessed program.
529
530int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
531 #ifdef __DEBUG_H__
532 for ( int i = 0; env[i] != nullptr; i += 1 ) {
533 cerr << env[i] << endl;
534 } // for
535 #endif // __DEBUG_H__
536
537 signal( SIGINT, sigTermHandler );
538 signal( SIGTERM, sigTermHandler );
539
540 string arg( argv[1] );
541
542 // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
543
544 if ( arg == "-E" ) {
545 Stage1( argc, argv );
546 } else if ( arg == "-fpreprocessed" ) {
547 Stage2( argc, argv );
548 } else {
549 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
550 exit( EXIT_FAILURE );
551 } // if
552} // main
553
554// Local Variables: //
555// tab-width: 4 //
556// mode: c++ //
557// compile-command: "make install" //
558// End: //
Note: See TracBrowser for help on using the repository browser.