lint: clean up

This commit is contained in:
rillig 2024-01-20 12:02:09 +00:00
parent 3f351f34c6
commit 9de7a84bd2
3 changed files with 47 additions and 56 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: externs.h,v 1.33 2024/01/20 10:25:57 rillig Exp $ */ /* $NetBSD: externs.h,v 1.34 2024/01/20 12:02:09 rillig Exp $ */
/* /*
* Copyright (c) 1994, 1995 Jochen Pohl * Copyright (c) 1994, 1995 Jochen Pohl
@ -42,8 +42,8 @@ const char *tspec_name(tspec_t);
/* /*
* mem.c * mem.c
*/ */
void *xmalloc(size_t);
#if IS_LINT1 || IS_LINT2 #if IS_LINT1 || IS_LINT2
void *xmalloc(size_t);
void *xcalloc(size_t, size_t); void *xcalloc(size_t, size_t);
#endif #endif
void *xrealloc(void *, size_t); void *xrealloc(void *, size_t);

View File

@ -1,4 +1,4 @@
/* $NetBSD: mem.c,v 1.24 2024/01/20 10:25:57 rillig Exp $ */ /* $NetBSD: mem.c,v 1.25 2024/01/20 12:02:09 rillig Exp $ */
/* /*
* Copyright (c) 1994, 1995 Jochen Pohl * Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(__RCSID) #if defined(__RCSID)
__RCSID("$NetBSD: mem.c,v 1.24 2024/01/20 10:25:57 rillig Exp $"); __RCSID("$NetBSD: mem.c,v 1.25 2024/01/20 12:02:09 rillig Exp $");
#endif #endif
#include <stdarg.h> #include <stdarg.h>
@ -51,10 +51,11 @@ not_null(void *ptr)
{ {
if (ptr == NULL) if (ptr == NULL)
errx(1, "virtual memory exhausted"); errx(1, "out of memory");
return ptr; return ptr;
} }
#if IS_LINT1 || IS_LINT2
void * void *
xmalloc(size_t s) xmalloc(size_t s)
{ {
@ -62,7 +63,6 @@ xmalloc(size_t s)
return not_null(malloc(s)); return not_null(malloc(s));
} }
#if IS_LINT1 || IS_LINT2
void * void *
xcalloc(size_t n, size_t s) xcalloc(size_t n, size_t s)
{ {
@ -85,7 +85,7 @@ xstrdup(const char *s)
return not_null(strdup(s)); return not_null(strdup(s));
} }
#if defined(IS_XLINT) #if IS_XLINT
char * char *
xasprintf(const char *fmt, ...) xasprintf(const char *fmt, ...)
{ {

View File

@ -1,4 +1,4 @@
/* $NetBSD: xlint.c,v 1.121 2023/12/10 14:59:47 rillig Exp $ */ /* $NetBSD: xlint.c,v 1.122 2024/01/20 12:02:10 rillig Exp $ */
/* /*
* Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
@ -38,7 +38,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(__RCSID) #if defined(__RCSID)
__RCSID("$NetBSD: xlint.c,v 1.121 2023/12/10 14:59:47 rillig Exp $"); __RCSID("$NetBSD: xlint.c,v 1.122 2024/01/20 12:02:10 rillig Exp $");
#endif #endif
#include <sys/param.h> #include <sys/param.h>
@ -60,8 +60,6 @@ __RCSID("$NetBSD: xlint.c,v 1.121 2023/12/10 14:59:47 rillig Exp $");
#include "pathnames.h" #include "pathnames.h"
#include "findcc.h" #include "findcc.h"
#define DEFAULT_PATH _PATH_DEFPATH
typedef struct { typedef struct {
char **items; char **items;
size_t len; size_t len;
@ -73,8 +71,8 @@ static struct {
list flags; /* flags always passed */ list flags; /* flags always passed */
list lcflags; /* flags, controlled by sflag/tflag */ list lcflags; /* flags, controlled by sflag/tflag */
char *outfile; /* path name for preprocessed C source */ char *outfile; /* path name for preprocessed C source */
int outfd; /* file descriptor for outfile */ int output_fd; /* file descriptor for outfile */
} cpp = { .outfd = -1 }; } cpp;
/* Parameters for lint1, which checks an isolated translation unit. */ /* Parameters for lint1, which checks an isolated translation unit. */
static struct { static struct {
@ -85,9 +83,9 @@ static struct {
/* Parameters for lint2, which performs cross-translation-unit checks. */ /* Parameters for lint2, which performs cross-translation-unit checks. */
static struct { static struct {
list flags; list flags;
list infiles; /* input files (without libraries) */ list input_files; /* without libraries */
list inlibs; /* input libraries */ list input_libraries;
char *outlib; /* output library that will be created */ char *output_library;
} lint2; } lint2;
static const char *tmpdir; static const char *tmpdir;
@ -100,8 +98,8 @@ static char *output_filename; /* filename for -o */
static bool seen_filename; static bool seen_filename;
/* /*
* name of a file which is currently written by a child and should * The file that is currently written by a child process and should be removed
* be removed after abnormal termination of the child * in case the child process fails.
*/ */
static const char *currfn; static const char *currfn;
@ -168,10 +166,9 @@ concat2(const char *s1, const char *s2)
size_t len1 = strlen(s1); size_t len1 = strlen(s1);
size_t len2 = strlen(s2); size_t len2 = strlen(s2);
char *s = xmalloc(len1 + len2 + 1); char *s = xrealloc(NULL, len1 + len2 + 1);
memcpy(s, s1, len1); memcpy(s, s1, len1);
memcpy(s + len1, s2, len2 + 1); memcpy(s + len1, s2, len2 + 1);
return s; return s;
} }
@ -191,8 +188,8 @@ __dead static void
terminate(int signo) terminate(int signo)
{ {
if (cpp.outfd != -1) if (cpp.output_fd != -1)
(void)close(cpp.outfd); (void)close(cpp.output_fd);
if (cpp.outfile != NULL) { if (cpp.outfile != NULL) {
const char *keep_env = getenv("LINT_KEEP_CPPOUT"); const char *keep_env = getenv("LINT_KEEP_CPPOUT");
bool keep = keep_env != NULL && (strcmp(keep_env, "yes") == 0 bool keep = keep_env != NULL && (strcmp(keep_env, "yes") == 0
@ -207,8 +204,8 @@ terminate(int signo)
for (size_t i = 0; i < lint1.outfiles.len; i++) for (size_t i = 0; i < lint1.outfiles.len; i++)
(void)remove(lint1.outfiles.items[i]); (void)remove(lint1.outfiles.items[i]);
if (lint2.outlib != NULL) if (lint2.output_library != NULL)
(void)remove(lint2.outlib); (void)remove(lint2.output_library);
if (currfn != NULL && currfn != cpp.outfile) if (currfn != NULL && currfn != cpp.outfile)
(void)remove(currfn); (void)remove(currfn);
@ -245,12 +242,8 @@ usage(const char *fmt, ...)
terminate(-1); terminate(-1);
} }
/*
* Returns a pointer to the last component of path after delim.
* Returns path if the string does not contain delim.
*/
static const char * static const char *
lbasename(const char *path, int delim) skip_last(const char *path, int delim)
{ {
const char *base = path; const char *base = path;
@ -294,7 +287,7 @@ needs_quoting:
} }
static void static void
run_child(const char *path, list *args, const char *crfn, int fdout) run_child(const char *path, const list *args, const char *crfn, int fdout)
{ {
if (Vflag) { if (Vflag) {
@ -362,7 +355,7 @@ run_cpp(const char *name)
cc = DEFAULT_CC; cc = DEFAULT_CC;
char *abs_cc = findcc(cc); char *abs_cc = findcc(cc);
if (abs_cc == NULL && setenv("PATH", DEFAULT_PATH, 1) == 0) if (abs_cc == NULL && setenv("PATH", _PATH_DEFPATH, 1) == 0)
abs_cc = findcc(cc); abs_cc = findcc(cc);
if (abs_cc == NULL) { if (abs_cc == NULL) {
(void)fprintf(stderr, "%s: %s: not found\n", getprogname(), cc); (void)fprintf(stderr, "%s: %s: not found\n", getprogname(), cc);
@ -377,16 +370,16 @@ run_cpp(const char *name)
list_add_ref(&args, NULL); list_add_ref(&args, NULL);
/* Rewind after a possible previous run of cpp and lint1. */ /* Rewind after a possible previous run of cpp and lint1. */
if (lseek(cpp.outfd, 0, SEEK_SET) != 0) { if (lseek(cpp.output_fd, 0, SEEK_SET) != 0) {
warn("lseek"); warn("lseek");
terminate(-1); terminate(-1);
} }
if (ftruncate(cpp.outfd, 0) != 0) { if (ftruncate(cpp.output_fd, 0) != 0) {
warn("ftruncate"); warn("ftruncate");
terminate(-1); terminate(-1);
} }
run_child(abs_cc, &args, cpp.outfile, cpp.outfd); run_child(abs_cc, &args, cpp.outfile, cpp.output_fd);
list_clear(&args); list_clear(&args);
} }
@ -409,30 +402,28 @@ run_lint1(const char *out_fname)
list_clear(&args); list_clear(&args);
} }
/*
* Read a file name from the command line
* and pass it through lint1 if it is a C source.
*/
static void static void
handle_filename(const char *name) handle_filename(const char *name)
{ {
const char *base = lbasename(name, '/'); const char *base = skip_last(name, '/');
const char *suff = lbasename(base, '.'); const char *suff = skip_last(base, '.');
if (strcmp(suff, "ln") == 0) { if (strcmp(suff, "ln") == 0) {
/* only for lint2 */ /* only for lint2 */
if (!iflag) if (!iflag)
list_add(&lint2.infiles, name); list_add(&lint2.input_files, name);
return; return;
} }
if (!(strcmp(suff, "c") == 0 || if (strcmp(suff, "c") == 0)
(strncmp(base, "llib-l", 6) == 0 && base == suff))) { goto c_file;
warnx("unknown file type: %s", name); if (strncmp(base, "llib-l", 6) == 0 && base == suff)
return; goto c_file;
} warnx("unknown file type: %s", name);
return;
c_file:
if (!iflag || seen_filename) if (!iflag || seen_filename)
(void)printf("%s:\n", Fflag ? name : base); (void)printf("%s:\n", Fflag ? name : base);
@ -461,7 +452,7 @@ handle_filename(const char *name)
run_cpp(name); run_cpp(name);
run_lint1(ofn); run_lint1(ofn);
list_add(&lint2.infiles, ofn); list_add(&lint2.input_files, ofn);
free(ofn); free(ofn);
} }
@ -497,7 +488,7 @@ find_lib(const char *lib)
return; return;
found: found:
list_add_ref(&lint2.inlibs, concat2("-l", lfn)); list_add_ref(&lint2.input_libraries, concat2("-l", lfn));
free(lfn); free(lfn);
} }
@ -520,11 +511,11 @@ run_lint2(void)
list args = { NULL, 0, 0 }; list args = { NULL, 0, 0 };
list_add_ref(&args, abs_lint2); list_add_ref(&args, abs_lint2);
list_add_all(&args, &lint2.flags); list_add_all(&args, &lint2.flags);
list_add_all(&args, &lint2.inlibs); list_add_all(&args, &lint2.input_libraries);
list_add_all(&args, &lint2.infiles); list_add_all(&args, &lint2.input_files);
list_add_ref(&args, NULL); list_add_ref(&args, NULL);
run_child(abs_lint2, &args, lint2.outlib, -1); run_child(abs_lint2, &args, lint2.output_library, -1);
list_clear(&args); list_clear(&args);
} }
@ -569,8 +560,8 @@ main(int argc, char *argv[])
set_tmpdir(); set_tmpdir();
cpp.outfile = concat2(tmpdir, "lint0.XXXXXX"); cpp.outfile = concat2(tmpdir, "lint0.XXXXXX");
cpp.outfd = mkstemp(cpp.outfile); cpp.output_fd = mkstemp(cpp.outfile);
if (cpp.outfd == -1) { if (cpp.output_fd == -1) {
warn("can't make temp"); warn("can't make temp");
terminate(-1); terminate(-1);
} }
@ -707,7 +698,7 @@ main(int argc, char *argv[])
Cflag = true; Cflag = true;
list_add_flag(&lint2.flags, c); list_add_flag(&lint2.flags, c);
list_add(&lint2.flags, optarg); list_add(&lint2.flags, optarg);
lint2.outlib = xasprintf("llib-l%s.ln", optarg); lint2.output_library = xasprintf("llib-l%s.ln", optarg);
list_clear(&default_libraries); list_clear(&default_libraries);
break; break;
@ -816,10 +807,10 @@ main(int argc, char *argv[])
run_lint2(); run_lint2();
if (output_filename != NULL) if (output_filename != NULL)
cat(&lint2.infiles, output_filename); cat(&lint2.input_files, output_filename);
if (Cflag) if (Cflag)
lint2.outlib = NULL; lint2.output_library = NULL;
terminate(0); terminate(0);
/* NOTREACHED */ /* NOTREACHED */