Changeset 7959e56


Ignore:
Timestamp:
Feb 3, 2025, 1:27:20 PM (7 months ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
dfe8f78
Parents:
59fdd0d
Message:

Eliminate libcfa-build warnings of missing int-to-pointer casts.

Replace a zero_t variable use with literal 0 when it's an argument to an intrinsic and we're generating final C code. Partially revert e0330d2cd1a. Such intrinsics are initialization/assignment of pointers; using the variable implies a missing cast, while using literal 0 needs no cast.

CodeGenerator.hpp
CodeGenerator.cpp

Put attibute unused on all zero_t/one_t object decls. It is needed on those whose uses are rewritten by the rule above.

Generate.cpp

Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cpp

    r59fdd0d r7959e56  
    130130        // TODO: Which means the ast::Pass is just providing a default no visit?
    131131        visit_children = false;
     132        changeState_ArgToIntrinsic(false);
    132133}
    133134
     
    466467                if ( var->var->linkage == ast::Linkage::Intrinsic &&
    467468                                ( opInfo = operatorLookup( var->var->name ) ) ) {
     469                        changeState_ArgToIntrinsic(true);
    468470                        auto arg = expr->args.begin();
    469471                        switch ( opInfo->type ) {
     
    558560        if ( auto name = expr->func.as<ast::NameExpr>() ) {
    559561                if ( const OperatorInfo * opInfo = operatorLookup( name->name ) ) {
     562                        changeState_ArgToIntrinsic(true);
    560563                        auto arg = expr->args.begin();
    561564                        switch ( opInfo->type ) {
     
    743746        extension( expr );
    744747        const OperatorInfo * opInfo;
    745         if ( expr->var->linkage == ast::Linkage::Intrinsic
     748        if ( visitingArgToIntrinsic
     749                        && options.genC
     750                        && dynamic_cast<ast::ZeroType const *>( expr->var->get_type() ) ) {
     751                // int * p; p = 0;               ==>  ?=?( p, (zero_t){} );  ==>  p = 0;
     752                // void f( zero_t z ) { g(z); }  ==>  g(z);                  ==>  g(z);
     753                // (we are at the last '==>')
     754                output << "0";
     755        } else if ( expr->var->linkage == ast::Linkage::Intrinsic
    746756                        && ( opInfo = operatorLookup( expr->var->name ) )
    747757                        && opInfo->type == OT_CONSTANT ) {
  • src/CodeGen/CodeGenerator.hpp

    r59fdd0d r7959e56  
    181181        void handleTypedef( ast::NamedTypeDecl const * type );
    182182        std::string mangleName( ast::DeclWithType const * decl );
     183
     184        bool nextVisitedNodeIsArgToIntrinsic = false;
     185        bool visitingArgToIntrinsic = false;
     186        void changeState_ArgToIntrinsic( bool newValue ) {
     187                GuardValue( visitingArgToIntrinsic ) = nextVisitedNodeIsArgToIntrinsic;
     188                GuardValue( nextVisitedNodeIsArgToIntrinsic ) = newValue;
     189        }
    183190};
    184191
  • src/CodeGen/Generate.cpp

    r59fdd0d r7959e56  
    4646                }
    4747        };
     48
     49        struct ZeroOneObjectHider final {
     50                ast::ObjectDecl const * postvisit( ast::ObjectDecl const * decl ) {
     51                        if ( decl->type.as<ast::ZeroType>() || decl->type.as<ast::OneType>() ) {
     52                                ast::ObjectDecl * mutDecl = ast::mutate( decl );
     53                                mutDecl->attributes.push_back( new ast::Attribute( "unused" ) );
     54                                return mutDecl;
     55                        }
     56                        return decl;
     57                }
     58        };
    4859} // namespace
    4960
     
    5263        erase_if( translationUnit.decls, shouldClean );
    5364        ast::Pass<TreeCleaner>::run( translationUnit );
     65        ast::Pass<ZeroOneObjectHider>::run( translationUnit );
    5466
    5567        ast::Pass<CodeGenerator> cgv( os,
  • tests/.expect/zero_one.txt

    r59fdd0d r7959e56  
    33It's a Number!
    442 2
     50 0
     642 42
     70 0
     81 1
     942 42
     101 1
     11zero true
     12zero true
     13one false
     14one false
  • tests/Makefile.am

    r59fdd0d r7959e56  
    354354        -cp ${test} ${abspath ${@}}
    355355
     356zero_one-ERR1 : zero_one.cfa ${CFACCBIN}
     357        ${CFACOMPILE_SYNTAX} -DERR1
     358        -cp ${test} ${abspath ${@}}
     359
    356360ctrl-flow/loop_else : ctrl-flow/loop_else.cfa ${CFACCBIN}
    357361        ${CC} ${AM_CFLAGS} -Wno-superfluous-else $< -o $@
  • tests/zero_one.cfa

    r59fdd0d r7959e56  
    3939}
    4040
     41void testCompats() {
     42    zero_t zero = 0;
     43    one_t one = 1;
     44
     45    int x = 0;
     46        int xx = zero;
     47
     48        sout | x | xx;
     49
     50        x = xx = 42;
     51        sout | x | xx;
     52
     53        x = 0;
     54        xx = zero;
     55        sout | x | xx;
     56
     57        int y = 1;
     58        int yy = one;
     59
     60        sout | y | yy;
     61
     62        y = yy = 42;
     63        sout | y | yy;
     64
     65        y = 1;
     66        yy = one;
     67        sout | y | yy;
     68
     69        void z_helper( int * p, zero_t z ) {
     70                p = z;  // expect z not reported unused here; expect no missing cast from -Wint-conversion
     71                sout | "zero" | (bool) (p == 0);
     72        }
     73
     74        void z_call( int * p, zero_t z ) {
     75                z_helper(p, z);
     76        }
     77
     78        void o_helper( int * p, one_t o ) {
     79          #ifdef ERR1
     80                p = o;
     81          #else
     82                (void) x;  (void) o;
     83          #endif
     84                sout | "one" | (bool) (p == 0);
     85        }
     86
     87        void o_call( int * p, one_t o ) {
     88                o_helper(p, o);
     89        }
     90
     91        z_call( &x, 0 );
     92        z_call( &x, zero );
     93
     94        o_call( &x, 1 );
     95        o_call( &x, one );
     96}
     97
    4198int main() {
    4299        testOverloads();
    43100        testInitAssignQueryIncrement();
     101        testCompats();
    44102        return 0;
    45103}
Note: See TracChangeset for help on using the changeset viewer.