Skip to content
Permalink
Browse files

Annotate SyntaxNode

  • Loading branch information
jaredpar committed Dec 19, 2019
1 parent 3ddf1c5 commit 8eaf6a97b91557cf28577a164ca4457aa0a61f04
Showing with 386 additions and 157 deletions.
  1. +1 −0 .vscode/tasks.json
  2. +180 −0 src/Compilers/CSharp/Portable/CSharpExtensions.cs
  3. +1 −1 src/Compilers/CSharp/Portable/Symbols/Tuples/TupleTypeSymbol.cs
  4. +54 −52 src/Compilers/Core/Portable/Syntax/SyntaxNode.cs
  5. +1 −1 src/Features/CSharp/Portable/CodeRefactorings/SyncNamespace/CSharpChangeNamespaceService.cs
  6. +75 −64 src/Features/CSharp/Portable/EditAndContinue/CSharpEditAndContinueAnalyzer.cs
  7. +6 −4 src/Features/CSharp/Portable/EditAndContinue/StatementSyntaxComparer.cs
  8. +1 −1 src/Features/CSharp/Portable/EditAndContinue/SyntaxComparer.cs
  9. +1 −0 src/Features/CSharp/Portable/ExtractMethod/CSharpSelectionResult.ExpressionResult.cs
  10. +1 −1 src/Features/CSharp/Portable/ImplementInterface/AbstractChangeImplementionCodeRefactoringProvider.cs
  11. +3 −0 src/Features/CSharp/Portable/ImplementInterface/CSharpImplementExplicitlyCodeRefactoringProvider.cs
  12. +4 −0 src/Features/CSharp/Portable/MakeLocalFunctionStatic/MakeLocalFunctionStaticHelper.cs
  13. +4 −4 src/Features/Core/Portable/CodeRefactorings/AbstractRefactoringHelpersService.cs
  14. +24 −19 src/Features/Core/Portable/EditAndContinue/AbstractEditAndContinueAnalyzer.cs
  15. +4 −3 src/Workspaces/CSharp/Portable/Classification/ClassificationHelpers.cs
  16. +1 −1 src/Workspaces/CSharp/Portable/Extensions/ILocalSymbolExtensions.cs
  17. +3 −1 src/Workspaces/CSharp/Portable/LanguageServices/CSharpSyntaxFactsService.cs
  18. +1 −1 src/Workspaces/Core/Portable/AddImports/AbstractAddImportsService.cs
  19. +2 −1 src/Workspaces/Core/Portable/Differencing/TreeComparer.cs
  20. +4 −1 src/Workspaces/Core/Portable/LanguageServices/SyntaxFactsService/AbstractSyntaxFactsService.cs
  21. +10 −2 src/Workspaces/Core/Portable/LanguageServices/SyntaxFactsService/ISyntaxFactsService.cs
  22. +5 −0 src/Workspaces/Core/Portable/Shared/Extensions/ParenthesizedExpressionSyntaxExtensions.cs
@@ -29,6 +29,7 @@
"args": [
"msbuild",
"-p:UseRoslynAnalyzers=false",
"-p:GenerateFullPaths=true",
"src/Compilers/CSharp/csc/csc.csproj"
],
"problemMatcher": "$msCompile",

Large diffs are not rendered by default.

@@ -298,7 +298,7 @@ internal static void ReportTupleNamesMismatchesIfAny(TypeSymbol destination, Bou

if (sourceName != null && !wasInferred && (allMissing || string.CompareOrdinal(destinationNames[i], sourceName) != 0))
{
diagnostics.Add(ErrorCode.WRN_TupleLiteralNameMismatch, literal.Arguments[i].Syntax.Parent.Location, sourceName, destination);
diagnostics.Add(ErrorCode.WRN_TupleLiteralNameMismatch, literal.Arguments[i].Syntax.Parent!.Location, sourceName, destination);
}
}
}

Large diffs are not rendered by default.

@@ -403,7 +403,7 @@ static bool ContainsNamespaceDeclaration(SyntaxNode node)
.OfType<NamespaceDeclarationSyntax>().Any();
}

