From e9d56aa2541777c95ec51115ac69dc3133a5cd07 Mon Sep 17 00:00:00 2001 From: Chad Loder Date: Sat, 10 Dec 2005 17:51:51 +0000 Subject: Add a new lint flag '-f' which, for each warning or error, prints the offending line from the corresponding source code file. The general idea is OK deraadt --- usr.bin/xlint/lint1/err.c | 64 ++++++++++++++++++++++++++++++++++++++++-- usr.bin/xlint/lint1/externs1.h | 3 +- usr.bin/xlint/lint1/main1.c | 10 +++++-- usr.bin/xlint/xlint/lint.1 | 9 ++++-- usr.bin/xlint/xlint/xlint.c | 7 +++-- 5 files changed, 80 insertions(+), 13 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/xlint/lint1/err.c b/usr.bin/xlint/lint1/err.c index c7267d39352..86795a81c8d 100644 --- a/usr.bin/xlint/lint1/err.c +++ b/usr.bin/xlint/lint1/err.c @@ -1,4 +1,4 @@ -/* $OpenBSD: err.c,v 1.11 2005/11/23 22:25:37 cloder Exp $ */ +/* $OpenBSD: err.c,v 1.12 2005/12/10 17:51:49 cloder Exp $ */ /* $NetBSD: err.c,v 1.8 1995/10/02 17:37:00 jpo Exp $ */ /* @@ -33,7 +33,7 @@ */ #ifndef lint -static char rcsid[] = "$OpenBSD: err.c,v 1.11 2005/11/23 22:25:37 cloder Exp $"; +static char rcsid[] = "$OpenBSD: err.c,v 1.12 2005/12/10 17:51:49 cloder Exp $"; #endif /* number of errors found */ @@ -50,7 +50,7 @@ int sytxerr; static const char *lbasename(const char *); static void verror(int, va_list); static void vwarning(int, va_list); - +static void excerpt(pos_t *); const char *msgs[] = { "syntax error: empty declaration", /* 0 */ @@ -399,6 +399,10 @@ verror(int n, va_list ap) (void)vprintf(msgs[n], ap); (void)printf("\n"); nerr++; + + if (fflag) + excerpt(&curr_pos); + } static void @@ -414,6 +418,9 @@ vwarning(int n, va_list ap) (void)printf("%s(%d): warning: ", fn, curr_pos.p_line); (void)vprintf(msgs[n], ap); (void)printf("\n"); + + if (fflag) + excerpt(&curr_pos); } void @@ -463,6 +470,9 @@ message(int n, ...) (void)vprintf(msgs[n], ap); (void)printf("\n"); va_end(ap); + + if (fflag) + excerpt(&curr_pos); } int @@ -485,3 +495,51 @@ gnuism(int n, ...) return (msg); } + +static void +excerpt(pos_t *pos) +{ + static FILE *fp = NULL; + static const char *file = NULL; + static int lineno = 0; + char *buf, *lbuf; + size_t len; + + if (!pos || !pos->p_file) + return; + + /* don't print the same line twice */ + if (pos->p_line == lineno) + return; + + if (fp == NULL || file != pos->p_file || pos->p_line < lineno) { + if (fp) + fclose(fp); + + if (!(fp = fopen(pos->p_file, "r"))) + return; + + file = pos->p_file; + lineno = 0; + } + + lbuf = NULL; + while (lineno < pos->p_line && (buf = fgetln(fp, &len))) { + if (buf[len - 1] == '\n') + buf[len - 1] = '\0'; + else { + /* EOF without EOL, copy and add the NUL */ + if (!(lbuf = malloc(len + 1))) + err(1, NULL); + + lbuf[len] = '\0'; + buf = lbuf; + } + lineno++; + } + + if (buf) + printf("%s(%d): %s\n", pos->p_file, lineno, buf); + + free(lbuf); +} diff --git a/usr.bin/xlint/lint1/externs1.h b/usr.bin/xlint/lint1/externs1.h index c65d9d5c1af..d2022da340d 100644 --- a/usr.bin/xlint/lint1/externs1.h +++ b/usr.bin/xlint/lint1/externs1.h @@ -1,4 +1,4 @@ -/* $OpenBSD: externs1.h,v 1.5 2005/12/10 17:41:03 cloder Exp $ */ +/* $OpenBSD: externs1.h,v 1.6 2005/12/10 17:51:49 cloder Exp $ */ /* $NetBSD: externs1.h,v 1.7 1995/10/02 17:31:39 jpo Exp $ */ /* @@ -40,6 +40,7 @@ extern int bflag; extern int cflag; extern int dflag; extern int eflag; +extern int fflag; extern int Fflag; extern int gflag; extern int hflag; diff --git a/usr.bin/xlint/lint1/main1.c b/usr.bin/xlint/lint1/main1.c index 73eb3eaef67..e91cd428a57 100644 --- a/usr.bin/xlint/lint1/main1.c +++ b/usr.bin/xlint/lint1/main1.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main1.c,v 1.7 2005/11/29 19:38:09 cloder Exp $ */ +/* $OpenBSD: main1.c,v 1.8 2005/12/10 17:51:49 cloder Exp $ */ /* $NetBSD: main1.c,v 1.3 1995/10/02 17:29:56 jpo Exp $ */ /* @@ -33,7 +33,7 @@ */ #ifndef lint -static char rcsid[] = "$OpenBSD: main1.c,v 1.7 2005/11/29 19:38:09 cloder Exp $"; +static char rcsid[] = "$OpenBSD: main1.c,v 1.8 2005/12/10 17:51:49 cloder Exp $"; #endif #include @@ -69,6 +69,9 @@ int eflag; /* Print complete pathnames, not only the basename. */ int Fflag = 1; +/* After an error or warning, print the actual text of the program source code */ +int fflag = 0; + /* Enable some extensions of gcc */ int gflag; @@ -112,7 +115,7 @@ main(int argc, char *argv[]) { int c; - while ((c = getopt(argc, argv, "abcdeghprstuvyzF")) != -1) { + while ((c = getopt(argc, argv, "abcdefghprstuvyzF")) != -1) { switch (c) { case 'a': aflag++; break; case 'b': bflag = 1; break; @@ -122,6 +125,7 @@ main(int argc, char *argv[]) case 'F': Fflag = 1; break; case 'g': gflag = 1; break; case 'h': hflag = 1; break; + case 'f': fflag = 1; break; case 'p': pflag = 1; break; case 'r': rflag = 1; break; case 's': sflag = 1; break; diff --git a/usr.bin/xlint/xlint/lint.1 b/usr.bin/xlint/xlint/lint.1 index 8c85d5537d9..c29f67903b5 100644 --- a/usr.bin/xlint/xlint/lint.1 +++ b/usr.bin/xlint/xlint/lint.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: lint.1,v 1.17 2004/01/23 23:08:47 jmc Exp $ +.\" $OpenBSD: lint.1,v 1.18 2005/12/10 17:51:49 cloder Exp $ .\" $NetBSD: lint.1,v 1.3 1995/10/23 13:45:31 jpo Exp $ .\" .\" Copyright (c) 1994, 1995 Jochen Pohl @@ -38,7 +38,7 @@ .Nd a C program verifier .Sh SYNOPSIS .Nm lint -.Op Fl abceghprvxzHFV +.Op Fl abcefghprvxzHFV .Op Fl s Ns | Ns Fl t .Op Fl i Ns | Ns Fl nu .Op Fl D Ns Ar name Ns Op =def @@ -49,7 +49,7 @@ .Op Fl o Ns Ar outputfile .Ar .Nm lint -.Op Fl abceghprvzHFV +.Op Fl abcefghprvzHFV .Op Fl s Ns | Ns Fl t .Fl C Ns Ar library .Op Fl D Ns Ar name Ns Op =def @@ -180,6 +180,9 @@ and combinations of .Sy enum Ns - and .Sy integer Ns -Types. +.It Fl f +For each warning or error, print the offending line of the +corresponding source code file. .It Fl g Don't print warnings for some extensions of .Xr gcc 1 diff --git a/usr.bin/xlint/xlint/xlint.c b/usr.bin/xlint/xlint/xlint.c index 37a7c8a8a47..f958379acef 100644 --- a/usr.bin/xlint/xlint/xlint.c +++ b/usr.bin/xlint/xlint/xlint.c @@ -1,4 +1,4 @@ -/* $OpenBSD: xlint.c,v 1.24 2005/12/06 01:15:31 deraadt Exp $ */ +/* $OpenBSD: xlint.c,v 1.25 2005/12/10 17:51:50 cloder Exp $ */ /* $NetBSD: xlint.c,v 1.3 1995/10/23 14:29:30 jpo Exp $ */ /* @@ -33,7 +33,7 @@ */ #ifndef lint -static char rcsid[] = "$OpenBSD: xlint.c,v 1.24 2005/12/06 01:15:31 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: xlint.c,v 1.25 2005/12/10 17:51:50 cloder Exp $"; #endif #include @@ -346,7 +346,7 @@ main(int argc, char *argv[]) (void)signal(SIGTERM, terminate); while (argc > optind) { - c = getopt(argc, argv, "abceghil:no:prstuvxyzC:D:FHI:L:U:V"); + c = getopt(argc, argv, "abcefghil:no:prstuvxyzC:D:FHI:L:U:V"); switch (c) { @@ -354,6 +354,7 @@ main(int argc, char *argv[]) case 'b': case 'c': case 'e': + case 'f': case 'g': case 'r': case 'v': -- cgit v1.2.3