public static TestOperationVisitor GetInstance() { if (s_instance == null) { s_instance = new TestOperationVisitor(); } return(s_instance); }
public static ControlFlowGraph GetControlFlowGraph(SyntaxNode syntaxNode, SemanticModel model) { IOperation operationRoot = model.GetOperation(syntaxNode); // Workaround for unit tests designed to work on IBlockOperation with ConstructorBodyOperation/MethodBodyOperation parent. operationRoot = operationRoot.Kind == OperationKind.Block && (operationRoot.Parent?.Kind == OperationKind.ConstructorBodyOperation || operationRoot.Parent?.Kind == OperationKind.MethodBodyOperation) ? operationRoot.Parent : operationRoot; TestOperationVisitor.VerifySubTree(operationRoot); ControlFlowGraph graph; switch (operationRoot) { case IBlockOperation blockOperation: graph = SemanticModel.GetControlFlowGraph(blockOperation); break; case IMethodBodyOperation methodBodyOperation: graph = SemanticModel.GetControlFlowGraph(methodBodyOperation); break; case IConstructorBodyOperation constructorBodyOperation: graph = SemanticModel.GetControlFlowGraph(constructorBodyOperation); break; case IFieldInitializerOperation fieldInitializerOperation: graph = SemanticModel.GetControlFlowGraph(fieldInitializerOperation); break; case IPropertyInitializerOperation propertyInitializerOperation: graph = SemanticModel.GetControlFlowGraph(propertyInitializerOperation); break; case IParameterInitializerOperation parameterInitializerOperation: graph = SemanticModel.GetControlFlowGraph(parameterInitializerOperation); break; default: return(null); } Assert.NotNull(graph); Assert.Same(operationRoot, graph.OriginalOperation); return(graph); }
public static void ValidateIOperations(Func <Compilation> createCompilation) { if (!EnableVerifyIOperation) { return; } var compilation = createCompilation(); var roots = ArrayBuilder <IOperation> .GetInstance(); var stopWatch = new Stopwatch(); if (!System.Diagnostics.Debugger.IsAttached) { stopWatch.Start(); } void checkTimeout() { const int timeout = 10000; Assert.False(stopWatch.ElapsedMilliseconds > timeout, "ValidateIOperations took too long"); } foreach (var tree in compilation.SyntaxTrees) { var semanticModel = compilation.GetSemanticModel(tree); var root = tree.GetRoot(); foreach (var node in root.DescendantNodesAndSelf()) { checkTimeout(); var operation = semanticModel.GetOperation(node); if (operation != null) { // Make sure IOperation returned by GetOperation(syntaxnode) will have same syntaxnode as the given syntaxnode(IOperation.Syntax == syntaxnode). Assert.True(node == operation.Syntax, $"Expected : {node} - Actual : {operation.Syntax}"); Assert.True(operation.Type == null || !operation.MustHaveNullType(), $"Unexpected non-null type: {operation.Type}"); if (operation.Parent == null) { roots.Add(operation); } } } } var explictNodeMap = new Dictionary <SyntaxNode, IOperation>(); var visitor = TestOperationVisitor.GetInstance(); foreach (var root in roots) { foreach (var operation in root.DescendantsAndSelf()) { checkTimeout(); if (!operation.IsImplicit) { try { explictNodeMap.Add(operation.Syntax, operation); } catch (ArgumentException) { Assert.False(true, $"Duplicate explicit node for syntax ({operation.Syntax.RawKind}): {operation.Syntax.ToString()}"); } } visitor.Visit(operation); } } roots.Free(); stopWatch.Stop(); }