-
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
Changes from 5 commits
c7daeb1
d8108db
d801f2a
b970a90
e7f6a2f
bb7bfc1
0ce6ff7
3d3cd5f
c823ee5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,20 @@ | |
// 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 | ||
{ | ||
public static class TypedConstantExtensions | ||
{ | ||
/// <summary> | ||
/// Returns the System.String that represents the current TypedConstant. | ||
/// For testing and debugging only | ||
/// </summary> | ||
/// <returns>A System.String that represents the current TypedConstant.</returns> | ||
public static string ToCSharpString(this TypedConstant constant) | ||
|
@@ -37,6 +32,7 @@ public static string ToCSharpString(this TypedConstant constant) | |
|
||
if (constant.Kind == TypedConstantKind.Type || constant.TypeInternal.SpecialType == SpecialType.System_Object) | ||
{ | ||
Debug.Assert(constant.Value is object); | ||
return "typeof(" + constant.Value.ToString() + ")"; | ||
} | ||
|
||
|
@@ -46,6 +42,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 +52,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); | ||
|
@@ -78,18 +76,18 @@ private static string DisplayUnsignedEnumConstant(TypedConstant constant, Specia | |
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(); | ||
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,7 +138,10 @@ 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); | ||
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. Because this is the result of a check in a calling method ( In reply to: 443063994 [](ancestors = 443063994) |
||
var result = constant.ValueInternal.ToString(); | ||
Debug.Assert(result is object); | ||
return result; | ||
} | ||
|
||
private static string DisplaySignedEnumConstant(TypedConstant constant, SpecialType specialType, long constantToDecode, string typeName) | ||
|
@@ -152,17 +153,17 @@ private static string DisplaySignedEnumConstant(TypedConstant constant, SpecialT | |
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(); | ||
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 +214,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; | ||
} | ||
} | ||
} |
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 method is part of the public API so I don't think we can state how this method is used. #Closed
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.
Thanks
In reply to: 443063564 [](ancestors = 443063564)