Changeset d3d54b3


Ignore:
Timestamp:
Feb 5, 2025, 1:45:29 PM (7 months ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
ab94c37
Parents:
92aeae1
Message:

Eliminate warnings due to sizeof returning type inconsistent with %zu. Fix #269.

Test

nowarn/printf-sizeof

: Activate direct test of the bug.

Tests

alloc-ERROR
extension
array-collections/dimexpr-match-c-ERRS

: Accommodate incidental changes in error-message text or generated code.

Files:
2 added
9 edited
1 moved

Legend:

Unmodified
Added
Removed
  • src/AST/Expr.cpp

    r92aeae1 rd3d54b3  
    2626#include "Stmt.hpp"
    2727#include "Type.hpp"
     28#include "Util.hpp"                // for TranslationDeps
    2829#include "TypeSubstitution.hpp"
    2930#include "Common/Utility.hpp"
     
    281282
    282283SizeofExpr::SizeofExpr( const CodeLocation & loc, const Type * t )
    283 : Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), type( t ) {}
     284: Expr( loc, ast::TranslationDeps::getSizeType() ), type( t ) {}
    284285
    285286// --- AlignofExpr
    286287
    287288AlignofExpr::AlignofExpr( const CodeLocation & loc, const Type * t )
    288 : Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), type( t ) {}
     289: Expr( loc, ast::TranslationDeps::getSizeType() ), type( t ) {}
    289290
    290291// --- CountofExpr
    291292
    292293CountofExpr::CountofExpr( const CodeLocation & loc, const Type * t )
    293 : Expr( loc, new BasicType( BasicKind::LongUnsignedInt) ), type( t ) {}
     294: Expr( loc, ast::TranslationDeps::getSizeType() ), type( t ) {}
    294295
    295296// --- OffsetofExpr
    296297
    297298OffsetofExpr::OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem )
    298 : Expr( loc, new BasicType{ BasicKind::LongUnsignedInt } ), type( ty ), member( mem ) {
     299: Expr( loc, ast::TranslationDeps::getSizeType() ), type( ty ), member( mem ) {
    299300        assert( type );
    300301        assert( member );
     
    305306OffsetPackExpr::OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty )
    306307: Expr( loc, new ArrayType{
    307         new BasicType{ BasicKind::LongUnsignedInt }, nullptr, FixedLen, DynamicDim }
     308        ast::TranslationDeps::getSizeType(), nullptr, FixedLen, DynamicDim }
    308309), type( ty ) {
    309310        assert( type );
  • src/AST/Type.hpp

    r92aeae1 rd3d54b3  
    344344struct TypeEnvKey;
    345345
    346 /// instance of named type alias (typedef or variable)
     346/// instance of named type alias (typedef, variable, or even, just after parsing, the name of a struct)
    347347class TypeInstType final : public BaseInstType {
    348348public:
  • src/AST/Util.cpp

    r92aeae1 rd3d54b3  
    2222#include "Common/Utility.hpp"
    2323#include "GenPoly/ScopedSet.hpp"
     24#include "Decl.hpp"
     25#include "Type.hpp"
    2426
    2527#include <vector>
     
    382384}
    383385
     386namespace {
     387        const TranslationUnit * transUnit = 0;
     388}
     389
     390void TranslationDeps::evolve( TranslationUnit & u ) {
     391        transUnit = &u;
     392}
     393
     394const ast::Type * TranslationDeps::getSizeType() {
     395        static const ast::Type * zd_abstract = new TypeInstType{ "size_t", TypeDecl::Kind::Dtype };
     396        static const ast::Type * ld_concrete = new BasicType( BasicKind::LongUnsignedInt );
     397        if ( ! transUnit ) {
     398                // early state
     399                // as if `size_t` in program text were freshly parsed
     400                return zd_abstract;
     401        } else if ( transUnit->global.sizeType ) {
     402                // late state, normal run
     403                // whatever size_t was defined as
     404                return transUnit->global.sizeType;
     405        } else {
     406                // late state, no prelude (-n)
     407                // placeholder: cfa-cpp is being used experimentally, stay out of the way
     408                return ld_concrete;
     409        }
     410}
     411
     412
    384413} // namespace ast
  • src/AST/Util.hpp

    r92aeae1 rd3d54b3  
    1616#pragma once
    1717
     18#include "Fwd.hpp"
     19
    1820namespace ast {
    1921
     
    2224/// Check anything that should always be true of the AST between passes.
    2325/// Insert this whenever you want additional debugging checks.
    24 void checkInvariants( TranslationUnit & transUnit );
     26void checkInvariants( TranslationUnit & );
     27
     28/// Maintains an AST-module state for contextual information needed in
     29/// ast::* implementations, notably constructors:
     30///    early: while parsing, use bootstrap versions
     31///    late: once a whole TranslationUnit exists, use its answers
     32/// When the program is in the later state, ast::* construcors effectively get
     33/// the benefit of WithTranslationUnit, without having to pass them one.
     34class TranslationDeps {
     35
     36    TranslationDeps() = delete;
     37
     38    friend class SizeofExpr;
     39    friend class AlignofExpr;
     40    friend class CountofExpr;
     41    friend class OffsetofExpr;
     42    friend class OffsetPackExpr;
     43
     44    /// Appropriate return type for built-in expressions that report on sizes
     45    static const Type * getSizeType();
     46
     47  public:
     48    /// Transition from early to late states
     49    static void evolve( TranslationUnit & );
     50};
    2551
    2652}
  • src/main.cpp

    r92aeae1 rd3d54b3  
    199199                Stats::Time::StopBlock();
    200200
     201                ast::TranslationDeps::evolve( transUnit );
     202
    201203                PASS( "Hoist Type Decls", Validate::hoistTypeDecls, transUnit );
    202204
  • tests/.expect/extension.x86.txt

    r92aeae1 rd3d54b3  
    120120static inline void _X12_constructorFv_U1UU1U_autogen___1(__attribute__ ((unused)) union U *_X4_dstU1U_1, __attribute__ ((unused)) union U _X4_srcU1U_1){
    121121    {
    122         ((void)__builtin_memcpy(((void *)_X4_dstU1U_1), ((const void *)(&_X4_srcU1U_1)), sizeof(union U )));
     122        ((void)__builtin_memcpy(((void *)_X4_dstU1U_1), ((const void *)(&_X4_srcU1U_1)), ((unsigned long int )sizeof(union U ))));
    123123    }
    124124
     
    129129    __attribute__ ((unused)) union U _X4_retU1U_1;
    130130    {
    131         ((void)__builtin_memcpy(((void *)_X4_dstU1U_1), ((const void *)(&_X4_srcU1U_1)), sizeof(union U )));
     131        ((void)__builtin_memcpy(((void *)_X4_dstU1U_1), ((const void *)(&_X4_srcU1U_1)), ((unsigned long int )sizeof(union U ))));
    132132    }
    133133
     
    140140static inline void _X12_constructorFv_U1Ui_autogen___1(__attribute__ ((unused)) union U *_X4_dstU1U_1, signed int _X1ai_1){
    141141    {
    142         ((void)__builtin_memcpy(((void *)_X4_dstU1U_1), ((const void *)(&_X1ai_1)), sizeof(signed int )));
     142        ((void)__builtin_memcpy(((void *)_X4_dstU1U_1), ((const void *)(&_X1ai_1)), ((unsigned long int )sizeof(signed int ))));
    143143    }
    144144
  • tests/array-collections/.expect/dimexpr-match-c-ERRS.x86.txt

    r92aeae1 rd3d54b3  
    208208  Address of:
    209209    Name: a  InitAlternative: pointer to array of float with dimension of Generated Cast of:
    210     Sizeof Expression on: instance of type dim7 (not function type)
    211     ... with resolved type:
    212       unsigned long int
    213   ... to:
    214     unsigned int
    215   ... with resolved type:
    216     unsigned int
    217 array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
    218   Address of:
    219     Name: a  InitAlternative: pointer to array of float with dimension of Generated Cast of:
    220     Sizeof Expression on: instance of type dim7 (not function type)
    221     ... with resolved type:
    222       unsigned long int
    223   ... to:
    224     unsigned int
    225   ... with resolved type:
    226     unsigned int
    227 array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
    228   Address of:
    229     Name: a  InitAlternative: pointer to array of float with dimension of Generated Cast of:
    230     Sizeof Expression on: instance of type dim7 (not function type)
    231     ... with resolved type:
    232       unsigned long int
    233   ... to:
    234     unsigned int
    235   ... with resolved type:
    236     unsigned int
    237 array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
    238   Address of:
    239     Name: a  InitAlternative: pointer to array of float with dimension of Generated Cast of:
    240     Sizeof Expression on: instance of type dim7 (not function type)
    241     ... with resolved type:
    242       unsigned long int
    243   ... to:
    244     unsigned int
    245   ... with resolved type:
    246     unsigned int
    247 array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
    248   Address of:
    249     Name: a  InitAlternative: pointer to array of float with dimension of Generated Cast of:
    250     Sizeof Expression on: instance of type dim7 (not function type)
    251     ... with resolved type:
    252       unsigned long int
    253   ... to:
    254     unsigned int
    255   ... with resolved type:
    256     unsigned int
    257 array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
    258   Address of:
    259     Name: a  InitAlternative: pointer to array of float with dimension of Generated Cast of:
    260210    Variable Expression: enu7: const instance of enum __anonymous0 with body
    261211    ... with resolved type:
     
    307257array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
    308258  Address of:
     259    Name: a  InitAlternative: pointer to array of float with dimension of Sizeof Expression on: instance of type dim7 (not function type)
     260  ... with resolved type:
     261    unsigned int
     262array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
     263  Address of:
     264    Name: a  InitAlternative: pointer to array of float with dimension of Sizeof Expression on: instance of type dim7 (not function type)
     265  ... with resolved type:
     266    unsigned int
     267array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
     268  Address of:
     269    Name: a  InitAlternative: pointer to array of float with dimension of Sizeof Expression on: instance of type dim7 (not function type)
     270  ... with resolved type:
     271    unsigned int
     272array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
     273  Address of:
     274    Name: a  InitAlternative: pointer to array of float with dimension of Sizeof Expression on: instance of type dim7 (not function type)
     275  ... with resolved type:
     276    unsigned int
     277array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
     278  Address of:
     279    Name: a  InitAlternative: pointer to array of float with dimension of Sizeof Expression on: instance of type dim7 (not function type)
     280  ... with resolved type:
     281    unsigned int
     282array-collections/dimexpr-match-c.cfa:49:1 error: Invalid application of existing declaration(s) in expression Untyped Init Expression
     283  Address of:
    309284    Name: a  InitAlternative: pointer to variable length array of float with dimension of Generated Cast of:
    310285    Variable Expression: _array_dim16: const unsigned int
  • tests/nowarn/.expect/printf-sizeof.txt

    r92aeae1 rd3d54b3  
    221
    3310
     40 1 2 3 4 5 6 7 8 9
  • tests/nowarn/printf-sizeof.cfa

    r92aeae1 rd3d54b3  
    1111forall( [N] )
    1212void mary( array(char, N) & ) {
    13     printf( "%zu\n", N );   
    14   #if defined TRY_TRAC_269_REMAINDER
    15         for ( i; N ) {
     13    printf( "%zu\n", N );
     14    for ( i; N ) {
    1615        if (i > 0) printf(" ");
    17         printf( "%zu", i); // FIX ME: i still getting type long unsigned int on -m32
    18         }
     16        printf( "%zu", i);
     17    }
    1918    printf("\n");
    20   #endif
    2119}
    2220
Note: See TracChangeset for help on using the changeset viewer.