Skip to content

Commit 60131e5

Browse files
committed
Fix static being applied to classes without static methods/fields
1 parent 51f4c3c commit 60131e5

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/Generator.Tests/Passes/TestPasses.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ public void TestCheckStaticClassPass()
7373
var staticStruct = AstContext.Class("TestCheckStaticStruct");
7474
var staticClassDeletedCtor = AstContext.Class("TestCheckStaticClassDeleted");
7575
var nonStaticClass = AstContext.Class("TestCheckNonStaticClass");
76+
var nonStaticEmptyClass = AstContext.Class("TestCommentsPass");
7677

7778
Assert.IsFalse(staticClass.IsStatic);
7879
Assert.IsFalse(staticStruct.IsStatic);
7980
Assert.IsFalse(staticClassDeletedCtor.IsStatic);
8081
Assert.IsFalse(nonStaticClass.IsStatic);
82+
Assert.IsFalse(nonStaticEmptyClass.IsStatic);
8183

8284
passBuilder.AddPass(new CheckStaticClassPass());
8385
passBuilder.RunPasses(pass => pass.VisitASTContext(AstContext));
@@ -87,6 +89,7 @@ public void TestCheckStaticClassPass()
8789
Assert.IsTrue(staticClassDeletedCtor.IsStatic, "`TestCheckStaticClassDeleted` should be static");
8890

8991
Assert.IsFalse(nonStaticClass.IsStatic, "`TestCheckNonStaticClass` should NOT be static, since it has a private data field with default ctor");
92+
Assert.IsFalse(nonStaticEmptyClass.IsStatic, "`TestCommentsPass` should NOT be static, since it doesn't have any static declarations");
9093
}
9194

9295
[Test]

src/Generator/Passes/CheckStaticClassPass.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ public override bool VisitClassDecl(Class @class)
7777
if (@class.IsDependent)
7878
return false;
7979

80+
// Make sure we have at least one accessible static method or field
81+
if (!@class.Methods.Any(m => m.Kind == CXXMethodKind.Normal && m.Access != AccessSpecifier.Private && m.IsStatic)
82+
&& @class.Variables.All(v => v.Access == AccessSpecifier.Private))
83+
return false;
84+
85+
// Check for any non-static fields or methods, in which case we
86+
// assume the class is not meant to be static.
87+
// Note: Static fields are represented as variables in the AST.
88+
if (@class.Fields.Any())
89+
return false;
90+
91+
if (@class.Methods.Any(m => m.Kind == CXXMethodKind.Normal && !m.IsStatic))
92+
return false;
93+
8094
if (@class.Constructors.Any(m =>
8195
{
8296
// Implicit constructors are not user-defined, so assume this was unintentional.
@@ -97,13 +111,6 @@ public override bool VisitClassDecl(Class @class)
97111
{
98112
return false;
99113
}
100-
// Check for any non-static fields or methods, in which case we
101-
// assume the class is not meant to be static.
102-
// Note: Static fields are represented as variables in the AST.
103-
if (@class.Fields.Any() ||
104-
@class.Methods.Any(m => m.Kind == CXXMethodKind.Normal
105-
&& !m.IsStatic))
106-
return false;
107114

108115
// Check for any static function that return a pointer to the class.
109116
// If one exists, we assume it's a factory function and the class is

0 commit comments

Comments
 (0)