Skip to content

Commit

Permalink
Ignore statics in metric source-gen (#4843)
Browse files Browse the repository at this point in the history
  • Loading branch information
xakep139 committed Dec 29, 2023
2 parents f4315cd + b4c110d commit ac68568
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 222 deletions.
59 changes: 29 additions & 30 deletions src/Generators/Microsoft.Gen.Metrics/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ private static bool AreTagNamesValid(MetricMethod metricMethod)
: symbol.TypeArguments[0];

private static (InstrumentKind instrumentKind, ITypeSymbol? genericType) GetInstrumentType(
INamedTypeSymbol? methodAttributeSymbol,
SymbolHolder symbols)
INamedTypeSymbol? methodAttributeSymbol,
SymbolHolder symbols)
{
if (methodAttributeSymbol == null)
{
Expand Down Expand Up @@ -492,13 +492,11 @@ private string GetSymbolXmlCommentSummary(ISymbol symbol)
Diag(DiagDescriptors.ErrorMethodHasBody, methodSymbol.GetLocation());
keepMethod = false;
}
#pragma warning disable S2583 // Conditionally executed code should be reachable
else if (!isPartial)
{
Diag(DiagDescriptors.ErrorNotPartialMethod, methodSymbol.GetLocation());
keepMethod = false;
}
#pragma warning restore S2583 // Conditionally executed code should be reachable

// ensure Metric name is not empty and starts with a Capital letter.
// ensure there are no duplicate ids.
Expand Down Expand Up @@ -668,14 +666,14 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[
/// <summary>
/// Called recursively to build all required StrongTypeDimensionConfigs.
/// </summary>
/// <param name="symbol">The Symbol being extracted.</param>
/// <param name="member">The Symbol being extracted.</param>
/// <param name="typesChain">A set of symbols in the current type chain.</param>
/// <param name="tagHashSet">HashSet of all dimensions seen so far.</param>
/// <param name="symbols">Shared symbols.</param>
/// <param name="stringBuilder">List of all property names when walking down the object model. See StrongTypeDimensionConfigs for an example.</param>
/// <returns>List of all StrongTypeDimensionConfigs seen so far.</returns>
private List<StrongTypeConfig> BuildTagConfigs(
ISymbol symbol,
ISymbol member,
ISet<ITypeSymbol> typesChain,
HashSet<string> tagHashSet,
Dictionary<string, string> tagDescriptionDictionary,
Expand All @@ -688,23 +686,24 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[
SpecialType specialType;
ITypeSymbol typeSymbol;

if (symbol.IsImplicitlyDeclared)
if (member.IsImplicitlyDeclared ||
member.IsStatic)
{
return tagConfigs;
}

switch (symbol.Kind)
switch (member.Kind)
{
case SymbolKind.Property:
var propertySymbol = symbol as IPropertySymbol;
var propertySymbol = member as IPropertySymbol;

kind = propertySymbol!.Type.TypeKind;
specialType = propertySymbol.Type.SpecialType;
typeSymbol = propertySymbol.Type;
break;

case SymbolKind.Field:
var fieldSymbol = symbol as IFieldSymbol;
var fieldSymbol = member as IFieldSymbol;

kind = fieldSymbol!.Type.TypeKind;
specialType = fieldSymbol.Type.SpecialType;
Expand All @@ -726,28 +725,28 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[
{
if (kind == TypeKind.Enum)
{
var name = TryGetTagNameFromAttribute(symbol, symbols, out var tagName)
var name = TryGetTagNameFromAttribute(member, symbols, out var tagName)
? tagName
: symbol.Name;
: member.Name;

if (!tagHashSet.Add(name))
{
Diag(DiagDescriptors.ErrorDuplicateTagName, symbol.Locations[0], symbol.Name);
Diag(DiagDescriptors.ErrorDuplicateTagName, member.Locations[0], member.Name);
}
else
{
tagConfigs.Add(new StrongTypeConfig
{
Name = symbol.Name,
Name = member.Name,
Path = stringBuilder.ToString(),
TagName = name,
StrongTypeMetricObjectType = StrongTypeMetricObjectType.Enum
});

var xmlDefinition = GetSymbolXmlCommentSummary(symbol);
var xmlDefinition = GetSymbolXmlCommentSummary(member);
if (!string.IsNullOrEmpty(xmlDefinition))
{
tagDescriptionDictionary.Add(string.IsNullOrEmpty(tagName) ? symbol.Name : tagName, xmlDefinition);
tagDescriptionDictionary.Add(string.IsNullOrEmpty(tagName) ? member.Name : tagName, xmlDefinition);
}
}

Expand All @@ -758,28 +757,28 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[
{
if (specialType == SpecialType.System_String)
{
var name = TryGetTagNameFromAttribute(symbol, symbols, out var tagName)
var name = TryGetTagNameFromAttribute(member, symbols, out var tagName)
? tagName
: symbol.Name;
: member.Name;

if (!tagHashSet.Add(name))
{
Diag(DiagDescriptors.ErrorDuplicateTagName, symbol.Locations[0], symbol.Name);
Diag(DiagDescriptors.ErrorDuplicateTagName, member.Locations[0], member.Name);
}
else
{
tagConfigs.Add(new StrongTypeConfig
{
Name = symbol.Name,
Name = member.Name,
Path = stringBuilder.ToString(),
TagName = name,
StrongTypeMetricObjectType = StrongTypeMetricObjectType.String
});

var xmlDefinition = GetSymbolXmlCommentSummary(symbol);
var xmlDefinition = GetSymbolXmlCommentSummary(member);
if (!string.IsNullOrEmpty(xmlDefinition))
{
tagDescriptionDictionary.Add(string.IsNullOrEmpty(tagName) ? symbol.Name : tagName, xmlDefinition);
tagDescriptionDictionary.Add(string.IsNullOrEmpty(tagName) ? member.Name : tagName, xmlDefinition);
}
}

Expand All @@ -793,21 +792,21 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[

tagConfigs.Add(new StrongTypeConfig
{
Name = symbol.Name,
Name = member.Name,
Path = stringBuilder.ToString(),
StrongTypeMetricObjectType = StrongTypeMetricObjectType.Class
});

tagConfigs.AddRange(
WalkObjectModel(symbol, typesChain, namedTypeSymbol, stringBuilder,
WalkObjectModel(member, typesChain, namedTypeSymbol, stringBuilder,
tagHashSet, tagDescriptionDictionary, symbols, true));

return tagConfigs;
}
}
else
{
Diag(DiagDescriptors.ErrorInvalidTagNameType, symbol.Locations[0]);
Diag(DiagDescriptors.ErrorInvalidTagNameType, member.Locations[0]);
return tagConfigs;
}
}
Expand All @@ -816,28 +815,28 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[
{
if (typeSymbol is not INamedTypeSymbol namedTypeSymbol)
{
Diag(DiagDescriptors.ErrorInvalidTagNameType, symbol.Locations[0]);
Diag(DiagDescriptors.ErrorInvalidTagNameType, member.Locations[0]);
}
else
{
// User defined struct. First add into dimensionConfigs, then walk down the rest of the struct.
tagConfigs.Add(new StrongTypeConfig
{
Name = symbol.Name,
Name = member.Name,
Path = stringBuilder.ToString(),
StrongTypeMetricObjectType = StrongTypeMetricObjectType.Struct
});

tagConfigs.AddRange(
WalkObjectModel(symbol, typesChain, namedTypeSymbol, stringBuilder,
WalkObjectModel(member, typesChain, namedTypeSymbol, stringBuilder,
tagHashSet, tagDescriptionDictionary, symbols, false));
}

return tagConfigs;
}
else
{
Diag(DiagDescriptors.ErrorInvalidTagNameType, symbol.Locations[0]);
Diag(DiagDescriptors.ErrorInvalidTagNameType, member.Locations[0]);
return tagConfigs;
}
}
Expand All @@ -846,9 +845,9 @@ private void Diag(DiagnosticDescriptor desc, Location? location, params object?[
_builders.ReturnStringBuilder(stringBuilder);
}
}
#pragma warning disable S107 // Methods should not have too many parameters

// we can deal with this warning later
#pragma warning disable S107 // Methods should not have too many parameters
private List<StrongTypeConfig> WalkObjectModel(
ISymbol parentSymbol,
ISet<ITypeSymbol> typesChain,
Expand Down

0 comments on commit ac68568

Please sign in to comment.