Browse Source

Remove private fields from the generated C#

They are useless because they aren't used in accessible generated methods and properties, and layouts only need the correct size to allocate.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
remove-private-fields
Dimitar Dobrev 6 years ago
parent
commit
32a684cdef
  1. 8
      src/CppParser/Parser.cpp
  2. 8
      src/Generator.Tests/AST/TestAST.cs
  3. 5
      src/Generator/Generators/CSharp/CSharpSources.cs
  4. 13
      src/Generator/Passes/CheckAbiParameters.cs
  5. 33
      tests/CSharp/CSharp.Tests.cs

8
src/CppParser/Parser.cpp

@ -173,6 +173,11 @@ void Parser::ReadClassLayout(Class* Class, const clang::RecordDecl* RD, @@ -173,6 +173,11 @@ void Parser::ReadClassLayout(Class* Class, const clang::RecordDecl* RD,
// Dump fields.
uint64_t FieldNo = 0;
for (const clang::FieldDecl* Field : RD->fields()) {
if (Field->getAccess() == clang::AccessSpecifier::AS_private)
{
++FieldNo;
continue;
}
uint64_t LocalFieldOffsetInBits = Layout.getFieldOffset(FieldNo++);
CharUnits FieldOffset =
Offset + c->getASTContext().toCharUnitsFromBits(LocalFieldOffsetInBits);
@ -1007,7 +1012,8 @@ void Parser::WalkRecord(const clang::RecordDecl* Record, Class* RC) @@ -1007,7 +1012,8 @@ void Parser::WalkRecord(const clang::RecordDecl* Record, Class* RC)
}
for (auto FD : Record->fields())
WalkFieldCXX(FD, RC);
if (FD->getAccess() != clang::AccessSpecifier::AS_private)
WalkFieldCXX(FD, RC);
if (c->getSourceManager().isInSystemHeader(Record->getBeginLoc()))
{

8
src/Generator.Tests/AST/TestAST.cs

@ -342,14 +342,6 @@ namespace CppSharp.Generator.Tests.AST @@ -342,14 +342,6 @@ namespace CppSharp.Generator.Tests.AST
Assert.IsTrue(AstContext.FindClass("HasAmbiguousFunctions").Single().FindMethod("ambiguous").IsAmbiguous);
}
[Test]
public void TestAtomics()
{
var type = AstContext.FindClass("Atomics").Single().Fields
.Find(f => f.Name == "AtomicInt").Type as BuiltinType;
Assert.IsTrue(type != null && type.IsPrimitiveType(PrimitiveType.Int));
}
[Test]
public void TestMacroLineNumber()
{

5
src/Generator/Generators/CSharp/CSharpSources.cs

@ -315,8 +315,7 @@ namespace CppSharp.Generators.CSharp @@ -315,8 +315,7 @@ namespace CppSharp.Generators.CSharp
foreach (var specialization in generated.KeepSingleAllPointersSpecialization())
GenerateClassInternals(specialization);
foreach (var group in generated.SelectMany(s => s.Classes).Where(
c => !c.IsIncomplete).GroupBy(c => c.Name))
foreach (var group in generated.SelectMany(s => s.Classes).GroupBy(c => c.Name))
{
var nested = classTemplate.Classes.FirstOrDefault(c => c.Name == group.Key);
if (nested != null)
@ -524,7 +523,7 @@ namespace CppSharp.Generators.CSharp @@ -524,7 +523,7 @@ namespace CppSharp.Generators.CSharp
{
PushBlock(BlockKind.InternalsClass);
if (!Options.GenerateSequentialLayout || @class.IsUnion)
WriteLine($"[StructLayout(LayoutKind.Explicit, Size = {@class.Layout.Size})]");
WriteLine($"[StructLayout(LayoutKind.Explicit, Size = {@class.Layout.DataSize})]");
else if (@class.MaxFieldAlignment > 0)
WriteLine($"[StructLayout(LayoutKind.Sequential, Pack = {@class.MaxFieldAlignment})]");

13
src/Generator/Passes/CheckAbiParameters.cs

@ -25,19 +25,6 @@ namespace CppSharp.Passes @@ -25,19 +25,6 @@ namespace CppSharp.Passes
/// </summary>
public class CheckAbiParameters : TranslationUnitPass
{
public override bool VisitClassDecl(Class @class)
{
if (!base.VisitClassDecl(@class))
return false;
if (@class.IsDependent || @class.Layout.Fields.Count > 0 || @class.Fields.Count > 0)
return false;
@class.Layout.Size = @class.Layout.DataSize = 0;
return true;
}
public override bool VisitFunctionDecl(Function function)
{
if (!VisitDeclaration(function))

33
tests/CSharp/CSharp.Tests.cs

@ -613,39 +613,6 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -613,39 +613,6 @@ public unsafe class CSharpTests : GeneratorTestFixture
Assert.That(CSharp.HasFreeConstant.AnotherUnit.STD_STRING_CONSTANT, Is.EqualTo("test"));
}
[Test]
public void TestTemplateInternals()
{
foreach (var internalType in new[]
{
typeof(CSharp.IndependentFields.__Internal),
typeof(CSharp.DependentValueFields.__Internalc__S_DependentValueFields__b),
typeof(CSharp.DependentValueFields.__Internalc__S_DependentValueFields__f),
typeof(CSharp.DependentPointerFields.__Internal),
typeof(CSharp.DependentValueFields.__Internal_Ptr),
typeof(CSharp.HasDefaultTemplateArgument.__Internalc__S_HasDefaultTemplateArgument__I___S_IndependentFields__I)
})
{
var independentFields = internalType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
var fieldOffset = (FieldOffsetAttribute) independentFields[0].GetCustomAttribute(typeof(FieldOffsetAttribute));
Assert.That(fieldOffset.Value, Is.EqualTo(0));
}
foreach (var internalType in new Type[]
{
typeof(CSharp.TwoTemplateArgs.__Internal_Ptr),
typeof(CSharp.TwoTemplateArgs.__Internalc__S_TwoTemplateArgs___I_I),
typeof(CSharp.TwoTemplateArgs.__Internalc__S_TwoTemplateArgs___I_f)
})
{
var independentFields = internalType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
Assert.That(independentFields.Length, Is.EqualTo(2));
var fieldOffsetKey = (FieldOffsetAttribute) independentFields[0].GetCustomAttribute(typeof(FieldOffsetAttribute));
Assert.That(fieldOffsetKey.Value, Is.EqualTo(0));
var fieldOffsetValue = (FieldOffsetAttribute) independentFields[1].GetCustomAttribute(typeof(FieldOffsetAttribute));
Assert.That(fieldOffsetValue.Value, Is.EqualTo(Marshal.SizeOf(IntPtr.Zero)));
}
}
[Test]
public void TestConstantArray()
{

Loading…
Cancel
Save