Skip to content

Commit

Permalink
Cancellation token and some other speed stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Feb 16, 2024
1 parent 3de4104 commit 2bb00f7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
33 changes: 12 additions & 21 deletions sourcegeneration/MonologueSourceGenerator/LogGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,46 @@

namespace Monologue.SourceGenerator;

internal record ClassData
{
public readonly EquatableArray<string> LoggedItems;
public readonly string Name;
public readonly string ClassDeclaration;
public readonly string? Namespace;

public ClassData(ImmutableArray<string> loggedItems, string name, string classDeclaration, string? ns)
{
LoggedItems = new (loggedItems);
Name = name;
ClassDeclaration = classDeclaration;
Namespace = ns;
}
}
internal record ClassData(ImmutableArray<string> LoggedItems, string Name, string ClassDeclaration, string? Namespace);

[Generator]
public class LogGenerator : IIncrementalGenerator
{
static ClassData? GetClassData(SemanticModel semanticModel, SyntaxNode classDeclarationSyntax)
static ClassData? GetClassData(SemanticModel semanticModel, SyntaxNode classDeclarationSyntax, CancellationToken token)
{
if (semanticModel.GetDeclaredSymbol(classDeclarationSyntax) is not INamedTypeSymbol classSymbol)
{
return null;
}
token.ThrowIfCancellationRequested();

var ns = classSymbol.ContainingNamespace?.ToDisplayString();
token.ThrowIfCancellationRequested();
StringBuilder typeBuilder = new StringBuilder();
classSymbol.GetTypeDeclaration(typeBuilder);
classSymbol.GetTypeDeclaration(typeBuilder, token);
token.ThrowIfCancellationRequested();

var classMembers = classSymbol.GetMembers();

var loggableMembers = ImmutableArray.CreateBuilder<string>(classMembers.Length);

foreach (var member in classMembers)
{
token.ThrowIfCancellationRequested();
var attributes = member.GetAttributes();
token.ThrowIfCancellationRequested();

foreach (AttributeData attribute in attributes)
{
token.ThrowIfCancellationRequested();
var attributeClass = attribute.AttributeClass;
if (attributeClass is null)
{
continue;
}
if (attributeClass.ToDisplayString() == "Monologue.LogAttribute")
{
token.ThrowIfCancellationRequested();
string getOperation;
string defaultPathName;
ITypeSymbol logType;
Expand Down Expand Up @@ -100,10 +93,8 @@ public class LogGenerator : IIncrementalGenerator
// TODO the rest of the types
fullOperation = "";
}

token.ThrowIfCancellationRequested();
loggableMembers.Add(fullOperation);


break;
}
}
Expand All @@ -118,7 +109,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.ForAttributeWithMetadataName(
"Monologue.GenerateLogAttribute",
predicate: static (s, _) => s is TypeDeclarationSyntax,
transform: static (ctx, _) => GetClassData(ctx.SemanticModel, ctx.TargetNode))
transform: static (ctx, token) => GetClassData(ctx.SemanticModel, ctx.TargetNode, token))
.Where(static m => m is not null);

context.RegisterSourceOutput(attributedTypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
<PackageReference Include="PolySharp" Version="1.14.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<!-- This ensures the library will be packaged as a source generator when we use `dotnet pack` -->
<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true"
PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ namespace Monologue.SourceGenerator;

public static class SyntaxExtensions
{
public static void GetTypeDeclaration(this ITypeSymbol symbol, StringBuilder builder)
public static void GetTypeDeclaration(this ITypeSymbol symbol, StringBuilder builder, CancellationToken token)
{
var displayFormat = new SymbolDisplayFormat(
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypes,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters | SymbolDisplayGenericsOptions.IncludeVariance);

var nameString = symbol.ToDisplayString(displayFormat);
token.ThrowIfCancellationRequested();

if (symbol.IsReadOnly)
{
Expand Down

0 comments on commit 2bb00f7

Please sign in to comment.