Browse Source

pastojs: warn for bitwise and,or,xor with native(u)int

git-svn-id: trunk@41063 -
Mattias Gaertner 6 years ago
parent
commit
4ab30223d3

+ 2 - 0
packages/fcl-passrc/src/pasresolveeval.pas

@@ -180,6 +180,7 @@ const
   nDerivedXMustExtendASubClassY = 3114;
   nDefaultPropertyNotAllowedInHelperForX = 3115;
   nHelpersCannotBeUsedAsTypes = 3116;
+  nBitWiseOperationsAre32Bit = 3117;
 
   // using same IDs as FPC
   nVirtualMethodXHasLowerVisibility = 3250; // was 3050
@@ -307,6 +308,7 @@ resourcestring
   sDerivedXMustExtendASubClassY = 'Derived %s must extend a subclass of "%s" or the class itself';
   sDefaultPropertyNotAllowedInHelperForX = 'Default property not allowed in helper for %s';
   sHelpersCannotBeUsedAsTypes = 'helpers cannot be used as types';
+  sBitWiseOperationsAre32Bit = 'Bitwise operations are 32-bit';
 
 type
   { TResolveData - base class for data stored in TPasElement.CustomData }

+ 8 - 1
packages/pastojs/src/fppas2js.pp

@@ -6572,8 +6572,15 @@ begin
         eopXor:
           begin
           if aResolver<>nil then
+            begin
             UseBitwiseOp:=((LeftResolved.BaseType in btAllJSInteger)
-                       or (RightResolved.BaseType in btAllJSInteger))
+                       or (RightResolved.BaseType in btAllJSInteger));
+            if UseBitwiseOp
+                and (LeftResolved.BaseType in [btIntDouble,btUIntDouble])
+                and (RightResolved.BaseType in [btIntDouble,btUIntDouble]) then
+              aResolver.LogMsg(20190124233439,mtWarning,nBitWiseOperationsAre32Bit,
+                sBitWiseOperationsAre32Bit,[],El);
+            end
           else
             UseBitwiseOp:=(GetExpressionValueType(El.left,AContext)=jstNumber)
               or (GetExpressionValueType(El.right,AContext)=jstNumber);

+ 22 - 0
packages/pastojs/tests/tcmodules.pas

@@ -257,6 +257,7 @@ type
     Procedure TestInteger;
     Procedure TestIntegerRange;
     Procedure TestIntegerTypecasts;
+    Procedure TestBitwiseAndNativeIntWarn;
     Procedure TestCurrency;
     Procedure TestForBoolDo;
     Procedure TestForIntDo;
@@ -6072,6 +6073,27 @@ begin
     '']));
 end;
 
+procedure TTestModule.TestBitwiseAndNativeIntWarn;
+begin
+  StartProgram(false);
+  Add([
+  'var',
+  '  i,j: nativeint;',
+  'begin',
+  '  i:=i and j;',
+  '']);
+  ConvertProgram;
+  CheckSource('TestBitwiseAndNativeIntWarn',
+    LinesToStr([
+    'this.i = 0;',
+    'this.j = 0;',
+    '']),
+    LinesToStr([
+    '$mod.i = $mod.i & $mod.j;',
+    '']));
+  CheckHint(mtWarning,nBitWiseOperationsAre32Bit,sBitWiseOperationsAre32Bit);
+end;
+
 procedure TTestModule.TestCurrency;
 begin
   StartProgram(false);