[PATCH harec v3] Print paths relative to cwd on build errors
Export this patch
Signed-off-by: Max Schillinger <max@mxsr.de>
---
This fixes two issues:
- Help vim finding the file containing an error:
https://lists.sr.ht/~sircmpwn/hare-users/%3Cbee48d87-53db-41fd-86be-6989733c7a6a@upyum.com%3E
- Let `hare build` print the actual error line in case of
duplicate file names:
https://lists.sr.ht/~sircmpwn/hare-users/%3CCWQ9IWWELP77.2TIL8M24VF5F5@mxsr.de%3E
include/util.h | 3 +++
src/check.c | 2 +-
src/gen.c | 6 ++++++
src/main.c | 10 ----------
src/util.c | 18 ++++++++++++++++++
5 files changed, 28 insertions(+), 11 deletions(-)
diff --git a/include/util.h b/include/util.h
index b6c3513..2baaab1 100644
--- a/include/util.h
+++ b/include/util.h
@@ -18,6 +18,7 @@ enum exit_status {
extern const char **sources;
extern size_t nsources;
+extern const char *modpath;
#define FNV1A_INIT 2166136261u
@@ -50,6 +51,8 @@ int xvfprintf(FILE *restrict f, const char *restrict fmt, va_list ap) FORMAT(0);
char *gen_name(int *id, const char *fmt);
+const char *relpath(const char *dir);
+
void errline(struct location loc);
#endif
diff --git a/src/check.c b/src/check.c
index 91b2e81..065423f 100644
--- a/src/check.c
+++ b/src/check.c
@@ -74,7 +74,7 @@ handle_errors(struct errors *errors)
{
struct errors *error = errors;
while (error) {
- xfprintf(stderr, "%s:%d:%d: error: %s\n", sources[error->loc.file],
+ xfprintf(stderr, "%s:%d:%d: error: %s\n", relpath(sources[error->loc.file]),
error->loc.lineno, error->loc.colno, error->msg);
errline(error->loc);
free(error->msg);
diff --git a/src/gen.c b/src/gen.c
index 9766959..6eaaf88 100644
--- a/src/gen.c
+++ b/src/gen.c
@@ -3904,6 +3904,12 @@ gen(const struct unit *unit, struct qbe_program *out)
ctx.sources = xcalloc(nsources + 1, sizeof(struct gen_value));
for (size_t i = 1; i <= nsources; i++) {
struct expression eloc;
+ if (modpath) {
+ size_t modlen = strlen(modpath);
+ if (strncmp(sources[i], modpath, modlen) == 0) {
+ sources[i] += modlen;
+ }
+ }
mkstrliteral(&eloc, "%s", sources[i]);
ctx.sources[i] = gen_literal_string(&ctx, &eloc);
}
diff --git a/src/main.c b/src/main.c
index eeb440f..50e35f2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -73,7 +73,6 @@ main(int argc, char *argv[])
{
const char *output = NULL, *typedefs = NULL;
const char *target = DEFAULT_TARGET;
- const char *modpath = NULL;
const char *mainsym = "main";
bool is_test = false;
struct unit unit = {0};
@@ -150,15 +149,6 @@ main(int argc, char *argv[])
memcpy((char **)sources + 1, argv + optind, sizeof(char **) * nsources);
sources[0] = "<unknown>";
- if (modpath) {
- size_t modlen = strlen(modpath);
- for (size_t i = 1; i <= nsources; i++) {
- if (strncmp(sources[i], modpath, modlen) == 0) {
- sources[i] += modlen;
- }
- }
- }
-
for (size_t i = 0; i < nsources; ++i) {
FILE *in;
const char *path = argv[optind + i];
diff --git a/src/util.c b/src/util.c
index 3a45ca2..9497648 100644
--- a/src/util.c
+++ b/src/util.c
@@ -15,6 +15,7 @@
const char **sources;
size_t nsources;
+const char *modpath;
uint32_t
fnv1a(uint32_t hash, unsigned char c)
@@ -127,6 +128,23 @@ gen_name(int *id, const char *fmt)
return str;
}
+const char *
+relpath(const char *filepath)
+{
+ if (filepath[0] != '/') {
+ return filepath;
+ }
+ char cwd[PATH_MAX];
+ if (getcwd(cwd, sizeof(cwd)) == NULL) {
+ return filepath;
+ }
+ size_t cwdlen = strlen(cwd);
+ if (strncmp(filepath, cwd, cwdlen) == 0) {
+ return filepath + cwdlen + 1;
+ }
+ return filepath;
+}
+
void
errline(struct location loc)
{
--
2.46.2
harec/patches: SUCCESS in 39s
[ Print paths relative to cwd on build errors][0] v3 from [Max Schillinger][1]
[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/55328
[1]: mailto:max@mxsr.de
✓ #1344289 SUCCESS harec/patches/alpine.yml https://builds.sr.ht/~sircmpwn/job/1344289
✓ #1344290 SUCCESS harec/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/1344290
✓ #1344292 SUCCESS harec/patches/openbsd.yml https://builds.sr.ht/~sircmpwn/job/1344292
✓ #1344291 SUCCESS harec/patches/netbsd.yml https://builds.sr.ht/~sircmpwn/job/1344291
This does not seem to work from my testing. It seems to unconditionally
print the absolute path to the file when printing errors.