in src/dotnet/PowerToys.CyclomaticComplexity/ComplexityAnalysisElementProblemAnalyzer.cs [161:200]
private static HashSet<IControlFlowEdge> FudgeGraph(IControlFlowGraph graph, HashSet<IControlFlowEdge> edges)
{
// The C# graph treats the unary negation operator `!` as a conditional. It's not,
// but having it so means that the CC can be way higher than it should be.
var dodgyEdges = new HashSet<IControlFlowEdge>(new EdgeEqualityComparer());
// Look at all of the non-leaf elements (the graph is a tree as well as a graph.
// The tree represents the structure of the code, with the control flow graph
// running through it)
foreach (var element in graph.AllElements.Where(e => e.Children.Count != 0))
{
var unaryOperatorExpression = element.SourceElement as IUnaryOperatorExpression;
if (unaryOperatorExpression != null && unaryOperatorExpression.UnaryOperatorType == UnaryOperatorType.EXCL)
{
// The unary operator shouldn't have 2 exits. It's not a conditional. If it does,
// remove one of the edges, and it's path, from the collected edges.
if (element.Exits.Count == 2)
{
var edge = element.Exits.FirstOrDefault();
do
{
dodgyEdges.Add(edge);
// Get the source of the exit edge. This is the node in the control flow graph,
// not the element in the program structure tree.
var source = edge.Source;
edge = null;
// Walk back to the control flow graph node that represents the exit of the
// dodgy condition, so keep going until we have an element with 2 exits.
if (source.Entries.Count == 1 && source.Exits.Count == 1)
edge = source.Entries.FirstOrDefault();
} while (edge != null);
}
}
}
edges.ExceptWith(dodgyEdges);
return edges;
}