Changeset 7030dab for src/main.cc


Ignore:
Timestamp:
Apr 6, 2020, 4:46:28 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
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.
Message:

Merge branch 'master' into new-ast

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    r71d6bd8 r7030dab  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Aug 23 06:50:08 2019
    13 // Update Count     : 607
     12// Last Modified On : Sat Feb  8 08:33:50 2020
     13// Update Count     : 633
    1414//
    1515
     
    2020#include <cstdio>                           // for fopen, FILE, fclose, stdin
    2121#include <cstdlib>                          // for exit, free, abort, EXIT_F...
    22 #include <csignal>                         // for signal, SIGABRT, SIGSEGV
     22#include <csignal>                          // for signal, SIGABRT, SIGSEGV
    2323#include <cstring>                          // for index
    2424#include <fstream>                          // for ofstream
     
    2828#include <list>                             // for list
    2929#include <string>                           // for char_traits, operator<<
     30
     31using namespace std;
    3032
    3133#include "AST/Convert.hpp"
     
    5456#include "InitTweak/GenInit.h"              // for genInit
    5557#include "MakeLibCfa.h"                     // for makeLibCfa
    56 #include "Parser/LinkageSpec.h"             // for Spec, Cforall, Intrinsic
    5758#include "Parser/ParseNode.h"               // for DeclarationNode, buildList
    5859#include "Parser/TypedefTable.h"            // for TypedefTable
     
    6061#include "ResolvExpr/Resolver.h"            // for resolve
    6162#include "SymTab/Validate.h"                // for validate
     63#include "SynTree/LinkageSpec.h"            // for Spec, Cforall, Intrinsic
    6264#include "SynTree/Declaration.h"            // for Declaration
    6365#include "SynTree/Visitor.h"                // for acceptAll
     
    6567#include "Virtual/ExpandCasts.h"            // for expandCasts
    6668
    67 
    68 using namespace std;
    6969
    7070static void NewPass( const char * const name ) {
     
    9898static bool waiting_for_gdb = false;                                    // flag to set cfa-cpp to wait for gdb on start
    9999
    100 static std::string PreludeDirector = "";
     100static string PreludeDirector = "";
    101101
    102102static void parse_cmdline( int argc, char *argv[] );
     
    105105
    106106static void backtrace( int start ) {                                    // skip first N stack frames
    107         enum { Frames = 50 };
     107        enum { Frames = 50, };                                                          // maximum number of stack frames
    108108        void * array[Frames];
    109         int size = ::backtrace( array, Frames );
     109        size_t size = ::backtrace( array, Frames );
    110110        char ** messages = ::backtrace_symbols( array, size ); // does not demangle names
    111111
     
    114114
    115115        // 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 ) {
    117117                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
    119120                        if ( *p == '(' ) {
    120121                                mangled_name = p;
     
    154155} // backtrace
    155156
    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
     159static 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
     171static 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
    160178        backtrace( 2 );                                                                         // skip first 2 stack frames
    161         //_exit( EXIT_FAILURE );
    162179        abort();                                                                                        // cause core dump for debugging
    163180} // sigSegvBusHandler
    164181
    165 static void sigAbortHandler( __attribute__((unused)) int sig_num ) {
     182static 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
     199static void sigAbortHandler( SIGPARMS ) {
    166200        backtrace( 6 );                                                                         // skip first 6 stack frames
    167         signal( SIGABRT, SIG_DFL);                                                      // reset default signal handler
     201        Signal( SIGABRT, (void (*)(SIGPARMS))SIG_DFL, SA_SIGINFO );     // reset default signal handler
    168202        raise( SIGABRT );                                                                       // reraise SIGABRT
    169203} // sigAbortHandler
     
    174208        list< Declaration * > translationUnit;
    175209
    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;
    181216        // for ( int i = 0; i < argc; i += 1 ) {
    182         //      std::cout << '\t' << argv[i] << std::endl;
     217        //      cout << '\t' << argv[i] << endl;
    183218        // } // for
    184219
     
    187222
    188223        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;
    192227                raise(SIGSTOP);
    193228        } // if
     
    395430                return EXIT_FAILURE;
    396431        } catch ( ... ) {
    397                 std::exception_ptr eptr = std::current_exception();
     432                exception_ptr eptr = current_exception();
    398433                try {
    399434                        if (eptr) {
    400                                 std::rethrow_exception(eptr);
     435                                rethrow_exception(eptr);
    401436                        } 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";
    406441                } // try
    407442                return EXIT_FAILURE;
     
    414449
    415450
    416 static const char optstring[] = ":hlLmNnpP:S:tgwW:D:";
     451static const char optstring[] = ":c:ghlLmNnpP:S:twW:D:";
    417452
    418453enum { PreludeDir = 128 };
    419454static struct option long_opts[] = {
     455        { "colors", required_argument, nullptr, 'c' },
     456        { "gdb", no_argument, nullptr, 'g' },
    420457        { "help", no_argument, nullptr, 'h' },
    421458        { "libcfa", no_argument, nullptr, 'l' },
     
    429466        { "statistics", required_argument, nullptr, 'S' },
    430467        { "tree", no_argument, nullptr, 't' },
    431         { "gdb", no_argument, nullptr, 'g' },
    432468        { "", no_argument, nullptr, 0 },                                        // -w
    433469        { "", no_argument, nullptr, 0 },                                        // -W
     
    437473
    438474static 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
    445483        "generate prototypes for prelude functions",            // -p
    446         "print",                                                                                        // -P
     484        "print",                                              // -P
    447485        "<directory> prelude directory for debug/nodebug",      // no flag
    448486        "<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
    454491}; // description
    455492
     
    519556        while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) {
    520557                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;
    521567                  case 'h':                                                                             // help message
    522568                        usage( argv );                                                          // no return
Note: See TracChangeset for help on using the changeset viewer.