Skip to content

Commit

Permalink
GH-50 Enable more compiler warnings and resolve.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirWumpus committed Oct 11, 2024
1 parent b0a2d31 commit 102b424
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 29 deletions.
16 changes: 13 additions & 3 deletions jni/Post4.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ isClassClass(JNIEnv *env, jobject obj)
return is_class;
}

#ifdef HMM

#ifdef NDEBUG
# define getClassName(e, o) (NULL)
# define prints(e, s)
Expand All @@ -63,6 +65,8 @@ prints(JNIEnv *env, jstring jstr)
}
#endif

#endif /* HMM */

static jobject
post4Exception(JNIEnv *env, int code)
{
Expand All @@ -73,6 +77,8 @@ post4Exception(JNIEnv *env, int code)
return p4err;
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
JNIEXPORT void JNICALL
Java_post4_jni_Post4_p4Init(JNIEnv *env, jobject self)
{
Expand All @@ -91,6 +97,7 @@ Java_post4_jni_Post4_p4Free(JNIEnv *env, jobject self, jlong xtc)
}
p4Free(ctx);
}
#pragma GCC diagnostic pop

static jobject
getStacks(JNIEnv *env, P4_Ctx *ctx)
Expand Down Expand Up @@ -312,7 +319,7 @@ static void
jUnboxArray(P4_Ctx *ctx)
{
JNIEnv *env = ctx->jenv;
size_t ds_depth = P4_LENGTH(ctx->ds);
ptrdiff_t ds_depth = P4_LENGTH(ctx->ds);
jarray arr = (jarray) P4_POP(ctx->ds).v;
jsize size = (*env)->GetArrayLength(env, arr);
/* Enough stack space to hold array items? */
Expand Down Expand Up @@ -619,7 +626,7 @@ jField(P4_Ctx *ctx)
int is_static = 0;
JNIEnv *env = ctx->jenv;

size_t len = P4_POP(ctx->ds).z;
P4_DROP(ctx->ds, 1); /* Ignore s */
char *sig = P4_POP(ctx->ds).s;
P4_DROP(ctx->ds, 1);
char *field = P4_POP(ctx->ds).s;
Expand Down Expand Up @@ -729,7 +736,7 @@ jSetField(P4_Ctx *ctx)
int is_static = 0;
JNIEnv *env = ctx->jenv;

size_t len = P4_POP(ctx->ds).z;
P4_DROP(ctx->ds, 1); /* Ignore s */
char *sig = P4_POP(ctx->ds).s;
P4_DROP(ctx->ds, 1);
char *field = P4_POP(ctx->ds).s;
Expand Down Expand Up @@ -853,6 +860,8 @@ static P4_Hook jHooks[] = {
};
#endif

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
JNIEXPORT jlong JNICALL
Java_post4_jni_Post4_p4Create(JNIEnv *env, jobject self, jobject opts)
{
Expand Down Expand Up @@ -919,3 +928,4 @@ Java_post4_jni_Post4_p4Create(JNIEnv *env, jobject self, jobject opts)
// https://stackoverflow.com/questions/1632367/passing-pointers-between-c-and-java-through-jni
return (jlong) ctx;
}
#pragma GCC diagnostic pop
3 changes: 2 additions & 1 deletion jni/makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ CC = @CC@
CC_E = @CC_E@
CC_O = @CC_O@
CPPFLAGS = @CPPFLAGS@
CFLAGS = -O2 @JNI_CFLAGS@ ${DBG}
CWARNINGS = -Wall -Wextra
CFLAGS = -O2 -std=c11 @JNI_CFLAGS@ ${CWARNINGS} ${DBG}
LDFLAGS = @LDFLAGS@
JFLAGS = -Xlint:deprecation @JFLAGS@
LIBS = @LIBS@
Expand Down
9 changes: 6 additions & 3 deletions src/aline.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ alineSetMode(int mode)
return prev;
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
static void
sig_winch(int signum)
{
Expand All @@ -55,6 +57,7 @@ sig_winch(int signum)
}
#endif
}
#pragma GCC diagnostic pop

void
alineFini(void)
Expand Down Expand Up @@ -122,8 +125,8 @@ alineGetRowCol(int pos[2])
int
alineInput(FILE *fp, const char *prompt, char *buf, size_t size)
{
unsigned char ch;
int pcol, i, pos[2];
unsigned i;
int ch, pcol, pos[2];

if (buf == NULL || size < 1) {
return EOF;
Expand Down Expand Up @@ -196,7 +199,7 @@ alineInput(FILE *fp, const char *prompt, char *buf, size_t size)
return EOF;
} else if (i < size) {
(void) memmove(buf+i+1, buf+i, strlen(buf+i)+1);
buf[i++] = ch;
buf[i++] = (unsigned char) ch;
}
}
if (0 < i) {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ p4System(P4_Ctx *ctx)
static void
p4SystemString(P4_Ctx *ctx)
{
P4_POP(ctx->ds);
P4_DROP(ctx->ds, 1); /* Ignore u */
char *s = P4_TOP(ctx->ds).s;
// Assumes caddr NUL terminated.
P4_TOP(ctx->ds).n = system(s);
Expand Down
8 changes: 6 additions & 2 deletions src/makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ CC = @CC@
CC_E = @CC_E@
CC_O = @CC_O@
CPPFLAGS = @CPPFLAGS@
CWARNINGS = -Wno-unused-label -funsigned-char -Wno-unused-const-variable
CFLAGS = -Os -std=c11 ${CPPFLAGS} ${CWARNINGS} ${DBG}

# Note do not use -pedantic as it generates way too many warnings.
CWARNINGS = -Wall -Wextra -Wno-char-subscripts -Wno-unused-label \
-Wno-unused-const-variable -Wno-missing-field-initializers

CFLAGS = -Os -std=c11 -funsigned-char ${CPPFLAGS} ${CWARNINGS} ${DBG}
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@

Expand Down
47 changes: 33 additions & 14 deletions src/post4.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ p4Base36(int digit)
}

int
p4StrNum(P4_String str, P4_Uint base, P4_Cell *out, int *is_float)
p4StrNum(P4_String str, int base, P4_Cell *out, int *is_float)
{
int offset = 0;
size_t offset = 0;
*is_float = 0;
if (str.length == 0) {
return -1;
Expand Down Expand Up @@ -538,9 +538,9 @@ p4LeadZeroBits(P4_Uint x)
# endif
# endif
/* Count the ones */
x -= x >> 1 & (P4_Uint)0x5555555555555555L;
x = (x >> 2 & (P4_Uint)0x3333333333333333L) + (x & (P4_Uint)0x3333333333333333L);
x = (x >> 4) + x & (P4_Uint)0x0f0f0f0f0f0f0f0fL;
x -= (x >> 1) & (P4_Uint)0x5555555555555555L;
x = ((x >> 2) & (P4_Uint)0x3333333333333333L) + (x & (P4_Uint)0x3333333333333333L);
x = ((x >> 4) + x) & (P4_Uint)0x0f0f0f0f0f0f0f0fL;
x += x >> 8;
# if P4_UINT_BITS >= 32
x += x >> 16;
Expand Down Expand Up @@ -773,7 +773,7 @@ p4FindNameIn(P4_Ctx *ctx, const char *caddr, P4_Size length, unsigned wid)
for (P4_Word *word = ctx->lists[wid-1]; word != NULL; word = word->prev) {
if (!P4_WORD_IS_HIDDEN(word)
&& word->length > 0 && word->length == length
&& strncasecmp(word->name, (const char *)caddr, length) == 0) {
&& strncasecmp(word->name, caddr, length) == 0) {
return word;
}
}
Expand Down Expand Up @@ -927,17 +927,18 @@ p4Bp(P4_Ctx *ctx)
P4_Input *input = ctx->input;
int has_nl = input->buffer[input->length-(0 < input->length)] == '\n';
(void) fprintf(STDERR, "\r\n>> ");
for (int i = 0; i < input->length-has_nl; i++) {
for (unsigned i = 0; i < input->length-has_nl; i++) {
(void) fputc(input->buffer[i] == '\t' ? ' ' : input->buffer[i], STDERR);
}
(void) fprintf(STDERR, "\r\n>> %*c\r\n", (int)input->offset, '^' );
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
static void
p4TraceStack(P4_Ctx *ctx, P4_Stack *stk, int u, const char *prefix)
{
P4_Cell w;
unsigned i;
int is_small;

(void) fprintf(STDERR, "%s%s", prefix, 0 < u ? "" : "-");
Expand All @@ -947,6 +948,7 @@ p4TraceStack(P4_Ctx *ctx, P4_Stack *stk, int u, const char *prefix)
(void) fprintf(STDERR, is_small ? P4_INT_FMT"%s" : P4_HEX_FMT"%s", w.n, 1 < u ? " " : "");
}
}
#pragma GCC diagnostic pop

static void
p4Trace(P4_Ctx *ctx, P4_Xt xt, P4_Cell *ip)
Expand Down Expand Up @@ -1027,7 +1029,6 @@ p4StackIsFull(P4_Ctx *ctx, P4_Stack *stack, int over)
static void
p4StackGuard(P4_Ctx *ctx, P4_Stack *stack, int over, int under)
{
int i;
ptrdiff_t length = P4_PLENGTH(stack);
if (length < 0 || stack->base[-1].u != P4_SENTINEL) {
p4Bp(ctx);
Expand Down Expand Up @@ -1060,6 +1061,9 @@ p4Repl(P4_Ctx *ctx, int thrown)
P4_String str;
P4_Cell w, x, y, *ip;

#pragma GCC diagnostic push
/* Ignore pedantic warning about "address of a label", required extension. */
#pragma GCC diagnostic ignored "-Wpedantic"
static P4_Word words[] = {
P4_WORD("_nop", &&_nop, 0, 0x00), //_p4
#define w_nop words[0]
Expand Down Expand Up @@ -1250,6 +1254,7 @@ p4Repl(P4_Ctx *ctx, int thrown)

P4_WORD(NULL, NULL, 0, 0),
};
#pragma GCC diagnostic pop

if (p4_builtin_words == NULL) {
/* Link up the base dictionary. */
Expand All @@ -1268,6 +1273,9 @@ p4Repl(P4_Ctx *ctx, int thrown)
#define THROW(e) { if (p4_throw != NULL) { word = p4_throw; \
P4_PUSH(ctx->ds, (P4_Int)(e)); goto _forth; } THROWHARD(e); }

#pragma GCC diagnostic push
/* Ignore pedantic warning about "address of a label", required extension. */
#pragma GCC diagnostic ignored "-Wpedantic"
static const P4_Word w_inter_loop = P4_WORD("_inter_loop", &&_inter_loop, P4_BIT_HIDDEN, 0x00);
static const P4_Word w_halt = P4_WORD("_halt", &&_halt, P4_BIT_HIDDEN, 0x00);
static const P4_Word w_ok = P4_WORD("_ok", &&_ok, P4_BIT_HIDDEN, 0x00);
Expand All @@ -1282,6 +1290,7 @@ p4Repl(P4_Ctx *ctx, int thrown)
* into the C driven REPL.
*/
static P4_Cell exec[] = { { 0 }, {.cw = &w_inter_loop} };
#pragma GCC diagnostic pop

SETJMP_PUSH(ctx->longjmp);
rc = SETJMP(ctx->longjmp);
Expand Down Expand Up @@ -1390,17 +1399,18 @@ _inter_loop: while (ctx->input->offset < ctx->input->length) {
p4WordAppend(ctx, (P4_Cell) word);
p4WordAppend(ctx, x);
} else {
p4StackIsFull(ctx, &ctx->P4_FLOAT_STACK, P4_THROW_FS_OVER);
P4_PUSH(ctx->P4_FLOAT_STACK, x);
// p4StackGuard(ctx, &ctx->fs, P4_THROW_FS_OVER, P4_THROW_FS_UNDER);
}
p4StackGuards(ctx);
} else
#endif
if (ctx->state == P4_STATE_COMPILE) {
p4WordAppend(ctx, (P4_Cell) &w_lit);
p4WordAppend(ctx, x);
} else {
p4StackIsFull(ctx, &ctx->ds, P4_THROW_DS_OVER);
P4_PUSH(ctx->ds, x);
p4StackGuards(ctx);
}
} else if (ctx->state == P4_STATE_INTERPRET && P4_WORD_IS(word, P4_BIT_COMPILE)) {
THROW(P4_THROW_COMPILE_ONLY);
Expand Down Expand Up @@ -1482,13 +1492,19 @@ _branch: w = *ip;
// ( flag -- )
_branchz: w = *ip;
P4_DROP(ctx->ds, 1);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
ip = (P4_Cell *)((P4_Char *) ip + (x.u == 0 ? w.n : P4_CELL));
#pragma GCC diagnostic pop
NEXT;

// ( flag -- )
_branchnz: w = *ip;
P4_DROP(ctx->ds, 1);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
ip = (P4_Cell *)((P4_Char *) ip + (x.u != 0 ? w.n : P4_CELL));
#pragma GCC diagnostic pop
NEXT;

#ifdef HAVE_HOOKS
Expand Down Expand Up @@ -1815,8 +1831,8 @@ _drop: P4STACKISEMPTY(ctx, &ctx->ds, P4_THROW_DS_UNDER);

// ( x -- x x )
_dup: P4STACKISEMPTY(ctx, &ctx->ds, P4_THROW_DS_UNDER);
P4_PUSH(ctx->ds, x);
P4STACKISFULL(ctx, &ctx->ds, P4_THROW_DS_OVER);
P4_PUSH(ctx->ds, x);
NEXT;

// ( xu ... x1 x0 u -- xu ... x1 x0 xu )
Expand Down Expand Up @@ -2179,7 +2195,10 @@ _fa_rline: errno = 0;
(void) fgets(w.s, (int) x.n, fp);
x.z = strlen(w.s);
if (0 < x.z && w.s[x.z-1] == '\n') {
x.z -= 0 < --x.z && w.s[x.z-1] == '\r';
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsequence-point"
x.z -= (0 < --x.z && w.s[x.z-1] == '\r');
#pragma GCC diagnostic pop
}
P4_PUSH(ctx->ds, x.z);
P4_PUSH(ctx->ds, P4_BOOL(!eof));
Expand Down Expand Up @@ -2288,7 +2307,7 @@ _rs_to_fs: P4STACKISEMPTY(ctx, &ctx->rs, P4_THROW_RS_UNDER);
_to_float: errno = 0;
w = P4_DROPTOP(ctx->ds);
y.f = strtod((const char *)w.s, &stop);
P4_PUSH(ctx->ds, (P4_Uint) P4_BOOL(errno == 0 && stop - (char *)w.s == x.u));
P4_PUSH(ctx->ds, (P4_Uint) P4_BOOL(errno == 0 && stop - (char *)w.s == x.n));
if (P4_TOP(ctx->ds).n == P4_TRUE) {
P4_PUSH(ctx->P4_FLOAT_STACK, y);
}
Expand Down
10 changes: 5 additions & 5 deletions src/post4.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ struct p4_word {
/* Header */
P4_Word * prev; /* Previous word definition. */
P4_Size length;
P4_Char * name;
char * name;
P4_Uint bits;

#define P4_BIT_IMM 0x0001
Expand Down Expand Up @@ -408,7 +408,7 @@ struct p4_word {
# define P4_WORD(name, code, bits, pp) { NULL, STRLEN(name), name, bits, pp, code, 0 }

typedef struct {
P4_Size size; /* Size of table in cells. */
P4_Int size; /* Size of table in cells. */
P4_Cell * top; /* Last element in the stack / array. */
P4_Cell * base; /* Base of array; might be reallocated. */
} P4_Array, P4_Stack;
Expand All @@ -419,13 +419,13 @@ typedef struct {
# define P4_DROPTOP(stack) (*--(stack).top)
# define P4_POP(stack) (*(stack).top--)
# define P4_PUSH(stack, x) (*++(stack).top = (P4_Cell)(x))
# define P4_LENGTH(stack) ((stack).top + 1 - (stack).base)
# define P4_LENGTH(stack) ((ptrdiff_t)((stack).top + 1 - (stack).base))
# define P4_DROP(stack, n) ((stack).top -= (n))
# define P4_SET(stack, n) ((stack).top = (stack).base + (n) - 1)
# define P4_RESET(stack) P4_SET(stack, 0)
# define P4_GUARD_CELLS 4

# define P4_PLENGTH(stk) ((stk)->top + 1 - (stk)->base)
# define P4_PLENGTH(stk) ((ptrdiff_t)((stk)->top + 1 - (stk)->base))
# define P4_PSET(stk, n) ((stk)->top = (stk)->base + (n) - 1)
# define P4_PRESET(stk) P4_PSET(stk, 0)

Expand Down Expand Up @@ -698,7 +698,7 @@ extern int p4CharLiteral(int ch);
*/
extern void p4StrRev(P4_Char *s, P4_Size length);

extern int p4StrNum(P4_String str, P4_Uint base, P4_Cell *out, int *is_float);
extern int p4StrNum(P4_String str, int base, P4_Cell *out, int *is_float);

extern int p4Accept(P4_Input *source, char *buffer, size_t size);

Expand Down

0 comments on commit 102b424

Please sign in to comment.