Changes in src/main.cc [74330e7:e0bd0f9]
- File:
-
- 1 edited
-
src/main.cc (modified) (25 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/main.cc
r74330e7 re0bd0f9 10 10 // Created On : Fri May 15 23:12:02 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Feb 8 08:33:50 202013 // Update Count : 6 3312 // Last Modified On : Thu Aug 22 13:06:18 2019 13 // Update Count : 605 14 14 // 15 15 … … 17 17 #include <execinfo.h> // for backtrace, backtrace_symbols 18 18 #include <getopt.h> // for no_argument, optind, geto... 19 #include <signal.h> // for signal, SIGABRT, SIGSEGV 19 20 #include <cassert> // for assertf 20 21 #include <cstdio> // for fopen, FILE, fclose, stdin 21 22 #include <cstdlib> // for exit, free, abort, EXIT_F... 22 #include <csignal> // for signal, SIGABRT, SIGSEGV23 23 #include <cstring> // for index 24 24 #include <fstream> // for ofstream … … 28 28 #include <list> // for list 29 29 #include <string> // for char_traits, operator<< 30 31 using namespace std;32 33 30 34 31 #include "CompilationState.h" … … 56 53 #include "InitTweak/GenInit.h" // for genInit 57 54 #include "MakeLibCfa.h" // for makeLibCfa 55 #include "Parser/LinkageSpec.h" // for Spec, Cforall, Intrinsic 58 56 #include "Parser/ParseNode.h" // for DeclarationNode, buildList 59 57 #include "Parser/TypedefTable.h" // for TypedefTable … … 61 59 #include "ResolvExpr/Resolver.h" // for resolve 62 60 #include "SymTab/Validate.h" // for validate 63 #include "SynTree/ LinkageSpec.h" // for Spec, Cforall, Intrinsic61 #include "SynTree/TopLvalue.h" // for assertTopLvalue, clearInn... 64 62 #include "SynTree/Declaration.h" // for Declaration 65 63 #include "SynTree/Visitor.h" // for acceptAll … … 67 65 #include "Virtual/ExpandCasts.h" // for expandCasts 68 66 67 68 using namespace std; 69 69 70 70 static void NewPass( const char * const name ) { … … 96 96 DeclarationNode * parseTree = nullptr; // program parse tree 97 97 98 static bool waiting_for_gdb = false; // flag to set cfa-cpp to wait for gdb on start 99 100 static string PreludeDirector = ""; 98 static std::string PreludeDirector = ""; 101 99 102 100 static void parse_cmdline( int argc, char *argv[] ); … … 105 103 106 104 static void backtrace( int start ) { // skip first N stack frames 107 enum { Frames = 50 , }; // maximum number of stack frames105 enum { Frames = 50 }; 108 106 void * array[Frames]; 109 size_t size = ::backtrace( array, Frames );107 int size = ::backtrace( array, Frames ); 110 108 char ** messages = ::backtrace_symbols( array, size ); // does not demangle names 111 109 … … 114 112 115 113 // skip last 2 stack frames after main 116 for ( unsignedint i = start; i < size - 2 && messages != nullptr; i += 1 ) {114 for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) { 117 115 char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; 118 119 for ( char * p = messages[i]; *p; p += 1 ) { // find parantheses and +offset 116 for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset 120 117 if ( *p == '(' ) { 121 118 mangled_name = p; … … 155 152 } // backtrace 156 153 157 #define SIGPARMS int sig __attribute__(( unused )), siginfo_t * sfp __attribute__(( unused )), ucontext_t * cxt __attribute__(( unused )) 158 159 static void Signal( int sig, void (*handler)(SIGPARMS), int flags ) { 160 struct sigaction act; 161 162 act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler; 163 act.sa_flags = flags; 164 165 if ( sigaction( sig, &act, nullptr ) == -1 ) { 166 cerr << "*CFA runtime error* problem installing signal handler, error(" << errno << ") " << strerror( errno ) << endl; 167 _exit( EXIT_FAILURE ); 168 } // if 169 } // Signal 170 171 static void sigSegvBusHandler( SIGPARMS ) { 172 if ( sfp->si_addr == nullptr ) { 173 cerr << "Null pointer (nullptr) dereference." << endl; 174 } else { 175 cerr << (sig == SIGSEGV ? "Segment fault" : "Bus error") << " at memory location " << sfp->si_addr << "." << endl 176 << "Possible cause is reading outside the address space or writing to a protected area within the address space with an invalid pointer or subscript." << endl; 177 } // if 154 static void sigSegvBusHandler( int sig_num ) { 155 cerr << "*CFA runtime error* program cfa-cpp terminated with " 156 << (sig_num == SIGSEGV ? "segment fault" : "bus error") 157 << "." << endl; 178 158 backtrace( 2 ); // skip first 2 stack frames 159 //_exit( EXIT_FAILURE ); 179 160 abort(); // cause core dump for debugging 180 161 } // sigSegvBusHandler 181 162 182 static void sigFpeHandler( SIGPARMS ) { 183 const char * msg; 184 185 switch ( sfp->si_code ) { 186 case FPE_INTDIV: case FPE_FLTDIV: msg = "divide by zero"; break; 187 case FPE_FLTOVF: msg = "overflow"; break; 188 case FPE_FLTUND: msg = "underflow"; break; 189 case FPE_FLTRES: msg = "inexact result"; break; 190 case FPE_FLTINV: msg = "invalid operation"; break; 191 default: msg = "unknown"; 192 } // choose 193 cerr << "Computation error " << msg << " at location " << sfp->si_addr << endl 194 << "Possible cause is constant-expression evaluation invalid." << endl; 195 backtrace( 2 ); // skip first 2 stack frames 196 abort(); // cause core dump for debugging 197 } // sigFpeHandler 198 199 static void sigAbortHandler( SIGPARMS ) { 163 static void sigAbortHandler( __attribute__((unused)) int sig_num ) { 200 164 backtrace( 6 ); // skip first 6 stack frames 201 Signal( SIGABRT, (void (*)(SIGPARMS))SIG_DFL, SA_SIGINFO );// reset default signal handler165 signal( SIGABRT, SIG_DFL); // reset default signal handler 202 166 raise( SIGABRT ); // reraise SIGABRT 203 167 } // sigAbortHandler 168 204 169 205 170 int main( int argc, char * argv[] ) { … … 208 173 list< Declaration * > translationUnit; 209 174 210 Signal( SIGSEGV, sigSegvBusHandler, SA_SIGINFO ); 211 Signal( SIGBUS, sigSegvBusHandler, SA_SIGINFO ); 212 Signal( SIGFPE, sigFpeHandler, SA_SIGINFO ); 213 Signal( SIGABRT, sigAbortHandler, SA_SIGINFO ); 214 215 // cout << "main" << endl; 175 signal( SIGSEGV, sigSegvBusHandler ); 176 signal( SIGBUS, sigSegvBusHandler ); 177 signal( SIGABRT, sigAbortHandler ); 178 179 // std::cout << "main" << std::endl; 216 180 // for ( int i = 0; i < argc; i += 1 ) { 217 // cout << '\t' << argv[i] <<endl;181 // std::cout << '\t' << argv[i] << std::endl; 218 182 // } // for 219 183 220 184 parse_cmdline( argc, argv ); // process command-line arguments 221 185 CodeGen::FixMain::setReplaceMain( !nomainp ); 222 223 if ( waiting_for_gdb ) {224 cerr << "Waiting for gdb" << endl;225 cerr << "run :" << endl;226 cerr << " gdb attach " << getpid() << endl;227 raise(SIGSTOP);228 } // if229 186 230 187 try { … … 294 251 Stats::Time::StopBlock(); 295 252 253 //std::cerr << "Post-Parse Check" << std::endl; 254 clearInnerLvalue( translationUnit ); 255 assertTopLvalue( translationUnit ); 256 296 257 // add the assignment statement after the initialization of a type parameter 297 258 PASS( "Validate", SymTab::validate( translationUnit, symtabp ) ); … … 312 273 } // if 313 274 275 assertTopLvalue( translationUnit ); 314 276 PASS( "Fix Labels", ControlStruct::fixLabels( translationUnit ) ); 277 assertTopLvalue( translationUnit ); 315 278 PASS( "Fix Names", CodeGen::fixNames( translationUnit ) ); 279 assertTopLvalue( translationUnit ); 316 280 PASS( "Gen Init", InitTweak::genInit( translationUnit ) ); 281 assertTopLvalue( translationUnit ); 317 282 PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( translationUnit ) ); 283 assertTopLvalue( translationUnit ); 318 284 if ( libcfap ) { 319 285 // generate the bodies of cfa library functions … … 339 305 } // if 340 306 307 assertTopLvalue( translationUnit ); 308 341 309 PASS( "Resolve", ResolvExpr::resolve( translationUnit ) ); 342 310 if ( exprp ) { … … 345 313 } // if 346 314 315 clearInnerLvalue( translationUnit ); 316 assertTopLvalue( translationUnit ); 317 347 318 // fix ObjectDecl - replaces ConstructorInit nodes 348 319 PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) ); 320 clearInnerLvalue( translationUnit ); 321 assertTopLvalue( translationUnit ); 349 322 if ( ctorinitp ) { 350 323 dump ( translationUnit ); … … 353 326 354 327 PASS( "Expand Unique Expr", Tuples::expandUniqueExpr( translationUnit ) ); // xxx - is this the right place for this? want to expand ASAP so tha, sequent passes don't need to worry about double-visiting a unique expr - needs to go after InitTweak::fix so that copy constructed return declarations are reused 328 assertTopLvalue( translationUnit ); 355 329 356 330 PASS( "Translate EHM" , ControlStruct::translateEHM( translationUnit ) ); 331 assertTopLvalue( translationUnit ); 357 332 358 333 PASS( "Gen Waitfor" , Concurrency::generateWaitFor( translationUnit ) ); 334 clearInnerLvalue( translationUnit ); 335 assertTopLvalue( translationUnit ); 359 336 360 337 PASS( "Convert Specializations", GenPoly::convertSpecializations( translationUnit ) ); // needs to happen before tuple types are expanded 338 clearInnerLvalue( translationUnit ); 339 assertTopLvalue( translationUnit ); 361 340 362 341 PASS( "Expand Tuples", Tuples::expandTuples( translationUnit ) ); // xxx - is this the right place for this? 342 assertTopLvalue( translationUnit ); 363 343 364 344 if ( tuplep ) { … … 368 348 369 349 PASS( "Virtual Expand Casts", Virtual::expandCasts( translationUnit ) ); // Must come after translateEHM 350 assertTopLvalue( translationUnit ); 370 351 371 352 PASS( "Instantiate Generics", GenPoly::instantiateGeneric( translationUnit ) ); … … 374 355 return EXIT_SUCCESS; 375 356 } // if 376 357 clearInnerLvalue( translationUnit ); 358 assertTopLvalue( translationUnit ); 377 359 PASS( "Convert L-Value", GenPoly::convertLvalue( translationUnit ) ); 360 clearInnerLvalue( translationUnit ); 361 assertTopLvalue( translationUnit ); 378 362 379 363 if ( bboxp ) { … … 382 366 } // if 383 367 PASS( "Box", GenPoly::box( translationUnit ) ); 368 clearInnerLvalue( translationUnit ); 369 assertTopLvalue( translationUnit ); 384 370 385 371 if ( bcodegenp ) { … … 393 379 394 380 CodeTools::fillLocations( translationUnit ); 381 assertTopLvalue( translationUnit ); 395 382 PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ) ); 396 383 … … 424 411 return EXIT_FAILURE; 425 412 } catch ( ... ) { 426 exception_ptr eptr =current_exception();413 std::exception_ptr eptr = std::current_exception(); 427 414 try { 428 415 if (eptr) { 429 rethrow_exception(eptr);416 std::rethrow_exception(eptr); 430 417 } else { 431 cerr << "Exception Uncaught and Unknown" <<endl;432 } // if 433 } catch(const exception& e) {434 cerr << "Uncaught Exception \"" << e.what() << "\"\n";418 std::cerr << "Exception Uncaught and Unknown" << std::endl; 419 } // if 420 } catch(const std::exception& e) { 421 std::cerr << "Uncaught Exception \"" << e.what() << "\"\n"; 435 422 } // try 436 423 return EXIT_FAILURE; … … 443 430 444 431 445 static const char optstring[] = ": c:ghlLmNnpP:S:twW:D:";432 static const char optstring[] = ":hlLmNnpP:S:twW:D:"; 446 433 447 434 enum { PreludeDir = 128 }; 448 435 static struct option long_opts[] = { 449 { "colors", required_argument, nullptr, 'c' },450 { "gdb", no_argument, nullptr, 'g' },451 436 { "help", no_argument, nullptr, 'h' }, 452 437 { "libcfa", no_argument, nullptr, 'l' }, … … 467 452 468 453 static const char * description[] = { 469 "diagnostic color: never, always, or auto.", // -c 470 "wait for gdb to attach", // -g 471 "print help message", // -h 472 "generate libcfa.c", // -l 473 "generate line marks", // -L 474 "do not replace main", // -m 475 "do not generate line marks", // -N 476 "do not read prelude", // -n 454 "print help message", // -h 455 "generate libcfa.c", // -l 456 "generate line marks", // -L 457 "do not replace main", // -m 458 "do not generate line marks", // -N 459 "do not read prelude", // -n 477 460 "generate prototypes for prelude functions", // -p 478 "print", // -P461 "print", // -P 479 462 "<directory> prelude directory for debug/nodebug", // no flag 480 463 "<option-list> enable profiling information:\n counters,heap,time,all,none", // -S 481 "build ing cfa standard lib",// -t482 "", // -w483 "", // -W484 "", // -D464 "build in tree", // -t 465 "", // -w 466 "", // -W 467 "", // -D 485 468 }; // description 486 469 … … 550 533 while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) { 551 534 switch ( c ) { 552 case 'c': // diagnostic colors553 if ( strcmp( optarg, "always" ) == 0 ) {554 ErrorHelpers::colors = ErrorHelpers::Colors::Always;555 } else if ( strcmp( optarg, "never" ) == 0 ) {556 ErrorHelpers::colors = ErrorHelpers::Colors::Never;557 } else if ( strcmp( optarg, "auto" ) == 0 ) {558 ErrorHelpers::colors = ErrorHelpers::Colors::Auto;559 } // if560 break;561 535 case 'h': // help message 562 536 usage( argv ); // no return … … 598 572 Stats::parse_params( optarg ); 599 573 break; 600 case 't': // build ing cfa stdlib574 case 't': // build in tree 601 575 treep = true; 602 break;603 case 'g': // wait for gdb604 waiting_for_gdb = true;605 576 break; 606 577 case 'w': // suppress all warnings, hidden
Note:
See TracChangeset
for help on using the changeset viewer.