Skip to content

Commit 037b90d

Browse files
authored
Suppress CS1591 warnings in ComInterfaceGenerator generated code (#120104)
1 parent b4161e2 commit 037b90d

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComInterfaceGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
204204

205205
using StringWriter source = new();
206206
source.WriteLine("// <auto-generated />");
207-
source.WriteLine("#pragma warning disable CS0612, CS0618, CS0649"); // Suppress warnings about [Obsolete] and "lack of assignment" in generated code.
207+
source.WriteLine("#pragma warning disable CS0612, CS0618, CS0649, CS1591"); // Suppress warnings about [Obsolete], "lack of assignment", and missing XML documentation in generated code.
208208

209209
// If the user has specified 'ManagedObjectWrapper', it means that the COM interface will never be used to marshal a native
210210
// object as an RCW (eg. the IDIC vtable will also not be generated, nor any additional supporting code). To reduce binary

src/libraries/System.Runtime.InteropServices/tests/ComInterfaceGenerator.Unit.Tests/Compiles.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Linq;
78
using System.Runtime.CompilerServices;
89
using System.Threading.Tasks;
10+
using Microsoft.CodeAnalysis;
11+
using Microsoft.CodeAnalysis.CSharp;
12+
using Microsoft.CodeAnalysis.Testing;
913
using Microsoft.Interop.UnitTests;
1014
using Xunit;
1115
using VerifyComInterfaceGenerator = Microsoft.Interop.UnitTests.Verifiers.CSharpSourceGeneratorVerifier<Microsoft.Interop.ComInterfaceGenerator>;
@@ -369,5 +373,80 @@ public async Task ValidateComInterfaceSnippets(string id, string source)
369373

370374
await VerifyComInterfaceGenerator.VerifySourceGeneratorAsync(source);
371375
}
376+
377+
[Fact]
378+
public async Task DocumentedComInterfaceDoesNotProduceCS1591Warnings()
379+
{
380+
string source = """
381+
using System.Runtime.InteropServices;
382+
using System.Runtime.InteropServices.Marshalling;
383+
384+
namespace Test
385+
{
386+
/// <summary>
387+
/// This is my interface.
388+
/// </summary>
389+
[GeneratedComInterface, Guid("27dd3a3d-4c16-485a-a123-7cd8f39c6ef2")]
390+
public partial interface IMyInterface
391+
{
392+
/// <summary>
393+
/// This does something.
394+
/// </summary>
395+
void DoSomething();
396+
}
397+
398+
/// <summary>
399+
/// This is my other interface.
400+
/// </summary>
401+
[GeneratedComInterface, Guid("1b681178-368a-4d13-8893-66b4673d2ff9")]
402+
public partial interface MyOtherInterface : IMyInterface
403+
{
404+
/// <summary>
405+
/// This does something else.
406+
/// </summary>
407+
void DoSomethingElse();
408+
}
409+
}
410+
""";
411+
412+
var test = new VerifyCompilationTest<Microsoft.Interop.ComInterfaceGenerator>(false)
413+
{
414+
TestCode = source,
415+
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck | TestBehaviors.SkipGeneratedCodeCheck,
416+
CompilationVerifier = compilation =>
417+
{
418+
// Verify that no CS1591 warnings are produced in the generated code
419+
var diagnostics = compilation.GetDiagnostics();
420+
var cs1591Diagnostics = diagnostics.Where(d => d.Id == "CS1591").ToList();
421+
422+
Assert.Empty(cs1591Diagnostics);
423+
}
424+
};
425+
426+
// Enable XML documentation warnings to ensure CS1591 would be raised if not suppressed
427+
test.SolutionTransforms.Add((solution, projectId) =>
428+
{
429+
var project = solution.GetProject(projectId);
430+
if (project is null) return solution;
431+
432+
// Set parse options to enable documentation mode which is required for CS1591 validation
433+
var parseOptions = (CSharpParseOptions?)project.ParseOptions;
434+
if (parseOptions is not null)
435+
{
436+
parseOptions = parseOptions.WithDocumentationMode(DocumentationMode.Diagnose);
437+
solution = solution.WithProjectParseOptions(projectId, parseOptions);
438+
project = solution.GetProject(projectId)!;
439+
}
440+
441+
var compilationOptions = project.CompilationOptions!
442+
.WithSpecificDiagnosticOptions(new Dictionary<string, ReportDiagnostic>
443+
{
444+
["CS1591"] = ReportDiagnostic.Warn
445+
});
446+
return solution.WithProjectCompilationOptions(projectId, compilationOptions);
447+
});
448+
449+
await test.RunAsync();
450+
}
372451
}
373452
}

0 commit comments

Comments
 (0)