Skip to content

Commit f72aae4

Browse files
committed
Merge remote-tracking branch 'dotnet/main'
2 parents 6721c65 + 4d4e062 commit f72aae4

File tree

12 files changed

+233
-119
lines changed

12 files changed

+233
-119
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,18 @@ cd artifacts/bin
8484
cmake -DCMAKE_INSTALL_PREFIX=../install -DLLVM_ENABLE_PROJECTS=clang -G "Visual Studio 17 2022" -A x64 -Thost=x64 ../../llvm
8585
```
8686

87-
You can then open `LLVM.sln` in Visual Studio, change the configuration to `Release` and build the `install` project.
87+
You can then open `LLVM.sln` in Visual Studio, change the configuration to `Release` and build the `INSTALL` project. You may need to build the `ALL_BUILD` first.
8888

8989
Afterwards, you can then build `libClangSharp` where the process followed is roughly:
9090
```cmd
9191
git clone https://github.com/dotnet/clangsharp
9292
cd clangsharp
9393
mkdir artifacts/bin/native
9494
cd artifacts/bin/native
95-
cmake -DCMAKE_INSTALL_PREFIX=../install -DPATH_TO_LLVM=../../../../llvm-project/artifacts/install -G "Visual Studio 17 2022" -A x64 -Thost=x64 ../../..
95+
cmake -DCMAKE_INSTALL_PREFIX=../install -DPATH_TO_LLVM=absolute/path/to/repos/llvm-project/artifacts/install -G "Visual Studio 17 2022" -A x64 -Thost=x64 ../../..
9696
```
9797

98-
You can then open `libClangSharp.sln` in Visual Studio, change the configuration to `Release` and build the `install` project.
98+
You can then open `libClangSharp.sln` in Visual Studio, change the configuration to `Release` and build the `INSTALL` project.
9999

100100
#### Linux
101101

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3121,6 +3121,7 @@ void VisitConstantOrIncompleteArrayFieldDecl(RecordDecl recordDecl, FieldDecl co
31213121

31223122
if (arraySize == 1)
31233123
{
3124+
code.AddUsingDirective("System.Runtime.CompilerServices");
31243125
code.Write("Unsafe.Add(ref e0, index)");
31253126
}
31263127
else

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,60 @@ private void VisitBinaryOperator(BinaryOperator binaryOperator)
4141
outputBuilder.Write(' ');
4242
outputBuilder.Write(binaryOperator.OpcodeStr);
4343
outputBuilder.Write(' ');
44-
Visit(binaryOperator.RHS);
44+
45+
if (binaryOperator.IsShiftOp || binaryOperator.IsShiftAssignOp)
46+
{
47+
// RHS of shift operation in C# must be an int
48+
49+
// Negative shifts are undefined behavior in C/C++, but still needs to have a compilable output
50+
var isNegated = binaryOperator.RHS is UnaryOperator { Opcode: CXUnaryOperator_Minus };
51+
var sign = isNegated ? -1 : 1;
52+
53+
var rhs = isNegated ? ((UnaryOperator)binaryOperator.RHS).SubExpr : binaryOperator.RHS;
54+
switch (rhs)
55+
{
56+
case IntegerLiteral literal:
57+
{
58+
var value = sign * literal.Value;
59+
if (value is > int.MaxValue or < int.MinValue)
60+
{
61+
// Literal is in int32 range
62+
outputBuilder.Write("(int)(");
63+
outputBuilder.Write(sign * literal.Value);
64+
outputBuilder.Write(")");
65+
}
66+
else
67+
{
68+
// Literal is not in int32 range
69+
outputBuilder.Write(sign * literal.Value);
70+
}
71+
72+
break;
73+
}
74+
// Already the correct type or implicitly castable
75+
case { Type.Kind: CXType_Int or CXType_UShort or CXType_Short or CXType_Char_U or CXType_Char_S or CXType_UChar or CXType_SChar }:
76+
case { Type.Kind: CXType_Long } when _config is { GenerateUnixTypes: false }:
77+
case { Type.Kind: CXType_Char16 } when _config is { GenerateDisableRuntimeMarshalling: false }:
78+
case { Type.Kind: CXType_WChar } when _config is { GenerateDisableRuntimeMarshalling: false, GenerateUnixTypes: false }:
79+
{
80+
Visit(binaryOperator.RHS);
81+
break;
82+
}
83+
// Fallback
84+
default:
85+
{
86+
outputBuilder.Write("(int)(");
87+
Visit(binaryOperator.RHS);
88+
outputBuilder.Write(")");
89+
break;
90+
}
91+
}
92+
}
93+
else
94+
{
95+
Visit(binaryOperator.RHS);
96+
}
97+
4598
StopCSharpCode();
4699
}
47100

sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
<None Include="Properties/GenerateClang-LICENSE.txt" CopyToOutputDirectory="PreserveNewest" />
1515
<None Include="Properties/GenerateClangSharp.rsp" />
1616
<None Include="Properties/GenerateClangSharp-LICENSE.txt" CopyToOutputDirectory="PreserveNewest" />
17-
<None Include="Properties/GenerateLLVM.rsp" />
18-
<None Include="Properties/GenerateLLVM-LICENSE.txt" CopyToOutputDirectory="PreserveNewest" />
1917
<None Include="Properties/launchsettings.json" />
2018
</ItemGroup>
2119

@@ -30,7 +28,6 @@
3028
<PropertyGroup>
3129
<LibClangName Condition="'$(LibClangName)' == ''">libclang</LibClangName>
3230
<LibClangSharpName Condition="'$(LibClangSharpName)' == ''">libClangSharp</LibClangSharpName>
33-
<LibLLVMName Condition="'$(LibLLVMName)' == ''">libLLVM</LibLLVMName>
3431
</PropertyGroup>
3532

3633
<!-- Auto-detect LLVM... more or less -->

sources/ClangSharpPInvokeGenerator/Properties/GenerateLLVM-LICENSE.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

sources/ClangSharpPInvokeGenerator/Properties/GenerateLLVM.rsp

Lines changed: 0 additions & 104 deletions
This file was deleted.

sources/ClangSharpPInvokeGenerator/Properties/launchSettings.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
"commandName": "Project",
1010
"commandLineArgs": "\"@$(MSBuildProjectDirectory)/Properties/GenerateClangSharp.rsp\" --file-directory \"$(MSBuildProjectDirectory)/..\" --include-directory \"$(LLVMIncludePath)\" --libraryPath $(LibClangSharpName)"
1111
},
12-
"GenerateLLVM": {
13-
"commandName": "Project",
14-
"commandLineArgs": "\"@$(MSBuildProjectDirectory)/Properties/GenerateLLVM.rsp\" --file-directory \"$(LLVMIncludePath)\" --include-directory \"$(LLVMIncludePath)\" --libraryPath $(LibLLVMName)"
15-
},
1612
"GenerateLocal": {
1713
"commandName": "Project",
1814
"commandLineArgs": "@generate.rsp",

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protected override Task IncompleteArraySizeTestImpl(string nativeType, string ex
2121

2222
var expectedOutputContents = $@"using System;
2323
using System.Diagnostics.CodeAnalysis;
24+
using System.Runtime.CompilerServices;
2425
using System.Runtime.InteropServices;
2526
2627
namespace ClangSharp.Test

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protected override Task IncompleteArraySizeTestImpl(string nativeType, string ex
2121

2222
var expectedOutputContents = $@"using System;
2323
using System.Diagnostics.CodeAnalysis;
24+
using System.Runtime.CompilerServices;
2425
using System.Runtime.InteropServices;
2526
2627
namespace ClangSharp.Test

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protected override Task IncompleteArraySizeTestImpl(string nativeType, string ex
2121

2222
var expectedOutputContents = $@"using System;
2323
using System.Diagnostics.CodeAnalysis;
24+
using System.Runtime.CompilerServices;
2425
using System.Runtime.InteropServices;
2526
2627
namespace ClangSharp.Test

0 commit comments

Comments
 (0)