Skip to content

Commit

Permalink
Added #pragma warning disable CS1591 to supress warnings in the sourc…
Browse files Browse the repository at this point in the history
…e generated code (#8940)

* Added  #pragma to supress warning CS1591

* Roslyn-native way to add pragma warnings

* Added check for empty namespace and attribute list

* Updated warnings implementation
  • Loading branch information
m3nax committed May 30, 2024
1 parent 559b95e commit 47b29f8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
67 changes: 58 additions & 9 deletions src/Orleans.CodeGenerator/CodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using Orleans.CodeGenerator.SyntaxGeneration;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Orleans.CodeGenerator.Diagnostics;
using Orleans.CodeGenerator.Hashing;
using System.Text;
using Orleans.CodeGenerator.SyntaxGeneration;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
using static Orleans.CodeGenerator.SyntaxGeneration.SymbolExtensions;
using Orleans.CodeGenerator.Diagnostics;

namespace Orleans.CodeGenerator
{
Expand All @@ -32,6 +32,7 @@ public class CodeGenerator
private readonly Dictionary<string, List<MemberDeclarationSyntax>> _namespacedMembers = new();
private readonly Dictionary<InvokableMethodId, InvokableMethodDescription> _invokableMethodDescriptions = new();
private readonly HashSet<INamedTypeSymbol> _visitedInterfaces = new(SymbolEqualityComparer.Default);
private readonly List<string> DisabledWarnings = new() { "CS1591" };

public CodeGenerator(Compilation compilation, CodeGeneratorOptions options)
{
Expand Down Expand Up @@ -166,7 +167,7 @@ public CompilationUnitSyntax GenerateCode(CancellationToken cancellationToken)
{
(_, GenerateFieldIds.PublicProperties) => GenerateFieldIds.PublicProperties,
(GenerateFieldIds.PublicProperties, _) => GenerateFieldIds.PublicProperties,
_ => GenerateFieldIds.None
_ => GenerateFieldIds.None
};
var fieldIdAssignmentHelper = new FieldIdAssignmentHelper(symbol, constructorParameters, implicitMemberSelectionStrategy, LibraryTypes);
if (!fieldIdAssignmentHelper.IsValidForSerialization)
Expand Down Expand Up @@ -315,6 +316,30 @@ bool ShouldIncludePrimaryConstructorParameters(INamedTypeSymbol t)
var assemblyAttributes = ApplicationPartAttributeGenerator.GenerateSyntax(LibraryTypes, MetadataModel);
assemblyAttributes.Add(metadataAttribute);

if (assemblyAttributes.Count > 0)
{
assemblyAttributes[0] = assemblyAttributes[0]
.WithLeadingTrivia(
SyntaxFactory.TriviaList(
new List<SyntaxTrivia>
{
Trivia(
PragmaWarningDirectiveTrivia(
Token(SyntaxKind.DisableKeyword),
SeparatedList(DisabledWarnings.Select(str =>
{
var syntaxToken = SyntaxFactory.Literal(
SyntaxFactory.TriviaList(),
str,
str,
SyntaxFactory.TriviaList());
return (ExpressionSyntax)SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, syntaxToken);
})),
isActive: true)),
}));
}

var usings = List(new[] { UsingDirective(ParseName("global::Orleans.Serialization.Codecs")), UsingDirective(ParseName("global::Orleans.Serialization.GeneratedCodeHelpers")) });
var namespaces = new List<MemberDeclarationSyntax>(_namespacedMembers.Count);
foreach (var pair in _namespacedMembers)
Expand All @@ -325,6 +350,30 @@ bool ShouldIncludePrimaryConstructorParameters(INamedTypeSymbol t)
namespaces.Add(NamespaceDeclaration(ParseName(ns)).WithMembers(List(member)).WithUsings(usings));
}

if (namespaces.Count > 0)
{
namespaces[0] = namespaces[0]
.WithTrailingTrivia(
SyntaxFactory.TriviaList(
new List<SyntaxTrivia>
{
Trivia(
PragmaWarningDirectiveTrivia(
Token(SyntaxKind.RestoreKeyword),
SeparatedList(DisabledWarnings.Select(str =>
{
var syntaxToken = SyntaxFactory.Literal(
SyntaxFactory.TriviaList(),
str,
str,
SyntaxFactory.TriviaList());
return (ExpressionSyntax)SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, syntaxToken);
})),
isActive: true)),
}));
}

return CompilationUnit()
.WithAttributeLists(List(assemblyAttributes))
.WithMembers(List(namespaces));
Expand Down
6 changes: 3 additions & 3 deletions src/Orleans.CodeGenerator/Orleans.CodeGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md" />
</ItemGroup>

<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
Expand Down
7 changes: 4 additions & 3 deletions src/Orleans.CodeGenerator/OrleansSourceGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Orleans.CodeGenerator.Diagnostics;
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Orleans.CodeGenerator.Diagnostics;

#pragma warning disable RS1035 // Do not use APIs banned for analyzers
namespace Orleans.CodeGenerator
Expand Down

0 comments on commit 47b29f8

Please sign in to comment.