Skip to content

Commit

Permalink
Merge pull request #542 from dpaoliello/excludeintrinsics
Browse files Browse the repository at this point in the history
Exclude intrinsics from translation
  • Loading branch information
tannergooding authored May 12, 2024
2 parents ce3e5c3 + b304acd commit 43d45ef
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3968,7 +3968,7 @@ private bool IsConstant(string targetTypeName, Expr initExpr)
}
}

private bool IsPrimitiveValue(Cursor? cursor, Type type)
private static bool IsPrimitiveValue(Cursor? cursor, Type type)
{
if (IsType<BuiltinType>(cursor, type, out var builtinType))
{
Expand Down
100 changes: 61 additions & 39 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4526,7 +4526,7 @@ private bool HasVtbl(CXXRecordDecl cxxRecordDecl, out bool hasBaseVtbl)
return hasVtbl;
}

private bool IsEnumOperator(FunctionDecl functionDecl, string name)
private static bool IsEnumOperator(FunctionDecl functionDecl, string name)
{
if (name.StartsWith("operator", StringComparison.Ordinal) && ((functionDecl.Parameters.Count == 1) || (functionDecl.Parameters.Count == 2)))
{
Expand Down Expand Up @@ -4573,7 +4573,7 @@ private bool IsExcluded(Cursor cursor, out bool isExcludedByConflictingDefinitio
{
if (!_isExcluded.TryGetValue(cursor, out var isExcludedValue))
{
isExcludedValue |= (!IsAlwaysIncluded(cursor) && (IsExcludedByConfig(cursor) || IsExcludedByFile(cursor) || IsExcludedByName(cursor, ref isExcludedValue))) ? 0b01u : 0b00u;
isExcludedValue |= (!IsAlwaysIncluded(cursor) && (IsExcludedByConfig(cursor) || IsExcludedByFile(cursor) || IsExcludedByName(cursor, ref isExcludedValue) || IsExcludedByAttributes(cursor))) ? 0b01u : 0b00u;
_isExcluded.Add(cursor, isExcludedValue);
}
isExcludedByConflictingDefinition = (isExcludedValue & 0b10) != 0;
Expand Down Expand Up @@ -5042,6 +5042,23 @@ bool IsEmptyRecord(RecordDecl recordDecl)

return !TryGetUuid(recordDecl, out _);
}

bool IsExcludedByAttributes(Cursor cursor)
{
if (cursor is NamedDecl namedDecl)
{
foreach (var attr in GetAttributesFor(namedDecl))
{
switch (attr.Kind)
{
case CX_AttrKind_Builtin:
return true;
}
}
}

return false;
}
}

private bool IsBaseExcluded(CXXRecordDecl cxxRecordDecl, CXXRecordDecl baseCxxRecordDecl, CXXBaseSpecifier cxxBaseSpecifier, out string baseFieldName)
Expand Down Expand Up @@ -5226,22 +5243,22 @@ private static bool IsStmtAsWritten(Stmt stmt, Stmt expectedStmt, bool removePar
return expr == expectedStmt;
}

private bool IsType<T>(Expr expr)
private static bool IsType<T>(Expr expr)
where T : Type => IsType<T>(expr, out _);

private bool IsType<T>(Expr expr, [MaybeNullWhen(false)] out T value)
private static bool IsType<T>(Expr expr, [MaybeNullWhen(false)] out T value)
where T : Type => IsType(expr, expr.Type, out value);

private bool IsType<T>(ValueDecl valueDecl)
private static bool IsType<T>(ValueDecl valueDecl)
where T : Type => IsType<T>(valueDecl, out _);

private bool IsType<T>(ValueDecl typeDecl, [MaybeNullWhen(false)] out T value)
private static bool IsType<T>(ValueDecl typeDecl, [MaybeNullWhen(false)] out T value)
where T : Type => IsType(typeDecl, typeDecl.Type, out value);

private bool IsType<T>(Cursor? cursor, Type type)
private static bool IsType<T>(Cursor? cursor, Type type)
where T : Type => IsType<T>(cursor, type, out _);

private bool IsType<T>(Cursor? cursor, Type type, [MaybeNullWhen(false)] out T value)
private static bool IsType<T>(Cursor? cursor, Type type, [MaybeNullWhen(false)] out T value)
where T : Type
{
if (type is T t)
Expand Down Expand Up @@ -5325,36 +5342,36 @@ private bool IsType<T>(Cursor? cursor, Type type, [MaybeNullWhen(false)] out T v
return false;
}

private bool IsTypeConstantOrIncompleteArray(Expr expr)
private static bool IsTypeConstantOrIncompleteArray(Expr expr)
=> IsTypeConstantOrIncompleteArray(expr, out _);

private bool IsTypeConstantOrIncompleteArray(Expr expr, [MaybeNullWhen(false)] out ArrayType arrayType)
private static bool IsTypeConstantOrIncompleteArray(Expr expr, [MaybeNullWhen(false)] out ArrayType arrayType)
=> IsTypeConstantOrIncompleteArray(expr, expr.Type, out arrayType);

private bool IsTypeConstantOrIncompleteArray(ValueDecl valueDecl)
=> IsTypeConstantOrIncompleteArray(valueDecl, out _);

private bool IsTypeConstantOrIncompleteArray(ValueDecl valueDecl, [MaybeNullWhen(false)] out ArrayType arrayType)
private static bool IsTypeConstantOrIncompleteArray(ValueDecl valueDecl, [MaybeNullWhen(false)] out ArrayType arrayType)
=> IsTypeConstantOrIncompleteArray(valueDecl, valueDecl.Type, out arrayType);

private bool IsTypeConstantOrIncompleteArray(Cursor? cursor, Type type)
private static bool IsTypeConstantOrIncompleteArray(Cursor? cursor, Type type)
=> IsTypeConstantOrIncompleteArray(cursor, type, out _);

private bool IsTypeConstantOrIncompleteArray(Cursor? cursor, Type type, [MaybeNullWhen(false)] out ArrayType arrayType)
private static bool IsTypeConstantOrIncompleteArray(Cursor? cursor, Type type, [MaybeNullWhen(false)] out ArrayType arrayType)
=> IsType(cursor, type, out arrayType)
&& (arrayType is ConstantArrayType or IncompleteArrayType);

private bool IsTypePointerOrReference(Expr expr)
private static bool IsTypePointerOrReference(Expr expr)
=> IsTypePointerOrReference(expr, expr.Type);

private bool IsTypePointerOrReference(ValueDecl valueDecl)
private static bool IsTypePointerOrReference(ValueDecl valueDecl)
=> IsTypePointerOrReference(valueDecl, valueDecl.Type);

private bool IsTypePointerOrReference(Cursor? cursor, Type type)
private static bool IsTypePointerOrReference(Cursor? cursor, Type type)
=> IsType<PointerType>(cursor, type)
|| IsType<ReferenceType>(cursor, type);

private bool IsTypeVoid(Cursor? cursor, Type type)
private static bool IsTypeVoid(Cursor? cursor, Type type)
=> IsType<BuiltinType>(cursor, type, out var builtinType)
&& (builtinType.Kind == CXType_Void);

Expand Down Expand Up @@ -6597,6 +6614,32 @@ private void Visit(IEnumerable<Cursor> cursors)

private void Visit(IEnumerable<Cursor> cursors, IEnumerable<Cursor> excludedCursors) => Visit(cursors.Except(excludedCursors));

private static IEnumerable<Attr> GetAttributesFor(NamedDecl namedDecl)
{
var declAttrs = namedDecl.HasAttrs
? namedDecl.Attrs
: Enumerable.Empty<Attr>();

if (namedDecl is FieldDecl fieldDecl)
{
if (IsType<TypedefType>(fieldDecl, out var typedefType))
{
declAttrs = declAttrs.Concat(typedefType.Decl.Attrs);
}
}
else if (namedDecl is RecordDecl recordDecl)
{
var typedefName = recordDecl.TypedefNameForAnonDecl;

if ((typedefName is not null) && typedefName.HasAttrs)
{
declAttrs = declAttrs.Concat(typedefName.Attrs);
}
}

return declAttrs;
}

private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform = false, bool isTestOutput = false)
{
var outputBuilder = isTestOutput ? _testOutputBuilder : _outputBuilder;
Expand All @@ -6612,30 +6655,9 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform =

if (!isTestOutput)
{
var declAttrs = namedDecl.HasAttrs
? namedDecl.Attrs
: Enumerable.Empty<Attr>();

if (namedDecl is FieldDecl fieldDecl)
{
if (IsType<TypedefType>(fieldDecl, out var typedefType))
{
declAttrs = declAttrs.Concat(typedefType.Decl.Attrs);
}
}
else if (namedDecl is RecordDecl recordDecl)
{
var typedefName = recordDecl.TypedefNameForAnonDecl;

if ((typedefName is not null) && typedefName.HasAttrs)
{
declAttrs = declAttrs.Concat(typedefName.Attrs);
}
}

var obsoleteEmitted = false;

foreach (var attr in declAttrs)
foreach (var attr in GetAttributesFor(namedDecl))
{
switch (attr.Kind)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public abstract class FunctionDeclarationDllImportTest : PInvokeGeneratorTest
[Test]
public Task VarargsTest() => VarargsTestImpl();

[Test]
public Task IntrinsicsTest() => IntrinsicsTestImpl();

protected abstract Task BasicTestImpl();

protected abstract Task ArrayParameterTestImpl();
Expand Down Expand Up @@ -113,4 +116,6 @@ public abstract class FunctionDeclarationDllImportTest : PInvokeGeneratorTest
protected abstract Task SourceLocationTestImpl();

protected abstract Task VarargsTestImpl();

protected abstract Task IntrinsicsTestImpl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,14 @@ public static partial class Methods
}

protected override Task VarargsTestImpl() => Task.CompletedTask;

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,14 @@ public static partial class Methods

return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,14 @@ public static partial class Methods
}

protected override Task VarargsTestImpl() => Task.CompletedTask;

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using NUnit.Framework;
Expand Down Expand Up @@ -415,4 +416,14 @@ public static partial class Methods

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,14 @@ public static partial class Methods
}

protected override Task VarargsTestImpl() => Task.CompletedTask;

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,14 @@ public static partial class Methods

return ValidateGeneratedCSharpLatestWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,14 @@ public static partial class Methods
}

protected override Task VarargsTestImpl() => Task.CompletedTask;

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,14 @@ public static partial class Methods

return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,14 @@ protected override Task SourceLocationTestImpl()
}

protected override Task VarargsTestImpl() => Task.CompletedTask;

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,14 @@ protected override Task SourceLocationTestImpl()
}

protected override Task VarargsTestImpl() => Task.CompletedTask;

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,14 @@ protected override Task SourceLocationTestImpl()
}

protected override Task VarargsTestImpl() => Task.CompletedTask;

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,14 @@ protected override Task SourceLocationTestImpl()
}

protected override Task VarargsTestImpl() => Task.CompletedTask;

protected override Task IntrinsicsTestImpl()
{
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
#pragma intrinsic(__builtin_cpu_init)";

const string ExpectedOutputContents = @"";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
}
}
Loading

0 comments on commit 43d45ef

Please sign in to comment.