Skip to content

Commit 8ac6d65

Browse files
committed
Check if the RHS of the bitshift is an int before casting instead of always casting to int
1 parent 7eccea4 commit 8ac6d65

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private void VisitBinaryOperator(BinaryOperator binaryOperator)
4242
outputBuilder.Write(binaryOperator.OpcodeStr);
4343
outputBuilder.Write(' ');
4444

45-
if (binaryOperator.IsShiftOp || binaryOperator.IsShiftAssignOp)
45+
if ((binaryOperator.IsShiftOp || binaryOperator.IsShiftAssignOp) && binaryOperator.RHS.Type.Kind != CXType_Int)
4646
{
4747
// RHS of shift operation in C# must be an int
4848
outputBuilder.Write("(int)");

tests/ClangSharp.PInvokeGenerator.UnitTests/CTest.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,13 @@ public static partial class Methods
284284
public Task UnsignedIntBitshiftTest()
285285
{
286286
var inputContents = @"#define LEFT 1 << 1U
287-
#define RIGHT 1 >> 1U";
287+
#define RIGHT 1 >> 1U
288+
#define INT 1 << 1
289+
#define LONG 1 << 1L
290+
#define LONGLONG 1 << 1LL
291+
#define ULONG 1 << 1UL
292+
#define ULONGLONG 1 << 1ULL
293+
";
288294

289295
var expectedOutputContents = @"namespace ClangSharp.Test
290296
{
@@ -295,6 +301,21 @@ public static partial class Methods
295301
296302
[NativeTypeName(""#define RIGHT 1 >> 1U"")]
297303
public const int RIGHT = 1 >> (int)(1U);
304+
305+
[NativeTypeName(""#define INT 1 << 1"")]
306+
public const int INT = 1 << 1;
307+
308+
[NativeTypeName(""#define LONG 1 << 1L"")]
309+
public const int LONG = 1 << (int)(1);
310+
311+
[NativeTypeName(""#define LONGLONG 1 << 1LL"")]
312+
public const int LONGLONG = 1 << (int)(1L);
313+
314+
[NativeTypeName(""#define ULONG 1 << 1UL"")]
315+
public const int ULONG = 1 << (int)(1U);
316+
317+
[NativeTypeName(""#define ULONGLONG 1 << 1ULL"")]
318+
public const int ULONGLONG = 1 << (int)(1UL);
298319
}
299320
}
300321
";

0 commit comments

Comments
 (0)