Skip to content
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

Expose IsGeneratedCode boolean property on diagnostic analyzer contexts #63447

Merged
merged 4 commits into from
Sep 20, 2022
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 @@ -1447,13 +1447,17 @@ public void TestGeneratedCodeAnalyzer()
[System.CodeDom.Compiler.GeneratedCodeAttribute(""tool"", ""version"")]
class GeneratedCode{0}
{{
private class Nested{0} {{ }}
private class Nested{0} {{ void NestedMethod() {{ System.Console.WriteLine(0); }} }}

void GeneratedCodeMethod() {{ System.Console.WriteLine(0); }}
}}

class NonGeneratedCode{0}
{{
[System.CodeDom.Compiler.GeneratedCodeAttribute(""tool"", ""version"")]
private class NestedGeneratedCode{0} {{ }}
private class NestedGeneratedCode{0} {{ void NestedGeneratedCodeMethod() {{ System.Console.WriteLine(0); }} }}

void NonGeneratedCodeMethod() {{ System.Console.WriteLine(0); }}
}}
";
var generatedFileNames = new List<string>
Expand Down Expand Up @@ -1583,7 +1587,7 @@ partial class PartialType
AddExpectedLocalDiagnostics(builder, false, squiggledText, line, column, GeneratedCodeAnalysisFlags.ReportDiagnostics, diagnosticArgument);

// Expected compilation diagnostics
AddExpectedNonLocalDiagnostic(builder, "PartialType", compilation.SyntaxTrees[0].FilePath);
AddExpectedNonLocalDiagnostic(builder, GeneratedCodeAnalyzer.Summary, "PartialType(IsGeneratedCode:False)", $"{compilation.SyntaxTrees[0].FilePath}(IsGeneratedCode:False)");

var expected = builder.ToArrayAndFree();

Expand Down Expand Up @@ -1614,6 +1618,17 @@ class TypeInGeneratedFile { }
expected: Diagnostic("GeneratedCodeAnalyzer2Warning", "TypeInUserFile").WithArguments("TypeInUserFile", "2").WithLocation(2, 7));
}

