-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from sstalle/bitwise-shift-checks
Added bitwise shift checks
- Loading branch information
Showing
11 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Sstalle\php7cc\NodeVisitor; | ||
|
||
use PhpParser\Node; | ||
|
||
class BitwiseShiftVisitor extends AbstractVisitor | ||
{ | ||
|
||
const MIN_INT_SIZE = 32; | ||
|
||
public function enterNode(Node $node) | ||
{ | ||
$isLeftShift = $node instanceof Node\Expr\BinaryOp\ShiftLeft; | ||
$isRightShift = $node instanceof Node\Expr\BinaryOp\ShiftRight; | ||
if (!$isLeftShift && !$isRightShift) { | ||
return; | ||
} | ||
|
||
$rightOperand = $node->right; | ||
if ($rightOperand instanceof Node\Expr\UnaryMinus && $rightOperand->expr instanceof Node\Scalar\LNumber | ||
&& $rightOperand->expr->value > 0 | ||
) { | ||
$this->addContextMessage( | ||
'Bitwise shift by a negative number', | ||
$node | ||
); | ||
} elseif ($rightOperand instanceof Node\Scalar\LNumber && $rightOperand->value >= static::MIN_INT_SIZE) { | ||
$this->addContextMessage( | ||
sprintf('Bitwise shift by %d bits', $rightOperand->value), | ||
$node | ||
); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
test/resource/bitwiseShift/bitwiseShiftByNegativeBinaryNumber.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Bitwise shift by a negative binary number | ||
----- | ||
<?php $a >> -0b10; | ||
----- | ||
Bitwise shift by a negative number | ||
----- | ||
<?php 1 << -0b10; | ||
----- | ||
Bitwise shift by a negative number | ||
----- | ||
<?php $a >> 0b0; | ||
----- | ||
|
||
----- | ||
<?php 1 << 0b0; | ||
----- | ||
|
||
----- | ||
<?php $a >> 0b1010; | ||
----- | ||
|
||
----- | ||
<?php 1 << 0b1010; | ||
----- | ||
|
25 changes: 25 additions & 0 deletions
25
test/resource/bitwiseShift/bitwiseShiftByNegativeDecimalNumber.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Bitwise shift by a negative decimal number | ||
----- | ||
<?php $a >> -2; | ||
----- | ||
Bitwise shift by a negative number | ||
----- | ||
<?php 1 << -2; | ||
----- | ||
Bitwise shift by a negative number | ||
----- | ||
<?php $a >> 0; | ||
----- | ||
|
||
----- | ||
<?php 1 << 0; | ||
----- | ||
|
||
----- | ||
<?php $a >> 10; | ||
----- | ||
|
||
----- | ||
<?php 1 << 10; | ||
----- | ||
|
25 changes: 25 additions & 0 deletions
25
test/resource/bitwiseShift/bitwiseShiftByNegativeHexadecimalNumber.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Bitwise shift by a negative hexadecimal number | ||
----- | ||
<?php $a >> -0x2; | ||
----- | ||
Bitwise shift by a negative number | ||
----- | ||
<?php 1 << -0x2; | ||
----- | ||
Bitwise shift by a negative number | ||
----- | ||
<?php $a >> 0x0; | ||
----- | ||
|
||
----- | ||
<?php 1 << 0x0; | ||
----- | ||
|
||
----- | ||
<?php $a >> 0xa; | ||
----- | ||
|
||
----- | ||
<?php 1 << 0xa; | ||
----- | ||
|
25 changes: 25 additions & 0 deletions
25
test/resource/bitwiseShift/bitwiseShiftByNegativeOctalNumber.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Bitwise shift by a negative octal number | ||
----- | ||
<?php $a >> -02; | ||
----- | ||
Bitwise shift by a negative number | ||
----- | ||
<?php 1 << -02; | ||
----- | ||
Bitwise shift by a negative number | ||
----- | ||
<?php $a >> 00; | ||
----- | ||
|
||
----- | ||
<?php 1 << 00; | ||
----- | ||
|
||
----- | ||
<?php $a >> 012; | ||
----- | ||
|
||
----- | ||
<?php 1 << 012; | ||
----- | ||
|
25 changes: 25 additions & 0 deletions
25
test/resource/bitwiseShift/bitwiseShiftLargerThanIntWidthBinary.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Bitwise shift by a binary number of bits greater or equal to minimum int size constant | ||
----- | ||
<?php $a >> 0b100000; | ||
----- | ||
Bitwise shift by 32 bits | ||
----- | ||
<?php 1 << 0b100000; | ||
----- | ||
Bitwise shift by 32 bits | ||
----- | ||
<?php $a >> 0b1000000; | ||
----- | ||
Bitwise shift by 64 bits | ||
----- | ||
<?php 1 << 0b1000000; | ||
----- | ||
Bitwise shift by 64 bits | ||
----- | ||
<?php $a >> 0b10000; | ||
----- | ||
|
||
----- | ||
<?php 1 << 0b10000; | ||
----- | ||
|
24 changes: 24 additions & 0 deletions
24
test/resource/bitwiseShift/bitwiseShiftLargerThanIntWidthDecimal.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Bitwise shift by a decimal number of bits greater or equal to minimum int size constant | ||
----- | ||
<?php $a >> 32; | ||
----- | ||
Bitwise shift by 32 bits | ||
----- | ||
<?php 1 << 32; | ||
----- | ||
Bitwise shift by 32 bits | ||
----- | ||
<?php $a >> 64; | ||
----- | ||
Bitwise shift by 64 bits | ||
----- | ||
<?php 1 << 64; | ||
----- | ||
Bitwise shift by 64 bits | ||
----- | ||
<?php $a >> 16; | ||
----- | ||
|
||
----- | ||
<?php 1 << 16; | ||
----- |
25 changes: 25 additions & 0 deletions
25
test/resource/bitwiseShift/bitwiseShiftLargerThanIntWidthHexadecimal.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Bitwise shift by a hexadecimal number of bits greater or equal to minimum int size constant | ||
----- | ||
<?php $a >> 0x20; | ||
----- | ||
Bitwise shift by 32 bits | ||
----- | ||
<?php 1 << 0x20; | ||
----- | ||
Bitwise shift by 32 bits | ||
----- | ||
<?php $a >> 0x40; | ||
----- | ||
Bitwise shift by 64 bits | ||
----- | ||
<?php 1 << 0x40; | ||
----- | ||
Bitwise shift by 64 bits | ||
----- | ||
<?php $a >> 0x10; | ||
----- | ||
|
||
----- | ||
<?php 1 << 0x10; | ||
----- | ||
|
24 changes: 24 additions & 0 deletions
24
test/resource/bitwiseShift/bitwiseShiftLargerThanIntWidthOctal.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Bitwise shift by an octal number of bits greater or equal to minimum int size constant | ||
----- | ||
<?php $a >> 040; | ||
----- | ||
Bitwise shift by 32 bits | ||
----- | ||
<?php 1 << 040; | ||
----- | ||
Bitwise shift by 32 bits | ||
----- | ||
<?php $a >> 0100; | ||
----- | ||
Bitwise shift by 64 bits | ||
----- | ||
<?php 1 << 0100; | ||
----- | ||
Bitwise shift by 64 bits | ||
----- | ||
<?php $a >> 020; | ||
----- | ||
|
||
----- | ||
<?php 1 << 020; | ||
----- |