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

git-svn-id: trunk@41063 -
This commit is contained in:
Mattias Gaertner 2019-01-24 22:38:43 +00:00
parent 1a59a4a4a3
commit 4ab30223d3
3 changed files with 32 additions and 1 deletions

View File

@ -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 }

View File

@ -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);

View File

@ -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);