Skip to content

Commit

Permalink
cast const ptr checks:
Browse files Browse the repository at this point in the history
- Add conversion option AST_CONVOPT_DONTWARNCONST.
- Assume we always want to know that constness has changed.
- For pointers of different indirection levels, adjust the const mask when comparing
  • Loading branch information
jayrm committed Aug 5, 2018
1 parent 7185995 commit 03f8a7f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
48 changes: 32 additions & 16 deletions src/compiler/ast-node-conv.bas
Expand Up @@ -331,20 +331,16 @@ function astNewCONV _
n->cast.doconv = FALSE
n->cast.do_convfd2fs = FALSE

'' Discarding const qualifier bits ?
'' TODO: reuse symbCheckConstAssign()?
'' TODO: merge with hCheckPtr()?

n->cast.convconst = true

if( (options and AST_CONVOPT_DONTCHKPTR) = 0 ) then
if( n->cast.convconst ) then
if( fbPdCheckIsSet( FB_PDCHECK_CONSTNESS ) ) then
errReportWarn( FB_WARNINGMSG_CONSTQUALIFIERDISCARDED )
end if
'' data types and levels of pointer inderection are the same,
'' always record this as const conversion
n->cast.convconst = TRUE

if( (options and AST_CONVOPT_DONTWARNCONST) = 0 ) then
if( fbPdCheckIsSet( FB_PDCHECK_CONSTNESS ) ) then
'' TODO: return this warning to the caller?
errReportWarn( FB_WARNINGMSG_CONSTQUALIFIERDISCARDED )
end if
end if

end if
else
n = l
Expand Down Expand Up @@ -490,13 +486,33 @@ function astNewCONV _
n->cast.do_convfd2fs = FALSE
n->cast.convconst = FALSE

'' Discarding const qualifier bits ?
'' Discarding/changing const qualifier bits ?
'' TODO: reuse symbCheckConstAssign()?
'' TODO: merge with hCheckPtr()?

if( typeIsPtr( ldtype ) and typeIsPtr( to_dtype ) ) then
n->cast.convconst = ( typeGetConstMask( ldtype ) <> typeGetConstMask( to_dtype ) )
if( (options and AST_CONVOPT_DONTCHKPTR) = 0 ) then
if( n->cast.convconst ) then

var cbits = typeGetConstMask( ldtype )
var to_cbits = typeGetConstMask( to_dtype )

'' differing levels of inderection?
if( typeGetPtrCnt( ldtype ) <> typeGetPtrCnt( to_dtype ) ) then

'' increasing? check if we changed/gained any const bits
if( typeGetPtrCnt( to_dtype ) > typeGetPtrCnt( ldtype ) ) then
cbits shl= (typeGetPtrCnt( to_dtype ) - typeGetPtrCnt( ldtype ))

'' decreasing. check if we changed/lost any const bits
else
to_cbits shl= (typeGetPtrCnt( ldtype ) - typeGetPtrCnt( to_dtype ))

end if
end if

n->cast.convconst = ( cbits <> to_cbits )

if( n->cast.convconst ) then
if( (options and AST_CONVOPT_DONTWARNCONST) = 0 ) then
if( fbPdCheckIsSet( FB_PDCHECK_CONSTNESS ) ) then
errReportWarn( FB_WARNINGMSG_CONSTQUALIFIERDISCARDED )
end if
Expand Down
1 change: 1 addition & 0 deletions src/compiler/ast.bi
Expand Up @@ -530,6 +530,7 @@ enum AST_CONVOPT
AST_CONVOPT_CHECKSTR = &h2
AST_CONVOPT_PTRONLY = &h4
AST_CONVOPT_DONTCHKPTR = &h8
AST_CONVOPT_DONTWARNCONST = &h10
end enum

declare function astNewCONV _
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/rtl-mem.bas
Expand Up @@ -161,10 +161,9 @@ function rtlNullPtrCheck _
proc = astNewCALL( PROCLOOKUP( NULLPTRCHK ) )

'' ptr
'' we are CASTing to any ptr, so quiet the ptr checks with AST_CONVOPT_DONTCHKPTR
'' astNewCONV => hCheckPtr() will allow any ptr even without this option.
'' we are forcing a CAST to any ptr, so quiet the const checks with AST_CONVOPT_DONTWARNCONST
if( astNewARG( proc, _
astNewCONV( typeAddrOf( FB_DATATYPE_VOID ), NULL, p, AST_CONVOPT_DONTCHKPTR ), _
astNewCONV( typeAddrOf( FB_DATATYPE_VOID ), NULL, p, AST_CONVOPT_DONTWARNCONST ), _
typeAddrOf( FB_DATATYPE_VOID ) ) = NULL ) then
exit function
end if
Expand Down

0 comments on commit 03f8a7f

Please sign in to comment.