-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Code fix to add DisableRuntimeMarshalling when required #67676
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
Merged
jkoritzinsky
merged 7 commits into
dotnet:main
from
jkoritzinsky:disable-runtime-marshalling-code-fix
Apr 12, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
21ef7e0
Code fix to add DisableRuntimeMarshalling when required
jkoritzinsky a75b701
Merge branch 'main' of github.com:dotnet/runtime into disable-runtime…
jkoritzinsky 3588a62
Add static
jkoritzinsky d0937f4
PR Feedback
jkoritzinsky 29017af
Merge branch 'main' of github.com:dotnet/runtime into disable-runtime…
jkoritzinsky 549da88
Skip the generated code check for the new fixer and the associated mo…
jkoritzinsky 72e9969
Merge branch 'main' into disable-runtime-marshalling-code-fix
jkoritzinsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...rvices/gen/LibraryImportGenerator/Analyzers/AddDisableRuntimeMarshallingAttributeFixer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.Composition; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeActions; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.Editing; | ||
|
|
||
| namespace Microsoft.Interop.Analyzers | ||
| { | ||
| [ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
| public class AddDisableRuntimeMarshallingAttributeFixer : CodeFixProvider | ||
| { | ||
| private const string EquivalenceKey = nameof(AddDisableRuntimeMarshallingAttributeFixer); | ||
|
|
||
| private const string PropertiesFolderName = "Properties"; | ||
| private const string AssemblyInfoFileName = "AssemblyInfo.cs"; | ||
|
|
||
| public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(GeneratorDiagnostics.Ids.TypeNotSupported); | ||
|
|
||
| // TODO: Write a custom fix all provider | ||
| public override FixAllProvider? GetFixAllProvider() => null; | ||
|
|
||
| public override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
| { | ||
| List<Diagnostic> fixedDiagnostics = new(context.Diagnostics.Where(IsRequiresDiableRuntimeMarshallingDiagnostic)); | ||
|
|
||
| if (fixedDiagnostics.Count > 0) | ||
| { | ||
| context.RegisterCodeFix( | ||
| CodeAction.Create( | ||
| "Add DisableRuntimeMarshallingAttribute to the assembly.", | ||
| ct => AddDisableRuntimeMarshallingAttributeApplicationToProject(context.Document.Project, ct), | ||
| EquivalenceKey), | ||
| fixedDiagnostics); | ||
| } | ||
|
|
||
| return Task.CompletedTask; | ||
|
|
||
| static bool IsRequiresDiableRuntimeMarshallingDiagnostic(Diagnostic diagnostic) | ||
| { | ||
| return diagnostic.Properties.ContainsKey(GeneratorDiagnosticProperties.AddDisableRuntimeMarshallingAttribute); | ||
| } | ||
| } | ||
|
|
||
| private static async Task<Solution> AddDisableRuntimeMarshallingAttributeApplicationToProject(Project project, CancellationToken cancellationToken) | ||
| { | ||
| Document? assemblyInfo = project.Documents.FirstOrDefault(IsPropertiesAssemblyInfo); | ||
|
|
||
| if (assemblyInfo is null) | ||
| { | ||
| assemblyInfo = project.AddDocument(AssemblyInfoFileName, "", folders: new[] { PropertiesFolderName }); | ||
| } | ||
|
|
||
| DocumentEditor editor = await DocumentEditor.CreateAsync(assemblyInfo, cancellationToken).ConfigureAwait(false); | ||
|
|
||
| var syntaxRoot = await assemblyInfo.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| editor.ReplaceNode( | ||
| syntaxRoot, | ||
| editor.Generator.AddAttributes( | ||
| syntaxRoot, | ||
| editor.Generator.Attribute(editor.Generator.DottedName(TypeNames.System_Runtime_CompilerServices_DisableRuntimeMarshallingAttribute)))); | ||
|
|
||
| return editor.GetChangedDocument().Project.Solution; | ||
|
|
||
| static bool IsPropertiesAssemblyInfo(Document document) | ||
| { | ||
| // We specifically want to match a file in the Properties folder with the provided name (AssemblyInfo.cs) to match other VS templates that add this file. | ||
| // We are very strict about this to ensure that we discover the correct file when it is already created and added to the project. | ||
| return document.Name == AssemblyInfoFileName | ||
| && document.Folders.Count == 1 | ||
| && document.Folders[0] == PropertiesFolderName; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.