Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -20,11 +20,15 @@
using Roslyn.Test.Utilities.TestGenerators;
using Roslyn.Utilities;
using Xunit;
using Xunit.Abstractions;
Comment thread
jjonescz marked this conversation as resolved.

namespace Microsoft.CodeAnalysis.CSharp.Semantic.UnitTests.SourceGeneration
{
public class GeneratorDriverTests
public class GeneratorDriverTests(ITestOutputHelper output)
: CSharpTestBase
{
private readonly ITestOutputHelper _output = output;

[Fact]
public void Running_With_No_Changes_Is_NoOp()
{
Expand Down Expand Up @@ -3891,6 +3895,48 @@ public void Diagnostic_SpanOutsideRange_Incremental()
compilation.VerifyDiagnostics();
}

[Fact]
[WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1805836")]
[WorkItem("https://github.com/dotnet/roslyn/issues/82032")]
public void Diagnostic_SpanOutsideRange_Incremental_Update()
{
var source = "class SomewhatLongClassName {}";
var parseOptions = TestOptions.RegularPreview;
Compilation compilation = CreateCompilation(source, options: TestOptions.DebugDll, parseOptions: parseOptions, sourceFileName: "/original");
compilation.VerifyDiagnostics();

var generator = new PipelineCallbackGenerator(ctx =>
{
var inputs = ctx.SyntaxProvider.CreateSyntaxProvider(
static (node, ct) => node.IsKind(SyntaxKind.ClassDeclaration),
static (ctx, ct) => 0);
ctx.RegisterSourceOutput(inputs, (ctx, input) =>
{
var tree = compilation.SyntaxTrees.Single();
ctx.ReportDiagnostic(CodeAnalysis.Diagnostic.Create(
"TEST0001",
"Test",
"Test diagnostic",
DiagnosticSeverity.Warning,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
warningLevel: 1,
location: Location.Create(tree, TextSpan.FromBounds(20, 21))));
});
}).AsSourceGenerator();

GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { generator }, parseOptions: parseOptions);
driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out compilation, out var diagnostics);
diagnostics.Verify(Diagnostic("TEST0001", "a").WithLocation(1, 21));

source = "class C {}";
compilation = compilation.ReplaceSyntaxTree(compilation.SyntaxTrees.Single(), CSharpSyntaxTree.ParseText(source, parseOptions, path: "/original"));
driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out compilation, out diagnostics);

VerifyArgumentExceptionDiagnostic(diagnostics.Single(), nameof(PipelineCallbackGenerator), string.Format(CodeAnalysisResources.InvalidDiagnosticLocationReported, "TEST0001", "/original"), "diagnostic");
compilation.VerifyDiagnostics();
}

[ConditionalFact(typeof(IsEnglishLocal))]
[WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1805836")]
public void Diagnostic_SpanOutsideRange_Incremental_AdditionalLocations()
Expand Down Expand Up @@ -4237,19 +4283,16 @@ static void replace(ref Compilation compilation, CSharpParseOptions parseOptions
}
}

private static void VerifyArgumentExceptionDiagnostic(
private void VerifyArgumentExceptionDiagnostic(
Diagnostic diagnostic,
string generatorName,
string message,
string parameterName,
bool initialization = false)
{
var expectedMessage =
#if NET
$"{message} (Parameter '{parameterName}')";
#else
$"{message}{Environment.NewLine}Parameter name: {parameterName}";
#endif
_output.WriteLine(diagnostic.ToString());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It was useful to see the stacktrace that customers will see even though we don't validate it in the test obviously


var expectedMessage = new ArgumentException(message: message, paramName: parameterName).Message;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Neat :)

VerifyGeneratorExceptionDiagnostic<ArgumentException>(diagnostic, generatorName, expectedMessage, initialization);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,15 @@ private static ImmutableArray<Diagnostic> FilterDiagnostics(Compilation compilat
ArrayBuilder<Diagnostic> filteredDiagnostics = ArrayBuilder<Diagnostic>.GetInstance();
foreach (var diag in generatorDiagnostics)
{
try
{
DiagnosticAnalysisContextHelpers.VerifyArguments(diag, compilation, isSupportedDiagnostic: static (_, _) => true, cancellationToken);
}
catch (ArgumentException ex)
{
throw new UserFunctionException(ex);
}

if (compilation.Options.FilterDiagnostic(diag, cancellationToken) is { } filtered &&
suppressMessageState.ApplySourceSuppressions(filtered) is { } effective)
{
Expand Down
Loading