private static string? GetAliasQualifier(SyntaxNode name)
private static string? GetAliasQualifier(SyntaxNode? name)
{
while (true)
{

Large diffs are not rendered by default.

@@ -35,7 +35,8 @@ internal StatementSyntaxComparer(SyntaxNode oldRootChild, SyntaxNode newRootChil

#region Tree Traversal

protected internal override bool TryGetParent(SyntaxNode node, out SyntaxNode parent)
#pragma warning disable 8610 // Making the out parameter nullable
protected internal override bool TryGetParent(SyntaxNode node, [NotNullWhen(true)] out SyntaxNode? parent)
{
parent = node.Parent;
while (parent != null && !HasLabel(parent))
@@ -45,6 +46,7 @@ protected internal override bool TryGetParent(SyntaxNode node, out SyntaxNode pa

return parent != null;
}
#pragma warning restore 8610

protected internal override IEnumerable<SyntaxNode>? GetChildren(SyntaxNode node)
{
@@ -830,10 +832,10 @@ private bool TryComputeWeightedDistance(BlockSyntax leftBlock, BlockSyntax right
if (leftCatch.Declaration == null && leftCatch.Filter == null &&
rightCatch.Declaration == null && rightCatch.Filter == null)
{
var leftTry = (TryStatementSyntax)leftCatch.Parent;
var rightTry = (TryStatementSyntax)rightCatch.Parent;
var leftTry = (TryStatementSyntax?)leftCatch.Parent;
var rightTry = (TryStatementSyntax?)rightCatch.Parent;

distance = 0.5 * ComputeValueDistance(leftTry.Block, rightTry.Block) +
distance = 0.5 * ComputeValueDistance(leftTry?.Block, rightTry?.Block) +
0.5 * ComputeValueDistance(leftBlock, rightBlock);
}
else
@@ -61,7 +61,7 @@ public sealed override double GetDistance(SyntaxNode oldNode, SyntaxNode newNode
return ComputeValueDistance(oldNode, newNode);
}

internal static double ComputeValueDistance(SyntaxNode oldNode, SyntaxNode newNode)
internal static double ComputeValueDistance(SyntaxNode? oldNode, SyntaxNode? newNode)
{
if (SyntaxFactory.AreEquivalent(oldNode, newNode))
{
@@ -91,6 +91,7 @@ public override bool ContainingScopeHasAsyncKeyword()
{
// regular case. always use ConvertedType to get implicit conversion right.
var expression = node.GetUnparenthesizedExpression();
RoslynDebug.Assert(expression is object);

var info = semanticModel.GetTypeInfo(expression);
var conv = semanticModel.GetConversion(expression);
@@ -147,7 +147,7 @@ private static async Task<(SyntaxNode?, ExplicitInterfaceSpecifierSyntax?, Synta

private static (SyntaxNode?, ExplicitInterfaceSpecifierSyntax?, SyntaxToken) GetContainer(SyntaxToken token)
{
for (var node = token.Parent; node != null; node = node.Parent)
for (SyntaxNode? node = token.Parent; node != null; node = node.Parent)
{
var result = node switch
{
@@ -16,6 +16,7 @@
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.ImplementInterface
{
@@ -90,9 +91,11 @@ protected override bool CheckMemberCanBeConverted(ISymbol member)
? identifierName.Parent
: identifierName;

RoslynDebug.Assert(node is object);
if (syntaxFacts.IsInvocationExpression(node.Parent))
node = node.Parent;

RoslynDebug.Assert(node is object);
var operation = semanticModel.GetOperation(node);
var instance = operation switch
{
@@ -27,11 +27,15 @@ public static bool IsStaticLocalFunctionSupported(SyntaxTree tree)
public static bool TryGetCaputuredSymbols(LocalFunctionStatementSyntax localFunction, SemanticModel semanticModel, out ImmutableArray<ISymbol> captures)
{
var dataFlow = semanticModel.AnalyzeDataFlow(localFunction);
<<<<<<< HEAD
if (dataFlow is null)
{
captures = default;
return false;
}
=======
captures = dataFlow!.CapturedInside;
>>>>>>> Annotate SyntaxNode

captures = dataFlow.CapturedInside;
return dataFlow.Succeeded;
@@ -210,7 +210,7 @@ static bool IsAEqualOrSubclassOfB(Type a, Type b)
// there could be multiple (n) tokens to the left if first n-1 are Empty -> iterate over all of them
while (tokenToLeft != default)
{
var leftNode = tokenToLeft.Parent;
SyntaxNode? leftNode = tokenToLeft.Parent;
do
{
// Consider either a Node that is:
@@ -236,7 +236,7 @@ static bool IsAEqualOrSubclassOfB(Type a, Type b)
{
if (tokenToRightOrIn != default)
{
var rightNode = tokenToRightOrIn.Parent;
SyntaxNode? rightNode = tokenToRightOrIn.Parent;
do
{
// Consider either a Node that is:
@@ -272,7 +272,7 @@ static bool IsAEqualOrSubclassOfB(Type a, Type b)

private void AddRelevantNodesForSelection<TSyntaxNode>(ISyntaxFactsService syntaxFacts, SyntaxNode root, TextSpan selectionTrimmed, ArrayBuilder<TSyntaxNode> relevantNodesBuilder, CancellationToken cancellationToken) where TSyntaxNode : SyntaxNode
{
var selectionNode = root.FindNode(selectionTrimmed, getInnermostNodeForTie: true);
SyntaxNode? selectionNode = root.FindNode(selectionTrimmed, getInnermostNodeForTie: true);
var prevNode = selectionNode;
do
{
@@ -462,7 +462,7 @@ protected virtual IEnumerable<SyntaxNode> ExtractNodesInHeader(SyntaxNode root,
var token = root.FindTokenOnRightOfPosition(position, true);

// traverse upwards and add all parents if of correct type
var ancestor = token.Parent;
SyntaxNode? ancestor = token.Parent;
while (ancestor != null)
{
if (ancestor is TSyntaxNode correctTypeNode)
@@ -146,7 +146,7 @@ private SyntaxNode FindStatement(SyntaxNode declarationBody, TextSpan span, out
/// or null if the node isn't contained in a lambda. If a node is returned it must uniquely represent the lambda,
/// i.e. be no two distinct nodes may represent the same lambda.
/// </summary>
protected abstract SyntaxNode? FindEnclosingLambdaBody(SyntaxNode? container, SyntaxNode node);
protected abstract SyntaxNode? FindEnclosingLambdaBody(SyntaxNode? container, SyntaxNode? node);

/// <summary>
/// Given a node that represents a lambda body returns all nodes of the body in a syntax list.
@@ -238,21 +238,21 @@ internal TextSpan GetDiagnosticSpan(SyntaxNode node, EditKind editKind)

protected virtual TextSpan GetBodyDiagnosticSpan(SyntaxNode node, EditKind editKind)
{
var initialNode = node;

var current = node.Parent;
while (true)
{
node = node.Parent;
if (node == null)
if (current == null)
{
return initialNode.Span;
return node.Span;
}

var span = TryGetDiagnosticSpan(node, editKind);
var span = TryGetDiagnosticSpan(current, editKind);
if (span != null)
{
return span.Value;
}

current = current.Parent;
}
}

@@ -267,20 +267,21 @@ internal string GetDisplayName(SyntaxNode node, EditKind editKind = EditKind.Upd
/// </summary>
protected virtual string GetBodyDisplayName(SyntaxNode node, EditKind editKind = EditKind.Update)
{
var initialNode = node;
var current = node.Parent;
while (true)
{
node = node.Parent;
if (node == null)
if (current == null)
{
throw ExceptionUtilities.UnexpectedValue(initialNode.GetType().Name);
throw ExceptionUtilities.UnexpectedValue(node.GetType().Name);
}

var displayName = TryGetDisplayName(node, editKind);
var displayName = TryGetDisplayName(current, editKind);
if (displayName != null)
{
return displayName;
}

current = current.Parent;
}
}

@@ -314,7 +315,7 @@ protected virtual string GetSuspensionPointDisplayName(SyntaxNode node, EditKind
internal abstract bool ContainsLambda(SyntaxNode declaration);
internal abstract SyntaxNode GetLambda(SyntaxNode lambdaBody);
internal abstract IMethodSymbol GetLambdaExpressionSymbol(SemanticModel model, SyntaxNode lambdaExpression, CancellationToken cancellationToken);
internal abstract SyntaxNode GetContainingQueryExpression(SyntaxNode node);
internal abstract SyntaxNode? GetContainingQueryExpression(SyntaxNode node);
internal abstract bool QueryClauseLambdasTypeEquivalent(SemanticModel oldModel, SyntaxNode oldNode, SemanticModel newModel, SyntaxNode newNode, CancellationToken cancellationToken);

/// <summary>
@@ -1680,7 +1681,7 @@ internal TextSpan GetDeletedNodeDiagnosticSpan(IReadOnlyDictionary<SyntaxNode, S
/// <summary>
/// Finds the inner-most ancestor of the specified node that has a matching node in the new tree.
/// </summary>
private static bool TryGetMatchingAncestor(IReadOnlyDictionary<SyntaxNode, SyntaxNode> forwardMap, SyntaxNode oldNode, [NotNullWhen(true)]out SyntaxNode? newAncestor)
private static bool TryGetMatchingAncestor(IReadOnlyDictionary<SyntaxNode, SyntaxNode> forwardMap, SyntaxNode? oldNode, [NotNullWhen(true)]out SyntaxNode? newAncestor)
{
while (oldNode != null)
{
@@ -1753,9 +1754,12 @@ protected static bool HasParentEdit(Dictionary<SyntaxNode, EditKind> editMap, Ed
return HasEdit(editMap, node.Parent, edit.Kind);
}

protected static bool HasEdit(Dictionary<SyntaxNode, EditKind> editMap, SyntaxNode node, EditKind editKind)
protected static bool HasEdit(Dictionary<SyntaxNode, EditKind> editMap, SyntaxNode? node, EditKind editKind)
{
return editMap.TryGetValue(node, out var parentEdit) && parentEdit == editKind;
return
node is object &&
editMap.TryGetValue(node, out var parentEdit) &&
parentEdit == editKind;
}

#endregion
@@ -1951,11 +1955,11 @@ private static int IndexOfEquivalent<TSyntaxNode>(SyntaxNode newNode, List<Synta
return -1;
}

private static List<SyntaxNode?>? GetAncestors(SyntaxNode? root, SyntaxNode node, Func<SyntaxNode, bool> nodeSelector)
private static List<SyntaxNode?>? GetAncestors(SyntaxNode? root, SyntaxNode? node, Func<SyntaxNode, bool> nodeSelector)
{
List<SyntaxNode?>? list = null;

while (node != root)
while (node is object && node != root)
{
if (nodeSelector(node))
{
@@ -3812,9 +3816,10 @@ private SyntaxNode GetCapturedVariableScope(ISymbol localOrParameter, SyntaxNode
return memberBody;
}

var node = GetSymbolSyntax(localOrParameter, cancellationToken);
SyntaxNode? node = GetSymbolSyntax(localOrParameter, cancellationToken);
while (true)
{
RoslynDebug.Assert(node is object);
if (IsClosureScope(node))
{
return node;
@@ -249,7 +249,8 @@ private static bool IsVerbatimStringToken(SyntaxToken token)
// Handle nested Tuple deconstruction
while (parent.IsKind(SyntaxKind.ParenthesizedVariableDesignation))
{
parent = parent.Parent;
// https://github.com/dotnet/roslyn/issues/40509
parent = parent!.Parent;
}

// Checking for DeclarationExpression covers the following cases:
@@ -315,7 +316,7 @@ private static bool IsNamespaceName(IdentifierNameSyntax identifierSyntax)

public static bool IsStaticallyDeclared(SyntaxToken token)
{
var parentNode = token.Parent;
SyntaxNode? parentNode = token.Parent;

if (parentNode.IsKind(SyntaxKind.EnumMemberDeclaration))
{
@@ -328,7 +329,7 @@ public static bool IsStaticallyDeclared(SyntaxToken token)
{
// The parent of a VariableDeclarator is a VariableDeclarationSyntax node.
// It's parent will be the declaration syntax node.
parentNode = parentNode.Parent.Parent;
parentNode = parentNode!.Parent!.Parent;

// Check if this is a field constant declaration
if (parentNode.GetModifiers().Any(SyntaxKind.ConstKeyword))
@@ -27,7 +27,7 @@ public static bool CanSafelyMoveLocalToBlock(this ILocalSymbol localSymbol, Synt

return true;

bool HasTypeParameterWithName(SyntaxNode node, string name)
bool HasTypeParameterWithName(SyntaxNode? node, string name)
{
SeparatedSyntaxList<TypeParameterSyntax>? typeParameters;
switch (node)
@@ -156,10 +156,12 @@ public bool IsAttributeName(SyntaxNode node)
return SyntaxFacts.IsAttributeName(node);
}

public bool IsInvocationExpression(SyntaxNode node)
#nullable enable
public bool IsInvocationExpression(SyntaxNode? node)
{
return node is InvocationExpressionSyntax;
}
#nullable restore

public bool IsAnonymousFunction(SyntaxNode node)
{
@@ -170,7 +170,7 @@ private void GetContainers(SyntaxNode root, SyntaxNode contextLocation, out Synt
var usingDirective = contextNode.GetAncestor<TUsingOrAliasSyntax>();
if (usingDirective != null)
{
contextNode = usingDirective.Parent;
contextNode = usingDirective.Parent!;
}

return contextNode.GetAncestor<TNamespaceDeclarationSyntax>() ??
@@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

#nullable enable
@@ -95,7 +96,7 @@ public Match<TNode> ComputeMatch(TNode oldRoot, TNode newRoot, IEnumerable<KeyVa
/// <summary>
/// Returns a parent for the specified node.
/// </summary>
protected internal abstract bool TryGetParent(TNode node, out TNode parent);
protected internal abstract bool TryGetParent(TNode node, [NotNullWhen(true)][AllowNull] out TNode parent);

internal TNode GetParent(TNode node)
{
@@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text;
@@ -607,7 +608,9 @@ public bool IsUsingStatement(SyntaxNode node)
public bool IsReturnStatement(SyntaxNode node)
=> node?.RawKind == SyntaxKinds.ReturnStatement;

public bool IsExpressionStatement(SyntaxNode node)
#nullable enable
public bool IsExpressionStatement([NotNullWhen(true)] SyntaxNode? node)
=> node?.RawKind == SyntaxKinds.ExpressionStatement;
#nullable restore
}
}

0 comments on commit 8eaf6a9

Please sign in to comment.
You can’t perform that action at this time.