Commit 1720628
authored
[generator] Mark generated .cs files as generated (#1052)
Fixes: #1051
Context: dotnet/android#7449
[Roslyn determines if a file is generated or not][0] by looking for
the strings `<autogenerated` or `<auto-generated` within the file.
Roslyn analyzers specify whether they want to scan generated code or
not using [`GeneratedCodeAnalysisFlags`][1]. Many analyzers *disable*
their checks when processing generated code.
Until now, `generator` output did *not* indicate that the output is
generated code. Consequently, many Roslyn analyzers would process
and issue warnings about generated code, warnings which would *not*
be emitted if the analyzer knew it was generated code:
…\src\Uno.UI.BindingHelper.Android\obj\Release\net6.0-android\generated\src\Java.Interop.__TypeRegistrations.cs(16,6): error CA1825: Avoid unnecessary zero-length array allocations. Use Array.Empty<string>() instead.
…\src\Uno.UI.BindingHelper.Android\obj\Release\net6.0-android\generated\src\Java.Interop.__TypeRegistrations.cs(18,6): error CA1825: Avoid unnecessary zero-length array allocations. Use Array.Empty<Converter<string, Type>>() instead.
…\src\Uno.UI.BindingHelper.Android\obj\Release\net6.0-android\generated\src\Uno.UI.UnoRecyclerView.cs(168,25): error CA1812: 'UnoRecyclerViewInvoker' is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it 'static' (Module in Visual Basic).
…\src\Uno.UI.BindingHelper.Android\obj\Release\net6.0-android\generated\src\Uno.UI.UnoViewGroup.cs(829,25): error CA1812: 'UnoViewGroupInvoker' is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it 'static' (Module in Visual Basic).
Update `generator` so that generated `.cs` files contain the comment:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
This will help ensure that Roslyn considers the files to contain
generated code, which will prevent some analyzers from checking those
files, which will reduce the number of warnings or errors emitted.
This should allow slightly faster builds with fewer warnings.
Additionally, it should help make it clearer to users that these
files should not be modified by hand (dotnet/android#7449).
Note: by default [Roslyn disables Nullable Reference Types (NRT)][2]
for code marked as "generated", causing:
Error CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
Auto-generated code requires an explicit '#nullable' directive in source.
We have to explicitly restore NRT back to the project settings for
our generated files with:
#nullable restore
[0]: https://github.com/dotnet/roslyn/blob/3f694a3c39c44b5dccb4aaaa163080bdb3ad412f/src/Compilers/Core/Portable/InternalUtilities/GeneratedCodeUtilities.cs
[1]: https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.diagnostics.analysiscontext.configuregeneratedcodeanalysis
[2]: https://github.com/dotnet/roslyn/blob/70e158ba6c2c99bd3c3fc0754af0dbf82a6d353d/docs/features/nullable-reference-types.md#generated-code1 parent f498fcf commit 1720628
File tree
200 files changed
+2006
-0
lines changed- tests/generator-Tests
- expected.ji
- AccessModifiers
- Android.Graphics.Color
- Arrays
- CSharpKeywords
- Constructors
- Core_ClassParse
- InterfaceMethodsConflict
- NonStaticFields
- NormalProperties
- StaticFields
- StaticMethods
- StaticProperties
- Streams
- java.lang.Object
- expected.xaji
- AccessModifiers
- Adapters
- Android.Graphics.Color
- Arrays
- CSharpKeywords
- Constructors
- Core_ClassParse
- Core_Jar2Xml
- GenericArguments
- InterfaceMethodsConflict
- NestedTypes
- NonStaticFields
- NormalMethods
- NormalProperties
- ParameterXPath
- StaticFields
- StaticMethods
- StaticProperties
- Streams
- TestInterface
- java.lang.Enum
- java.util.List
- expected
- AccessModifiers
- Adapters
- Android.Graphics.Color
- Arrays
- CSharpKeywords
- Constructors
- Core_ClassParse
- Core_Jar2Xml
- EnumerationFixup
- GenericArguments
- InterfaceMethodsConflict
- NestedTypes
- NonStaticFields
- NormalMethods
- NormalProperties
- ParameterXPath
- StaticFields
- StaticMethods
- StaticProperties
- Streams
- TestInterface
- java.lang.Enum
- java.lang.Object
- java.util.List
- tools/generator/Java.Interop.Tools.Generator.ObjectModel
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
200 files changed
+2006
-0
lines changedLines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
1 | 11 | | |
2 | 12 | | |
3 | 13 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
1 | 11 | | |
2 | 12 | | |
3 | 13 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
1 | 11 | | |
2 | 12 | | |
3 | 13 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
1 | 11 | | |
2 | 12 | | |
3 | 13 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
1 | 11 | | |
2 | 12 | | |
3 | 13 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
1 | 11 | | |
2 | 12 | | |
3 | 13 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
1 | 11 | | |
2 | 12 | | |
3 | 13 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
1 | 11 | | |
2 | 12 | | |
3 | 13 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
1 | 11 | | |
2 | 12 | | |
3 | 13 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
1 | 11 | | |
2 | 12 | | |
3 | 13 | | |
| |||
0 commit comments