Skip to content

Commit

Permalink
Generator cleanup before updating code gen.
Browse files Browse the repository at this point in the history
- Remove constraints about not starting log method names or log method
parameters with _, the code now dynamically picks non-conflicting
symbol names. There's still a potential conflict with the name of
redactor instances, but I'll be removing all those in my follow-on PR.

- Simplified logic around handling of template parameters. I couldn't
figure out what the code was doing, so it needed a bit of cleanup.

- Remove some dead code I stumbled on.

- Fix broken .resx file handling in the generators, I messed that up
when simplifying the Roslyn dependencies last week.
  • Loading branch information
Martin Taillefer committed Jul 29, 2023
1 parent 8f5680b commit 43dbb90
Show file tree
Hide file tree
Showing 25 changed files with 248 additions and 331 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<Compile Include="..\Shared\*.cs" LinkBase="Shared" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<Compile Include="..\Shared\EmitterBase.cs" LinkBase="Shared" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<Compile Include="..\Shared\*.cs" LinkBase="Shared" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis" />
Expand Down
120 changes: 57 additions & 63 deletions src/Generators/Microsoft.Gen.Logging/Emission/Emitter.Method.cs

Large diffs are not rendered by default.

30 changes: 28 additions & 2 deletions src/Generators/Microsoft.Gen.Logging/Emission/Emitter.Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal static string EscapeMessageStringForXmlDocumentation(string s)
internal static IReadOnlyCollection<string> GetLogPropertiesAttributes(LoggingMethod lm)
{
var result = new HashSet<string?>();
var parametersWithLogProps = lm.AllParameters.Where(x => x.HasProperties && !x.HasPropsProvider);
var parametersWithLogProps = lm.Parameters.Where(x => x.HasProperties && !x.HasPropsProvider);
foreach (var parameter in parametersWithLogProps)
{
parameter.TraverseParameterPropertiesTransitively((_, property) => result.Add(property.ClassificationAttributeType));
Expand All @@ -96,7 +96,7 @@ internal static string GetLoggerMethodLogLevel(LoggingMethod lm)

if (lm.Level == null)
{
foreach (var p in lm.AllParameters)
foreach (var p in lm.Parameters)
{
if (p.IsLogLevel)
{
Expand Down Expand Up @@ -150,4 +150,30 @@ internal static string GetLoggerMethodLogLevel(LoggingMethod lm)
}

internal static string EncodeTypeName(string typeName) => typeName.Replace("_", "__").Replace('.', '_');

internal static string PickUniqueName(string baseName, IEnumerable<string> potentialConflicts)
{
var name = baseName;
while (true)
{
bool conflict = false;
foreach (var potentialConflict in potentialConflicts)
{
if (name == potentialConflict)
{
conflict = true;
break;
}
}

if (!conflict)
{
return name;
}

#pragma warning disable S1643 // Strings should not be concatenated using '+' in a loop
name += "_";
#pragma warning restore S1643 // Strings should not be concatenated using '+' in a loop
}
}
}
11 changes: 4 additions & 7 deletions src/Generators/Microsoft.Gen.Logging/Emission/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ internal sealed partial class Emitter : EmitterBase

private readonly StringBuilderPool _sbPool = new();
private bool _isRedactorProviderInTheInstance;
private int _memberCounter;

public string Emit(IEnumerable<LoggingType> logTypes, CancellationToken cancellationToken)
{
_isRedactorProviderInTheInstance = false;
_memberCounter = 0;

foreach (var lt in logTypes.OrderBy(static lt => lt.Namespace + "." + lt.Name))
{
Expand Down Expand Up @@ -72,7 +70,7 @@ private void GenType(LoggingType lt)

var isRedactionRequired =
lt.Methods
.SelectMany(static lm => lm.AllParameters)
.SelectMany(static lm => lm.Parameters)
.Any(static lp => lp.ClassificationAttributeType != null)
|| lt.Methods
.SelectMany(static lm => GetLogPropertiesAttributes(lm))
Expand All @@ -81,7 +79,7 @@ private void GenType(LoggingType lt)
if (isRedactionRequired)
{
_isRedactorProviderInTheInstance = lt.Methods
.SelectMany(static lm => lm.AllParameters)
.SelectMany(static lm => lm.Parameters)
.All(static lp => !lp.IsRedactorProvider);

if (_isRedactorProviderInTheInstance)
Expand All @@ -105,7 +103,6 @@ private void GenType(LoggingType lt)
}

GenLogMethod(lm);
_memberCounter++;
}

OutCloseBrace();
Expand All @@ -129,7 +126,7 @@ private void GenAttributeClassifications(LoggingType lt)

var logPropsDataClasses = lt.Methods.SelectMany(lm => GetLogPropertiesAttributes(lm));
var classificationAttributeTypes = lt.Methods
.SelectMany(static lm => lm.AllParameters)
.SelectMany(static lm => lm.Parameters)
.Where(static lp => lp.ClassificationAttributeType is not null)
.Select(static lp => lp.ClassificationAttributeType!)
.Concat(logPropsDataClasses)
Expand All @@ -151,7 +148,7 @@ private void GenRedactorProperties(LoggingType lt)

var logPropsDataClasses = lt.Methods.SelectMany(lm => GetLogPropertiesAttributes(lm));
var classificationAttributeTypes = lt.Methods
.SelectMany(static lm => lm.AllParameters)
.SelectMany(static lm => lm.Parameters)
.Where(static lp => lp.ClassificationAttributeType is not null)
.Select(static lp => lp.ClassificationAttributeType!)
.Concat(logPropsDataClasses)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>Microsoft.Gen.Logging</RootNamespace>
<Description>Code generator to support Microsoft.Extensions.Telemetry's logging features.</Description>
Expand All @@ -11,10 +11,14 @@
</PropertyGroup>

<ItemGroup>
<Compile Update="Resources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Resources.resx" />
<Compile Update="Parsing\Resources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Resources.resx" />
<Compile Include="..\Shared\*.cs" LinkBase="Shared" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Parsing\Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis" />
Expand Down
7 changes: 3 additions & 4 deletions src/Generators/Microsoft.Gen.Logging/Model/LoggingMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ namespace Microsoft.Gen.Logging.Model;
/// </summary>
internal sealed class LoggingMethod
{
public readonly List<LoggingMethodParameter> AllParameters = new();
public readonly List<LoggingMethodParameter> TemplateParameters = new();
public readonly Dictionary<string, string> TemplateMap = new(StringComparer.OrdinalIgnoreCase);
public readonly List<LoggingMethodParameter> Parameters = new();
public readonly Dictionary<string, string> TemplateToParameterName = new(StringComparer.OrdinalIgnoreCase);
public string Name = string.Empty;
public string Message = string.Empty;
public int? Level;
Expand All @@ -30,7 +29,7 @@ internal sealed class LoggingMethod
public bool HasXmlDocumentation;

public string GetParameterNameInTemplate(LoggingMethodParameter parameter)
=> TemplateMap.TryGetValue(parameter.Name, out var value)
=> TemplateToParameterName.TryGetValue(parameter.Name, out var value)
? value
: parameter.Name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ internal sealed class LoggingMethodParameter
public bool IsNullable;
public bool IsReference;
public bool ImplementsIConvertible;
public bool ImplementsIFormatable;
public bool ImplementsIFormattable;
public bool SkipNullProperties;
public bool OmitParameterName;
public bool UsedAsTemplate;
public string? ClassificationAttributeType;
public List<LoggingProperty> PropertiesToLog = new();
public LoggingPropertyProvider? LogPropertiesProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed record LoggingProperty(
bool IsReference,
bool IsEnumerable,
bool ImplementsIConvertible,
bool ImplementsIFormatable,
bool ImplementsIFormattable,
IReadOnlyCollection<LoggingProperty> TransitiveMembers)
{
public string NameWithAt => NeedsAtSign ? "@" + Name : Name;
Expand Down
44 changes: 17 additions & 27 deletions src/Generators/Microsoft.Gen.Logging/Parsing/DiagDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ internal sealed class DiagDescriptors : DiagDescriptorsBase
{
private const string Category = "LogMethod";

public static DiagnosticDescriptor InvalidLoggingMethodName { get; } = Make(
id: "R9G000",
title: Resources.InvalidLoggingMethodNameTitle,
messageFormat: Resources.InvalidLoggingMethodNameMessage,
category: Category);
// R9G000 retired

public static DiagnosticDescriptor ShouldntMentionLogLevelInMessage { get; } = Make(
id: "R9G001",
Expand All @@ -23,13 +19,7 @@ internal sealed class DiagDescriptors : DiagDescriptorsBase
category: Category,
DiagnosticSeverity.Warning);

public static DiagnosticDescriptor InvalidLoggingMethodParameterName { get; } = Make(
id: "R9G002",
title: Resources.InvalidLoggingMethodParameterNameTitle,
messageFormat: Resources.InvalidLoggingMethodParameterNameMessage,
category: Category);

// R9G003 is no longer in use
// R9G002,R9G003 retired

public static DiagnosticDescriptor MissingRequiredType { get; } = Make(
id: "R9G004",
Expand All @@ -50,10 +40,10 @@ internal sealed class DiagDescriptors : DiagDescriptorsBase
messageFormat: Resources.LoggingMethodMustReturnVoidMessage,
category: Category);

public static DiagnosticDescriptor MissingLoggerArgument { get; } = Make(
public static DiagnosticDescriptor MissingLoggerParameter { get; } = Make(
id: "R9G007",
title: Resources.MissingLoggerArgumentTitle,
messageFormat: Resources.MissingLoggerArgumentMessage,
title: Resources.MissingLoggerParameterTitle,
messageFormat: Resources.MissingLoggerParameterMessage,
category: Category);

public static DiagnosticDescriptor LoggingMethodShouldBeStatic { get; } = Make(
Expand Down Expand Up @@ -89,16 +79,16 @@ internal sealed class DiagDescriptors : DiagDescriptorsBase
category: Category,
DiagnosticSeverity.Warning);

public static DiagnosticDescriptor TemplateHasNoCorrespondingArgument { get; } = Make(
public static DiagnosticDescriptor TemplateHasNoCorrespondingParameter { get; } = Make(
id: "R9G013",
title: Resources.TemplateHasNoCorrespondingArgumentTitle,
messageFormat: Resources.TemplateHasNoCorrespondingArgumentMessage,
title: Resources.TemplateHasNoCorrespondingParameterTitle,
messageFormat: Resources.TemplateHasNoCorrespondingParameterMessage,
category: Category);

public static DiagnosticDescriptor ArgumentHasNoCorrespondingTemplate { get; } = Make(
public static DiagnosticDescriptor ParameterHasNoCorrespondingTemplate { get; } = Make(
id: "R9G014",
title: Resources.ArgumentHasNoCorrespondingTemplateTitle,
messageFormat: Resources.ArgumentHasNoCorrespondingTemplateMessage,
title: Resources.ParameterHasNoCorrespondingTemplateTitle,
messageFormat: Resources.ParameterHasNoCorrespondingTemplateMessage,
category: Category,
DiagnosticSeverity.Info);

Expand Down Expand Up @@ -139,16 +129,16 @@ internal sealed class DiagDescriptors : DiagDescriptorsBase
messageFormat: Resources.MultipleDataClassificationAttributesMessage,
category: Category);

public static DiagnosticDescriptor MissingRedactorProviderArgument { get; } = Make(
public static DiagnosticDescriptor MissingRedactorProviderParameter { get; } = Make(
id: "R9G021",
title: Resources.MissingRedactorProviderArgumentTitle,
messageFormat: Resources.MissingRedactorProviderArgumentMessage,
title: Resources.MissingRedactorProviderParameterTitle,
messageFormat: Resources.MissingRedactorProviderParameterMessage,
category: Category);

public static DiagnosticDescriptor MissingDataClassificationArgument { get; } = Make(
public static DiagnosticDescriptor MissingDataClassificationParameter { get; } = Make(
id: "R9G022",
title: Resources.MissingDataClassificationArgumentTitle,
messageFormat: Resources.MissingDataClassificationArgumentMessage,
title: Resources.MissingDataClassificationParameterTitle,
messageFormat: Resources.MissingDataClassificationParameterMessage,
category: Category,
DiagnosticSeverity.Warning);

Expand Down
Loading

0 comments on commit 43dbb90

Please sign in to comment.