Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest APIs for NativeMethods.txt when no match is found in the metadata #203

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,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));
}
}
}
}
}