[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal sealed class GeneratedCodeAnalyzer : AbstractGeneratedCodeAnalyzer<SyntaxKind>
{
public GeneratedCodeAnalyzer(GeneratedCodeAnalysisFlags? generatedCodeAnalysisFlags, bool testIsGeneratedCodeInCallbacks = false)
: base(generatedCodeAnalysisFlags, testIsGeneratedCodeInCallbacks)
{
}

protected override SyntaxKind ClassDeclarationSyntaxKind => SyntaxKind.ClassDeclaration;
}

internal class OwningSymbolTestAnalyzer : DiagnosticAnalyzer
{
public static readonly DiagnosticDescriptor ExpressionDescriptor = new DiagnosticDescriptor(
Expand Down Expand Up @@ -1677,21 +1692,32 @@ public void FunkyMethod()
private static void VerifyGeneratedCodeAnalyzerDiagnostics(Compilation compilation, AnalyzerOptions analyzerOptions, Func<string, bool> isGeneratedFileName, GeneratedCodeAnalysisFlags? generatedCodeAnalysisFlagsOpt)
{
var expected = GetExpectedGeneratedCodeAnalyzerDiagnostics(compilation, isGeneratedFileName, generatedCodeAnalysisFlagsOpt);
VerifyGeneratedCodeAnalyzerDiagnostics(compilation, expected, generatedCodeAnalysisFlagsOpt, analyzerOptions);
VerifyGeneratedCodeAnalyzerDiagnostics(compilation, expected, generatedCodeAnalysisFlagsOpt, analyzerOptions, testIsGeneratedCodeInCallbacks: true);
}

private static void VerifyGeneratedCodeAnalyzerDiagnostics(Compilation compilation, DiagnosticDescription[] expected, GeneratedCodeAnalysisFlags? generatedCodeAnalysisFlagsOpt, AnalyzerOptions analyzerOptions = null)
private static void VerifyGeneratedCodeAnalyzerDiagnostics(Compilation compilation, DiagnosticDescription[] expected, GeneratedCodeAnalysisFlags? generatedCodeAnalysisFlagsOpt, AnalyzerOptions analyzerOptions = null, bool testIsGeneratedCodeInCallbacks = false)
{
var analyzers = new DiagnosticAnalyzer[] { new GeneratedCodeAnalyzer(generatedCodeAnalysisFlagsOpt) };
var analyzers = new DiagnosticAnalyzer[] { new GeneratedCodeAnalyzer(generatedCodeAnalysisFlagsOpt, testIsGeneratedCodeInCallbacks) };
compilation.VerifyAnalyzerDiagnostics(analyzers, analyzerOptions, null, expected: expected);
}

private static DiagnosticDescription[] GetExpectedGeneratedCodeAnalyzerDiagnostics(Compilation compilation, Func<string, bool> isGeneratedFileName, GeneratedCodeAnalysisFlags? generatedCodeAnalysisFlagsOpt)
{
var analyzers = new DiagnosticAnalyzer[] { new GeneratedCodeAnalyzer(generatedCodeAnalysisFlagsOpt) };
var analyzers = new DiagnosticAnalyzer[] { new GeneratedCodeAnalyzer(generatedCodeAnalysisFlagsOpt, testIsGeneratedCodeInCallbacks: true) };
var files = compilation.SyntaxTrees.Select(t => t.FilePath).ToImmutableArray();
var sortedCallbackSymbolNames = new SortedSet<string>();
var sortedCallbackTreePaths = new SortedSet<string>();
var sortedCallbackSyntaxNodeNames = new SortedSet<string>();
var sortedCallbackOperationNames = new SortedSet<string>();
var sortedCallbackSemanticModelPaths = new SortedSet<string>();
var sortedCallbackSymbolStartNames = new SortedSet<string>();
var sortedCallbackSymbolEndNames = new SortedSet<string>();
var sortedCallbackOperationBlockStartNames = new SortedSet<string>();
var sortedCallbackOperationBlockEndNames = new SortedSet<string>();
var sortedCallbackOperationBlockNames = new SortedSet<string>();
var sortedCallbackCodeBlockStartNames = new SortedSet<string>();
var sortedCallbackCodeBlockEndNames = new SortedSet<string>();
var sortedCallbackCodeBlockNames = new SortedSet<string>();
var builder = ArrayBuilder<DiagnosticDescription>.GetInstance();
for (int i = 0; i < compilation.SyntaxTrees.Count(); i++)
{
Expand All @@ -1717,50 +1743,88 @@ private static DiagnosticDescription[] GetExpectedGeneratedCodeAnalyzerDiagnosti
// Type "NonGeneratedCode{0}"
squiggledText = string.Format("NonGeneratedCode{0}", i);
diagnosticArgument = squiggledText;
line = 8;
line = 10;
column = 7;
isGeneratedCode = isGeneratedFile;
AddExpectedLocalDiagnostics(builder, isGeneratedCode, squiggledText, line, column, generatedCodeAnalysisFlagsOpt, diagnosticArgument);

// Type "NestedGeneratedCode{0}"
squiggledText = string.Format("NestedGeneratedCode{0}", i);
diagnosticArgument = squiggledText;
line = 11;
line = 13;
column = 19;
isGeneratedCode = true;
AddExpectedLocalDiagnostics(builder, isGeneratedCode, squiggledText, line, column, generatedCodeAnalysisFlagsOpt, diagnosticArgument);

// File diagnostic
squiggledText = "}"; // last token in file.
diagnosticArgument = file;
line = 12;
line = 16;
column = 1;
isGeneratedCode = isGeneratedFile;
AddExpectedLocalDiagnostics(builder, isGeneratedCode, squiggledText, line, column, generatedCodeAnalysisFlagsOpt, diagnosticArgument);

// Compilation end summary diagnostic (verify callbacks into analyzer)
// Analyzer always called for generated code, unless generated code analysis is explicitly disabled.
Action<SortedSet<string>> addNames = null;
Action<SortedSet<string>> addPath = null;
if (generatedCodeAnalysisFlagsOpt == null || (generatedCodeAnalysisFlagsOpt & GeneratedCodeAnalysisFlags.Analyze) != 0)
{
sortedCallbackSymbolNames.Add(string.Format("GeneratedCode{0}", i));
sortedCallbackSymbolNames.Add(string.Format("Nested{0}", i));
sortedCallbackSymbolNames.Add(string.Format("NonGeneratedCode{0}", i));
sortedCallbackSymbolNames.Add(string.Format("NestedGeneratedCode{0}", i));
addNames = names =>
{
names.Add(string.Format("GeneratedCode{0}(IsGeneratedCode:True)", i));
names.Add(string.Format("Nested{0}(IsGeneratedCode:True)", i));
names.Add(string.Format("NonGeneratedCode{0}(IsGeneratedCode:{1})", i, isGeneratedFile));
names.Add(string.Format("NestedGeneratedCode{0}(IsGeneratedCode:True)", i));
};

sortedCallbackTreePaths.Add(file);
addPath = paths => paths.Add($"{file}(IsGeneratedCode:{isGeneratedFile})");
}
else if (!isGeneratedFile)
{
// Analyzer always called for non-generated code.
sortedCallbackSymbolNames.Add(string.Format("NonGeneratedCode{0}", i));
sortedCallbackTreePaths.Add(file);
addNames = names => names.Add(string.Format("NonGeneratedCode{0}(IsGeneratedCode:False)", i));

addPath = paths => paths.Add($"{file}(IsGeneratedCode:False)");
}

if (addNames != null)
{
addNames(sortedCallbackSymbolNames);
addNames(sortedCallbackSyntaxNodeNames);
addNames(sortedCallbackSymbolStartNames);
addNames(sortedCallbackSymbolEndNames);
addNames(sortedCallbackOperationNames);
addNames(sortedCallbackOperationBlockStartNames);
addNames(sortedCallbackOperationBlockEndNames);
addNames(sortedCallbackOperationBlockNames);
addNames(sortedCallbackCodeBlockStartNames);
addNames(sortedCallbackCodeBlockEndNames);
addNames(sortedCallbackCodeBlockNames);
}

if (addPath != null)
{
addPath(sortedCallbackTreePaths);
addPath(sortedCallbackSemanticModelPaths);
}
}

// Compilation end summary diagnostic (verify callbacks into analyzer)
var arg1 = sortedCallbackSymbolNames.Join(",");
var arg2 = sortedCallbackTreePaths.Join(",");
AddExpectedNonLocalDiagnostic(builder, arguments: new[] { arg1, arg2 });
var arg3 = sortedCallbackSyntaxNodeNames.Join(",") + ";" +
sortedCallbackOperationNames.Join(",") + ";" +
sortedCallbackSemanticModelPaths.Join(",") + ";" +
sortedCallbackSymbolStartNames.Join(",") + ";" +
sortedCallbackSymbolEndNames.Join(",") + ";" +
sortedCallbackOperationBlockStartNames.Join(",") + ";" +
sortedCallbackOperationBlockEndNames.Join(",") + ";" +
sortedCallbackOperationBlockNames.Join(",") + ";" +
sortedCallbackCodeBlockStartNames.Join(",") + ";" +
sortedCallbackCodeBlockEndNames.Join(",") + ";" +
sortedCallbackCodeBlockNames.Join(",");
AddExpectedNonLocalDiagnostic(builder, GeneratedCodeAnalyzer.Summary2, arguments: new[] { arg1, arg2, arg3 });

if (compilation.Options.GeneralDiagnosticOption == ReportDiagnostic.Error)
{
Expand Down Expand Up @@ -1800,9 +1864,9 @@ private static void AddExpectedLocalDiagnostics(
}
}

private static void AddExpectedNonLocalDiagnostic(ArrayBuilder<DiagnosticDescription> builder, params string[] arguments)
private static void AddExpectedNonLocalDiagnostic(ArrayBuilder<DiagnosticDescription> builder, DiagnosticDescriptor descriptor, params string[] arguments)
{
AddExpectedDiagnostic(builder, GeneratedCodeAnalyzer.Summary.Id, squiggledText: null, line: 1, column: 1, arguments: arguments);
AddExpectedDiagnostic(builder, descriptor.Id, squiggledText: null, line: 1, column: 1, arguments: arguments);
}

private static void AddExpectedDiagnostic(ArrayBuilder<DiagnosticDescription> builder, string diagnosticId, string squiggledText, int line, int column, params string[] arguments)
Expand Down Expand Up @@ -2117,15 +2181,15 @@ public class RegularClass
Diagnostic("GeneratedCodeAnalyzerError", "}").WithArguments("Source.cs").WithLocation(11, 1),
Diagnostic("GeneratedCodeAnalyzerWarning", "RegularClass").WithArguments("RegularClass").WithLocation(9, 14),
Diagnostic("GeneratedCodeAnalyzerError", "RegularClass").WithArguments("RegularClass").WithLocation(9, 14),
Diagnostic("GeneratedCodeAnalyzerSummary").WithArguments("RegularClass", "Source.cs").WithLocation(1, 1));
Diagnostic("GeneratedCodeAnalyzerSummary").WithArguments("RegularClass(IsGeneratedCode:False)", "Source.cs(IsGeneratedCode:False)").WithLocation(1, 1));

analyzers = new DiagnosticAnalyzer[] { new GeneratedCodeAnalyzer(GeneratedCodeAnalysisFlags.Analyze) };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null,
Diagnostic("GeneratedCodeAnalyzerWarning", "}").WithArguments("Source.cs").WithLocation(11, 1),
Diagnostic("GeneratedCodeAnalyzerError", "}").WithArguments("Source.cs").WithLocation(11, 1),
Diagnostic("GeneratedCodeAnalyzerWarning", "RegularClass").WithArguments("RegularClass").WithLocation(9, 14),
Diagnostic("GeneratedCodeAnalyzerError", "RegularClass").WithArguments("RegularClass").WithLocation(9, 14),
Diagnostic("GeneratedCodeAnalyzerSummary").WithArguments("HiddenClass,RegularClass", "Source.cs").WithLocation(1, 1));
Diagnostic("GeneratedCodeAnalyzerSummary").WithArguments("HiddenClass(IsGeneratedCode:True),RegularClass(IsGeneratedCode:False)", "Source.cs(IsGeneratedCode:False)").WithLocation(1, 1));

analyzers = new DiagnosticAnalyzer[] { new GeneratedCodeAnalyzer(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics) };
compilation.VerifyAnalyzerDiagnostics(analyzers, null, null,
Expand All @@ -2135,7 +2199,7 @@ public class RegularClass
Diagnostic("GeneratedCodeAnalyzerError", "HiddenClass").WithArguments("HiddenClass").WithLocation(4, 14),
Diagnostic("GeneratedCodeAnalyzerWarning", "RegularClass").WithArguments("RegularClass").WithLocation(9, 14),
Diagnostic("GeneratedCodeAnalyzerError", "RegularClass").WithArguments("RegularClass").WithLocation(9, 14),
Diagnostic("GeneratedCodeAnalyzerSummary").WithArguments("HiddenClass,RegularClass", "Source.cs").WithLocation(1, 1));
Diagnostic("GeneratedCodeAnalyzerSummary").WithArguments("HiddenClass(IsGeneratedCode:True),RegularClass(IsGeneratedCode:False)", "Source.cs(IsGeneratedCode:False)").WithLocation(1, 1));
}

[Fact, WorkItem(15903, "https://github.com/dotnet/roslyn/issues/15903")]
Expand Down
Loading