diff --git a/cobc/ChangeLog b/cobc/ChangeLog index 0c5659e71..b97301f40 100644 --- a/cobc/ChangeLog +++ b/cobc/ChangeLog @@ -1,4 +1,12 @@ +2023-10-02 Fabrice Le Fessant + + * 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 * typeck.c (search_set_keys): improving SEARCH ALL syntax checks diff --git a/cobc/error.c b/cobc/error.c index 203b67094..f1c2efbe9 100644 --- a/cobc/error.c +++ b/cobc/error.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "cobc.h" #include "tree.h" @@ -57,6 +58,28 @@ 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) { @@ -64,6 +87,7 @@ print_error_prefix (const char *file, int line, const char *prefix) } else { fprintf (stderr, "%s:%d: ", file, line); } + if (absfile) cobc_free (absfile); } if (prefix) { fprintf (stderr, "%s", prefix); diff --git a/cobc/flag.def b/cobc/flag.def index 4850daa60..aec8388f9 100644 --- a/cobc/flag.def +++ b/cobc/flag.def @@ -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")) diff --git a/tests/testsuite.src/used_binaries.at b/tests/testsuite.src/used_binaries.at index 03e6549d1..e8c295839 100644 --- a/tests/testsuite.src/used_binaries.at +++ b/tests/testsuite.src/used_binaries.at @@ -1000,5 +1000,14 @@ AT_CHECK([$COBC -fdiagnostics-plain-output -fdiagnostics-show-caret -Wno-others > ]]) + +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