Skip to content

Commit

Permalink
New flag -fdiagnostics-absolute-paths to print full paths within diag…
Browse files Browse the repository at this point in the history
…nostics

This flag is useful for editors that cannot correctly locate files
with recursive build systems.
  • Loading branch information
lefessan committed Oct 5, 2023
1 parent c0d64ad commit 6833e35
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@

2023-10-02 Fabrice Le Fessant <fabrice.le_fessant@ocamlpro.com>

* error.c (print_error_prefix), flag.def: add new flag
-fdiagnostics-absolute-paths to print the full path of
a file in case of error. This flag can be activated if
your editor and build system do not correctly work
together to locate files after errors within your project.

2023-07-26 Simon Sobisch <simonsobisch@gnu.org>

* typeck.c (search_set_keys): improving SEARCH ALL syntax checks
Expand Down
24 changes: 24 additions & 0 deletions cobc/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/stat.h>

#include "cobc.h"
#include "tree.h"
Expand Down Expand Up @@ -57,13 +58,36 @@ static void
print_error_prefix (const char *file, int line, const char *prefix)
{
if (file) {
char *absfile = NULL ;
if (cb_diagnostics_absolute_paths
&& strcmp (file, COB_DASH) != 0
&& file[0] != '/'
&& file[0] != '\\'
&& ( file[0] != 0 && file[1] != ':' )
){
int filelen = strlen (file);
int dirlen = 256;
char *cwd ;
absfile = cobc_malloc( dirlen + 1 + filelen + 1 );
cwd = getcwd (absfile, dirlen);
if (cwd != NULL ){
struct stat st;
dirlen = strlen (cwd);
absfile[dirlen] = '/';
memcpy (absfile+dirlen+1, file, filelen+1);
if (!stat(absfile,&st)){
file = absfile;
}
}
}
if (line <= 0) {
fprintf (stderr, "%s: ", file);
} else if (cb_msg_style == CB_MSG_STYLE_MSC) {
fprintf (stderr, "%s(%d): ", file, line);
} else {
fprintf (stderr, "%s:%d: ", file, line);
}
if (absfile) cobc_free (absfile);
}
if (prefix) {
fprintf (stderr, "%s", prefix);
Expand Down
3 changes: 3 additions & 0 deletions cobc/flag.def
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,6 @@ CB_FLAG_ON (cb_diagnostics_show_caret, 1, "diagnostics-show-caret",

CB_FLAG_ON (cb_diagnostics_show_line_numbers, 1, "diagnostics-show-line-numbers",
_(" -fno-diagnostics-show-line-numbers\tsuppress display of line numbers in diagnostics"))

CB_FLAG (cb_diagnostics_absolute_paths, 1, "diagnostics-absolute-paths",
_(" -fdiagnostics-absolute-paths\tprint absolute paths in diagnostics"))
9 changes: 9 additions & 0 deletions tests/testsuite.src/used_binaries.at
Original file line number Diff line number Diff line change
Expand Up @@ -1000,5 +1000,14 @@ AT_CHECK([$COBC -fdiagnostics-plain-output -fdiagnostics-show-caret -Wno-others
> <EOF>
]])


AT_CHECK([$COBC -fdiagnostics-plain-output -fdiagnostics-absolute-paths -Wall prog.cob 2> compiler.output], [1])

AT_CHECK([sed -e "s|$PWD|HOME|" compiler.output], [0],
[HOME/prog.cob:7: error: CRUD.CPY: No such file or directory
HOME/prog.cob:6: warning: numeric value is expected @<:@-Wothers@:>@
HOME/prog.cob:14: warning: ignoring redundant . @<:@-Wothers@:>@
])

AT_CLEANUP

0 comments on commit 6833e35

Please sign in to comment.