Changeset 4b97770 for src


Ignore:
Timestamp:
Jan 3, 2018, 10:40:02 AM (6 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, 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:
6138d0f, b834e98
Parents:
be9aa0f
Message:

Add error handling for C-style initialization of VLAs

Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/CurrentObject.cc

    rbe9aa0f r4b97770  
    125125                        base = at->get_base();
    126126                        memberIter = createMemberIterator( base );
     127                        if ( at->isVarLen ) throw SemanticError( "VLA initialization does not support @=", at );
    127128                        setSize( at->get_dimension() );
    128129                }
     
    135136                void setSize( Expression * expr ) {
    136137                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    137                                 size = constExpr->intValue();
    138                                 isVLA = false;
    139                                 PRINT( std::cerr << "array type with size: " << size << std::endl; )
     138                                try {
     139                                        size = constExpr->intValue();
     140                                        PRINT( std::cerr << "array type with size: " << size << std::endl; )
     141                                } catch ( SemanticError & ) {
     142                                        throw SemanticError( "Constant expression of non-integral type in array dimension: ", expr );
     143                                }
    140144                        }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    141145                                setSize( castExpr->get_arg() ); // xxx - need to perform the conversion specified by the cast
     
    145149                                        if ( inst->baseEnum->valueOf( varExpr->var, value ) ) {
    146150                                                size = value;
    147                                                 isVLA = false;
    148151                                        }
    149152                                }
     
    157160                        // need to permit integer-constant-expressions, including: integer constants, enumeration constants, character constants, sizeof expressions, _Alignof expressions, cast expressions
    158161                        if ( ConstantExpr * constExpr = dynamic_cast< ConstantExpr * >( expr ) ) {
    159                                 index = constExpr->intValue();
     162                                try {
     163                                        index = constExpr->intValue();
     164                                } catch( SemanticError & ) {
     165                                        throw SemanticError( "Constant expression of non-integral type in array designator: ", expr );
     166                                }
    160167                        } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( expr ) ) {
    161168                                setPosition( castExpr->get_arg() );
     
    186193                }
    187194
    188                 virtual operator bool() const { return ! isVLA && index < size; }
     195                virtual operator bool() const { return index < size; }
    189196
    190197                virtual MemberIterator & bigStep() {
     
    192199                        ++index;
    193200                        delete memberIter;
    194                         if ( ! isVLA && index < size ) memberIter = createMemberIterator( base );
     201                        if ( index < size ) memberIter = createMemberIterator( base );
    195202                        else memberIter = nullptr;
    196203                        return *this;
     
    239246                size_t index = 0;
    240247                size_t size = 0;
    241                 bool isVLA = true;
    242248                MemberIterator * memberIter = nullptr;
    243249        };
  • src/SymTab/Validate.cc

    rbe9aa0f r4b97770  
    754754                                throw SemanticError( "Cannot redefine typedef: " + tyDecl->name );
    755755                        }
    756                         // cannot redefine VLA typedefs
     756                        // Cannot redefine VLA typedefs. Note: this is slightly incorrect, because our notion of VLAs
     757                        // at this point in the translator is imprecise. In particular, this will disallow redefining typedefs
     758                        // with arrays whose dimension is an enumerator or a cast of a constant/enumerator. The effort required
     759                        // to fix this corner case likely outweighs the utility of allowing it.
    757760                        if ( isVariableLength( t1 ) || isVariableLength( t2 ) ) {
    758761                                throw SemanticError( "Cannot redefine typedef: " + tyDecl->name );
Note: See TracChangeset for help on using the changeset viewer.