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

Mark all generated C# classes with [GeneratedCode] attribute #4624

Conversation

martinskuta
Copy link

@martinskuta martinskuta commented May 22, 2024

Hi,

we use ANTLR to generate a C# parser for our grammar and we run for each build static code analysis and for obvious reasons we exclude generated code, including the generated ANTLR classes. For ANTLR generated code we have to manually exclude the generated namespace from the inspection, because not all of the generated classes are marked with [GeneratedCode] attribute, none of the public nested classes are, only the top level parent ones for Parser/Visitor are marked as generated.

We are using NDepend and JetBrains InspectCode to analyse the code. With the changes to the CSharp target template I propose in this PR no further work has to be done to exclude ANTLR generated code from the inspection as both tools understand that attribute, as do probably any other C# static analysis tools.

@sharwell
Copy link
Member

sharwell commented May 23, 2024

📝 This should not be necessary since the entire file is marked as generated code:

//------------------------------------------------------------------------------
// \<auto-generated>
// This code was generated by a tool.
// ANTLR Version: <ANTLRVersion>
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// \</auto-generated>
//------------------------------------------------------------------------------

If a tool is failing to account for this designator at the top of the file, it would be considered a bug in that tool that should be corrected.

Even the C# compiler recognizes this:
https://github.com/dotnet/roslyn/blob/d89c824648207390f5be355a782048812ba5f91e/src/Compilers/Core/Portable/SourceGeneration/GeneratedCodeUtilities.cs#L16

@martinskuta
Copy link
Author

You are right. I didn't think about it this way, but makes sense that a symbol that is inside of a containing symbol that is generated should be also considered generated. My thinking was that if you create a partial class then you could create a nested class that would not be considered auto generated, but in that case the symbol would be the partial class without the attribute I assume.

Thx for the reply, I will report it to NDepend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants