[PATCH/RFC] cobol: capture source ranges for tokens, rather than just points
Commit Message
This patch changes the output on the simple test I tried from:
$ ./gcobol -B. hello.cob -S
hello.cob:2:8: error: syntax error, unexpected NAME, expecting FUNCTION or PROGRAM-ID
2 | porgram-id. hello.
| ^
cobol1: error: failed compiling hello.cob
to:
$ ./gcobol -B. hello.cob -S
hello.cob:2:8: error: syntax error, unexpected NAME, expecting FUNCTION or PROGRAM-ID
2 | porgram-id. hello.
| ^~~~~~~~~~~
cobol1: error: failed compiling hello.cob
I haven't tested this patch properly yet.
gcc/cobol/ChangeLog:
* util.cc (gcc_location_set_impl): Capture the start and end of "loc"
as a range, rather than just the (last_line, first_column).
---
gcc/cobol/util.cc | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
Comments
On Tue, 2025-03-11 at 11:47 -0400, David Malcolm wrote:
> This patch changes the output on the simple test I tried from:
>
> $ ./gcobol -B. hello.cob -S
> hello.cob:2:8: error: syntax error, unexpected NAME, expecting
> FUNCTION or PROGRAM-ID
> 2 | porgram-id. hello.
> | ^
> cobol1: error: failed compiling hello.cob
>
> to:
>
> $ ./gcobol -B. hello.cob -S
> hello.cob:2:8: error: syntax error, unexpected NAME, expecting
> FUNCTION or PROGRAM-ID
> 2 | porgram-id. hello.
> | ^~~~~~~~~~~
> cobol1: error: failed compiling hello.cob
>
> I haven't tested this patch properly yet.
>
On a related note, gcobol seems to be correctly emitting diagnostics as
machine-readable SARIF. I tried the above with -fdiagnostics-
format=sarif-file. For the curious, I'm attaching a screenshot of VS
Code viewing the above errors (via the sarif file and its sarif
extension) I was hoping for a long squiggly underline on the
misspelled token, though for some reason VS Code isn't showing the
range (but the .sarif file looks right to me).
Dave
@@ -1857,11 +1857,23 @@ cobol_filename_restore() {
static location_t token_location;
+/* Update global token_location with a location_t expressing a source
+ range with start and caret at the first line/column of LOC, and
+ finishing at the last line/column of LOC. */
+
template <typename LOC>
static void
gcc_location_set_impl( const LOC& loc ) {
- token_location = linemap_line_start( line_table, loc.last_line, 80 );
- token_location = linemap_position_for_column( line_table, loc.first_column);
+ const location_t start_line
+ = linemap_line_start( line_table, loc.first_line, 80 );
+ const location_t token_start
+ = linemap_position_for_column( line_table, loc.first_column);
+ const location_t finish_line
+ = linemap_line_start( line_table, loc.last_line, 80 );
+ const location_t token_finish
+ = linemap_position_for_column( line_table, loc.last_column);
+ token_location
+ = make_location (token_start, token_start, token_finish);
location_dump(__func__, __LINE__, "parser", loc);
}