Skip to content

Commit 43d45ef

Browse files
Merge pull request #542 from dpaoliello/excludeintrinsics
Exclude intrinsics from translation
2 parents ce3e5c3 + b304acd commit 43d45ef

File tree

19 files changed

+228
-40
lines changed

19 files changed

+228
-40
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3968,7 +3968,7 @@ private bool IsConstant(string targetTypeName, Expr initExpr)
39683968
}
39693969
}
39703970

3971-
private bool IsPrimitiveValue(Cursor? cursor, Type type)
3971+
private static bool IsPrimitiveValue(Cursor? cursor, Type type)
39723972
{
39733973
if (IsType<BuiltinType>(cursor, type, out var builtinType))
39743974
{

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4526,7 +4526,7 @@ private bool HasVtbl(CXXRecordDecl cxxRecordDecl, out bool hasBaseVtbl)
45264526
return hasVtbl;
45274527
}
45284528

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

50435043
return !TryGetUuid(recordDecl, out _);
50445044
}
5045+
5046+
bool IsExcludedByAttributes(Cursor cursor)
5047+
{
5048+
if (cursor is NamedDecl namedDecl)
5049+
{
5050+
foreach (var attr in GetAttributesFor(namedDecl))
5051+
{
5052+
switch (attr.Kind)
5053+
{
5054+
case CX_AttrKind_Builtin:
5055+
return true;
5056+
}
5057+
}
5058+
}
5059+
5060+
return false;
5061+
}
50455062
}
50465063

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

5229-
private bool IsType<T>(Expr expr)
5246+
private static bool IsType<T>(Expr expr)
52305247
where T : Type => IsType<T>(expr, out _);
52315248

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

5235-
private bool IsType<T>(ValueDecl valueDecl)
5252+
private static bool IsType<T>(ValueDecl valueDecl)
52365253
where T : Type => IsType<T>(valueDecl, out _);
52375254

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

5241-
private bool IsType<T>(Cursor? cursor, Type type)
5258+
private static bool IsType<T>(Cursor? cursor, Type type)
52425259
where T : Type => IsType<T>(cursor, type, out _);
52435260

5244-
private bool IsType<T>(Cursor? cursor, Type type, [MaybeNullWhen(false)] out T value)
5261+
private static bool IsType<T>(Cursor? cursor, Type type, [MaybeNullWhen(false)] out T value)
52455262
where T : Type
52465263
{
52475264
if (type is T t)
@@ -5325,36 +5342,36 @@ private bool IsType<T>(Cursor? cursor, Type type, [MaybeNullWhen(false)] out T v
53255342
return false;
53265343
}
53275344

5328-
private bool IsTypeConstantOrIncompleteArray(Expr expr)
5345+
private static bool IsTypeConstantOrIncompleteArray(Expr expr)
53295346
=> IsTypeConstantOrIncompleteArray(expr, out _);
53305347

5331-
private bool IsTypeConstantOrIncompleteArray(Expr expr, [MaybeNullWhen(false)] out ArrayType arrayType)
5348+
private static bool IsTypeConstantOrIncompleteArray(Expr expr, [MaybeNullWhen(false)] out ArrayType arrayType)
53325349
=> IsTypeConstantOrIncompleteArray(expr, expr.Type, out arrayType);
53335350

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

5337-
private bool IsTypeConstantOrIncompleteArray(ValueDecl valueDecl, [MaybeNullWhen(false)] out ArrayType arrayType)
5354+
private static bool IsTypeConstantOrIncompleteArray(ValueDecl valueDecl, [MaybeNullWhen(false)] out ArrayType arrayType)
53385355
=> IsTypeConstantOrIncompleteArray(valueDecl, valueDecl.Type, out arrayType);
53395356

5340-
private bool IsTypeConstantOrIncompleteArray(Cursor? cursor, Type type)
5357+
private static bool IsTypeConstantOrIncompleteArray(Cursor? cursor, Type type)
53415358
=> IsTypeConstantOrIncompleteArray(cursor, type, out _);
53425359

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

5347-
private bool IsTypePointerOrReference(Expr expr)
5364+
private static bool IsTypePointerOrReference(Expr expr)
53485365
=> IsTypePointerOrReference(expr, expr.Type);
53495366

5350-
private bool IsTypePointerOrReference(ValueDecl valueDecl)
5367+
private static bool IsTypePointerOrReference(ValueDecl valueDecl)
53515368
=> IsTypePointerOrReference(valueDecl, valueDecl.Type);
53525369

5353-
private bool IsTypePointerOrReference(Cursor? cursor, Type type)
5370+
private static bool IsTypePointerOrReference(Cursor? cursor, Type type)
53545371
=> IsType<PointerType>(cursor, type)
53555372
|| IsType<ReferenceType>(cursor, type);
53565373

5357-
private bool IsTypeVoid(Cursor? cursor, Type type)
5374+
private static bool IsTypeVoid(Cursor? cursor, Type type)
53585375
=> IsType<BuiltinType>(cursor, type, out var builtinType)
53595376
&& (builtinType.Kind == CXType_Void);
53605377

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

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

6617+
private static IEnumerable<Attr> GetAttributesFor(NamedDecl namedDecl)
6618+
{
6619+
var declAttrs = namedDecl.HasAttrs
6620+
? namedDecl.Attrs
6621+
: Enumerable.Empty<Attr>();
6622+
6623+
if (namedDecl is FieldDecl fieldDecl)
6624+
{
6625+
if (IsType<TypedefType>(fieldDecl, out var typedefType))
6626+
{
6627+
declAttrs = declAttrs.Concat(typedefType.Decl.Attrs);
6628+
}
6629+
}
6630+
else if (namedDecl is RecordDecl recordDecl)
6631+
{
6632+
var typedefName = recordDecl.TypedefNameForAnonDecl;
6633+
6634+
if ((typedefName is not null) && typedefName.HasAttrs)
6635+
{
6636+
declAttrs = declAttrs.Concat(typedefName.Attrs);
6637+
}
6638+
}
6639+
6640+
return declAttrs;
6641+
}
6642+
66006643
private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform = false, bool isTestOutput = false)
66016644
{
66026645
var outputBuilder = isTestOutput ? _testOutputBuilder : _outputBuilder;
@@ -6612,30 +6655,9 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform =
66126655

66136656
if (!isTestOutput)
66146657
{
6615-
var declAttrs = namedDecl.HasAttrs
6616-
? namedDecl.Attrs
6617-
: Enumerable.Empty<Attr>();
6618-
6619-
if (namedDecl is FieldDecl fieldDecl)
6620-
{
6621-
if (IsType<TypedefType>(fieldDecl, out var typedefType))
6622-
{
6623-
declAttrs = declAttrs.Concat(typedefType.Decl.Attrs);
6624-
}
6625-
}
6626-
else if (namedDecl is RecordDecl recordDecl)
6627-
{
6628-
var typedefName = recordDecl.TypedefNameForAnonDecl;
6629-
6630-
if ((typedefName is not null) && typedefName.HasAttrs)
6631-
{
6632-
declAttrs = declAttrs.Concat(typedefName.Attrs);
6633-
}
6634-
}
6635-
66366658
var obsoleteEmitted = false;
66376659

6638-
foreach (var attr in declAttrs)
6660+
foreach (var attr in GetAttributesFor(namedDecl))
66396661
{
66406662
switch (attr.Kind)
66416663
{

tests/ClangSharp.PInvokeGenerator.UnitTests/Base/FunctionDeclarationDllImportTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public abstract class FunctionDeclarationDllImportTest : PInvokeGeneratorTest
7878
[Test]
7979
public Task VarargsTest() => VarargsTestImpl();
8080

81+
[Test]
82+
public Task IntrinsicsTest() => IntrinsicsTestImpl();
83+
8184
protected abstract Task BasicTestImpl();
8285

8386
protected abstract Task ArrayParameterTestImpl();
@@ -113,4 +116,6 @@ public abstract class FunctionDeclarationDllImportTest : PInvokeGeneratorTest
113116
protected abstract Task SourceLocationTestImpl();
114117

115118
protected abstract Task VarargsTestImpl();
119+
120+
protected abstract Task IntrinsicsTestImpl();
116121
}

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/FunctionDeclarationDllImportTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,14 @@ public static partial class Methods
399399
}
400400

401401
protected override Task VarargsTestImpl() => Task.CompletedTask;
402+
403+
protected override Task IntrinsicsTestImpl()
404+
{
405+
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
406+
#pragma intrinsic(__builtin_cpu_init)";
407+
408+
const string ExpectedOutputContents = @"";
409+
410+
return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
411+
}
402412
}

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/FunctionDeclarationDllImportTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,4 +416,14 @@ public static partial class Methods
416416

417417
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(InputContents, ExpectedOutputContents);
418418
}
419+
420+
protected override Task IntrinsicsTestImpl()
421+
{
422+
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
423+
#pragma intrinsic(__builtin_cpu_init)";
424+
425+
const string ExpectedOutputContents = @"";
426+
427+
return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
428+
}
419429
}

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/FunctionDeclarationDllImportTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,14 @@ public static partial class Methods
398398
}
399399

400400
protected override Task VarargsTestImpl() => Task.CompletedTask;
401+
402+
protected override Task IntrinsicsTestImpl()
403+
{
404+
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
405+
#pragma intrinsic(__builtin_cpu_init)";
406+
407+
const string ExpectedOutputContents = @"";
408+
409+
return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
410+
}
401411
}

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultWindows/FunctionDeclarationDllImportTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// 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.
22

33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Runtime.InteropServices;
56
using System.Threading.Tasks;
67
using NUnit.Framework;
@@ -415,4 +416,14 @@ public static partial class Methods
415416

416417
return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
417418
}
419+
420+
protected override Task IntrinsicsTestImpl()
421+
{
422+
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
423+
#pragma intrinsic(__builtin_cpu_init)";
424+
425+
const string ExpectedOutputContents = @"";
426+
427+
return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
428+
}
418429
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,14 @@ public static partial class Methods
398398
}
399399

400400
protected override Task VarargsTestImpl() => Task.CompletedTask;
401+
402+
protected override Task IntrinsicsTestImpl()
403+
{
404+
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
405+
#pragma intrinsic(__builtin_cpu_init)";
406+
407+
const string ExpectedOutputContents = @"";
408+
409+
return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
410+
}
401411
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,14 @@ public static partial class Methods
415415

416416
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(InputContents, ExpectedOutputContents);
417417
}
418+
419+
protected override Task IntrinsicsTestImpl()
420+
{
421+
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
422+
#pragma intrinsic(__builtin_cpu_init)";
423+
424+
const string ExpectedOutputContents = @"";
425+
426+
return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
427+
}
418428
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,14 @@ public static partial class Methods
398398
}
399399

400400
protected override Task VarargsTestImpl() => Task.CompletedTask;
401+
402+
protected override Task IntrinsicsTestImpl()
403+
{
404+
const string InputContents = @"extern ""C"" void __builtin_cpu_init();
405+
#pragma intrinsic(__builtin_cpu_init)";
406+
407+
const string ExpectedOutputContents = @"";
408+
409+
return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(InputContents, ExpectedOutputContents);
410+
}
401411
}

0 commit comments

Comments
 (0)