Ignore:
Timestamp:
Sep 9, 2024, 6:16:09 PM (5 weeks ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
aa14aafe
Parents:
d93b813 (diff), f5dbc8d (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' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Validate/InitializerLength.cpp

    rd93b813 r3d618a0  
    2929///   int x[] = { 1, 2, 3 };
    3030///   int y[][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
     31///   char z[] = "hello";
    3132/// here x and y are known at compile-time to have length 3, so change this into
    3233///   int x[3] = { 1, 2, 3 };
    3334///   int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
     35///   char z[6] = "hello";
    3436struct InitializerLength {
    3537        const ast::ObjectDecl * previsit( const ast::ObjectDecl * decl );
    3638};
    3739
     40ast::ConstantExpr * makeDimension( const ast::ObjectDecl * decl ) {
     41        if ( auto init = decl->init.as<ast::ListInit>() ) {
     42                return ast::ConstantExpr::from_ulong( decl->location, init->size() );
     43        } else if ( auto init = decl->init.as<ast::SingleInit>() ) {
     44                if ( auto constant = init->value.as<ast::ConstantExpr>() ) {
     45                        if ( auto type = constant->result.as<ast::ArrayType>() ) {
     46                                if ( auto dim = type->dimension.as<ast::ConstantExpr>() ) {
     47                                        ast::ConstantExpr * dimension = ast::deepCopy( dim );
     48                                        dimension->location = decl->location;
     49                                        return dimension;
     50                                }
     51                        }
     52                }
     53        }
     54        return nullptr;
     55}
     56
    3857const ast::ObjectDecl * InitializerLength::previsit( const ast::ObjectDecl * decl ) {
    3958        if ( auto type = decl->type.as<ast::ArrayType>() ) {
    4059                if ( type->dimension ) return decl;
    41                 if ( auto init = decl->init.as<ast::ListInit>() ) {
     60                if ( auto dimension = makeDimension( decl ) ) {
    4261                        ast::ObjectDecl * mutDecl = ast::mutate( decl );
    4362                        ast::ArrayType * mutType = ast::mutate( type );
    44                         mutType->dimension = ast::ConstantExpr::from_ulong(
    45                                 mutDecl->location, init->size() );
     63                        mutType->dimension = dimension;
    4664                        mutDecl->type = mutType;
    4765                        return mutDecl;
Note: See TracChangeset for help on using the changeset viewer.