Skip to content

Commit 9830302

Browse files
authored
Merge pull request #1028 from rokups/feature/customize-getter-setter-pass
GetterSetterToPropertyPass customization
2 parents 67b241d + a03a0ea commit 9830302

File tree

5 files changed

+73
-11
lines changed

5 files changed

+73
-11
lines changed

src/AST/Method.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public Method(Method method)
105105
SynthKind = method.SynthKind;
106106
AdjustedOffset = method.AdjustedOffset;
107107
OverriddenMethods.AddRange(method.OverriddenMethods);
108+
ConvertToProperty = method.ConvertToProperty;
108109
}
109110

110111
public Method(Function function)
@@ -172,6 +173,8 @@ public bool IsDestructor
172173

173174
public List<Method> OverriddenMethods { get; } = new List<Method>();
174175

176+
public bool ConvertToProperty { get; set; }
177+
175178
public Method GetRootBaseMethod()
176179
{
177180
return BaseMethod == null || BaseMethod.BaseMethod == null ?
@@ -185,4 +188,4 @@ public override T Visit<T>(IDeclVisitor<T> visitor)
185188

186189
private bool? isOverride;
187190
}
188-
}
191+
}

src/Generator/Library.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text.RegularExpressions;
66
using CppSharp.AST;
7+
using CppSharp.Passes;
78

89
namespace CppSharp
910
{
@@ -351,6 +352,53 @@ public static void IgnoreClassField(this ASTContext context, string name, string
351352
}
352353
}
353354

355+
private static IEnumerable<Class> GetClasses(DeclarationContext decl)
356+
{
357+
foreach (var @class in decl.Classes)
358+
{
359+
yield return @class;
360+
361+
foreach (var class2 in GetClasses(@class))
362+
yield return class2;
363+
}
364+
365+
foreach (var ns in decl.Namespaces)
366+
{
367+
foreach (var @class in GetClasses(ns))
368+
yield return @class;
369+
}
370+
}
371+
372+
public static void IgnoreConversionToProperty(this ASTContext context, string pattern)
373+
{
374+
foreach (var unit in context.TranslationUnits)
375+
{
376+
foreach (var @class in GetClasses(unit))
377+
{
378+
foreach (var method in @class.Methods)
379+
{
380+
if (Regex.Match(method.QualifiedLogicalOriginalName, pattern).Success)
381+
method.ExcludeFromPasses.Add(typeof(GetterSetterToPropertyPass));
382+
}
383+
}
384+
}
385+
}
386+
387+
public static void ForceConversionToProperty(this ASTContext context, string pattern)
388+
{
389+
foreach (var unit in context.TranslationUnits)
390+
{
391+
foreach (var @class in GetClasses(unit))
392+
{
393+
foreach (var method in @class.Methods)
394+
{
395+
if (Regex.Match(method.QualifiedLogicalOriginalName, pattern).Success)
396+
method.ConvertToProperty = true;
397+
}
398+
}
399+
}
400+
}
401+
354402
#endregion
355403

356404
#region Module Helpers
@@ -379,4 +427,4 @@ public static void IgnoreHeadersWithName(this ASTContext context, string pattern
379427

380428
#endregion
381429
}
382-
}
430+
}

src/Generator/Options.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ public bool DoAllModulesHaveLibraries() =>
151151
/// </summary>
152152
public HashSet<string> ExplicitlyPatchedVirtualFunctions { get; }
153153

154+
public bool UsePropertyDetectionHeuristics { get; set; } = true;
155+
154156
#endregion
155157
}
156158

src/Generator/Passes/GetterSetterToPropertyPass.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ private class PropertyGenerator
1919
private readonly List<Method> setters = new List<Method>();
2020
private readonly List<Method> setMethods = new List<Method>();
2121
private readonly List<Method> nonSetters = new List<Method>();
22+
private bool useHeuristics = true;
2223

23-
public PropertyGenerator(Class @class)
24+
public PropertyGenerator(Class @class, bool useHeuristics)
2425
{
26+
this.useHeuristics = useHeuristics;
2527
foreach (var method in @class.Methods.Where(
26-
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated))
28+
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated &&
29+
!m.ExcludeFromPasses.Contains(typeof(GetterSetterToPropertyPass))))
2730
DistributeMethod(method);
2831
}
2932

@@ -259,23 +262,28 @@ private void DistributeMethod(Method method)
259262
}
260263
else
261264
{
262-
if (IsGetter(method))
265+
if (method.ConvertToProperty || IsGetter(method))
263266
getters.Add(method);
264267
if (method.Parameters.All(p => p.Kind == ParameterKind.IndirectReturnType))
265268
nonSetters.Add(method);
266269
}
267270
}
268271

269-
private static bool IsGetter(Method method)
272+
private bool IsGetter(Method method)
270273
{
271274
if (method.IsDestructor ||
272275
(method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void)) ||
273276
method.Parameters.Any(p => p.Kind != ParameterKind.IndirectReturnType))
274277
return false;
275278
var firstWord = GetFirstWord(method.Name);
276-
return (firstWord.Length < method.Name.Length &&
277-
Match(firstWord, new[] { "get", "is", "has" })) ||
278-
(!Match(firstWord, new[] { "to", "new" }) && !verbs.Contains(firstWord));
279+
280+
if (firstWord.Length < method.Name.Length && Match(firstWord, new[] {"get", "is", "has"}))
281+
return true;
282+
283+
if (useHeuristics && !Match(firstWord, new[] {"to", "new"}) && !verbs.Contains(firstWord))
284+
return true;
285+
286+
return false;
279287
}
280288

281289
private static bool Match(string prefix, IEnumerable<string> prefixes)
@@ -354,7 +362,7 @@ public GetterSetterToPropertyPass()
354362
public override bool VisitClassDecl(Class @class)
355363
{
356364
if (base.VisitClassDecl(@class))
357-
new PropertyGenerator(@class).GenerateProperties();
365+
new PropertyGenerator(@class, Options.UsePropertyDetectionHeuristics).GenerateProperties();
358366
return false;
359367
}
360368
}

src/Generator/Passes/verbs.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7322,6 +7322,7 @@ ruckle
73227322
ruddle
73237323
ruggedize
73247324
ruminate
7325+
run
73257326
runtgenize
73267327
ruralise
73277328
ruralize
@@ -8828,4 +8829,4 @@ youthen
88288829
zap
88298830
zincify
88308831
zindabad
8831-
zipping
8832+
zipping

0 commit comments

Comments
 (0)