Skip to content

Commit 83d82f0

Browse files
Merge pull request #269 from microsoft/release/v12.0.0
Merging various fixes and improvements
2 parents 84e5f96 + f9c3c09 commit 83d82f0

File tree

9 files changed

+201
-49
lines changed

9 files changed

+201
-49
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<PackageOutputPath>$(BaseArtifactsPath)pkg/$(Configuration)/</PackageOutputPath>
4444
<Product>ClangSharp</Product>
4545
<VersionPrefix>12.0.0</VersionPrefix>
46-
<VersionSuffix>beta2</VersionSuffix>
46+
<VersionSuffix>beta3</VersionSuffix>
4747
<VersionSuffix Condition="'$(BUILD_REASON)' == 'PullRequest'">pr</VersionSuffix>
4848
</PropertyGroup>
4949

Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</PropertyGroup>
2222

2323
<!-- Settings that allow testing and packing to work by default -->
24-
<PropertyGroup Condition="'$(RuntimeIdentifier)' == '' AND '$(Contin)' != 'true'">
24+
<PropertyGroup Condition="'$(RuntimeIdentifier)' == ''">
2525
<RuntimeIdentifier>$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier>
2626
<RuntimeIdentifier Condition="'$(OVERRIDE_RUNTIME_IDENTIFIER)' != ''">$(OVERRIDE_RUNTIME_IDENTIFIER)</RuntimeIdentifier>
2727
</PropertyGroup>

scripts/azure-pipelines.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ jobs:
2929
architecture: x64
3030
testwin32metadata: false
3131

32+
- template: azure-windows.yml
33+
parameters:
34+
name: windows_release_x64
35+
pool: windows-latest
36+
configuration: Release
37+
architecture: x64
38+
testwin32metadata: false
39+
3240
- template: azure-windows.yml
3341
parameters:
3442
name: test_win32metadata

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,27 @@ void ForFunctionDecl(ParmVarDecl parmVarDecl, FunctionDecl functionDecl)
952952
if (parmVarDecl.HasDefaultArg)
953953
{
954954
_outputBuilder.BeginParameterDefault();
955-
UncheckStmt(typeName, parmVarDecl.DefaultArg);
955+
956+
var defaultArg = parmVarDecl.DefaultArg;
957+
958+
if (parmVarDecl.Type.CanonicalType.IsPointerType && (defaultArg.Handle.Evaluate.Kind == CXEvalResultKind.CXEval_UnExposed))
959+
{
960+
if (!IsStmtAsWritten<CXXNullPtrLiteralExpr>(defaultArg, out _, removeParens: true) &&
961+
(!IsStmtAsWritten<CastExpr>(defaultArg, out var castExpr, removeParens: true) || (castExpr.CastKind != CX_CastKind.CX_CK_NullToPointer)) &&
962+
(!IsStmtAsWritten<IntegerLiteral>(defaultArg, out var integerLiteral, removeParens: true) || (integerLiteral.Value != 0)))
963+
{
964+
AddDiagnostic(DiagnosticLevel.Info, $"Unsupported default parameter: '{name}'. Generated bindings may be incomplete.", defaultArg);
965+
}
966+
967+
var outputBuilder = StartCSharpCode();
968+
outputBuilder.Write("null");
969+
StopCSharpCode();
970+
}
971+
else
972+
{
973+
UncheckStmt(typeName, parmVarDecl.DefaultArg);
974+
}
975+
956976
_outputBuilder.EndParameterDefault();
957977
}
958978

