Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixInit.cc

    rd56e5bc r62423350  
    5656                typedef std::unordered_map< int, int > UnqCount;
    5757
    58                 class InsertImplicitCalls {
     58                class InsertImplicitCalls : public WithTypeSubstitution {
    5959                public:
    6060                        /// wrap function application expressions as ImplicitCopyCtorExpr nodes so that it is easy to identify which
     
    6969                        // collects environments for relevant nodes
    7070                        EnvMap & envMap;
    71                         TypeSubstitution * env; //Magically populated by the PassVisitor
    7271                };
    7372
     
    105104                        typedef AddStmtVisitor Parent;
    106105                        using Parent::visit;
    107                         typedef std::set< ObjectDecl * > ObjectSet;
     106                        // use ordered data structure to maintain ordering for set_difference and for consistent error messages
     107                        typedef std::list< ObjectDecl * > ObjectSet;
    108108                        virtual void visit( CompoundStmt *compoundStmt ) override;
    109109                        virtual void visit( DeclStmt *stmt ) override;
     
    117117
    118118                // debug
    119                 struct printSet {
    120                         typedef ObjDeclCollector::ObjectSet ObjectSet;
    121                         printSet( const ObjectSet & objs ) : objs( objs ) {}
     119                template<typename ObjectSet>
     120                struct PrintSet {
     121                        PrintSet( const ObjectSet & objs ) : objs( objs ) {}
    122122                        const ObjectSet & objs;
    123123                };
    124                 std::ostream & operator<<( std::ostream & out, const printSet & set) {
     124                template<typename ObjectSet>
     125                PrintSet<ObjectSet> printSet( const ObjectSet & objs ) { return PrintSet<ObjectSet>( objs ); }
     126                template<typename ObjectSet>
     127                std::ostream & operator<<( std::ostream & out, const PrintSet<ObjectSet> & set) {
    125128                        out << "{ ";
    126129                        for ( ObjectDecl * obj : set.objs ) {
     
    192195                };
    193196
    194                 class FixInit {
     197                class FixInit : public WithStmtsToAdd {
    195198                  public:
    196199                        /// expand each object declaration to use its constructor after it is declared.
     
    200203
    201204                        std::list< Declaration * > staticDtorDecls;
    202                         std::list< Statement * > stmtsToAddAfter; // found by PassVisitor
    203205                };
    204206
     
    726728                                                // static bool __objName_uninitialized = true
    727729                                                BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool );
    728                                                 SingleInit * boolInitExpr = new SingleInit( new ConstantExpr( Constant::from_int( 1 ) ), noDesignators );
     730                                                SingleInit * boolInitExpr = new SingleInit( new ConstantExpr( Constant::from_int( 1 ) ) );
    729731                                                ObjectDecl * isUninitializedVar = new ObjectDecl( objDecl->get_mangleName() + "_uninitialized", Type::StorageClasses( Type::Static ), LinkageSpec::Cforall, 0, boolType, boolInitExpr );
    730732                                                isUninitializedVar->fixUniqueId();
     
    747749
    748750                                                Statement * dtor = ctorInit->get_dtor();
    749                                                 objDecl->set_init( NULL );
    750                                                 ctorInit->set_ctor( NULL );
     751                                                objDecl->set_init( nullptr );
     752                                                ctorInit->set_ctor( nullptr );
    751753                                                ctorInit->set_dtor( nullptr );
    752754                                                if ( dtor ) {
     
    801803                                                } else {
    802804                                                        stmtsToAddAfter.push_back( ctor );
    803                                                         objDecl->set_init( NULL );
    804                                                         ctorInit->set_ctor( NULL );
     805                                                        objDecl->set_init( nullptr );
     806                                                        ctorInit->set_ctor( nullptr );
    805807                                                }
    806808                                        } // if
    807809                                } else if ( Initializer * init = ctorInit->get_init() ) {
    808810                                        objDecl->set_init( init );
    809                                         ctorInit->set_init( NULL );
     811                                        ctorInit->set_init( nullptr );
    810812                                } else {
    811813                                        // no constructor and no initializer, which is okay
    812                                         objDecl->set_init( NULL );
     814                                        objDecl->set_init( nullptr );
    813815                                } // if
    814816                                delete ctorInit;
     
    818820
    819821                void ObjDeclCollector::visit( CompoundStmt * compoundStmt ) {
    820                         std::set< ObjectDecl * > prevVars = curVars;
     822                        ObjectSet prevVars = curVars;
    821823                        Parent::visit( compoundStmt );
    822824                        curVars = prevVars;
     
    826828                        // keep track of all variables currently in scope
    827829                        if ( ObjectDecl * objDecl = dynamic_cast< ObjectDecl * > ( stmt->get_decl() ) ) {
    828                                 curVars.insert( objDecl );
     830                                curVars.push_back( objDecl );
    829831                        } // if
    830832                        Parent::visit( stmt );
     
    941943                        )
    942944                        if ( ! diff.empty() ) {
     945                                // create an auxilliary set for fast lookup -- can't make diff a set, because diff ordering should be consistent for error messages.
     946                                std::unordered_set<ObjectDecl *> needsDestructor( diff.begin(), diff.end() );
     947
    943948                                // go through decl ordered list of objectdecl. for each element that occurs in diff, output destructor
    944949                                OrderedDecls ordered;
    945950                                for ( OrderedDecls & rdo : reverseDeclOrder ) {
    946951                                        // add elements from reverseDeclOrder into ordered if they occur in diff - it is key that this happens in reverse declaration order.
    947                                         copy_if( rdo.begin(), rdo.end(), back_inserter( ordered ), [&]( ObjectDecl * objDecl ) { return diff.count( objDecl ); } );
     952                                        copy_if( rdo.begin(), rdo.end(), back_inserter( ordered ), [&]( ObjectDecl * objDecl ) { return needsDestructor.count( objDecl ); } );
    948953                                } // for
    949954                                insertDtors( ordered.begin(), ordered.end(), back_inserter( stmtsToAdd ) );
Note: See TracChangeset for help on using the changeset viewer.