-
Notifications
You must be signed in to change notification settings - Fork 128
Share DiagnosticStrings add mechanism to generate a DiagnosticDescriptor given a DiagnosticId #2170
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
10 commits
Select commit
Hold shift + click to select a range
24e13e8
Add DiagnosticId enum
30beedd
Check that the diagnostic id is in the range of the supported linker …
4e0ef04
Lint
ee71998
Share DiagnosticString
84d4db0
Noisy whitespace
abf96d9
Warnings go up to 6000 inclusive
d448d37
PR feedback
fb34464
Get diagnostic string
844d3a7
Lint
9d874ec
PR feedback
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using ILLink.Shared; | ||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace ILLink.RoslynAnalyzer | ||
| { | ||
| public static class DiagnosticDescriptors | ||
| { | ||
| public static DiagnosticDescriptor GetDiagnosticDescriptor (DiagnosticId diagnosticId) | ||
| { | ||
| var diagnosticString = new DiagnosticString (diagnosticId); | ||
| return new DiagnosticDescriptor (diagnosticId.AsString (), | ||
| diagnosticString.GetTitleFormat (), | ||
| diagnosticString.GetMessageFormat (), | ||
| GetDiagnosticCategory (diagnosticId), | ||
| DiagnosticSeverity.Warning, | ||
| true); | ||
| } | ||
|
|
||
| public static DiagnosticDescriptor GetDiagnosticDescriptor (DiagnosticId diagnosticId, | ||
| LocalizableResourceString? lrsTitle = null, | ||
| LocalizableResourceString? lrsMessage = null, | ||
| string? diagnosticCategory = null, | ||
| DiagnosticSeverity diagnosticSeverity = DiagnosticSeverity.Warning, | ||
| bool isEnabledByDefault = true, | ||
| string? helpLinkUri = null) | ||
| { | ||
| if (lrsTitle == null || lrsMessage == null) { | ||
| var diagnosticString = new DiagnosticString (diagnosticId); | ||
| return new DiagnosticDescriptor (diagnosticId.AsString (), | ||
| diagnosticString.GetTitleFormat (), | ||
| diagnosticString.GetMessageFormat (), | ||
| diagnosticCategory ?? GetDiagnosticCategory (diagnosticId), | ||
| diagnosticSeverity, | ||
| isEnabledByDefault, | ||
| helpLinkUri); | ||
| } | ||
|
|
||
| return new DiagnosticDescriptor (diagnosticId.AsString (), | ||
| lrsTitle!, | ||
| lrsMessage!, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if one of the title or message is |
||
| diagnosticCategory ?? GetDiagnosticCategory (diagnosticId), | ||
| diagnosticSeverity, | ||
| isEnabledByDefault, | ||
| helpLinkUri); | ||
| } | ||
|
|
||
| static string GetDiagnosticCategory (DiagnosticId diagnosticId) | ||
| { | ||
| switch ((int) diagnosticId) { | ||
| case > 2000 and < 3000: | ||
| return DiagnosticCategory.Trimming; | ||
|
|
||
| case >= 3000 and <= 6000: | ||
| return DiagnosticCategory.SingleFile; | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
|
|
||
| throw new ArgumentException ($"The provided diagnostic id '{diagnosticId}' does not fall into the range of supported warning codes 2001 to 6000 (inclusive)."); | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| namespace ILLink.Shared | ||
| { | ||
| public enum DiagnosticId | ||
| { | ||
| // Linker diagnostic ids. | ||
| RequiresUnreferencedCode = 2026, | ||
| RequiresUnreferencedCodeAttributeMismatch = 2046, | ||
|
|
||
| // Single-file diagnostic ids. | ||
| AvoidAssemblyLocationInSingleFile = 3000, | ||
| AvoidAssemblyGetFilesInSingleFile = 3001, | ||
| RequiresAssemblyFiles = 3002, | ||
| RequiresAssembyFilesAttributeMismatch = 3003 | ||
| } | ||
|
|
||
| public static class DiagnosticIdExtensions | ||
| { | ||
| public static string AsString (this DiagnosticId diagnosticId) => $"IL{(int) diagnosticId}"; | ||
| } | ||
| } |
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,25 @@ | ||
| namespace ILLink.Shared | ||
| { | ||
| public readonly struct DiagnosticString | ||
| { | ||
| readonly string _titleFormat; | ||
| readonly string _messageFormat; | ||
|
|
||
| public DiagnosticString (DiagnosticId diagnosticId) | ||
| { | ||
| var resourceManager = SharedStrings.ResourceManager; | ||
| _titleFormat = resourceManager.GetString ($"{diagnosticId}Title"); | ||
| _messageFormat = resourceManager.GetString ($"{diagnosticId}Message"); | ||
| } | ||
|
|
||
| public string GetMessage (params string[] args) => | ||
| string.Format (_messageFormat, args); | ||
|
|
||
| public string GetMessageFormat () => _messageFormat; | ||
|
|
||
| public string GetTitle (params string[] args) => | ||
| string.Format (_titleFormat, args); | ||
|
|
||
| public string GetTitleFormat () => _titleFormat; | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To educate myself whats this code doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code was generated by VS. Couldn't find anything particularly interesting; looks like the first entry (13) is referencing the shared project itself and the bellow ones are the projects that use the shared proj.