Skip to content

Extensions: analyzer actions #78319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ private static void ComputeDeclarations(
case SyntaxKind.StructDeclaration:
case SyntaxKind.RecordDeclaration:
case SyntaxKind.RecordStructDeclaration:
// Tracked by https://github.com/dotnet/roslyn/issues/76130 : likely needs work for analyzers
{
if (associatedSymbol is IMethodSymbol ctor)
{
Expand All @@ -123,6 +122,7 @@ private static void ComputeDeclarations(
goto case SyntaxKind.InterfaceDeclaration;
}
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.ExtensionDeclaration:
{
var t = (TypeDeclarationSyntax)node;
foreach (var decl in t.Members)
Expand Down
165 changes: 165 additions & 0 deletions src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.VisualBasic;
Expand Down Expand Up @@ -37322,4 +37325,166 @@ static class E
var model = comp.GetSemanticModel(tree);
Assert.Equal(["(T, null)", "(T, T)"], PrintXmlNameSymbols(tree, model));
}

[Fact]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Add tests for symbol start/stop

public void AnalyzerActions_01()
{
var src = """
static class E
{
extension<T>([Attr] T t)
{
[Attr2]
public void M() { }

[Attr3]
public int P => 0;
}
}
""";

var analyzer = new AnalyzerActions_01_Analyzer();
var comp = CreateCompilation(src);
comp.GetAnalyzerDiagnostics([analyzer], null).Verify();

AssertEx.SetEqual([
"Attr2 -> void E.<>E__0<T>.M()",
"M -> void E.<>E__0<T>.M()",
"Attr3 -> System.Int32 E.<>E__0<T>.P { get; }",
"P -> System.Int32 E.<>E__0<T>.P { get; }",
"T -> E.<>E__0<T>",
"Attr -> E.<>E__0<T>",
"extension -> E.<>E__0<T>"],
analyzer._results.ToArray());
}

private class AnalyzerActions_01_Analyzer : DiagnosticAnalyzer
{
public ConcurrentQueue<string> _results = new ConcurrentQueue<string>();

private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor("XY0000", "Test", "Test", "Test", DiagnosticSeverity.Warning, true, "Test", "Test");

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Descriptor];

public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(handle, SyntaxKind.ExtensionDeclaration);
context.RegisterSyntaxNodeAction(handle, SyntaxKind.IdentifierName);
context.RegisterSyntaxNodeAction(handle, SyntaxKind.MethodDeclaration);
context.RegisterSyntaxNodeAction(handle, SyntaxKind.PropertyDeclaration);

void handle(SyntaxNodeAnalysisContext context)
{
_results.Enqueue(print(context));
Assert.Same(context.Node.SyntaxTree, context.ContainingSymbol!.DeclaringSyntaxReferences.Single().SyntaxTree);
}

static string print(SyntaxNodeAnalysisContext context)
{
var syntaxString = context.Node switch
{
ExtensionDeclarationSyntax => "extension",
MethodDeclarationSyntax method => method.Identifier.ValueText,
PropertyDeclarationSyntax property => property.Identifier.ValueText,
_ => context.Node.ToString()
};

return $"{syntaxString} -> {context.ContainingSymbol.ToTestDisplayString()}";
}
}
}

[Fact]
public void AnalyzerActions_02()
{
var src = """
static class E
{
extension<T>(T t)
{
public void M() { }
public int P => 0;
}
}
""";

var analyzer = new AnalyzerActions_02_Analyzer();
var comp = CreateCompilation(src);
comp.GetAnalyzerDiagnostics([analyzer], null).Verify();

AssertEx.SetEqual([
"System.Int32 E.<>E__0<T>.P.get",
"void E.<>E__0<T>.M()",
"E.<>E__0<T>",
"System.Int32 E.<>E__0<T>.P { get; }",
"E"],
analyzer._results.ToArray());
}

private class AnalyzerActions_02_Analyzer : DiagnosticAnalyzer
{
public ConcurrentQueue<string> _results = new ConcurrentQueue<string>();

private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor("XY0000", "Test", "Test", "Test", DiagnosticSeverity.Warning, true, "Test", "Test");

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Descriptor];

public override void Initialize(AnalysisContext context)
{
context.RegisterSymbolAction(handle, SymbolKind.NamedType);
context.RegisterSymbolAction(handle, SymbolKind.Method);
context.RegisterSymbolAction(handle, SymbolKind.Property);

void handle(SymbolAnalysisContext context)
{
_results.Enqueue(context.Symbol.ToTestDisplayString());
}
}
}

[Fact]
public void AnalyzerActions_03()
{
var src = """
static class E
{
extension<T>(T t)
{
public void M() { }
public int P { get { return 0; } }
}
}
""";

var analyzer = new AnalyzerActions_03_Analyzer();
var comp = CreateCompilation(src);
comp.GetAnalyzerDiagnostics([analyzer], null).Verify();

AssertEx.SetEqual([
"public void M() { } -> void E.<>E__0<T>.M()",
"get { return 0; } -> System.Int32 E.<>E__0<T>.P.get"],
analyzer._results.ToArray());
}

private class AnalyzerActions_03_Analyzer : DiagnosticAnalyzer
{
public ConcurrentQueue<string> _results = new ConcurrentQueue<string>();

private static readonly DiagnosticDescriptor Descriptor =
new DiagnosticDescriptor("XY0000", "Test", "Test", "Test", DiagnosticSeverity.Warning, true, "Test", "Test");

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Descriptor];

public override void Initialize(AnalysisContext context)
{
context.RegisterOperationAction(handle, OperationKind.MethodBody);

void handle(OperationAnalysisContext context)
{
_results.Enqueue($"{context.Operation.Syntax.ToString()} -> {context.ContainingSymbol.ToTestDisplayString()}");
}
}
}
}
Loading