-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Annotate more public syntax APIs #44266
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c7daeb1
Annotate more syntax APIs
jcouv d8108db
Tweaks
jcouv d801f2a
Address feedback from Fred
jcouv b970a90
Address feedback from Chuck
jcouv e7f6a2f
Tweak
jcouv bb7bfc1
Merge remote-tracking branch 'dotnet/master' into annotate-more6
jcouv 0ce6ff7
Address feedback
jcouv 3d3cd5f
TypedConstant._type may be null
jcouv c823ee5
Adjust caller of ResolveReference
jcouv 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 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 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 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 |
---|---|---|
|
@@ -2,18 +2,12 @@ | |
// 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 System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
#nullable enable | ||
|
||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.CodeAnalysis.Collections; | ||
using Microsoft.CodeAnalysis.CSharp.Symbols; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.PooledObjects; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp | ||
{ | ||
|
@@ -35,8 +29,9 @@ public static string ToCSharpString(this TypedConstant constant) | |
return "{" + string.Join(", ", constant.Values.Select(v => v.ToCSharpString())) + "}"; | ||
} | ||
|
||
if (constant.Kind == TypedConstantKind.Type || constant.TypeInternal.SpecialType == SpecialType.System_Object) | ||
if (constant.Kind == TypedConstantKind.Type || constant.TypeInternal!.SpecialType == SpecialType.System_Object) | ||
{ | ||
Debug.Assert(constant.Value is object); | ||
return "typeof(" + constant.Value.ToString() + ")"; | ||
} | ||
|
||
|
@@ -46,6 +41,7 @@ public static string ToCSharpString(this TypedConstant constant) | |
return DisplayEnumConstant(constant); | ||
} | ||
|
||
Debug.Assert(constant.ValueInternal is object); | ||
return SymbolDisplay.FormatPrimitive(constant.ValueInternal, quoteStrings: true, useHexadecimalNumbers: false); | ||
} | ||
|
||
|
@@ -55,7 +51,8 @@ private static string DisplayEnumConstant(TypedConstant constant) | |
Debug.Assert(constant.Kind == TypedConstantKind.Enum); | ||
|
||
// Create a ConstantValue of enum underlying type | ||
SpecialType splType = ((INamedTypeSymbol)constant.Type).EnumUnderlyingType.SpecialType; | ||
SpecialType splType = ((INamedTypeSymbol)constant.Type!).EnumUnderlyingType!.SpecialType; | ||
Debug.Assert(constant.ValueInternal is object); | ||
ConstantValue valueConstant = ConstantValue.Create(constant.ValueInternal, splType); | ||
|
||
string typeName = constant.Type.ToDisplayString(SymbolDisplayFormat.QualifiedNameOnlyFormat); | ||
|
@@ -71,25 +68,27 @@ private static string DisplayEnumConstant(TypedConstant constant) | |
|
||
private static string DisplayUnsignedEnumConstant(TypedConstant constant, SpecialType specialType, ulong constantToDecode, string typeName) | ||
{ | ||
Debug.Assert(constant.Kind == TypedConstantKind.Enum); | ||
|
||
// Specified valueConstant might have an exact matching enum field | ||
// or it might be a bitwise Or of multiple enum fields. | ||
// For the later case, we keep track of the current value of | ||
// bitwise Or of possible enum fields. | ||
ulong curValue = 0; | ||
|
||
// Initialize the value string to empty | ||
PooledStringBuilder pooledBuilder = null; | ||
StringBuilder valueStringBuilder = null; | ||
PooledStringBuilder? pooledBuilder = null; | ||
StringBuilder? valueStringBuilder = null; | ||
|
||
// Iterate through all the constant members in the enum type | ||
var members = constant.Type.GetMembers(); | ||
var members = constant.Type!.GetMembers(); | ||
foreach (var member in members) | ||
{ | ||
var field = member as IFieldSymbol; | ||
|
||
if ((object)field != null && field.HasConstantValue) | ||
if (field is object && field.HasConstantValue) | ||
{ | ||
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue, specialType); | ||
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue!, specialType); // use MemberNotNull when available https://github.com/dotnet/roslyn/issues/41964 | ||
ulong memberValue = memberConstant.UInt64Value; | ||
|
||
// Do we have an exact matching enum field | ||
|
@@ -140,29 +139,34 @@ private static string DisplayUnsignedEnumConstant(TypedConstant constant, Specia | |
} | ||
|
||
// Unable to decode the enum constant, just display the integral value | ||
return constant.ValueInternal.ToString(); | ||
Debug.Assert(constant.ValueInternal is object); | ||
var result = constant.ValueInternal.ToString(); | ||
Debug.Assert(result is object); | ||
return result; | ||
} | ||
|
||
private static string DisplaySignedEnumConstant(TypedConstant constant, SpecialType specialType, long constantToDecode, string typeName) | ||
{ | ||
Debug.Assert(constant.Kind == TypedConstantKind.Enum); | ||
|
||
// Specified valueConstant might have an exact matching enum field | ||
// or it might be a bitwise Or of multiple enum fields. | ||
// For the later case, we keep track of the current value of | ||
// bitwise Or of possible enum fields. | ||
long curValue = 0; | ||
|
||
// Initialize the value string to empty | ||
PooledStringBuilder pooledBuilder = null; | ||
StringBuilder valueStringBuilder = null; | ||
PooledStringBuilder? pooledBuilder = null; | ||
StringBuilder? valueStringBuilder = null; | ||
|
||
// Iterate through all the constant members in the enum type | ||
var members = constant.Type.GetMembers(); | ||
var members = constant.Type!.GetMembers(); | ||
foreach (var member in members) | ||
{ | ||
var field = member as IFieldSymbol; | ||
if ((object)field != null && field.HasConstantValue) | ||
if (field is object && field.HasConstantValue) | ||
{ | ||
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue, specialType); | ||
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue!, specialType); // use MemberNotNull when available https://github.com/dotnet/roslyn/issues/41964 | ||
long memberValue = memberConstant.Int64Value; | ||
|
||
// Do we have an exact matching enum field | ||
|
@@ -213,7 +217,10 @@ private static string DisplaySignedEnumConstant(TypedConstant constant, SpecialT | |
} | ||
|
||
// Unable to decode the enum constant, just display the integral value | ||
return constant.ValueInternal.ToString(); | ||
Debug.Assert(constant.ValueInternal is object); | ||
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.
Why assert rather than using 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. |
||
var result = constant.ValueInternal.ToString(); | ||
Debug.Assert(result is object); | ||
return result; | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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.
Why assert rather than using
!
? #ByDesignThere 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.
Because this is the result of a check in a calling method (
if (constant.IsNull)
inToCSharpString
), but not in the present method.In reply to: 443063994 [](ancestors = 443063994)