diff --git a/src/MIDebugEngine/Natvis.Impl/Natvis.cs b/src/MIDebugEngine/Natvis.Impl/Natvis.cs index d4a7fab6e..41711e6fc 100755 --- a/src/MIDebugEngine/Natvis.Impl/Natvis.cs +++ b/src/MIDebugEngine/Natvis.Impl/Natvis.cs @@ -1440,6 +1440,13 @@ private string GetExpressionValue(string expression, IVariableInformation variab string processedExpr = ReplaceNamesInExpression(expression, variable, scopedNames); IVariableInformation expressionVariable = new VariableInformation(processedExpr, variable, _process.Engine, null); expressionVariable.SyncEval(); + + // Avoid recursive natvis formatting when expression is 'this' + if (expression.Trim() == "this") + { + return expressionVariable.Value; + } + return FormatDisplayString(expressionVariable).value; } diff --git a/test/CppTests/Tests/NatvisTests.cs b/test/CppTests/Tests/NatvisTests.cs index 0d67f0085..58b2a2223 100644 --- a/test/CppTests/Tests/NatvisTests.cs +++ b/test/CppTests/Tests/NatvisTests.cs @@ -38,8 +38,8 @@ public NatvisTests(ITestOutputHelper outputHelper) : base(outputHelper) private const string NatvisSourceName = "main.cpp"; // These line numbers will need to change if src/natvis/main.cpp changes - private const int SimpleClassAssignmentLine = 64; - private const int ReturnSourceLine = 76; + private const int SimpleClassAssignmentLine = 65; + private const int ReturnSourceLine = 80; [Theory] [RequiresTestSettings] @@ -397,6 +397,47 @@ public void TestThisConditional(ITestSettings settings) } } + [Theory] + [DependsOnTest(nameof(CompileNatvisDebuggee))] + [RequiresTestSettings] + public void TestThisInDisplayString(ITestSettings settings) + { + this.TestPurpose("This test checks that {this} in a DisplayString value does not produce extra braces."); + this.WriteSettings(settings); + + IDebuggee debuggee = Debuggee.Open(this, settings.CompilerSettings, NatvisName, DebuggeeMonikers.Natvis.Default); + + using (IDebuggerRunner runner = CreateDebugAdapterRunner(settings)) + { + this.Comment("Configure launch"); + string visFile = Path.Join(debuggee.SourceRoot, "visualizer_files", "Simple.natvis"); + + LaunchCommand launch = new LaunchCommand(settings.DebuggerSettings, debuggee.OutputPath, visFile, false); + runner.RunCommand(launch); + + this.Comment("Set Breakpoint"); + SourceBreakpoints writerBreakpoints = debuggee.Breakpoints(NatvisSourceName, ReturnSourceLine); + runner.SetBreakpoints(writerBreakpoints); + + runner.Expects.StoppedEvent(StoppedReason.Breakpoint).AfterConfigurationDone(); + + using (IThreadInspector threadInspector = runner.GetThreadInspector()) + { + IFrameInspector currentFrame = threadInspector.Stack.First(); + + this.Comment("Verifying {this} in DisplayString does not recurse"); + var dpPtr = currentFrame.GetVariable("dpPtr"); + + // Natvis: {{ {this}={*this} }} + // Expected: "{ 0x=42 }" — {this} is the raw address, {*this} is the dereferenced value + Assert.Matches(@"^\{ 0x[0-9a-fA-F]+=42 \}$", dpPtr.Value); + } + + runner.Expects.ExitedEvent(exitCode: 0).TerminatedEvent().AfterContinue(); + runner.DisconnectAndVerify(); + } + } + [Theory] [DependsOnTest(nameof(CompileNatvisDebuggee))] [RequiresTestSettings] diff --git a/test/CppTests/debuggees/natvis/src/DataPoint.h b/test/CppTests/debuggees/natvis/src/DataPoint.h new file mode 100644 index 000000000..8b673b3e8 --- /dev/null +++ b/test/CppTests/debuggees/natvis/src/DataPoint.h @@ -0,0 +1,5 @@ +struct DataPoint +{ + int value; + DataPoint(int v) : value(v) {} +}; diff --git a/test/CppTests/debuggees/natvis/src/main.cpp b/test/CppTests/debuggees/natvis/src/main.cpp index 460663c37..1a578fa08 100644 --- a/test/CppTests/debuggees/natvis/src/main.cpp +++ b/test/CppTests/debuggees/natvis/src/main.cpp @@ -5,6 +5,7 @@ #include "SimpleClass.h" #include "SimpleMatrix.h" #include "SimpleTemplated.h" +#include "DataPoint.h" class SimpleDisplayObject { @@ -73,5 +74,8 @@ int main(int argc, char** argv) HideRawViewObject hideRawObj; ShowRawViewObject showRawObj; + DataPoint dp(42); + DataPoint *dpPtr = &dp; + return 0; } diff --git a/test/CppTests/debuggees/natvis/src/visualizer_files/Simple.natvis b/test/CppTests/debuggees/natvis/src/visualizer_files/Simple.natvis index 192772d34..189136e35 100644 --- a/test/CppTests/debuggees/natvis/src/visualizer_files/Simple.natvis +++ b/test/CppTests/debuggees/natvis/src/visualizer_files/Simple.natvis @@ -113,4 +113,12 @@ + + {value} + + + + {{ {this}={*this} }} + + \ No newline at end of file