Changeset 7030dab for src/main.cc
- Timestamp:
- Apr 6, 2020, 4:46:28 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- e3bc51c
- Parents:
- 71d6bd8 (diff), 057298e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main.cc
r71d6bd8 r7030dab 10 10 // Created On : Fri May 15 23:12:02 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Aug 23 06:50:08 201913 // Update Count : 6 0712 // Last Modified On : Sat Feb 8 08:33:50 2020 13 // Update Count : 633 14 14 // 15 15 … … 20 20 #include <cstdio> // for fopen, FILE, fclose, stdin 21 21 #include <cstdlib> // for exit, free, abort, EXIT_F... 22 #include <csignal> // for signal, SIGABRT, SIGSEGV22 #include <csignal> // for signal, SIGABRT, SIGSEGV 23 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; 30 32 31 33 #include "AST/Convert.hpp" … … 54 56 #include "InitTweak/GenInit.h" // for genInit 55 57 #include "MakeLibCfa.h" // for makeLibCfa 56 #include "Parser/LinkageSpec.h" // for Spec, Cforall, Intrinsic57 58 #include "Parser/ParseNode.h" // for DeclarationNode, buildList 58 59 #include "Parser/TypedefTable.h" // for TypedefTable … … 60 61 #include "ResolvExpr/Resolver.h" // for resolve 61 62 #include "SymTab/Validate.h" // for validate 63 #include "SynTree/LinkageSpec.h" // for Spec, Cforall, Intrinsic 62 64 #include "SynTree/Declaration.h" // for Declaration 63 65 #include "SynTree/Visitor.h" // for acceptAll … … 65 67 #include "Virtual/ExpandCasts.h" // for expandCasts 66 68 67 68 using namespace std;69 69 70 70 static void NewPass( const char * const name ) { … … 98 98 static bool waiting_for_gdb = false; // flag to set cfa-cpp to wait for gdb on start 99 99 100 static st d::string PreludeDirector = "";100 static string PreludeDirector = ""; 101 101 102 102 static void parse_cmdline( int argc, char *argv[] ); … … 105 105 106 106 static void backtrace( int start ) { // skip first N stack frames 107 enum { Frames = 50 };107 enum { Frames = 50, }; // maximum number of stack frames 108 108 void * array[Frames]; 109 int size = ::backtrace( array, Frames );109 size_t size = ::backtrace( array, Frames ); 110 110 char ** messages = ::backtrace_symbols( array, size ); // does not demangle names 111 111 … … 114 114 115 115 // skip last 2 stack frames after main 116 for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) {116 for ( unsigned int i = start; i < size - 2 && messages != nullptr; i += 1 ) { 117 117 char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; 118 for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset 118 119 for ( char * p = messages[i]; *p; p += 1 ) { // find parantheses and +offset 119 120 if ( *p == '(' ) { 120 121 mangled_name = p; … … 154 155 } // backtrace 155 156 156 static void sigSegvBusHandler( int sig_num ) { 157 cerr << "*CFA runtime error* program cfa-cpp terminated with " 158 << (sig_num == SIGSEGV ? "segment fault" : "bus error") 159 << "." << endl; 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 160 178 backtrace( 2 ); // skip first 2 stack frames 161 //_exit( EXIT_FAILURE );162 179 abort(); // cause core dump for debugging 163 180 } // sigSegvBusHandler 164 181 165 static void sigAbortHandler( __attribute__((unused)) int sig_num ) { 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 ) { 166 200 backtrace( 6 ); // skip first 6 stack frames 167 signal( SIGABRT, SIG_DFL);// reset default signal handler201 Signal( SIGABRT, (void (*)(SIGPARMS))SIG_DFL, SA_SIGINFO ); // reset default signal handler 168 202 raise( SIGABRT ); // reraise SIGABRT 169 203 } // sigAbortHandler … … 174 208 list< Declaration * > translationUnit; 175 209 176 signal( SIGSEGV, sigSegvBusHandler ); 177 signal( SIGBUS, sigSegvBusHandler ); 178 signal( SIGABRT, sigAbortHandler ); 179 180 // std::cout << "main" << std::endl; 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; 181 216 // for ( int i = 0; i < argc; i += 1 ) { 182 // std::cout << '\t' << argv[i] << std::endl;217 // cout << '\t' << argv[i] << endl; 183 218 // } // for 184 219 … … 187 222 188 223 if ( waiting_for_gdb ) { 189 std::cerr << "Waiting for gdb" << std::endl;190 std::cerr << "run :" << std::endl;191 std::cerr << " gdb attach " << getpid() << std::endl;224 cerr << "Waiting for gdb" << endl; 225 cerr << "run :" << endl; 226 cerr << " gdb attach " << getpid() << endl; 192 227 raise(SIGSTOP); 193 228 } // if … … 395 430 return EXIT_FAILURE; 396 431 } catch ( ... ) { 397 std::exception_ptr eptr = std::current_exception();432 exception_ptr eptr = current_exception(); 398 433 try { 399 434 if (eptr) { 400 std::rethrow_exception(eptr);435 rethrow_exception(eptr); 401 436 } else { 402 std::cerr << "Exception Uncaught and Unknown" << std::endl;403 } // if 404 } catch(const std::exception& e) {405 std::cerr << "Uncaught Exception \"" << e.what() << "\"\n";437 cerr << "Exception Uncaught and Unknown" << endl; 438 } // if 439 } catch(const exception& e) { 440 cerr << "Uncaught Exception \"" << e.what() << "\"\n"; 406 441 } // try 407 442 return EXIT_FAILURE; … … 414 449 415 450 416 static const char optstring[] = ": hlLmNnpP:S:tgwW:D:";451 static const char optstring[] = ":c:ghlLmNnpP:S:twW:D:"; 417 452 418 453 enum { PreludeDir = 128 }; 419 454 static struct option long_opts[] = { 455 { "colors", required_argument, nullptr, 'c' }, 456 { "gdb", no_argument, nullptr, 'g' }, 420 457 { "help", no_argument, nullptr, 'h' }, 421 458 { "libcfa", no_argument, nullptr, 'l' }, … … 429 466 { "statistics", required_argument, nullptr, 'S' }, 430 467 { "tree", no_argument, nullptr, 't' }, 431 { "gdb", no_argument, nullptr, 'g' },432 468 { "", no_argument, nullptr, 0 }, // -w 433 469 { "", no_argument, nullptr, 0 }, // -W … … 437 473 438 474 static const char * description[] = { 439 "print help message", // -h 440 "generate libcfa.c", // -l 441 "generate line marks", // -L 442 "do not replace main", // -m 443 "do not generate line marks", // -N 444 "do not read prelude", // -n 475 "diagnostic color: never, always, or auto.", // -c 476 "wait for gdb to attach", // -g 477 "print help message", // -h 478 "generate libcfa.c", // -l 479 "generate line marks", // -L 480 "do not replace main", // -m 481 "do not generate line marks", // -N 482 "do not read prelude", // -n 445 483 "generate prototypes for prelude functions", // -p 446 "print", 484 "print", // -P 447 485 "<directory> prelude directory for debug/nodebug", // no flag 448 486 "<option-list> enable profiling information:\n counters,heap,time,all,none", // -S 449 "building cfa standard lib", // -t 450 "wait for gdb to attach", // -g 451 "", // -w 452 "", // -W 453 "", // -D 487 "building cfa standard lib", // -t 488 "", // -w 489 "", // -W 490 "", // -D 454 491 }; // description 455 492 … … 519 556 while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) { 520 557 switch ( c ) { 558 case 'c': // diagnostic colors 559 if ( strcmp( optarg, "always" ) == 0 ) { 560 ErrorHelpers::colors = ErrorHelpers::Colors::Always; 561 } else if ( strcmp( optarg, "never" ) == 0 ) { 562 ErrorHelpers::colors = ErrorHelpers::Colors::Never; 563 } else if ( strcmp( optarg, "auto" ) == 0 ) { 564 ErrorHelpers::colors = ErrorHelpers::Colors::Auto; 565 } // if 566 break; 521 567 case 'h': // help message 522 568 usage( argv ); // no return
Note: See TracChangeset
for help on using the changeset viewer.