Skip to content

Commit

Permalink
Update to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Taillefer committed May 31, 2023
1 parent 3d9bb86 commit 6369c9f
Show file tree
Hide file tree
Showing 39 changed files with 1,798 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,32 @@ namespace Microsoft.Gen.ComplianceReports;
/// </summary>
[Generator]
[ExcludeFromCodeCoverage]
public sealed class Generator : ISourceGenerator
public sealed class ComplianceReportsGenerator : ISourceGenerator
{
private const string GenerateComplianceReportsMSBuildProperty = "build_property.GenerateComplianceReport";
private const string ComplianceReportOutputPathMSBuildProperty = "build_property.ComplianceReportOutputPath";

private string? _reportOutputPath;
private const string FallbackFileName = "ComplianceReport.json";

public Generator()
private string? _directory;
private string _fileName;

public ComplianceReportsGenerator()
: this(null)
{
}

public Generator(string? reportOutputPath)
public ComplianceReportsGenerator(string? filePath)
{
_reportOutputPath = reportOutputPath;
if (filePath is not null)
{
_directory = Path.GetDirectoryName(filePath);
_fileName = Path.GetFileName(filePath);
}
else
{
_fileName = FallbackFileName;
}
}

public void Initialize(GeneratorInitializationContext context)
Expand Down Expand Up @@ -70,21 +81,21 @@ public void Execute(GeneratorExecutionContext context)

context.CancellationToken.ThrowIfCancellationRequested();

if (_reportOutputPath == null)
if (_directory == null)
{
_ = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(ComplianceReportOutputPathMSBuildProperty, out _reportOutputPath);
if (string.IsNullOrWhiteSpace(_reportOutputPath))
_ = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(ComplianceReportOutputPathMSBuildProperty, out _directory);
if (string.IsNullOrWhiteSpace(_directory))
{
// no valid output path
return;
}
}

#pragma warning disable R9A017 // Switch to an asynchronous method for increased performance.
_ = Directory.CreateDirectory(Path.GetDirectoryName(_reportOutputPath));
_ = Directory.CreateDirectory(_directory);

// Write properties to CSV file.
File.WriteAllText(_reportOutputPath, report);
// Write report as JSON file.
File.WriteAllText(Path.Combine(_directory, _fileName), report);
#pragma warning restore R9A017 // Switch to an asynchronous method for increased performance.
}
}
47 changes: 45 additions & 2 deletions src/Generators/Microsoft.Gen.ComplianceReports/Common/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public IReadOnlyList<ClassifiedType> GetClassifiedTypes(IEnumerable<TypeDeclarat
{
Dictionary<string, ClassifiedItem>? classifiedMembers = null;

// include the annotations provided in record constructor parameters
if (typeSyntax is RecordDeclarationSyntax recordSyntax)
{
classifiedMembers = GetClassifiedMembers(recordSyntax, classifiedMembers, sm);
}

// grab the annotated members
classifiedMembers = GetClassifiedMembers(typeSymbol, classifiedMembers);

Expand Down Expand Up @@ -94,6 +100,43 @@ private static string FormatType(ITypeSymbol typeSymbol)
return result;
}

private Dictionary<string, ClassifiedItem>? GetClassifiedMembers(RecordDeclarationSyntax recordSyntax,
Dictionary<string, ClassifiedItem>? classifiedMembers,
SemanticModel sm)
{
var parameters = recordSyntax.ParameterList?.Parameters;
if (parameters != null)
{
foreach (var parameter in parameters)
{
var ps = sm.GetDeclaredSymbol(parameter);
if (ps == null)
{
continue;
}

foreach (var attribute in ps!.GetAttributes())
{
ClassifiedItem? ci = null;
ci = AppendAttributeClassifications(ci, attribute);
if (ci != null)
{
FileLinePositionSpan fileLine = ps.Locations[0].GetLineSpan();
ci.SourceFilePath = fileLine.Path;
ci.SourceLine = fileLine.StartLinePosition.Line + 1;
ci.Name = ps.Name;
ci.TypeName = FormatType(ps.Type);

classifiedMembers ??= new();
classifiedMembers[ci.Name] = ci;
}
}
}
}

return classifiedMembers;
}

private Dictionary<string, ClassifiedItem>? GetClassifiedMembers(ITypeSymbol typeSymbol, Dictionary<string, ClassifiedItem>? classifiedMembers)
{
foreach (var property in typeSymbol.GetMembers().OfType<IPropertySymbol>())
Expand Down Expand Up @@ -131,7 +174,7 @@ private static string FormatType(ITypeSymbol typeSymbol)
ci = AppendAttributeClassifications(ci, attribute);
}

// classificaiton coming from the member's attributes
// classification coming from the member's attributes
foreach (AttributeData attribute in member.GetAttributes())
{
ci = AppendAttributeClassifications(ci, attribute);
Expand Down Expand Up @@ -188,7 +231,7 @@ private static string FormatType(ITypeSymbol typeSymbol)
ci = AppendAttributeClassifications(ci, attribute);
}

// classificaiton coming from the parameter's attributes
// classification coming from the parameter's attributes
foreach (AttributeData attribute in p.GetAttributes())
{
ci = AppendAttributeClassifications(ci, attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Microsoft.Gen.ComplianceReports;

/// <summary>
/// Holds required symbols for the <see cref="Generator"/>.
/// Holds required symbols for the <see cref="ComplianceReportsGenerator"/>.
/// </summary>
internal sealed record class SymbolHolder(
INamedTypeSymbol DataClassificationAttributeSymbol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.Gen.ContextualOptions;

[Generator]
[ExcludeFromCodeCoverage]
public class Generator : IIncrementalGenerator
public class ContextualOptionsGenerator : IIncrementalGenerator
{
private static readonly HashSet<string> _attributeNames = new()
{
Expand Down Expand Up @@ -78,7 +78,7 @@ namespace Microsoft.Gen.ContextualOptions;
/// </summary>
[Generator]
[ExcludeFromCodeCoverage]
public class Generator : ISourceGenerator
public class ContextualOptionsGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context) =>
context.RegisterForSyntaxNotifications(() => new ContextReceiver(context.CancellationToken));
Expand Down
12 changes: 12 additions & 0 deletions src/Generators/Microsoft.Gen.Metering/Common/DiagDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,16 @@ internal sealed class DiagDescriptors : DiagDescriptorsBase
title: Resources.ErrorInvalidMethodReturnTypeArityTitle,
messageFormat: Resources.ErrorInvalidMethodReturnTypeArityMessage,
category: Category);

public static DiagnosticDescriptor ErrorGaugeNotSupported { get; } = Make(
id: "R9G069",
title: Resources.ErrorGaugeNotSupportedTitle,
messageFormat: Resources.ErrorGaugeNotSupportedMessage,
category: Category);

public static DiagnosticDescriptor ErrorXmlNotLoadedCorrectly { get; } = Make(
id: "R9G070",
title: Resources.ErrorXmlNotLoadedCorrectlyTitle,
messageFormat: Resources.ErrorXmlNotLoadedCorrectlyMessage,
category: Category);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ namespace Microsoft.Gen.Metering;

[Generator]
[ExcludeFromCodeCoverage]
public class Generator : IIncrementalGenerator
public class MeteringGenerator : IIncrementalGenerator
{
private static readonly HashSet<string> _attributeNames = new()
{
SymbolLoader.CounterAttribute,
SymbolLoader.CounterTAttribute.Replace("`1", "<T>"),
SymbolLoader.HistogramAttribute,
SymbolLoader.HistogramTAttribute.Replace("`1", "<T>")
SymbolLoader.HistogramTAttribute.Replace("`1", "<T>"),
SymbolLoader.GaugeAttribute
};

public void Initialize(IncrementalGeneratorInitializationContext context)
Expand Down Expand Up @@ -55,13 +56,14 @@ private static void HandleAnnotatedTypes(Compilation compilation, IEnumerable<Sy
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Gen.Metering;
using Microsoft.Gen.Shared;

namespace Microsoft.Gen.Metering;

[Generator]
[ExcludeFromCodeCoverage]
public class Generator : ISourceGenerator
public class MeteringGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ internal sealed class MetricMethod
{
public readonly List<MetricParameter> AllParameters = new();
public HashSet<string> DimensionsKeys = new();
public Dictionary<string, string> DimensionDescriptionDictionary = new();
public string? Name;
public string? MetricName;
public string? XmlDefinition;
public bool IsExtensionMethod;
public string Modifiers = string.Empty;
public string MetricTypeModifiers = string.Empty;
Expand Down
Loading

0 comments on commit 6369c9f

Please sign in to comment.