本文整理汇总了C#中BlockSyntax.DescendantTokens方法的典型用法代码示例。如果您正苦于以下问题:C# BlockSyntax.DescendantTokens方法的具体用法?C# BlockSyntax.DescendantTokens怎么用?C# BlockSyntax.DescendantTokens使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BlockSyntax
的用法示例。
在下文中一共展示了BlockSyntax.DescendantTokens方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitBlock
public void VisitBlock(BlockSyntax node)
{
var tokens = node.DescendantTokens().ToList();
var dictionary = ParseTokens(tokens, Operands.All);
var dictionary2 = ParseTokens(tokens, Operators.All);
var metrics = new HalsteadMetrics(
numOperands: dictionary.Values.Sum(x => x.Count),
numUniqueOperands: dictionary.Values.SelectMany(x => x).Distinct().Count(),
numOperators: dictionary2.Values.Sum(x => x.Count),
numUniqueOperators: dictionary2.Values.SelectMany(x => x).Distinct().Count());
_metrics = metrics;
}
示例2: WouldCauseDefiniteAssignmentErrors
private bool WouldCauseDefiniteAssignmentErrors(
SemanticModel semanticModel, VariableDeclaratorSyntax localDeclarator,
BlockSyntax enclosingBlock, ISymbol outSymbol, CancellationToken cancellationToken)
{
// See if we have something like:
//
// int i = 0;
// if (Foo() || Bar(out i))
// {
// Console.WriteLine(i);
// }
//
// In this case, inlining the 'i' would cause it to longer be definitely
// assigned in the WriteLine invocation.
if (localDeclarator.Initializer == null)
{
// Don't need to examine this unless the variable has an initializer.
return false;
}
// Find all the current read-references to the local.
var query = from t in enclosingBlock.DescendantTokens()
where t.Kind() == SyntaxKind.IdentifierToken
where t.ValueText == outSymbol.Name
let id = t.Parent as IdentifierNameSyntax
where id != null
where !id.IsOnlyWrittenTo()
let symbol = semanticModel.GetSymbolInfo(id).GetAnySymbol()
where outSymbol.Equals(symbol)
select id;
var references = query.ToImmutableArray<SyntaxNode>();
var root = semanticModel.SyntaxTree.GetCompilationUnitRoot(cancellationToken);
// Ensure we can track the references and the local variable as we make edits
// to the tree.
var rootWithTrackedNodes = root.TrackNodes(references.Concat(localDeclarator).Concat(enclosingBlock));
// Now, take the local variable and remove it's initializer. Then go to all
// the locations where we read from it. If they're definitely assigned, then
// that means the out-var did it's work and assigned the variable across all
// paths. If it's not definitely assigned, then we can't inline this variable.
var currentLocalDeclarator = rootWithTrackedNodes.GetCurrentNode(localDeclarator);
var rootWithoutInitializer = rootWithTrackedNodes.ReplaceNode(
currentLocalDeclarator,
currentLocalDeclarator.WithInitializer(null));
// Fork the compilation so we can do this analysis.
var newCompilation = semanticModel.Compilation.ReplaceSyntaxTree(
root.SyntaxTree, rootWithoutInitializer.SyntaxTree);
var newSemanticModel = newCompilation.GetSemanticModel(rootWithoutInitializer.SyntaxTree);
// NOTE: there is no current compiler API to determine if a variable is definitely
// assigned or not. So, for now, we just get diagnostics for this block and see if
// we get any definite assigment errors where we have a reference to the symbol. If
// so, then we don't offer the fix.
var currentBlock = rootWithoutInitializer.GetCurrentNode(enclosingBlock);
var diagnostics = newSemanticModel.GetDiagnostics(currentBlock.Span, cancellationToken);
var diagnosticSpans = diagnostics.Where(d => d.Id == CS0165)
.Select(d => d.Location.SourceSpan)
.Distinct();
var newReferenceSpans = rootWithoutInitializer.GetCurrentNodes<SyntaxNode>(references)
.Select(n => n.Span)
.Distinct();
return diagnosticSpans.Intersect(newReferenceSpans).Any();
}