diff options
author | Tlakaelel Axayakatl Ceja <tlakaelel.ceja@microsoft.com> | 2022-04-20 01:40:34 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-20 01:40:34 +0300 |
commit | 36b61348be2b93f2add8886174f19c0ed4aacd36 (patch) | |
tree | 6970d27088e300e31bed40b33ba8140ec234bb31 | |
parent | c2fff337c62780b2c84d5fda975a4e001bb6fe8b (diff) |
Add support for Debug output in Analyzer (#2750)
When debugging applications via de console is useful to have a Console print about the different values being use. In the case of debugging via the Test Explorer in Visual Studio the Console output is not available to the user.
This PR adds the functionality to be able to choose to print to the Debug console that is available in Visual Studio while running a test via the Test Explorer
Adds enum to choose between Console and Debug output
Choose the Tracing type based on the debugger state
-rw-r--r-- | src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimDataFlowAnalysis.cs | 55 |
1 files changed, 46 insertions, 9 deletions
diff --git a/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimDataFlowAnalysis.cs b/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimDataFlowAnalysis.cs index de27ed42e..05ddd21c7 100644 --- a/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimDataFlowAnalysis.cs +++ b/src/ILLink.RoslynAnalyzer/TrimAnalysis/TrimDataFlowAnalysis.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.Diagnostics; using System.Linq; using ILLink.RoslynAnalyzer.DataFlow; using ILLink.Shared.DataFlow; @@ -52,9 +53,17 @@ namespace ILLink.RoslynAnalyzer.TrimAnalysis // Set this to true to print out the dataflow states encountered during the analysis. readonly bool showStates = false; + + static readonly TracingType tracingMechanism = Debugger.IsAttached ? TracingType.Debug : TracingType.Console; #pragma warning restore CA1805 // Do not initialize unnecessarily ControlFlowGraphProxy cfg; + private enum TracingType + { + Console, + Debug + } + public override void TraceStart (ControlFlowGraphProxy cfg) { this.cfg = cfg; @@ -80,20 +89,48 @@ namespace ILLink.RoslynAnalyzer.TrimAnalysis if (!trace) return; - Console.Write ("block " + block.Block.Ordinal + ": "); + TraceWrite ("block " + block.Block.Ordinal + ": "); if (block.Block.Operations.FirstOrDefault () is IOperation firstBlockOp) { - Console.WriteLine (firstBlockOp.Syntax.ToString ()); + TraceWriteLine (firstBlockOp.Syntax.ToString ()); } else if (block.Block.BranchValue is IOperation branchOp) { - Console.WriteLine (branchOp.Syntax.ToString ()); + TraceWriteLine (branchOp.Syntax.ToString ()); } else { - Console.WriteLine (); + TraceWriteLine (""); } - Console.Write ("predecessors: "); + TraceWrite ("predecessors: "); foreach (var predecessor in cfg.GetPredecessors (block)) { var predProxy = predecessor.Block; - Console.Write (predProxy.Block.Ordinal + " "); + TraceWrite (predProxy.Block.Ordinal + " "); + } + TraceWriteLine (""); + } + + private static void TraceWriteLine (string tracingInfo) + { + switch (tracingMechanism) { + case TracingType.Console: + Console.WriteLine (tracingInfo); + break; + case TracingType.Debug: + Debug.WriteLine (tracingInfo); + break; + default: + throw new NotImplementedException (message: "invalid TracingType is being used"); + } + } + + private static void TraceWrite (string tracingInfo) + { + switch (tracingMechanism) { + case TracingType.Console: + Console.Write (tracingInfo); + break; + case TracingType.Debug: + Debug.Write (tracingInfo); + break; + default: + throw new NotImplementedException (message: "invalid TracingType is being used"); } - Console.WriteLine (); } static void WriteIndented (string? s, int level) @@ -102,8 +139,8 @@ namespace ILLink.RoslynAnalyzer.TrimAnalysis if (lines == null) return; foreach (var line in lines) { - Console.Write (new String ('\t', level)); - Console.WriteLine (line); + TraceWrite (new String ('\t', level)); + TraceWriteLine (line); } } |