Skip to content

Commit 777fd6c

Browse files
committed
Remove more allocations
1 parent 2a8d1db commit 777fd6c

11 files changed

+80
-85
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/INotifyPropertyChangedGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ public INotifyPropertyChangedGenerator()
3636
INotifyPropertyChangedInfo? info = null;
3737

3838
// Check if the type already implements INotifyPropertyChanged
39-
if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedName("global::System.ComponentModel.INotifyPropertyChanged")))
39+
if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedMetadataName("System.ComponentModel.INotifyPropertyChanged")))
4040
{
4141
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(DuplicateINotifyPropertyChangedInterfaceForINotifyPropertyChangedAttributeError, typeSymbol, typeSymbol));
4242

4343
goto End;
4444
}
4545

4646
// Check if the type uses [INotifyPropertyChanged] or [ObservableObject] already (in the type hierarchy too)
47-
if (typeSymbol.HasOrInheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") ||
48-
typeSymbol.InheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute"))
47+
if (typeSymbol.HasOrInheritsAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") ||
48+
typeSymbol.InheritsAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute"))
4949
{
5050
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(InvalidAttributeCombinationForINotifyPropertyChangedAttributeError, typeSymbol, typeSymbol));
5151

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableObjectGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ private protected override int ValidateTargetTypeAndGetInfo(INamedTypeSymbol typ
3232
diagnostics = ImmutableArray<DiagnosticInfo>.Empty;
3333

3434
// Check if the type already implements INotifyPropertyChanged...
35-
if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedName("global::System.ComponentModel.INotifyPropertyChanged")))
35+
if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedMetadataName("System.ComponentModel.INotifyPropertyChanged")))
3636
{
3737
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(DuplicateINotifyPropertyChangedInterfaceForObservableObjectAttributeError, typeSymbol, typeSymbol));
3838

3939
goto End;
4040
}
4141

4242
// ...or INotifyPropertyChanging
43-
if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedName("global::System.ComponentModel.INotifyPropertyChanging")))
43+
if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedMetadataName("System.ComponentModel.INotifyPropertyChanging")))
4444
{
4545
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(DuplicateINotifyPropertyChangingInterfaceForObservableObjectAttributeError, typeSymbol, typeSymbol));
4646

4747
goto End;
4848
}
4949

