-
Notifications
You must be signed in to change notification settings - Fork 130
Add an analyzer for [Obsolete] on authored APIs without [Deprecated] #2508
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
Sergio Pedri (Sergio0694)
merged 4 commits into
user/sergiopedri/project-experimental-attribute
from
user/sergiopedri/obsolete-on-authored-types
Aug 12, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
981471a
Add CSWINRT2022 diagnostic descriptor for [Obsolete] without [Depreca…
Sergio0694 0549143
Add the ObsoleteWithoutDeprecated analyzer for CSWINRT2022
Sergio0694 8ad0333
Add tests and docs for the ObsoleteWithoutDeprecated analyzer
Sergio0694 fd7a968
Also report [Obsolete] on authored enum members and struct fields
Sergio0694 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
141 changes: 141 additions & 0 deletions
141
...thoring/WinRT.SourceGenerator2/Diagnostics/Analyzers/ObsoleteWithoutDeprecatedAnalyzer.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,141 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| namespace WindowsRuntime.SourceGenerator.Diagnostics; | ||
|
|
||
| /// <summary> | ||
| /// A diagnostic analyzer that reports when a publicly exposed API of a Windows Runtime component has an | ||
| /// <c>[Obsolete]</c> attribute applied, but no <c>[Windows.Foundation.Metadata.Deprecated]</c> attribute. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <c>[Obsolete]</c> has no Windows Runtime counterpart, so the WinMD generator copies it verbatim rather | ||
| /// than translating it, which leaves the deprecation invisible to every consumer of the component. Having | ||
| /// both attributes applied is the supported way to deprecate an API for .NET and Windows Runtime consumers | ||
| /// alike, so that combination is not reported. | ||
| /// </remarks> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| public sealed class ObsoleteWithoutDeprecatedAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| /// <inheritdoc/> | ||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = [DiagnosticDescriptors.ObsoleteWithoutDeprecated]; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
| context.EnableConcurrentExecution(); | ||
|
|
||
| context.RegisterCompilationStartAction(static context => | ||
| { | ||
| // This analyzer only applies to Windows Runtime component authoring scenarios | ||
| if (!context.Options.AnalyzerConfigOptionsProvider.GlobalOptions.GetCsWinRTComponent()) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Get the '[Obsolete]' symbol | ||
| if (context.Compilation.GetTypeByMetadataName("System.ObsoleteAttribute") is not { } obsoleteAttributeType) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Get the '[Deprecated]' symbol. Without it there is no way to author the deprecation the | ||
| // diagnostic would be suggesting, so nothing is reported (this also means the analyzer is | ||
| // inert when the component does not reference a Windows SDK projection at all). | ||
| if (context.Compilation.GetTypeByMetadataName("Windows.Foundation.Metadata.DeprecatedAttribute") is not { } deprecatedAttributeType) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| context.RegisterSymbolAction(context => | ||
| { | ||
| if (!IsPubliclyExposedFromComponent(context.Symbol)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Only report APIs that are deprecated for .NET consumers, but not for Windows Runtime ones | ||
| if (!context.Symbol.HasAttributeWithType(obsoleteAttributeType) || | ||
| context.Symbol.HasAttributeWithType(deprecatedAttributeType)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| context.ReportDiagnostic(Diagnostic.Create( | ||
| DiagnosticDescriptors.ObsoleteWithoutDeprecated, | ||
| context.Symbol.Locations.FirstOrDefault(), | ||
| context.Symbol)); | ||
| }, SymbolKind.NamedType, SymbolKind.Method, SymbolKind.Property, SymbolKind.Event, SymbolKind.Field); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks whether a given symbol is publicly exposed from a Windows Runtime component, and so ends up | ||
| /// in the generated <c>.winmd</c>. | ||
| /// </summary> | ||
| /// <param name="symbol">The symbol to check.</param> | ||
| /// <returns>Whether <paramref name="symbol"/> is publicly exposed from the component.</returns> | ||
| private static bool IsPubliclyExposedFromComponent(ISymbol symbol) | ||
| { | ||
| // Skip symbols with no declaration in source (eg. the implicit parameterless constructor of a class, | ||
| // or the 'value__' field of an enum), which cannot carry an attribute of their own and have nowhere | ||
| // to report a diagnostic | ||
| if (symbol.IsImplicitlyDeclared) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (symbol.DeclaredAccessibility is not Accessibility.Public) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Types are only exported when they are top level: Windows Runtime has no nested types | ||
| if (symbol is INamedTypeSymbol) | ||
| { | ||
| return symbol.ContainingType is null; | ||
| } | ||
|
|
||
| // Every remaining symbol is a member, and a member is only exported when its declaring type is | ||
| if (symbol.ContainingType is not { DeclaredAccessibility: Accessibility.Public, ContainingType: null } containingType) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Fields are exported from the two type kinds that are pure data in Windows Runtime, and metadata | ||
| // supports member markers on them individually (the Windows SDK uses this to mark a single new | ||
| // member of an existing enum experimental). | ||
| if (symbol is IFieldSymbol fieldSymbol) | ||
| { | ||
| // Every enum member is exported, while a struct only exports its public instance fields, so its | ||
| // static and const fields are not reported. No other type kind exports a field at all. | ||
| return containingType.TypeKind is TypeKind.Enum | ||
| || (containingType.TypeKind is TypeKind.Struct && !fieldSymbol.IsStatic); | ||
| } | ||
|
|
||
| // Property and event accessors are only exported as part of the property or event they belong to, | ||
| // which is the symbol reported instead. The generator moves a '[Deprecated]' from the property or | ||
| // event down onto the accessor row itself, and never reads one written on a C# accessor. | ||
| if (symbol is IMethodSymbol { AssociatedSymbol: not null }) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Constructors are exported as activation factory methods, and '[Deprecated]' cannot be applied to | ||
| // them ('AttributeTargets.Constructor' is not part of its usage), so there would be no way to act | ||
| // on the diagnostic. Deprecating the whole type is the only option, and that is reported already. | ||
| if (symbol is IMethodSymbol { MethodKind: MethodKind.Constructor or MethodKind.StaticConstructor }) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Only classes and interfaces export members other than fields: a Windows Runtime struct is a plain | ||
| // field aggregate, so the generator drops every member of one other than its public instance fields. | ||
| return containingType.TypeKind is TypeKind.Class or TypeKind.Interface; | ||
| } | ||
| } | ||
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.