Browse Source

Fix static being applied to classes without static methods/fields

pull/1923/head
duckdoom5 4 months ago
parent
commit
919eb7f2ba
  1. 3
      src/Generator.Tests/Passes/TestPasses.cs
  2. 26
      src/Generator/Passes/CheckStaticClassPass.cs

3
src/Generator.Tests/Passes/TestPasses.cs

@ -73,11 +73,13 @@ namespace CppSharp.Generator.Tests.Passes
var staticStruct = AstContext.Class("TestCheckStaticStruct"); var staticStruct = AstContext.Class("TestCheckStaticStruct");
var staticClassDeletedCtor = AstContext.Class("TestCheckStaticClassDeleted"); var staticClassDeletedCtor = AstContext.Class("TestCheckStaticClassDeleted");
var nonStaticClass = AstContext.Class("TestCheckNonStaticClass"); var nonStaticClass = AstContext.Class("TestCheckNonStaticClass");
var nonStaticEmptyClass = AstContext.Class("TestCommentsPass");
Assert.IsFalse(staticClass.IsStatic); Assert.IsFalse(staticClass.IsStatic);
Assert.IsFalse(staticStruct.IsStatic); Assert.IsFalse(staticStruct.IsStatic);
Assert.IsFalse(staticClassDeletedCtor.IsStatic); Assert.IsFalse(staticClassDeletedCtor.IsStatic);
Assert.IsFalse(nonStaticClass.IsStatic); Assert.IsFalse(nonStaticClass.IsStatic);
Assert.IsFalse(nonStaticEmptyClass.IsStatic);
passBuilder.AddPass(new CheckStaticClassPass()); passBuilder.AddPass(new CheckStaticClassPass());
passBuilder.RunPasses(pass => pass.VisitASTContext(AstContext)); passBuilder.RunPasses(pass => pass.VisitASTContext(AstContext));
@ -87,6 +89,7 @@ namespace CppSharp.Generator.Tests.Passes
Assert.IsTrue(staticClassDeletedCtor.IsStatic, "`TestCheckStaticClassDeleted` should be static"); Assert.IsTrue(staticClassDeletedCtor.IsStatic, "`TestCheckStaticClassDeleted` should be static");
Assert.IsFalse(nonStaticClass.IsStatic, "`TestCheckNonStaticClass` should NOT be static, since it has a private data field with default ctor"); Assert.IsFalse(nonStaticClass.IsStatic, "`TestCheckNonStaticClass` should NOT be static, since it has a private data field with default ctor");
Assert.IsFalse(nonStaticEmptyClass.IsStatic, "`TestCommentsPass` should NOT be static, since it doesn't have any static declarations");
} }
[Test] [Test]

26
src/Generator/Passes/CheckStaticClassPass.cs

@ -77,6 +77,25 @@ namespace CppSharp.Passes
if (@class.IsDependent) if (@class.IsDependent)
return false; return false;
// Polymorphic classes are currently not supported.
// TODO: We could support this if the base class is also static, since it's composition then.
if (@class.IsPolymorphic)
return false;
// Make sure we have at least one accessible static method or field
if (!@class.Methods.Any(m => m.Kind == CXXMethodKind.Normal && m.Access != AccessSpecifier.Private && m.IsStatic)
&& @class.Variables.All(v => v.Access == AccessSpecifier.Private))
return false;
// Check for any non-static fields or methods, in which case we
// assume the class is not meant to be static.
// Note: Static fields are represented as variables in the AST.
if (@class.Fields.Count != 0)
return false;
if (@class.Methods.Any(m => m.Kind == CXXMethodKind.Normal && !m.IsStatic))
return false;
if (@class.Constructors.Any(m => if (@class.Constructors.Any(m =>
{ {
// Implicit constructors are not user-defined, so assume this was unintentional. // Implicit constructors are not user-defined, so assume this was unintentional.
@ -97,13 +116,6 @@ namespace CppSharp.Passes
{ {
return false; return false;
} }
// Check for any non-static fields or methods, in which case we
// assume the class is not meant to be static.
// Note: Static fields are represented as variables in the AST.
if (@class.Fields.Any() ||
@class.Methods.Any(m => m.Kind == CXXMethodKind.Normal
&& !m.IsStatic))
return false;
// Check for any static function that return a pointer to the class. // Check for any static function that return a pointer to the class.
// If one exists, we assume it's a factory function and the class is // If one exists, we assume it's a factory function and the class is

Loading…
Cancel
Save