Skip to content

Commit

Permalink
[generator] Mark generated .cs files as generated (#1052)
Browse files Browse the repository at this point in the history
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-code
  • Loading branch information
jpobst authored Oct 20, 2022
1 parent f498fcf commit 1720628
Show file tree
Hide file tree
Showing 200 changed files with 2,006 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
10 changes: 10 additions & 0 deletions tests/generator-Tests/expected.ji/Arrays/Java.Lang.Object.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
10 changes: 10 additions & 0 deletions tests/generator-Tests/expected.ji/Constructors/Java.Lang.Object.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

#nullable restore
using System;
using System.Collections.Generic;
using Java.Interop;
Expand Down
Loading

0 comments on commit 1720628

Please sign in to comment.