Advertisement
Guest User

Untitled

a guest
Mar 6th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. open Pretty
  2. open Cil
  3.  
  4. let isSignedType (t: typ): bool =
  5. match (unrollType t) with
  6. | TInt(ik,_) -> isSigned ik;
  7. |_ -> true;
  8.  
  9. class chkIntVisitor = object
  10. inherit nopCilVisitor
  11.  
  12. method vexpr (e: exp) : exp visitAction =
  13. (* only check if result is a signed type (which would be
  14. * able to hold the correct signed result) *)
  15. match e with
  16. | CastE( castedType, BinOp(_, e1, e2, binOpType) ) when (isSignedType castedType) -> begin
  17. let bitsCasted = bitsSizeOf castedType and bitsOp = bitsSizeOf binOpType in
  18. if (bitsCasted < bitsOp) then begin
  19. ignore(warn "cast looses significant bits %a(%d) -> %a(%d) in expr: %a\n"
  20. d_type binOpType bitsOp
  21. d_type castedType bitsCasted
  22. d_exp e);
  23. DoChildren;
  24. end
  25. else
  26. let t1 = typeOf (stripCasts e1) and t2 = typeOf (stripCasts e2) in
  27. match t1,t2 with
  28. |TInt(ik1,_),TInt(ik2,_) -> begin
  29. (* one of the operands is signed, but the binop result
  30. * is an unsigned type, while the castedType is a
  31. * signed type capable of holding the signed result *)
  32. if (isSigned ik1) != (isSigned ik2) &&
  33. not (isSignedType binOpType) && (not (isIntegralType castedType) ||
  34. (bitsCasted > bitsOp)) then
  35. ignore(warn "conversion looses sign in expression: %a ;
  36. operands:%a,%a -> op-result: %a(%d) -> casted: %a(%d)\n"
  37. d_exp e
  38. d_ikind ik1
  39. d_ikind ik2
  40. d_type binOpType
  41. bitsOp
  42. d_type castedType
  43. bitsCasted
  44. );
  45.  
  46. DoChildren;
  47. end
  48. | _ -> DoChildren;
  49. end
  50. | BinOp( (Lt|Le|Gt|Ge) , _, CastE( castedType, (SizeOf(_) | SizeOfE(_) | SizeOfStr(_)) ), _) | BinOp((Lt|Le|Gt|Ge), CastE( castedType, (SizeOf(_) | SizeOfE(_) | SizeOfStr(_)) ), _, _) when (isSignedType castedType) ->
  51. ignore(warn "sizeof expression is cast to signed type in expression: %a; type: %a
  52. if this is a bounds test, make sure you also test the lower bound!"
  53. d_exp e d_type castedType);
  54. DoChildren;
  55. |_ -> DoChildren
  56. end
  57.  
  58. let feature : featureDescr =
  59. { fd_name = "chkints";
  60. fd_enabled = ref false;
  61. fd_description = "check compatibilty of integer types";
  62. fd_extraopt = [];
  63. fd_doit =
  64. (function (f: file) ->
  65. let ciVisitor = new chkIntVisitor in
  66. visitCilFileSameGlobals ciVisitor f);
  67. fd_post_check = true;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement