Skip to content

Commit 7bcf3ae

Browse files
committed
Exclude intrinsics from translation
1 parent 76c95c4 commit 7bcf3ae

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
@@ -3966,7 +3966,7 @@ private bool IsConstant(string targetTypeName, Expr initExpr)
39663966
}
39673967
}
39683968

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

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4539,7 +4539,7 @@ private bool HasVtbl(CXXRecordDecl cxxRecordDecl, out bool hasBaseVtbl)
45394539
return hasVtbl;
45404540
}
45414541

4542-
private bool IsEnumOperator(FunctionDecl functionDecl, string name)
4542+
private static bool IsEnumOperator(FunctionDecl functionDecl, string name)
45434543
{
45444544
if (name.StartsWith("operator", StringComparison.Ordinal) && ((functionDecl.Parameters.Count == 1) || (functionDecl.Parameters.Count == 2)))
45454545
{
@@ -4586,7 +4586,7 @@ private bool IsExcluded(Cursor cursor, out bool isExcludedByConflictingDefinitio
45864586
{
45874587
if (!_isExcluded.TryGetValue(cursor, out var isExcludedValue))
45884588
{
4589-
isExcludedValue |= (!IsAlwaysIncluded(cursor) && (IsExcludedByConfig(cursor) || IsExcludedByFile(cursor) || IsExcludedByName(cursor, ref isExcludedValue))) ? 0b01u : 0b00u;
4589+
isExcludedValue |= (!IsAlwaysIncluded(cursor) && (IsExcludedByConfig(cursor) || IsExcludedByFile(cursor) || IsExcludedByName(cursor, ref isExcludedValue) || IsExcludedByAttributes(cursor))) ? 0b01u : 0b00u;
45904590
_isExcluded.Add(cursor, isExcludedValue);
45914591
}
45924592
isExcludedByConflictingDefinition = (isExcludedValue & 0b10) != 0;
@@ -5055,6 +5055,23 @@ bool IsEmptyRecord(RecordDecl recordDecl)
50555055

50565056
return !TryGetUuid(recordDecl, out _);
50575057
}
5058+
5059+
bool IsExcludedByAttributes(Cursor cursor)
5060+
{
5061+
if (cursor is NamedDecl namedDecl)
5062+
{
5063+
foreach (var attr in GetAttributesFor(namedDecl))
5064+
{
5065+
switch (attr.Kind)
5066+
{
5067+
case CX_AttrKind_Builtin:
5068+
return true;
5069+
}
5070+
}
5071+
}
5072+
5073+
return false;
5074+
}
50585075
}
50595076

50605077
private bool IsBaseExcluded(CXXRecordDecl cxxRecordDecl, CXXRecordDecl baseCxxRecordDecl, CXXBaseSpecifier cxxBaseSpecifier, out string baseFieldName)
@@ -5239,22 +5256,22 @@ private static bool IsStmtAsWritten(Stmt stmt, Stmt expectedStmt, bool removePar
52395256
return expr == expectedStmt;
52405257
}
52415258

5242-
private bool IsType<T>(Expr expr)
5259+
private static bool IsType<T>(Expr expr)
52435260
where T : Type => IsType<T>(expr, out _);
52445261

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

5248-
private bool IsType<T>(ValueDecl valueDecl)
5265+
private static bool IsType<T>(ValueDecl valueDecl)
52495266
where T : Type => IsType<T>(valueDecl, out _);
52505267

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

5254-
private bool IsType<T>(Cursor? cursor, Type type)
5271+
private static bool IsType<T>(Cursor? cursor, Type type)
52555272
where T : Type => IsType<T>(cursor, type, out _);
52565273

5257-
private bool IsType<T>(Cursor? cursor, Type type, [MaybeNullWhen(false)] out T value)
5274+
private static bool IsType<T>(Cursor? cursor, Type type, [MaybeNullWhen(false)] out T value)
52585275
where T : Type
52595276
{
52605277
if (type is T t)
@@ -5338,36 +5355,36 @@ private bool IsType<T>(Cursor? cursor, Type type, [MaybeNullWhen(false)] out T v
53385355
return false;
53395356
}
53405357

5341-
private bool IsTypeConstantOrIncompleteArray(Expr expr)
5358+
private static bool IsTypeConstantOrIncompleteArray(Expr expr)
53425359
=> IsTypeConstantOrIncompleteArray(expr, out _);
53435360

5344-
private bool IsTypeConstantOrIncompleteArray(Expr expr, [MaybeNullWhen(false)] out ArrayType arrayType)
5361+
private static bool IsTypeConstantOrIncompleteArray(Expr expr, [MaybeNullWhen(false)] out ArrayType arrayType)
53455362
=> IsTypeConstantOrIncompleteArray(expr, expr.Type, out arrayType);
53465363

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

5350-
private bool IsTypeConstantOrIncompleteArray(ValueDecl valueDecl, [MaybeNullWhen(false)] out ArrayType arrayType)
5367+
private static bool IsTypeConstantOrIncompleteArray(ValueDecl valueDecl, [MaybeNullWhen(false)] out ArrayType arrayType)
53515368
=> IsTypeConstantOrIncompleteArray(valueDecl, valueDecl.Type, out arrayType);
53525369

5353-
private bool IsTypeConstantOrIncompleteArray(Cursor? cursor, Type type)
5370+
private static bool IsTypeConstantOrIncompleteArray(Cursor? cursor, Type type)
53545371
=> IsTypeConstantOrIncompleteArray(cursor, type, out _);
53555372

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

5360-
private bool IsTypePointerOrReference(Expr expr)
5377+
private static bool IsTypePointerOrReference(Expr expr)
53615378
=> IsTypePointerOrReference(expr, expr.Type);
53625379

5363-
private bool IsTypePointerOrReference(ValueDecl valueDecl)
5380+
private static bool IsTypePointerOrReference(ValueDecl valueDecl)
53645381
=> IsTypePointerOrReference(valueDecl, valueDecl.Type);
53655382

5366-
private bool IsTypePointerOrReference(Cursor? cursor, Type type)
5383+
private static bool IsTypePointerOrReference(Cursor? cursor, Type type)
53675384
=> IsType<PointerType>(cursor, type)
53685385
|| IsType<ReferenceType>(cursor, type);
53695386

5370-
private bool IsTypeVoid(Cursor? cursor, Type type)
5387+
private static bool IsTypeVoid(Cursor? cursor, Type type)
53715388
=> IsType<BuiltinType>(cursor, type, out var builtinType)
53725389
&& (builtinType.Kind == CXType_Void);
53735390

@@ -6610,6 +6627,32 @@ private void Visit(IEnumerable<Cursor> cursors)
66106627

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

6630+
private static IEnumerable<Attr> GetAttributesFor(NamedDecl namedDecl)
6631+
{
6632+
var declAttrs = namedDecl.HasAttrs
6633+
? namedDecl.Attrs
6634+
: Enumerable.Empty<Attr>();
6635+
6636+
if (namedDecl is FieldDecl fieldDecl)
6637+
{
6638+
if (IsType<TypedefType>(fieldDecl, out var typedefType))
6639+
{
6640+
declAttrs = declAttrs.Concat(typedefType.Decl.Attrs);
6641+
}
6642+
}
6643+
else if (namedDecl is RecordDecl recordDecl)
6644+
{
6645+
var typedefName = recordDecl.TypedefNameForAnonDecl;
6646+
6647+
if ((typedefName is not null) && typedefName.HasAttrs)
6648+
{
6649+
declAttrs = declAttrs.Concat(typedefName.Attrs);
6650+
}
6651+
}
6652+
6653+
return declAttrs;
6654+
}
6655+
66136656
private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform = false, bool isTestOutput = false)
66146657
{
66156658
var outputBuilder = isTestOutput ? _testOutputBuilder : _outputBuilder;
@@ -6625,30 +6668,9 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform =
66256668

66266669
if (!isTestOutput)
66276670
{
6628-
var declAttrs = namedDecl.HasAttrs
6629-
? namedDecl.Attrs
6630-
: Enumerable.Empty<Attr>();
6631-
6632-
if (namedDecl is FieldDecl fieldDecl)
6633-
{
6634-
if (IsType<TypedefType>(fieldDecl, out var typedefType))
6635-
{
6636-
declAttrs = declAttrs.Concat(typedefType.Decl.Attrs);
6637-
}
6638-
}
6639-
else if (namedDecl is RecordDecl recordDecl)
6640-
{
6641-
var typedefName = recordDecl.TypedefNameForAnonDecl;
6642-
6643-
if ((typedefName is not null) && typedefName.HasAttrs)
6644-
{
6645-
declAttrs = declAttrs.Concat(typedefName.Attrs);
6646-
}
6647-
}
6648-
66496671
var obsoleteEmitted = false;
66506672

6651-
foreach (var attr in declAttrs)
6673+
foreach (var attr in GetAttributesFor(namedDecl))
66526674
{
66536675
switch (attr.Kind)
66546676
{

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)