5050
// Check if the type uses [INotifyPropertyChanged] or [ObservableObject] already (in the type hierarchy too)
51-
if (typeSymbol.InheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") ||
52-
typeSymbol.HasOrInheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute"))
51+
if (typeSymbol.InheritsAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") ||
52+
typeSymbol.HasOrInheritsAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute"))
5353
{
5454
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(InvalidAttributeCombinationForObservableObjectAttributeError, typeSymbol, typeSymbol));
5555

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 31 additions & 31 deletions
Large diffs are not rendered by default.

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
124124
// Filter only the type symbols with [NotifyPropertyChangedRecipients] and create diagnostics for them
125125
IncrementalValuesProvider<Diagnostic> notifyRecipientsErrors =
126126
classSymbols
127-
.Where(static item => item.HasAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.NotifyPropertyChangedRecipientsAttribute"))
127+
.Where(static item => item.HasAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.NotifyPropertyChangedRecipientsAttribute"))
128128
.Select(static (item, _) => Execute.GetIsNotifyingRecipientsDiagnosticForType(item))
129129
.Where(static item => item is not null)!;
130130

@@ -134,7 +134,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
134134
// Filter only the type symbols with [NotifyDataErrorInfo] and create diagnostics for them
135135
IncrementalValuesProvider<Diagnostic> notifyDataErrorInfoErrors =
136136
classSymbols
137-
.Where(static item => item.HasAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.NotifyDataErrorInfoAttribute"))
137+
.Where(static item => item.HasAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.NotifyDataErrorInfoAttribute"))
138138
.Select(static (item, _) => Execute.GetIsNotifyDataErrorInfoDiagnosticForType(item))
139139
.Where(static item => item is not null)!;
140140

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public ObservableRecipientGenerator()
3838
ObservableRecipientInfo? info = null;
3939

4040
// Check if the type already inherits from ObservableRecipient
41-
if (typeSymbol.InheritsFromFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableRecipient"))
41+
if (typeSymbol.InheritsFromFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableRecipient"))
4242
{
4343
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(DuplicateObservableRecipientError, typeSymbol, typeSymbol));
4444

4545
goto End;
4646
}
4747

4848
// Check if the type already inherits [ObservableRecipient]
49-
if (typeSymbol.InheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableRecipientAttribute"))
49+
if (typeSymbol.InheritsAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableRecipientAttribute"))
5050
{
5151
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(InvalidAttributeCombinationForObservableRecipientAttributeError, typeSymbol, typeSymbol));
5252

@@ -55,10 +55,10 @@ public ObservableRecipientGenerator()
5555

5656
// In order to use [ObservableRecipient], the target type needs to inherit from ObservableObject,
5757
// or be annotated with [ObservableObject] or [INotifyPropertyChanged] (with additional helpers).
58-
if (!typeSymbol.InheritsFromFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObject") &&
59-
!typeSymbol.HasOrInheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") &&
58+
if (!typeSymbol.InheritsFromFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableObject") &&
59+
!typeSymbol.HasOrInheritsAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") &&
6060
!typeSymbol.HasOrInheritsAttribute(static a =>
61-
a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute") == true &&
61+
a.AttributeClass?.HasFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute") == true &&
6262
!a.HasNamedArgument("IncludeAdditionalHelperMethods", false)))
6363
{
6464
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(MissingBaseObservableObjectFunctionalityError, typeSymbol, typeSymbol));
@@ -70,7 +70,7 @@ public ObservableRecipientGenerator()
7070
string typeName = typeSymbol.Name;
7171
bool hasExplicitConstructors = !(typeSymbol.InstanceConstructors.Length == 1 && typeSymbol.InstanceConstructors[0] is { Parameters.IsEmpty: true, IsImplicitlyDeclared: true });
7272
bool isAbstract = typeSymbol.IsAbstract;
73-
bool isObservableValidator = typeSymbol.InheritsFromFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableValidator");
73+
bool isObservableValidator = typeSymbol.InheritsFromFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableValidator");
7474
bool isRequiresUnreferencedCodeAttributeAvailable = compilation.HasAccessibleTypeWithMetadataName("System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute");
7575
bool hasOnActivatedMethod = typeSymbol.GetMembers().Any(m => m is IMethodSymbol { Parameters.IsEmpty: true, Name: "OnActivated" });
7676
bool hasOnDeactivatedMethod = typeSymbol.GetMembers().Any(m => m is IMethodSymbol { Parameters.IsEmpty: true, Name: "OnDeactivated" });

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.Execute.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private static class Execute
3131
/// <returns>Whether <paramref name="typeSymbol"/> inherits from <c>ObservableValidator</c>.</returns>
3232
public static bool IsObservableValidator(INamedTypeSymbol typeSymbol)
3333
{
34-
return typeSymbol.InheritsFromFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableValidator");
34+
return typeSymbol.InheritsFromFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableValidator");
3535
}
3636

3737
/// <summary>
@@ -56,15 +56,15 @@ public static ValidationInfo GetInfo(INamedTypeSymbol typeSymbol)
5656
// all generators run in an undefined order and looking at the same original compilation, so the
5757
// current one wouldn't be able to see generated properties from other generators directly.
5858
if (memberSymbol is IFieldSymbol &&
59-
!attributes.Any(static a => a.AttributeClass?.HasFullyQualifiedName(
60-
"global::CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") == true))
59+
!attributes.Any(static a => a.AttributeClass?.HasFullyQualifiedMetadataName(
60+
"CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") == true))
6161
{
6262
continue;
6363
}
6464

6565
// Skip the current member if there are no validation attributes applied to it
66-
if (!attributes.Any(a => a.AttributeClass?.InheritsFromFullyQualifiedName(
67-
"global::System.ComponentModel.DataAnnotations.ValidationAttribute") == true))
66+
if (!attributes.Any(a => a.AttributeClass?.InheritsFromFullyQualifiedMetadataName(
67+
"System.ComponentModel.DataAnnotations.ValidationAttribute") == true))
6868
{
6969
continue;
7070
}

CommunityToolkit.Mvvm.SourceGenerators/Extensions/ISymbolExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ public static bool HasFullyQualifiedName(this ISymbol symbol, string name)
4747
}
4848

4949
/// <summary>
50-
/// Checks whether or not a given symbol has an attribute with the specified full name.
50+
/// Checks whether or not a given symbol has an attribute with the specified fully qualified metadata name.
5151
/// </summary>
5252
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
5353
/// <param name="name">The attribute name to look for.</param>
5454
/// <returns>Whether or not <paramref name="symbol"/> has an attribute with the specified name.</returns>
55-
public static bool HasAttributeWithFullyQualifiedName(this ISymbol symbol, string name)
55+
public static bool HasAttributeWithFullyQualifiedMetadataName(this ISymbol symbol, string name)
5656
{
5757
ImmutableArray<AttributeData> attributes = symbol.GetAttributes();
5858

5959
foreach (AttributeData attribute in attributes)
6060
{
61-
if (attribute.AttributeClass?.HasFullyQualifiedName(name) == true)
61+
if (attribute.AttributeClass?.HasFullyQualifiedMetadataName(name) == true)
6262
{
6363
return true;
6464
}
@@ -69,19 +69,19 @@ public static bool HasAttributeWithFullyQualifiedName(this ISymbol symbol, strin
6969

7070
#if !ROSLYN_4_3_1_OR_GREATER
7171
/// <summary>
72-
/// Tries to get an attribute with the specified full name.
72+
/// Tries to get an attribute with the specified fully qualified metadata name.
7373
/// </summary>
7474
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
7575
/// <param name="name">The attribute name to look for.</param>
7676
/// <param name="attributeData">The resulting attribute, if it was found.</param>
7777
/// <returns>Whether or not <paramref name="symbol"/> has an attribute with the specified name.</returns>
78-
public static bool TryGetAttributeWithFullyQualifiedName(this ISymbol symbol, string name, [NotNullWhen(true)] out AttributeData? attributeData)
78+
public static bool TryGetAttributeWithFullyQualifiedMetadataName(this ISymbol symbol, string name, [NotNullWhen(true)] out AttributeData? attributeData)
7979
{
8080
ImmutableArray<AttributeData> attributes = symbol.GetAttributes();
8181

8282
foreach (AttributeData attribute in attributes)
8383
{
84-
if (attribute.AttributeClass?.HasFullyQualifiedName(name) == true)
84+
if (attribute.AttributeClass?.HasFullyQualifiedMetadataName(name) == true)
8585
{
8686
attributeData = attribute;
8787

CommunityToolkit.Mvvm.SourceGenerators/Extensions/ITypeSymbolExtensions.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ internal static class ITypeSymbolExtensions
2020
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
2121
/// <param name="name">The full name of the type to check for inheritance.</param>
2222
/// <returns>Whether or not <paramref name="typeSymbol"/> is or inherits from <paramref name="name"/>.</returns>
23-
public static bool HasOrInheritsFromFullyQualifiedName(this ITypeSymbol typeSymbol, string name)
23+
public static bool HasOrInheritsFromFullyQualifiedMetadataName(this ITypeSymbol typeSymbol, string name)
2424
{
2525
for (ITypeSymbol? currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType)
2626
{
27-
if (currentType.HasFullyQualifiedName(name))
27+
if (currentType.HasFullyQualifiedMetadataName(name))
2828
{
2929
return true;
3030
}
@@ -39,13 +39,13 @@ public static bool HasOrInheritsFromFullyQualifiedName(this ITypeSymbol typeSymb
3939
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
4040
/// <param name="name">The full name of the type to check for inheritance.</param>
4141
/// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="name"/>.</returns>
42-
public static bool InheritsFromFullyQualifiedName(this ITypeSymbol typeSymbol, string name)
42+
public static bool InheritsFromFullyQualifiedMetadataName(this ITypeSymbol typeSymbol, string name)
4343
{
4444
INamedTypeSymbol? baseType = typeSymbol.BaseType;
4545

4646
while (baseType != null)
4747
{
48-
if (baseType.HasFullyQualifiedName(name))
48+
if (baseType.HasFullyQualifiedMetadataName(name))
4949
{
5050
return true;
5151
}
@@ -62,11 +62,11 @@ public static bool InheritsFromFullyQualifiedName(this ITypeSymbol typeSymbol, s
6262
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
6363
/// <param name="name">The full name of the type to check for interface implementation.</param>
6464
/// <returns>Whether or not <paramref name="typeSymbol"/> has an interface with the specified name.</returns>
65-
public static bool HasInterfaceWithFullyQualifiedName(this ITypeSymbol typeSymbol, string name)
65+
public static bool HasInterfaceWithFullyQualifiedMetadataName(this ITypeSymbol typeSymbol, string name)
6666
{
6767
foreach (INamedTypeSymbol interfaceType in typeSymbol.AllInterfaces)
6868
{
69-
if (interfaceType.HasFullyQualifiedName(name))
69+
if (interfaceType.HasFullyQualifiedMetadataName(name))
7070
{
7171
return true;
7272
}
@@ -100,11 +100,11 @@ public static bool HasOrInheritsAttribute(this ITypeSymbol typeSymbol, Func<Attr
100100
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
101101
/// <param name="name">The name of the attribute to look for.</param>
102102
/// <returns>Whether or not <paramref name="typeSymbol"/> has an attribute with the specified type name.</returns>
103-
public static bool HasOrInheritsAttributeWithFullyQualifiedName(this ITypeSymbol typeSymbol, string name)
103+
public static bool HasOrInheritsAttributeWithFullyQualifiedMetadataName(this ITypeSymbol typeSymbol, string name)
104104
{
105105
for (ITypeSymbol? currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType)
106106
{
107-
if (currentType.HasAttributeWithFullyQualifiedName(name))
107+
if (currentType.HasAttributeWithFullyQualifiedMetadataName(name))
108108
{
109109
return true;
110110
}
@@ -120,11 +120,11 @@ public static bool HasOrInheritsAttributeWithFullyQualifiedName(this ITypeSymbol
120120
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
121121
/// <param name="name">The name of the attribute to look for.</param>
122122
/// <returns>Whether or not <paramref name="typeSymbol"/> has an attribute with the specified type name.</returns>
123-
public static bool InheritsAttributeWithFullyQualifiedName(this ITypeSymbol typeSymbol, string name)
123+
public static bool InheritsAttributeWithFullyQualifiedMetadataName(this ITypeSymbol typeSymbol, string name)
124124
{
125125
if (typeSymbol.BaseType is INamedTypeSymbol baseTypeSymbol)
126126
{
127-
return HasOrInheritsAttributeWithFullyQualifiedName(baseTypeSymbol, name);
127+
return HasOrInheritsAttributeWithFullyQualifiedMetadataName(baseTypeSymbol, name);
128128
}
129129

130130
return false;

0 commit comments

Comments
 (0)