Skip to content

Commit cb07721

Browse files
committed
Only generate once
1 parent cf747cd commit cb07721

File tree

4 files changed

+10
-19
lines changed

4 files changed

+10
-19
lines changed

codehelp/CodeHelpers.Test/LogGeneratorFixerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public partial class MyNewClass
4646
";
4747
testString += InternalTypes;
4848
fixedCode += InternalTypes;
49-
var expected = Verify.Diagnostic(LoggerDiagnostics.MissingGenerateLog).WithLocation(4, 22).WithArguments(["Variable", "MyNewClass"]);
49+
var expected = Verify.Diagnostic(LoggerDiagnostics.MissingGenerateLog).WithLocation(4, 22).WithArguments(["MyNewClass"]);
5050
await Verify.VerifyCodeFixAsync(testString, expected, fixedCode);
5151
}
5252
}

codehelp/CodeHelpers/LogGenerator/Analyzer/LogGeneratorAnalyzer.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,6 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
3737
return;
3838
}
3939

40-
// Enumerate all members to check for [Log] without [GenerateLog]
41-
foreach (var member in namedTypeSymbol.GetMembers())
42-
{
43-
if (!member.HasLogAttribute())
44-
{
45-
continue;
46-
}
47-
48-
if (!namedTypeSymbol.HasGenerateLogAttribute())
49-
{
50-
foreach (var location in namedTypeSymbol.Locations)
51-
{
52-
context.ReportDiagnostic(Diagnostic.Create(LoggerDiagnostics.MissingGenerateLog, location, member.Name, namedTypeSymbol.ToDisplayString()));
53-
}
54-
}
55-
}
56-
5740
List<(FailureMode, ISymbol)> failures = [];
5841
Dictionary<LoggableMember, ISymbol> symbolMap = [];
5942

codehelp/CodeHelpers/LogGenerator/Analyzer/LoggerDiagnostics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Ids
4545
public static readonly DiagnosticDescriptor UnknownSpecialTypeIntArray = new(
4646
Ids.UnknownSpecialTypeIntArray, "Loggable member has invalid integer type for array use", "[Log] attribute cannot be applied to member '{0}'. Cannot log arrays of type '{1}'. Can only log arrays of 'long'.", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, "");
4747
public static readonly DiagnosticDescriptor MissingGenerateLog = new(
48-
Ids.MissingGenerateLog, "Type has Log member but is not GenerateLog", "Member '{0}' has [Log] attribute, however containing type {1} is not attributed with [GenerateLog]. No logging will be generated for this member.", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, "");
48+
Ids.MissingGenerateLog, "Type has Log member but is not GenerateLog", "Type {0} has a member annotated with [Log], but is not attributed with [GenerateLog]. No logging will be generated for this type.", Category, DiagnosticSeverity.Error, isEnabledByDefault: true, "");
4949

5050
public static void ReportDiagnostic(this SymbolAnalysisContext context, FailureMode failureMode, ISymbol symbol)
5151
{

codehelp/CodeHelpers/LogGenerator/LoggableType.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ public static void ExecuteAnalysis(this LoggableType? maybeType, SymbolAnalysisC
124124
// We're errored
125125
context.ReportDiagnostic(failureMode, symbolMap[call]);
126126
}
127+
128+
if (!loggableType.LoggableMembers.IsEmpty && !context.Symbol.HasGenerateLogAttribute())
129+
{
130+
foreach (var location in context.Symbol.Locations)
131+
{
132+
context.ReportDiagnostic(Diagnostic.Create(LoggerDiagnostics.MissingGenerateLog, location, context.Symbol.ToDisplayString()));
133+
}
134+
}
127135
}
128136
}
129137

0 commit comments

Comments
 (0)