Skip to content

Commit 72899d3

Browse files
authored
Merge pull request livecode#4551 from peter-b/bugfix-18463
[Bug 18463] lc-compile: Correct error position on lines with control chars
2 parents bc24eba + 6c44baa commit 72899d3

4 files changed

Lines changed: 42 additions & 13 deletions

File tree

docs/lcb/notes/lc-compile-error-context.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
* Errors, warnings and informational messages now display the affected
66
line of code and visually indicate the position where the problem
77
was found.
8+
9+
# [18463] Show correct error position when source line includes tabs

tests/_compilertestrunner.livecodescript

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ private function doesCompilerOutputSatisfyAssertion pCompilerOutput, pCompilerEx
352352
put "search" into tState
353353
repeat for each line tOutputLine in pCompilerOutput
354354
local tFile, tLine, tColumn, tType, tMessage
355-
local tSourceLine, tMarker
355+
local tSourceLine, tCharOffset, tOutputChar, tMarker
356356

357357
if tState is "search" then
358358
-- The format of each output line is:
@@ -383,13 +383,21 @@ private function doesCompilerOutputSatisfyAssertion pCompilerOutput, pCompilerEx
383383

384384
else if tState is "source line" then
385385
-- The format of the source line output is a space followed
386-
-- by the corresponding line of the input source file.
387-
388-
put " " & line tLine of pTestInfo["code"] into tSourceLine
386+
-- by the corresponding line of the input source file. The
387+
-- compiler may replace any of the source characters with
388+
-- spaces (for example, to remove control characters).
389+
390+
put " " & line tLine of pTestInfo["code"] into tSourceLine
391+
392+
repeat with tCharOffset = 1 to the number of chars in tOutputLine
393+
put char tCharOffset of tOutputLine into tOutputChar
394+
if tOutputChar is not " " and \
395+
tOutputChar is not char tCharOffset of tSourceLine then
396+
return false
397+
end if
398+
end repeat
389399

390-
if tOutputLine is tSourceLine then
391-
put "marker" into tState
392-
end if
400+
put "marker" into tState
393401

394402
else if tState is "marker" then
395403
-- The marker should point at the correct column within the
@@ -402,6 +410,8 @@ private function doesCompilerOutputSatisfyAssertion pCompilerOutput, pCompilerEx
402410

403411
if tOutputLine is tMarker then
404412
return true
413+
else
414+
return false
405415
end if
406416
end if
407417
end repeat

toolchain/lc-compile/src/LAYOUT.b

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
\ { AdvanceCurrentPosition(1); }
2-
\t { }
2+
\t { AdvanceCurrentPosition(1); }

toolchain/lc-compile/src/report.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stdio.h>
2222
#include <stdlib.h>
2323
#include <stdarg.h>
24+
#include <string.h>
2425

2526
extern int IsDependencyCompile(void);
2627

@@ -174,15 +175,31 @@ static void _PrintPosition(long p_position)
174175
* that the position specifies. */
175176
static void _PrintContext(long p_position)
176177
{
177-
const char *t_text = NULL;
178+
const char *t_raw_text = NULL;
179+
char *t_text = NULL;
178180
long t_column;
179-
GetRowTextOfPosition(p_position, &t_text);
181+
int i;
182+
GetRowTextOfPosition(p_position, &t_raw_text);
180183
GetColumnOfPosition(p_position, &t_column);
181-
if (NULL != t_text)
184+
185+
if (NULL == t_raw_text)
186+
return;
187+
188+
/* Replace ASCII control characters in the raw text with spaces.
189+
* This has two goals: it simplifies aligning the context
190+
* indicator caret, and it ensures that control characters in the
191+
* LCB source code can't screw with your terminal emulator. Note
192+
* that this doesn't try to cope with invalid Unicode values. */
193+
t_text = strdup(t_raw_text);
194+
if (NULL == t_text)
195+
Fatal_OutOfMemory();
196+
for (i = 0; t_text[i] != 0; ++i)
182197
{
183-
fprintf(stderr, " %s\n", t_text);
184-
fprintf(stderr, " %*c\n", (int)t_column, '^');
198+
if (t_text[i] < 0x20 || t_text[i] == 0x7F)
199+
t_text[i] = ' ';
185200
}
201+
202+
fprintf(stderr, " %s\n %*c\n", t_text, (int)t_column, '^');
186203
}
187204

188205
static void _Error(long p_position, const char *p_message)

0 commit comments

Comments
 (0)