@@ -2521,12 +2541,12 @@ void ForUnderlyingType(TypedefDecl typedefDecl, Type underlyingType)
25212541
if (_config.LogPotentialTypedefRemappings)
25222542
{
25232543
var typedefName = typedefDecl.UnderlyingDecl.Name;
2524-
var possibleNamesToRemap = new string[] {"_" + typedefName, "_tag" + typedefName, "tag" + typedefName};
2544+
var possibleNamesToRemap = new string[] {"_" + typedefName, "_tag" + typedefName, "tag" + typedefName, typedefName + "_tag" };
25252545
var underlyingName = underlyingTagType.AsString;
25262546

25272547
foreach (var possibleNameToRemap in possibleNamesToRemap)
25282548
{
2529-
if (!_config.RemappedNames.ContainsKey(possibleNameToRemap))
2549+
if (!_config.RemappedNames.ContainsKey(possibleNameToRemap) && !_config.RemappedNames.ContainsKey(possibleNameToRemap + "*"))
25302550
{
25312551
if (possibleNameToRemap == underlyingName)
25322552
{
@@ -2704,7 +2724,7 @@ private void VisitVarDecl(VarDecl varDecl)
27042724
}
27052725
}
27062726
}
2707-
else if ((type.IsLocalConstQualified || isMacroDefinitionRecord) && CanBeConstant(type, varDecl.Init))
2727+
else if ((type.IsLocalConstQualified || isMacroDefinitionRecord) && CanBeConstant(type, typeName, varDecl.Init))
27082728
{
27092729
kind |= ConstantKind.PrimitiveConstant;
27102730
}
@@ -2820,15 +2840,15 @@ void ForDeclStmt(VarDecl varDecl, DeclStmt declStmt)
28202840
StopCSharpCode();
28212841
}
28222842

2823-
bool CanBeConstant(Type type, Expr initExpr)
2843+
bool CanBeConstant(Type type, string targetTypeName, Expr initExpr)
28242844
{
28252845
if (type is AttributedType attributedType)
28262846
{
2827-
return CanBeConstant(attributedType.ModifiedType, initExpr);
2847+
return CanBeConstant(attributedType.ModifiedType, targetTypeName, initExpr);
28282848
}
28292849
else if (type is AutoType autoType)
28302850
{
2831-
return CanBeConstant(autoType.CanonicalType, initExpr);
2851+
return CanBeConstant(autoType.CanonicalType, targetTypeName, initExpr);
28322852
}
28332853
else if (type is BuiltinType builtinType)
28342854
{
@@ -2852,28 +2872,28 @@ bool CanBeConstant(Type type, Expr initExpr)
28522872
case CXTypeKind.CXType_Float:
28532873
case CXTypeKind.CXType_Double:
28542874
{
2855-
return IsConstant(initExpr);
2875+
return IsConstant(targetTypeName, initExpr);
28562876
}
28572877
}
28582878
}
28592879
else if (type is ElaboratedType elaboratedType)
28602880
{
2861-
return CanBeConstant(elaboratedType.NamedType, initExpr);
2881+
return CanBeConstant(elaboratedType.NamedType, targetTypeName, initExpr);
28622882
}
28632883
else if (type is EnumType enumType)
28642884
{
2865-
return CanBeConstant(enumType.Decl.IntegerType, initExpr);
2885+
return CanBeConstant(enumType.Decl.IntegerType, targetTypeName, initExpr);
28662886
}
28672887
else if (type is TypedefType typedefType)
28682888
{
2869-
return CanBeConstant(typedefType.Decl.UnderlyingType, initExpr);
2889+
return CanBeConstant(typedefType.Decl.UnderlyingType, targetTypeName, initExpr);
28702890
}
28712891

28722892
return false;
28732893
}
28742894
}
28752895

