Skip to content

Commit

Permalink
More lint from SFIO macros
Browse files Browse the repository at this point in the history
The problem is that too many of these macros are written in ways that cause
static analysis to complain about things like unreachable empty statements.

Simply converting all of them to the `do { ... } while(0)` pattern
resolves a lot of the lint warnings. However doing that to one of the most
frequently used macros, `SFMTXRETURN`, introduces a different lint warning
about a branch statement as the last statement of a loop. So instead of
treating these macros as function calls which need to be followed by a
semicolon just drop the semicolon where they macro is used. After all, most
of them cannot be turned into functions precisely because they do things,
such as `return`, that have to be done in the context where they are used.

This eliminates 100 lines of warnings from `bin/lint all`.

P.S., I consider `return` statements within macro definitions an
anti-pattern; i.e., bad practice. But that is something I'm not going to
change now as this is already quite invasive.
  • Loading branch information
krader1961 committed Sep 24, 2018
1 parent e39a895 commit f08d851
Show file tree
Hide file tree
Showing 51 changed files with 446 additions and 470 deletions.
8 changes: 4 additions & 4 deletions src/lib/libast/sfio/_sfopen.c
Expand Up @@ -43,7 +43,7 @@

Sfio_t *_sfopenat(int cwd, Sfio_t *f, const char *file, const char *mode) {
int fd, oldfd, oflags, fflags, sflags;
SFMTXDECL(f);
SFMTXDECL(f)

if (file && *file == '/') cwd = AT_FDCWD;
#if !defined(sysopenatf)
Expand All @@ -62,7 +62,7 @@ Sfio_t *_sfopenat(int cwd, Sfio_t *f, const char *file, const char *mode) {

/* changing the control flags */
if (f && !file && !((f->flags | sflags) & SF_STRING)) {
SFMTXENTER(f, NULL);
SFMTXBEGIN(f, NULL)

if (f->mode & SF_INIT) /* stream uninitialized, ok to set flags */
{
Expand All @@ -84,7 +84,7 @@ Sfio_t *_sfopenat(int cwd, Sfio_t *f, const char *file, const char *mode) {
}
} else /* make sure there is no buffered data */
{
if (sfsync(f) < 0) SFMTXRETURN(f, NULL);
if (sfsync(f) < 0) SFMTXRETURN(f, NULL)
}

if (f->file >= 0) {
Expand All @@ -98,7 +98,7 @@ Sfio_t *_sfopenat(int cwd, Sfio_t *f, const char *file, const char *mode) {
#endif
}

SFMTXRETURN(f, f);
SFMTXRETURN(f, f)
}

if (sflags & SF_STRING) {
Expand Down
18 changes: 9 additions & 9 deletions src/lib/libast/sfio/_sfputd.c
Expand Up @@ -38,12 +38,12 @@ int _sfputd(Sfio_t *f, Sfdouble_t v) {
int exp;
uchar c[N_ARRAY];
Sfdouble_t x;
SFMTXDECL(f);
SFMTXDECL(f)

SFMTXENTER(f, -1);
SFMTXENTER(f, -1)

if (f->mode != SF_WRITE && _sfmode(f, SF_WRITE, 0) < 0) SFMTXRETURN(f, -1);
SFLOCK(f, 0);
if (f->mode != SF_WRITE && _sfmode(f, SF_WRITE, 0) < 0) SFMTXRETURN(f, -1)
SFLOCK(f, 0)

/* get the sign of v */
if (v < 0.) {
Expand All @@ -65,9 +65,9 @@ int _sfputd(Sfio_t *f, Sfdouble_t v) {
}

/* write out the signs and the exp */
SFOPEN(f);
if (sfputc(f, n) < 0 || (w = sfputu(f, w)) < 0) SFMTXRETURN(f, -1);
SFLOCK(f, 0);
SFOPEN(f)
if (sfputc(f, n) < 0 || (w = sfputu(f, w)) < 0) SFMTXRETURN(f, -1)
SFLOCK(f, 0)
w += 1;

s = (ends = &c[0]) + sizeof(c);
Expand All @@ -86,6 +86,6 @@ int _sfputd(Sfio_t *f, Sfdouble_t v) {
n = ends - s + 1;
w = SFWRITE(f, (void *)s, n) == n ? w + n : -1;

SFOPEN(f);
SFMTXRETURN(f, w);
SFOPEN(f)
SFMTXRETURN(f, w)
}
12 changes: 6 additions & 6 deletions src/lib/libast/sfio/_sfputl.c
Expand Up @@ -35,11 +35,11 @@ int _sfputl(Sfio_t *f, Sflong_t v) {
uchar *s, *ps;
ssize_t n, p;
uchar c[N_ARRAY];
SFMTXDECL(f);
SFMTXDECL(f)

SFMTXENTER(f, -1);
if (f->mode != SF_WRITE && _sfmode(f, SF_WRITE, 0) < 0) SFMTXRETURN(f, -1);
SFLOCK(f, 0);
SFMTXENTER(f, -1)
if (f->mode != SF_WRITE && _sfmode(f, SF_WRITE, 0) < 0) SFMTXRETURN(f, -1)
SFLOCK(f, 0)

s = ps = &(c[N_ARRAY - 1]);
if (v < 0) { /* add 1 to avoid 2-complement problems with -SF_MAXINT */
Expand Down Expand Up @@ -88,6 +88,6 @@ int _sfputl(Sfio_t *f, Sflong_t v) {
f->next = ps;
}

SFOPEN(f);
SFMTXRETURN(f, n);
SFOPEN(f)
SFMTXRETURN(f, n)
}
12 changes: 6 additions & 6 deletions src/lib/libast/sfio/_sfputm.c
Expand Up @@ -35,12 +35,12 @@ int _sfputm(Sfio_t *f, Sfulong_t v, Sfulong_t m) {
uchar *s, *ps;
ssize_t n, p;
uchar c[N_ARRAY];
SFMTXDECL(f);
SFMTXDECL(f)

SFMTXENTER(f, -1);
SFMTXENTER(f, -1)

if (v > m || (f->mode != SF_WRITE && _sfmode(f, SF_WRITE, 0) < 0)) SFMTXRETURN(f, -1);
SFLOCK(f, 0);
if (v > m || (f->mode != SF_WRITE && _sfmode(f, SF_WRITE, 0) < 0)) SFMTXRETURN(f, -1)
SFLOCK(f, 0)

/* code v as integers in base SF_UBASE */
s = ps = &(c[N_ARRAY - 1]);
Expand Down Expand Up @@ -84,6 +84,6 @@ int _sfputm(Sfio_t *f, Sfulong_t v, Sfulong_t m) {
f->next = ps;
}

SFOPEN(f);
SFMTXRETURN(f, (int)n);
SFOPEN(f)
SFMTXRETURN(f, (int)n)
}
12 changes: 6 additions & 6 deletions src/lib/libast/sfio/_sfputu.c
Expand Up @@ -35,12 +35,12 @@ int _sfputu(Sfio_t *f, Sfulong_t v) {
uchar *s, *ps;
ssize_t n, p;
uchar c[N_ARRAY];
SFMTXDECL(f);
SFMTXDECL(f)

SFMTXENTER(f, -1);
SFMTXENTER(f, -1)

if (f->mode != SF_WRITE && _sfmode(f, SF_WRITE, 0) < 0) SFMTXRETURN(f, -1);
SFLOCK(f, 0);
if (f->mode != SF_WRITE && _sfmode(f, SF_WRITE, 0) < 0) SFMTXRETURN(f, -1)
SFLOCK(f, 0)

/* code v as integers in base SF_UBASE */
s = ps = &(c[N_ARRAY - 1]);
Expand Down Expand Up @@ -81,6 +81,6 @@ int _sfputu(Sfio_t *f, Sfulong_t v) {
f->next = ps;
}

SFOPEN(f);
SFMTXRETURN(f, (int)n);
SFOPEN(f)
SFMTXRETURN(f, (int)n)
}
18 changes: 9 additions & 9 deletions src/lib/libast/sfio/sfclose.c
Expand Up @@ -35,25 +35,25 @@
int sfclose(Sfio_t *f) {
int local, ex, rv;
void *data = NULL;
SFMTXDECL(f); /* declare a local stream variable for multithreading */
SFMTXDECL(f) // declare a local stream variable for multithreading

SFMTXENTER(f, -1);
SFMTXENTER(f, -1)

GETLOCAL(f, local);

if (!(f->mode & SF_INIT) && SFMODE(f, local) != (f->mode & SF_RDWR) &&
SFMODE(f, local) != (f->mode & (SF_READ | SF_SYNCED)) && _sfmode(f, SF_SYNCED, local) < 0)
SFMTXRETURN(f, -1);
SFMTXRETURN(f, -1)

/* closing a stack of streams */
while (f->push) {
Sfio_t *pop;

if (!(pop = (*_Sfstack)(f, NULL))) SFMTXRETURN(f, -1);
if (!(pop = (*_Sfstack)(f, NULL))) SFMTXRETURN(f, -1)

if (sfclose(pop) < 0) {
(*_Sfstack)(f, pop);
SFMTXRETURN(f, -1);
SFMTXRETURN(f, -1)
}
}

Expand All @@ -66,10 +66,10 @@ int sfclose(Sfio_t *f) {
rv = sfsync(f);
}

SFLOCK(f, 0);
SFLOCK(f, 0)

/* raise discipline exceptions */
if (f->disc && (ex = SFRAISE(f, local ? SF_NEW : SF_CLOSING, NULL)) != 0) SFMTXRETURN(f, ex);
if (f->disc && (ex = SFRAISE(f, local ? SF_NEW : SF_CLOSING, NULL)) != 0) SFMTXRETURN(f, ex)

if (!local && f->pool) { /* remove from pool */
if (f->pool == &_Sfpool) {
Expand All @@ -88,8 +88,8 @@ int sfclose(Sfio_t *f) {
f->mode &= ~SF_LOCK; /**/
ASSERT(_Sfpmove);
if ((*_Sfpmove)(f, -1) < 0) {
SFOPEN(f);
SFMTXRETURN(f, -1);
SFOPEN(f)
SFMTXRETURN(f, -1)
}
f->mode |= SF_LOCK;
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/libast/sfio/sfclrlock.c
Expand Up @@ -31,12 +31,12 @@
*/
int sfclrlock(Sfio_t *f) {
int rv;
SFMTXDECL(f); /* declare a local stream variable for multithreading */
SFMTXDECL(f) // declare a local stream variable for multithreading

/* already closed */
if (f && (f->mode & SF_AVAIL)) return 0;

SFMTXENTER(f, 0);
SFMTXENTER(f, 0)

/* clear error bits */
f->flags &= ~(SF_ERROR | SF_EOF);
Expand All @@ -54,5 +54,5 @@ int sfclrlock(Sfio_t *f) {

rv = (f->mode & SF_PUSH) ? 0 : (f->flags & SFIO_FLAGS);

SFMTXRETURN(f, rv);
SFMTXRETURN(f, rv)
}
32 changes: 16 additions & 16 deletions src/lib/libast/sfio/sfdisc.c
Expand Up @@ -100,21 +100,21 @@ Sfdisc_t *sfdisc(Sfio_t *f, Sfdisc_t *disc) {
Sfseek_f oseekf;
ssize_t n;
Dccache_t *dcca = NULL;
SFMTXDECL(f); /* declare a local stream variable for multithreading */
SFMTXDECL(f) // declare a local stream variable for multithreading

SFMTXENTER(f, NULL);
SFMTXENTER(f, NULL)

if ((Sfio_t *)disc == f) /* special case to get the top discipline */
SFMTXRETURN(f, f->disc);
SFMTXRETURN(f, f->disc)

if ((f->flags & SF_READ) && f->proc &&
(f->mode & SF_WRITE)) { /* make sure in read mode to check for read-ahead data */
if (_sfmode(f, SF_READ, 0) < 0) SFMTXRETURN(f, NULL);
if (_sfmode(f, SF_READ, 0) < 0) SFMTXRETURN(f, NULL)
} else {
if ((f->mode & SF_RDWR) != f->mode && _sfmode(f, 0, 0) < 0) SFMTXRETURN(f, NULL);
if ((f->mode & SF_RDWR) != f->mode && _sfmode(f, 0, 0) < 0) SFMTXRETURN(f, NULL)
}

SFLOCK(f, 0);
SFLOCK(f, 0)
rdisc = NULL;

/* disallow popping while there is cached data */
Expand All @@ -134,15 +134,15 @@ Sfdisc_t *sfdisc(Sfio_t *f, Sfdisc_t *disc) {
int rv = 0;
if (rv == 0 && f->disc && f->disc->exceptf) /* ask current discipline */
{
SFOPEN(f);
SFOPEN(f)
rv = (*f->disc->exceptf)(f, SF_DBUFFER, &n, f->disc);
SFLOCK(f, 0);
SFLOCK(f, 0)
}
if (rv == 0 && disc && disc->exceptf) /* ask discipline being pushed */
{
SFOPEN(f);
SFOPEN(f)
rv = (*disc->exceptf)(f, SF_DBUFFER, &n, disc);
SFLOCK(f, 0);
SFLOCK(f, 0)
}
if (rv < 0) goto done;
}
Expand Down Expand Up @@ -178,19 +178,19 @@ Sfdisc_t *sfdisc(Sfio_t *f, Sfdisc_t *disc) {
if (!(d = f->disc)) goto done;
disc = d->disc;
if (d->exceptf) {
SFOPEN(f);
SFOPEN(f)
if ((*(d->exceptf))(f, SF_DPOP, (void *)disc, d) < 0) goto done;
SFLOCK(f, 0);
SFLOCK(f, 0)
}
f->disc = disc;
rdisc = d;
} else { /* pushing, warn being pushed discipline */
do { /* loop to handle the case where d may pop itself */
d = f->disc;
if (d && d->exceptf) {
SFOPEN(f);
SFOPEN(f)
if ((*(d->exceptf))(f, SF_DPUSH, (void *)disc, d) < 0) goto done;
SFLOCK(f, 0);
SFLOCK(f, 0)
}
} while (d != f->disc);

Expand Down Expand Up @@ -239,6 +239,6 @@ Sfdisc_t *sfdisc(Sfio_t *f, Sfdisc_t *disc) {
}

done:
SFOPEN(f);
SFMTXRETURN(f, rdisc);
SFOPEN(f)
SFMTXRETURN(f, rdisc)
}
28 changes: 14 additions & 14 deletions src/lib/libast/sfio/sfexcept.c
Expand Up @@ -37,35 +37,35 @@ int _sfexcept(Sfio_t *f, int type, ssize_t io, Sfdisc_t *disc) {
int ev, local, lock;
ssize_t size;
uchar *data;
SFMTXDECL(f); /* declare a local stream variable for multithreading */
SFMTXDECL(f) // declare a local stream variable for multithreading

SFMTXENTER(f, -1);
SFMTXENTER(f, -1)

GETLOCAL(f, local);
lock = f->mode & SF_LOCK;

if (local && io <= 0) f->flags |= io < 0 ? SF_ERROR : SF_EOF;

if (disc && disc->exceptf) { /* let the stream be generally accessible for this duration */
if (local && lock) SFOPEN(f);
if (local && lock) SFOPEN(f)

/* so that exception handler knows what we are asking for */
_Sfi = f->val = io;
ev = (*(disc->exceptf))(f, type, &io, disc);

/* relock if necessary */
if (local && lock) SFLOCK(f, 0);
if (local && lock) SFLOCK(f, 0)

if (io > 0 && !(f->flags & SF_STRING)) SFMTXRETURN(f, ev);
if (ev < 0) SFMTXRETURN(f, SF_EDONE);
if (ev > 0) SFMTXRETURN(f, SF_EDISC);
if (io > 0 && !(f->flags & SF_STRING)) SFMTXRETURN(f, ev)
if (ev < 0) SFMTXRETURN(f, SF_EDONE)
if (ev > 0) SFMTXRETURN(f, SF_EDISC)
}

if (f->flags & SF_STRING) {
if (type == SF_READ)
goto chk_stack;
else if (type != SF_WRITE && type != SF_SEEK)
SFMTXRETURN(f, SF_EDONE);
SFMTXRETURN(f, SF_EDONE)
if (local && io >= 0) {
if (f->size >= 0 && !(f->flags & SF_MALLOC)) goto chk_stack;
/* extend buffer */
Expand All @@ -82,18 +82,18 @@ int _sfexcept(Sfio_t *f, int type, ssize_t io, Sfdisc_t *disc) {
f->endr = f->endw = f->data = data;
f->size = size;
}
SFMTXRETURN(f, SF_EDISC);
SFMTXRETURN(f, SF_EDISC)
}

if (errno == EINTR) {
if (_Sfexiting || (f->bits & SF_ENDING) || /* stop being a hero */
(f->flags & SF_IOINTR)) /* application requests to return */
SFMTXRETURN(f, SF_EDONE);
SFMTXRETURN(f, SF_EDONE)

/* a normal interrupt, we can continue */
errno = 0;
f->flags &= ~(SF_EOF | SF_ERROR);
SFMTXRETURN(f, SF_ECONT);
SFMTXRETURN(f, SF_ECONT)
}

chk_stack:
Expand All @@ -102,18 +102,18 @@ int _sfexcept(Sfio_t *f, int type, ssize_t io, Sfdisc_t *disc) {
(type == SF_WRITE && f->next <= f->data))) { /* pop the stack */
Sfio_t *pf;

if (lock) SFOPEN(f);
if (lock) SFOPEN(f)

/* pop and close */
pf = (*_Sfstack)(f, NULL);
if ((ev = sfclose(pf)) < 0) /* can't close, restack */
(*_Sfstack)(f, pf);

if (lock) SFLOCK(f, 0);
if (lock) SFLOCK(f, 0)

ev = ev < 0 ? SF_EDONE : SF_ESTACK;
} else
ev = SF_EDONE;

SFMTXRETURN(f, ev);
SFMTXRETURN(f, ev)
}

0 comments on commit f08d851

Please sign in to comment.