Skip to content

Commit

Permalink
Merge pull request #203 from microsoft/fix196
Browse files Browse the repository at this point in the history
Suggest APIs for NativeMethods.txt when no match is found in the metadata
  • Loading branch information
AArnott committed Mar 17, 2021
2 parents c5d596c + 1cfa5bc commit 4c3c8f4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,38 @@ public bool TryGenerateConstant(string name)
return false;
}

/// <summary>
/// Produces a sequence of suggested APIs with a similar name to the specified one.
/// </summary>
/// <param name="name">The user-supplied name.</param>
/// <returns>A sequence of API names.</returns>
public IEnumerable<string> GetSuggestions(string name)
{
if (name is null)
{
throw new ArgumentNullException(nameof(name));
}

// Trim suffixes off the name.
var suffixes = new List<string> { "A", "W", "32", "64", "Ex" };
foreach (string suffix in suffixes)
{
if (name.EndsWith(suffix, StringComparison.Ordinal))
{
name = name.Substring(0, name.Length - suffix.Length);
}
}

// We should match on any API for which the given string is a substring.
foreach (string candidate in this.fieldsByName.Keys.Concat(this.typesByName.Keys).Concat(this.methodsByName.Keys))
{
if (candidate.Contains(name))
{
yield return candidate;
}
}
}

/// <summary>
/// Collects the result of code generation.
/// </summary>
Expand Down
38 changes: 36 additions & 2 deletions src/Microsoft.Windows.CsWin32/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public class SourceGenerator : ISourceGenerator
DiagnosticSeverity.Warning,
isEnabledByDefault: true);

private static readonly DiagnosticDescriptor NoMatchingMethodOrTypeWithSuggestions = new DiagnosticDescriptor(
"PInvoke001",
"No matching method or type found",
"Method or type \"{0}\" not found. Did you mean {1}?",
"Functionality",
DiagnosticSeverity.Warning,
isEnabledByDefault: true);

private static readonly DiagnosticDescriptor NoMethodsForModule = new DiagnosticDescriptor(
"PInvoke001",
"No module found",
Expand Down Expand Up @@ -152,12 +160,12 @@ public void Execute(GeneratorExecutionContext context)
context.ReportDiagnostic(Diagnostic.Create(UseEnumValueDeclaringType, location, declaringEnum));
if (!generator.TryGenerate(declaringEnum, context.CancellationToken))
{
context.ReportDiagnostic(Diagnostic.Create(NoMatchingMethodOrType, location, declaringEnum));
ReportNoMatch(location, declaringEnum);
}
}
else
{
context.ReportDiagnostic(Diagnostic.Create(NoMatchingMethodOrType, location, name));
ReportNoMatch(location, name);
}
}
}
Expand All @@ -167,6 +175,32 @@ public void Execute(GeneratorExecutionContext context)
{
context.AddSource(unit.Key, unit.Value.ToFullString());
}

void ReportNoMatch(Location? location, string failedAttempt)
{
var suggestions = generator.GetSuggestions(failedAttempt).Take(4).ToList();
if (suggestions.Count > 0)
{
var suggestionBuilder = new StringBuilder();
for (int i = 0; i < suggestions.Count; i++)
{
if (i > 0)
{
suggestionBuilder.Append(i < suggestions.Count - 1 ? ", " : " or ");
}

suggestionBuilder.Append('"');
suggestionBuilder.Append(suggestions[i]);
suggestionBuilder.Append('"');
}

context.ReportDiagnostic(Diagnostic.Create(NoMatchingMethodOrTypeWithSuggestions, location, failedAttempt, suggestionBuilder));
}
else
{
context.ReportDiagnostic(Diagnostic.Create(NoMatchingMethodOrType, location, failedAttempt));
}
}
}
}
}

0 comments on commit 4c3c8f4

Please sign in to comment.