Skip to content

Commit

Permalink
Finally fix up fixers, they generate the right logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Feb 19, 2024
1 parent bf53024 commit cf747cd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
7 changes: 5 additions & 2 deletions codehelp/CodeHelpers.Test/LogGeneratorFixerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public async void Test1()
{
string testString = @"
using Stereologue;
public partial class MyNewClass
{
[Log]
Expand All @@ -35,15 +36,17 @@ public partial class MyNewClass
";
string fixedCode = @"
using Stereologue;
[GenerateLog]
public partial class MyNewClass
{
[Log]
public int Variable { get; }
}";
}
";
testString += InternalTypes;
fixedCode += InternalTypes;
var expected = Verify.Diagnostic(LoggerDiagnostics.MissingGenerateLog).WithLocation(3, 22).WithArguments(["Variable", "MyNewClass"]);
var expected = Verify.Diagnostic(LoggerDiagnostics.MissingGenerateLog).WithLocation(4, 22).WithArguments(["Variable", "MyNewClass"]);
await Verify.VerifyCodeFixAsync(testString, expected, fixedCode);
}
}
38 changes: 17 additions & 21 deletions codehelp/CodeHelpers/LogGenerator/Analyzer/LogGeneratorAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ public override void Initialize(AnalysisContext context)
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
context.RegisterSymbolAction(AnalyzeMembers, SymbolKind.Method);
context.RegisterSymbolAction(AnalyzeMembers, SymbolKind.Field);
context.RegisterSymbolAction(AnalyzeMembers, SymbolKind.Property);
}

private static void AnalyzeMembers(SymbolAnalysisContext context)
{
if (!context.Symbol.HasLogAttribute()) {
return;
}

var containingType = context.Symbol.ContainingType;
if (containingType is null) {
return;
}

if (!containingType.HasGenerateLogAttribute()) {
foreach (var location in containingType.Locations) {
context.ReportDiagnostic(Diagnostic.Create(LoggerDiagnostics.MissingGenerateLog, location, context.Symbol.Name, containingType.ToDisplayString()));
}
}
}

private static void AnalyzeSymbol(SymbolAnalysisContext context)
Expand All @@ -58,6 +37,23 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
return;
}

// Enumerate all members to check for [Log] without [GenerateLog]
foreach (var member in namedTypeSymbol.GetMembers())
{
if (!member.HasLogAttribute())
{
continue;
}

if (!namedTypeSymbol.HasGenerateLogAttribute())
{
foreach (var location in namedTypeSymbol.Locations)
{
context.ReportDiagnostic(Diagnostic.Create(LoggerDiagnostics.MissingGenerateLog, location, member.Name, namedTypeSymbol.ToDisplayString()));
}
}
}

List<(FailureMode, ISymbol)> failures = [];
Dictionary<LoggableMember, ISymbol> symbolMap = [];

Expand Down
22 changes: 16 additions & 6 deletions codehelp/CodeHelpers/LogGenerator/CodeFixer/LogGeneratorFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using WPILib.CodeHelpers.LogGenerator.Analyzer;

namespace WPILib.CodeHelpers.LogGenerator.CodeFixer;

[ExportCodeFixProvider(LanguageNames.CSharp)]
public class LogGeneratorFixer : CodeFixProvider
{
private const string title = "Make constant";
private const string title = "Add GenerateLog";

public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create([
LoggerDiagnostics.Ids.MissingGenerateLog,
Expand Down Expand Up @@ -44,15 +45,24 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
private async Task<Document> AddGenerateLogAttribute(Document document, TypeDeclarationSyntax typeSyntax, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken)!;
SyntaxToken firstToken = typeSyntax.GetFirstToken();
SyntaxTriviaList leadingTrivia = firstToken.LeadingTrivia;
TypeDeclarationSyntax trimmedLocal = typeSyntax.ReplaceToken(
firstToken, firstToken.WithLeadingTrivia(SyntaxTriviaList.Empty));

var attributes = typeSyntax.AttributeLists.Add(
SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("GenerateLog"))
)));
)).WithLeadingTrivia(leadingTrivia));

var newLocal = trimmedLocal.WithAttributeLists(attributes);
var formattedLocal = newLocal.WithAdditionalAnnotations(Formatter.Annotation);

return document.WithSyntaxRoot(
root!.ReplaceNode(
var newRoot = root!.ReplaceNode(
typeSyntax,
typeSyntax.WithAttributeLists(attributes).NormalizeWhitespace()
));
formattedLocal
);

return document.WithSyntaxRoot(newRoot);
}
}
1 change: 0 additions & 1 deletion codehelp/CodeHelpers/WPILib.CodeHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<LangVersion>Latest</LangVersion>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.0.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit cf747cd

Please sign in to comment.