Skip to content

Commit 0321a31

Browse files
authored
Fixed smart pointer for multiline source code (#1674)
1 parent b97bf60 commit 0321a31

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/BenchmarkDotNet.Disassembler.x64/SourceCodeProvider.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.Diagnostics.Runtime.Utilities.Pdb;
44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.IO;
78

89
namespace BenchmarkDotNet.Disassemblers
@@ -24,8 +25,9 @@ internal static IEnumerable<Sharp> GetSource(ClrMethod method, ILToNativeMap map
2425
continue;
2526

2627
var text = sourceLine + Environment.NewLine
27-
+ GetSmartPrefix(sourceLine, sourceLocation.ColStart - 1)
28-
+ new string('^', sourceLocation.ColEnd - sourceLocation.ColStart);
28+
+ GetSmartPointer(sourceLine,
29+
start: line == sourceLocation.LineNumber ? sourceLocation.ColStart - 1 : default(int?),
30+
end: line == sourceLocation.LineNumberEnd ? sourceLocation.ColEnd - 1 : default(int?));
2931

3032
yield return new Sharp
3133
{
@@ -54,17 +56,32 @@ private static string ReadSourceLine(string file, int line)
5456
: null; // "nop" can have no corresponding c# code ;)
5557
}
5658

57-
private static string GetSmartPrefix(string sourceLine, int length)
59+
private static string GetSmartPointer(string sourceLine, int? start, int? end)
5860
{
59-
if (length <= 0)
60-
return string.Empty;
61+
Debug.Assert(start is null || start < sourceLine.Length);
62+
Debug.Assert(end is null || end <= sourceLine.Length);
6163

62-
var prefix = new char[length];
63-
for (int i = 0; i < length; i++)
64+
var prefix = new char[end ?? sourceLine.Length];
65+
var index = 0;
66+
67+
// write offset using whitespaces
68+
while (index < (start ?? prefix.Length))
6469
{
65-
char sourceChar = i < sourceLine.Length ? sourceLine[i] : ' ';
66-
prefix[i] = sourceChar == '\t' ? sourceChar : ' ';
70+
prefix[index] =
71+
sourceLine.Length > index &&
72+
sourceLine[index] == '\t'
73+
? '\t'
74+
: ' ';
75+
index++;
6776
}
77+
78+
// write smart pointer
79+
while (index < prefix.Length)
80+
{
81+
prefix[index] = '^';
82+
index++;
83+
}
84+
6885
return new string(prefix);
6986
}
7087
}

0 commit comments

Comments
 (0)