Skip to content

Commit 804f694

Browse files
committed
Add UsePropertyDetectionHeuristics option to DriverOptions.
1 parent 511ec16 commit 804f694

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/Generator/Options.cs

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

165+
public bool UsePropertyDetectionHeuristics { get; set; } = true;
166+
165167
#endregion
166168
}
167169

src/Generator/Passes/GetterSetterToPropertyPass.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ 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(
2628
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated))
2729
DistributeMethod(method);
@@ -266,16 +268,21 @@ private void DistributeMethod(Method method)
266268
}
267269
}
268270

269-
private static bool IsGetter(Method method)
271+
private bool IsGetter(Method method)
270272
{
271273
if (method.IsDestructor ||
272274
(method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void)) ||
273275
method.Parameters.Any(p => p.Kind != ParameterKind.IndirectReturnType))
274276
return false;
275277
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));
278+
279+
if (firstWord.Length < method.Name.Length && Match(firstWord, new[] {"get", "is", "has"}))
280+
return true;
281+
282+
if (useHeuristics && !Match(firstWord, new[] {"to", "new"}) && !verbs.Contains(firstWord))
283+
return true;
284+
285+
return false;
279286
}
280287

281288
private static bool Match(string prefix, IEnumerable<string> prefixes)
@@ -354,7 +361,7 @@ public GetterSetterToPropertyPass()
354361
public override bool VisitClassDecl(Class @class)
355362
{
356363
if (base.VisitClassDecl(@class))
357-
new PropertyGenerator(@class).GenerateProperties();
364+
new PropertyGenerator(@class, Options.UsePropertyDetectionHeuristics).GenerateProperties();
358365
return false;
359366
}
360367
}

0 commit comments

Comments
 (0)