-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add RequiresUnsafe trimming analyzer capabilities #123588
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e34fa0b
Add RequiresUnsafe trimming analyzer capabilities
agocke 7f7bdca
Cleanup
agocke 9730f6a
Merge branch 'main' into unsafe-analzyer
agocke 0a78841
Merge branch 'main' into unsafe-analzyer
agocke cfa25bf
Update CompatibilitySuppressions.xml
agocke 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
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
19 changes: 19 additions & 0 deletions
19
...ies/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttribute.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,19 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace System.Diagnostics.CodeAnalysis | ||
| { | ||
| /// <summary> | ||
| /// Indicates that the specified method requires unsafe code that may not be available | ||
| /// in all execution environments. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This allows tools to understand which methods are unsafe to call when targeting | ||
| /// environments that do not support unsafe code. | ||
| /// </remarks> | ||
| [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)] | ||
| [Conditional("DEBUG")] | ||
| internal sealed class RequiresUnsafeAttribute : Attribute | ||
| { | ||
| } | ||
| } | ||
541 changes: 541 additions & 0 deletions
541
src/tools/illink/src/ILLink.CodeFix/RequiresUnsafeCodeFixProvider.cs
Large diffs are not rendered by default.
Oops, something went wrong.
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
107 changes: 107 additions & 0 deletions
107
src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresUnsafeAnalyzer.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,107 @@ | ||
| // Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| #if DEBUG | ||
| using System; | ||
| using System.Collections.Immutable; | ||
| using ILLink.Shared; | ||
| using ILLink.Shared.TrimAnalysis; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| namespace ILLink.RoslynAnalyzer | ||
| { | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class RequiresUnsafeAnalyzer : RequiresAnalyzerBase | ||
| { | ||
| private const string RequiresUnsafeAttribute = nameof(RequiresUnsafeAttribute); | ||
| public const string FullyQualifiedRequiresUnsafeAttribute = "System.Diagnostics.CodeAnalysis." + RequiresUnsafeAttribute; | ||
|
|
||
| private static readonly DiagnosticDescriptor s_requiresUnsafeOnStaticCtor = DiagnosticDescriptors.GetDiagnosticDescriptor(DiagnosticId.RequiresUnsafeOnStaticConstructor); | ||
| private static readonly DiagnosticDescriptor s_requiresUnsafeOnEntryPoint = DiagnosticDescriptors.GetDiagnosticDescriptor(DiagnosticId.RequiresUnsafeOnEntryPoint); | ||
| private static readonly DiagnosticDescriptor s_requiresUnsafeRule = DiagnosticDescriptors.GetDiagnosticDescriptor(DiagnosticId.RequiresUnsafe); | ||
| private static readonly DiagnosticDescriptor s_requiresUnsafeAttributeMismatch = DiagnosticDescriptors.GetDiagnosticDescriptor(DiagnosticId.RequiresUnsafeAttributeMismatch); | ||
|
|
||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => | ||
| ImmutableArray.Create(s_requiresUnsafeRule, s_requiresUnsafeAttributeMismatch, s_requiresUnsafeOnStaticCtor, s_requiresUnsafeOnEntryPoint); | ||
|
|
||
| private protected override string RequiresAttributeName => RequiresUnsafeAttribute; | ||
|
|
||
| internal override string RequiresAttributeFullyQualifiedName => FullyQualifiedRequiresUnsafeAttribute; | ||
|
|
||
| private protected override DiagnosticTargets AnalyzerDiagnosticTargets => DiagnosticTargets.MethodOrConstructor; | ||
|
|
||
| private protected override DiagnosticDescriptor RequiresDiagnosticRule => s_requiresUnsafeRule; | ||
|
|
||
| private protected override DiagnosticId RequiresDiagnosticId => DiagnosticId.RequiresUnsafe; | ||
|
|
||
| private protected override DiagnosticDescriptor RequiresAttributeMismatch => s_requiresUnsafeAttributeMismatch; | ||
|
|
||
| private protected override DiagnosticDescriptor RequiresOnStaticCtor => s_requiresUnsafeOnStaticCtor; | ||
|
|
||
| private protected override DiagnosticDescriptor RequiresOnEntryPoint => s_requiresUnsafeOnEntryPoint; | ||
|
|
||
| internal override bool IsAnalyzerEnabled(AnalyzerOptions options) => | ||
| options.IsMSBuildPropertyValueTrue(MSBuildPropertyOptionNames.EnableUnsafeAnalyzer); | ||
|
|
||
| private protected override bool IsRequiresCheck(IPropertySymbol propertySymbol, Compilation compilation) | ||
| { | ||
| // No feature check property for RequiresUnsafe | ||
| return false; | ||
| } | ||
|
|
||
| protected override bool IsInRequiresScope(ISymbol containingSymbol, in DiagnosticContext context) | ||
| { | ||
| if (base.IsInRequiresScope(containingSymbol, context)) | ||
| return true; | ||
|
|
||
| if (!context.Location.IsInSource) | ||
| return false; | ||
|
|
||
| // Check to see if we're in an unsafe block or unsafe member | ||
| var syntaxTree = context.Location.SourceTree!; | ||
| var root = syntaxTree.GetRoot(); | ||
| var node = root.FindNode(context.Location.SourceSpan); | ||
| while (node != null && node != root) | ||
| { | ||
| if (node.IsKind(SyntaxKind.UnsafeStatement)) | ||
| return true; | ||
|
|
||
| // Check for unsafe modifier on the containing member or type | ||
| if (node is MethodDeclarationSyntax method && method.Modifiers.Any(SyntaxKind.UnsafeKeyword)) | ||
| return true; | ||
| if (node is LocalFunctionStatementSyntax localFunc && localFunc.Modifiers.Any(SyntaxKind.UnsafeKeyword)) | ||
| return true; | ||
| if (node is PropertyDeclarationSyntax prop && prop.Modifiers.Any(SyntaxKind.UnsafeKeyword)) | ||
| return true; | ||
| if (node is IndexerDeclarationSyntax indexer && indexer.Modifiers.Any(SyntaxKind.UnsafeKeyword)) | ||
| return true; | ||
| if (node is OperatorDeclarationSyntax op && op.Modifiers.Any(SyntaxKind.UnsafeKeyword)) | ||
| return true; | ||
| if (node is ConversionOperatorDeclarationSyntax conv && conv.Modifiers.Any(SyntaxKind.UnsafeKeyword)) | ||
| return true; | ||
| if (node is ConstructorDeclarationSyntax ctor && ctor.Modifiers.Any(SyntaxKind.UnsafeKeyword)) | ||
| return true; | ||
| if (node is TypeDeclarationSyntax type && type.Modifiers.Any(SyntaxKind.UnsafeKeyword)) | ||
| return true; | ||
|
|
||
| // Break out of lambdas/anonymous methods - they create a new scope | ||
| if (node.IsKind(SyntaxKind.AnonymousMethodExpression) | ||
| || node.IsKind(SyntaxKind.SimpleLambdaExpression) | ||
| || node.IsKind(SyntaxKind.ParenthesizedLambdaExpression)) | ||
| break; | ||
|
|
||
|
agocke marked this conversation as resolved.
|
||
| node = node.Parent; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| protected override bool VerifyAttributeArguments(AttributeData attribute) => true; | ||
|
|
||
| protected override string GetMessageFromAttribute(AttributeData? requiresAttribute) => ""; | ||
|
agocke marked this conversation as resolved.
|
||
| } | ||
| } | ||
| #endif | ||
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.
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.