Changeset dcd73d1 for src/InitTweak


Ignore:
Timestamp:
Sep 5, 2016, 9:49:53 AM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
621701d
Parents:
b16898e
Message:

add error checking for managed object's initializer depth

Location:
src/InitTweak
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/GenInit.cc

    rb16898e rdcd73d1  
    245245                        // constructed objects cannot be designated
    246246                        if ( isDesignated( objDecl->get_init() ) ) throw SemanticError( "Cannot include designations in the initializer for a managed Object. If this is really what you want, then initialize with @=.", objDecl );
    247                         // xxx - constructed objects should not have initializers nested too deeply
     247                        // constructed objects should not have initializers nested too deeply
     248                        if ( ! checkInitDepth( objDecl ) ) throw SemanticError( "Managed object's initializer is too deep ", objDecl );
    248249
    249250                        // call into genImplicitCall from Autogen.h to generate calls to ctor/dtor
  • src/InitTweak/InitTweak.cc

    rb16898e rdcd73d1  
    2323                };
    2424
     25                class InitDepthChecker : public Visitor {
     26                public:
     27                        bool depthOkay = true;
     28                        Type * type;
     29                        int curDepth = 0, maxDepth = 0;
     30                        InitDepthChecker( Type * type ) : type( type ) {
     31                                Type * t = type;
     32                                while ( ArrayType * at = dynamic_cast< ArrayType * >( t ) ) {
     33                                        maxDepth++;
     34                                        t = at->get_base();
     35                                }
     36                                maxDepth++;
     37                        }
     38                        virtual void visit( ListInit * listInit ) {
     39                                curDepth++;
     40                                if ( curDepth > maxDepth ) depthOkay = false;
     41                                Visitor::visit( listInit );
     42                                curDepth--;
     43                        }
     44                };
     45
    2546                class InitFlattener : public Visitor {
    2647                        public:
     
    5374                maybeAccept( init, finder );
    5475                return finder.hasDesignations;
     76        }
     77
     78        bool checkInitDepth( ObjectDecl * objDecl ) {
     79                InitDepthChecker checker( objDecl->get_type() );
     80                maybeAccept( objDecl->get_init(), checker );
     81                return checker.depthOkay;
    5582        }
    5683
  • src/InitTweak/InitTweak.h

    rb16898e rdcd73d1  
    4040        /// True if the Initializer contains designations
    4141        bool isDesignated( Initializer * init );
     42
     43        /// True if the ObjectDecl's Initializer nesting level is not deeper than the depth of its
     44        /// type, where the depth of its type is the number of nested ArrayTypes + 1
     45        bool checkInitDepth( ObjectDecl * objDecl );
    4246
    4347  /// Non-Null if expr is a call expression whose target function is intrinsic
Note: See TracChangeset for help on using the changeset viewer.