2876-
private bool IsConstant(Expr initExpr)
2896+
private bool IsConstant(string targetTypeName, Expr initExpr)
28772897
{
28782898
switch (initExpr.StmtClass)
28792899
{
@@ -2882,7 +2902,7 @@ private bool IsConstant(Expr initExpr)
28822902
case CX_StmtClass.CX_StmtClass_ConditionalOperator:
28832903
{
28842904
var conditionalOperator = (ConditionalOperator)initExpr;
2885-
return IsConstant(conditionalOperator.Cond) && IsConstant(conditionalOperator.LHS) && IsConstant(conditionalOperator.RHS);
2905+
return IsConstant(targetTypeName, conditionalOperator.Cond) && IsConstant(targetTypeName, conditionalOperator.LHS) && IsConstant(targetTypeName, conditionalOperator.RHS);
28862906
}
28872907

28882908
// case CX_StmtClass.CX_StmtClass_AddrLabelExpr:
@@ -2901,7 +2921,7 @@ private bool IsConstant(Expr initExpr)
29012921
case CX_StmtClass.CX_StmtClass_BinaryOperator:
29022922
{
29032923
var binaryOperator = (BinaryOperator)initExpr;
2904-
return IsConstant(binaryOperator.LHS) && IsConstant(binaryOperator.RHS);
2924+
return IsConstant(targetTypeName, binaryOperator.LHS) && IsConstant(targetTypeName, binaryOperator.RHS);
29052925
}
29062926

29072927
// case CX_StmtClass.CX_StmtClass_CompoundAssignOperator:
@@ -2982,7 +3002,7 @@ private bool IsConstant(Expr initExpr)
29823002
case CX_StmtClass.CX_StmtClass_CXXFunctionalCastExpr:
29833003
{
29843004
var cxxFunctionalCastExpr = (ExplicitCastExpr)initExpr;
2985-
return IsConstant(cxxFunctionalCastExpr.SubExprAsWritten);
3005+
return IsConstant(targetTypeName, cxxFunctionalCastExpr.SubExprAsWritten);
29863006
}
29873007

29883008
// case CX_StmtClass.CX_StmtClass_CXXConstCastExpr:
@@ -2993,7 +3013,7 @@ private bool IsConstant(Expr initExpr)
29933013
case CX_StmtClass.CX_StmtClass_ImplicitCastExpr:
29943014
{
29953015
var implicitCastExpr = (ImplicitCastExpr)initExpr;
2996-
return IsConstant(implicitCastExpr.SubExprAsWritten);
3016+
return IsConstant(targetTypeName, implicitCastExpr.SubExprAsWritten);
29973017
}
29983018

29993019
case CX_StmtClass.CX_StmtClass_CharacterLiteral:
@@ -3012,7 +3032,7 @@ private bool IsConstant(Expr initExpr)
30123032
{
30133033
var declRefExpr = (DeclRefExpr)initExpr;
30143034
return (declRefExpr.Decl is EnumConstantDecl) ||
3015-
((declRefExpr.Decl is VarDecl varDecl) && varDecl.HasInit && IsConstant(varDecl.Init));
3035+
((declRefExpr.Decl is VarDecl varDecl) && varDecl.HasInit && IsConstant(targetTypeName, varDecl.Init));
30163036
}
30173037

30183038
// case CX_StmtClass.CX_StmtClass_DependentCoawaitExpr:
@@ -3033,7 +3053,7 @@ private bool IsConstant(Expr initExpr)
30333053
case CX_StmtClass.CX_StmtClass_ExprWithCleanups:
30343054
{
30353055
var exprWithCleanups = (ExprWithCleanups)initExpr;
3036-
return IsConstant(exprWithCleanups.SubExpr);
3056+
return IsConstant(targetTypeName, exprWithCleanups.SubExpr);
30373057
}
30383058

30393059
// case CX_StmtClass.CX_StmtClass_FunctionParmPackExpr:
@@ -3089,7 +3109,7 @@ private bool IsConstant(Expr initExpr)
30893109
case CX_StmtClass.CX_StmtClass_ParenExpr:
30903110
{
30913111
var parenExpr = (ParenExpr)initExpr;
3092-
return IsConstant(parenExpr.SubExpr);
3112+
return IsConstant(targetTypeName, parenExpr.SubExpr);
30933113
}
30943114

30953115
case CX_StmtClass.CX_StmtClass_ParenListExpr:
@@ -3098,7 +3118,7 @@ private bool IsConstant(Expr initExpr)
30983118

30993119
foreach (var expr in parenListExpr.Exprs)
31003120
{
3101-
if (IsConstant(expr))
3121+
if (IsConstant(targetTypeName, expr))
31023122
{
31033123
return true;
31043124
}
@@ -3159,7 +3179,18 @@ private bool IsConstant(Expr initExpr)
31593179
case CX_StmtClass.CX_StmtClass_UnaryOperator:
31603180
{
31613181
var unaryOperator = (UnaryOperator)initExpr;
3162-
return IsConstant(unaryOperator.SubExpr);
3182+
3183+
if (!IsConstant(targetTypeName, unaryOperator.SubExpr))
3184+
{
3185+
return false;
3186+
}
3187+
3188+
if (unaryOperator.Opcode != CX_UnaryOperatorKind.CX_UO_Minus)
3189+
{
3190+
return true;
3191+
}
3192+
3193+
return targetTypeName is not "IntPtr" and not "nint" and not "nuint" and not "UIntPtr";
31633194
}
31643195

31653196
// case CX_StmtClass.CX_StmtClass_VAArgExpr:

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,38 @@ private void VisitCharacterLiteral(CharacterLiteral characterLiteral)
188188
}
189189
else
190190
{
191-
var isPreviousExplicitCast = IsPrevContextStmt<ExplicitCastExpr>(out _);
191+
var castType = "";
192192

193-
if (!isPreviousExplicitCast)
193+
if (IsPrevContextStmt<ImplicitCastExpr>(out var implicitCastExpr))
194194
{
195-
outputBuilder.Write("(byte)(");
195+
// C# characters are effectively `ushort` while C defaults to "char" which is
196+
// most typically `sbyte`. Due to this we need to insert a correct implicit
197+
// cast to ensure things are correctly handled here.
198+
199+
var castExprTypeName = GetRemappedTypeName(implicitCastExpr, context: null, implicitCastExpr.Type, out _, skipUsing: true);
200+
201+
if (!IsUnsigned(castExprTypeName))
202+
{
203+
castType = "sbyte";
204+
}
205+
else if (implicitCastExpr.Type.Handle.NumBits < 16)
206+
{
207+
// Cast to byte if the target type is less
208+
209+
castType = "byte";
210+
}
211+
}
212+
213+
if (castType != "")
214+
{
215+
outputBuilder.Write("(sbyte)(");
196216
}
197217

198218
outputBuilder.Write('\'');
199219
outputBuilder.Write(EscapeCharacter((char)characterLiteral.Value));
200220
outputBuilder.Write('\'');
201221

202-
if (!isPreviousExplicitCast)
222+
if (castType != "")
203223
{
204224
outputBuilder.Write(')');
205225
}
@@ -634,6 +654,7 @@ private void VisitDoStmt(DoStmt doStmt)
634654
private void VisitExplicitCastExpr(ExplicitCastExpr explicitCastExpr)
635655
{
636656
var outputBuilder = StartCSharpCode();
657+
637658
if (IsPrevContextDecl<EnumConstantDecl>(out _) && explicitCastExpr.Type is EnumType enumType)
638659
{
639660
outputBuilder.Write('(');
@@ -643,10 +664,17 @@ private void VisitExplicitCastExpr(ExplicitCastExpr explicitCastExpr)
643664
}
644665

645666
var type = explicitCastExpr.Type;
646-
647-
648667
var typeName = GetRemappedTypeName(explicitCastExpr, context: null, type, out _);
649668

669+
if (typeName == "IntPtr")
670+
{
671+
typeName = "nint";
672+
}
673+
else if (typeName == "UIntPtr")
674+
{
675+
typeName = "nuint";
676+
}
677+
650678
outputBuilder.Write('(');
651679
outputBuilder.Write(typeName);
652680
outputBuilder.Write(')');
@@ -832,7 +860,7 @@ private void VisitImplicitCastExpr(ImplicitCastExpr implicitCastExpr)
832860

833861
default:
834862
{
835-
if ((subExpr is DeclRefExpr declRefExpr) && (declRefExpr.Decl is EnumConstantDecl enumConstantDecl))
863+
if (IsStmtAsWritten<DeclRefExpr>(subExpr, out var declRefExpr, removeParens: true) && (declRefExpr.Decl is EnumConstantDecl enumConstantDecl))
836864
{
837865
ForEnumConstantDecl(implicitCastExpr, enumConstantDecl);
838866
}
@@ -1901,7 +1929,7 @@ private void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr unaryExprOrT
19011929
{
19021930
var arg = args[i];
19031931

1904-
if (IsStmtAsWritten(arg, unaryExprOrTypeTraitExpr))
1932+
if (IsStmtAsWritten(arg, unaryExprOrTypeTraitExpr, removeParens: true))
19051933
{
19061934
index = i;
19071935
break;
@@ -2026,7 +2054,18 @@ private void VisitUnaryOperator(UnaryOperator unaryOperator)
20262054

20272055
if (canonicalType.IsIntegerType && (canonicalType.Kind != CXTypeKind.CXType_Bool))
20282056
{
2057+
var needsParens = IsStmtAsWritten<BinaryOperator>(subExpr, out _);
2058+
2059+
if (needsParens)
2060+
{
2061+
outputBuilder.Write('(');
2062+
}
20292063
Visit(subExpr);
2064+
2065+
if (needsParens)
2066+
{
2067+
outputBuilder.Write(')');
2068+
}
20302069
outputBuilder.Write(" == 0");
20312070
}
20322071
else if (canonicalType is PointerType or ReferenceType)

0 commit comments

Comments
 (0)