From 9b24c8a496d6354a9cd9571e1ab9778db55d387c Mon Sep 17 00:00:00 2001 From: Ellen Arvidsson Date: Tue, 15 Apr 2025 23:50:14 +0200 Subject: [PATCH] proper c flags --- CMakeLists.txt | 7 +++++++ include/msph/token.h | 7 ++++--- src/msph/token.c | 49 +++++++++++++++++++------------------------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a972050..3067de7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,13 @@ project(log-e-sappho C) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED True) +string(JOIN " " CMAKE_C_FLAGS + "-Wall -Wextra -Wformat=2" + "-Wconversion -Wsign-conversion -Wimplicit-fallthrough" + "-Werror=implicit -Werror=incompatible-pointer-types" + "-Werror=int-conversion") +set(CMAKE_VERBOSE_MAKEFILE ON) + include(CheckSymbolExists) diff --git a/include/msph/token.h b/include/msph/token.h index 93dc21a..2177b2c 100644 --- a/include/msph/token.h +++ b/include/msph/token.h @@ -102,7 +102,8 @@ enum msph_tok_type { TOK_AMP, // & TOK_PIPE, // | - TOK_RARROW, // => + TOK_IMPL, // => + TOK_RARROW, // -> TOK_SUB, // <: TOK_KW_TYPE, // type @@ -136,7 +137,7 @@ struct msph_token { }; #define MSPH_FILE_NAME_LEN 1024 -#define MSPH_FILE_BUF_LEN 4096 +#define MSPH_FILE_BUF_LEN 2048 #define MSPH_TOKEN_SRC_FILE 1 struct msph_token_src_file { @@ -187,7 +188,7 @@ ssize_t msph_token_stream_read_tok(struct msph_token *, size_t, struct msph_token_stream *); int msph_token_stream_print(struct msph_token_stream *, FILE *); -size_t msph_token_str(char *buf, size_t len, struct msph_token *tok); +ssize_t msph_token_str(char *buf, size_t len, struct msph_token *tok); #endif /* _MSPH_EXPR_H */ diff --git a/src/msph/token.c b/src/msph/token.c index 959546f..299e828 100644 --- a/src/msph/token.c +++ b/src/msph/token.c @@ -29,6 +29,7 @@ struct msph_matcher token_matcher[] = { { 0, 0, TOK_AMP }, { 0, 0, TOK_PIPE }, + { 0, 0, TOK_IMPL }, { 0, 0, TOK_RARROW }, { 0, 0, TOK_SUB }, @@ -64,7 +65,8 @@ struct msph_token_info { TOK_INFO(TOK_AMP, "&"), TOK_INFO(TOK_PIPE, "|"), - TOK_INFO(TOK_RARROW, "=>"), + TOK_INFO(TOK_IMPL, "=>"), + TOK_INFO(TOK_RARROW, "->"), TOK_INFO(TOK_SUB, "<:"), TOK_INFO(TOK_KW_TYPE, "type"), @@ -170,22 +172,22 @@ msph_token_stream_frombuf(struct msph_ctx *ctx, const char *buf, size_t len) return (ret); } -size_t +ssize_t msph_token_str(char *buf, size_t len, struct msph_token *tok) { - size_t ret; + ssize_t ret; - ret = snprintf(buf, len, "%s", tok_base_str(tok)); + ret = (ssize_t)snprintf(buf, len, "%s", tok_base_str(tok)); - if (ret > len) + if (ret < 0 || ret >= (ssize_t)len) return (ret); - len -= ret; + len -= (size_t)ret; buf += ret; switch (tok->type) { case TOK_IDENT: - ret += snprintf(buf, len, "(%s)", tok->d.s.buf); + ret += (ssize_t)snprintf(buf, len, "(%s)", tok->d.s.buf); break; default: break; @@ -199,20 +201,19 @@ int msph_token_stream_print(struct msph_token_stream *s, FILE *out) { ssize_t ret; - ssize_t i; struct msph_token tok; char tokstr[MSPH_TOKEN_PRINT_BUF_LEN]; while ((ret = msph_token_stream_read_tok( &tok, 1, s)) > 0) { - SPHO_DEBUG_PRINT("msph_token_stream_print: ret=%zd\n", ret); - if (msph_token_str( - tokstr, BUF_LEN(tokstr), &tok) > BUF_LEN(tokstr)) { - tokstr[BUF_LEN(tokstr) - 1] = '\0'; - } - fprintf(out, "%s\n", tokstr); - } + ret = msph_token_str(tokstr, BUF_LEN(tokstr), &tok); + if (ret < 0) + continue; - SPHO_DEBUG_PRINT("msph_token_stream_print: ret=%zd\n", ret); + if ((size_t)ret < BUF_LEN(tokstr)) + fprintf(out, "%s\n", tokstr); + else + fprintf(out, "%s...(trunkated)", tokstr); + } return ((int)ret); } @@ -281,7 +282,6 @@ read_single_tok(struct msph_token *ptr, struct msph_token_stream *s) max_m = 0; for (m = 1; token_matcher[m].type != TOK_END; m++) { res = tok_match(ctx, src, &token_matcher[m]); - SPHO_DEBUG_PRINT("read_single_tok: tok_match=%d\n", res); if (res == -1) return (-1); @@ -295,10 +295,8 @@ read_single_tok(struct msph_token *ptr, struct msph_token_stream *s) if (max_m == 0) return (0); - SPHO_DEBUG_PRINT("read_single_tok: commit=%zu\n", max_m); if (tok_commit(ctx, src, &token_matcher[max_m], ptr) == -1) return (-1); - SPHO_DEBUG_PRINT("read_single_tok: committed\n"); return (1); } @@ -393,6 +391,7 @@ char_at(struct msph_ctx *ctx, struct msph_token_src *src, size_t i, char *out) int ret; struct msph_token_src_str *str; + ret = -1; switch (src->type) { case MSPH_TOKEN_SRC_FILE: ret = file_char_at(ctx, src, i, out); @@ -443,7 +442,6 @@ tok_match(struct msph_ctx *ctx, struct msph_token_src *src, struct msph_matcher *m) { int res; - int more; char chr; const char *match_str; size_t off, len; @@ -506,8 +504,10 @@ tok_match(struct msph_ctx *ctx, struct msph_token_src *src, MATCH_CHAR('&'); case TOK_PIPE: MATCH_CHAR('|'); - case TOK_RARROW: + case TOK_IMPL: MATCH_STR("=>"); + case TOK_RARROW: + MATCH_STR("->"); case TOK_SUB: MATCH_STR("<:"); case TOK_KW_TYPE: @@ -567,20 +567,16 @@ tok_commit(struct msph_ctx *ctx, struct msph_token_src *src, SPHO_PRECOND(ctx != NULL && m != NULL); SPHO_PRECOND(m->matchlen != 0); - SPHO_DEBUG_PRINT("committing\n"); - switch (src->type) { case MSPH_TOKEN_SRC_FILE: file = &src->inner.file; pos_old = file->pos; - SPHO_DEBUG_PRINT("committing\n"); file->pos += m->matchlen; file->pos %= BUF_LEN(file->buf); SPHO_ASSERT(file->pos < BUF_LEN(file->buf) || file->pos < pos_old); - SPHO_DEBUG_PRINT("committing\n"); if (ptr == NULL) return (0); @@ -588,20 +584,17 @@ tok_commit(struct msph_ctx *ctx, struct msph_token_src *src, if (! TOK_HAS_DATA(ptr->type)) return (0); - SPHO_DEBUG_PRINT("committing\n"); if (m->matchlen >= sizeof(ptr->d.s.buf)) { MSPH_ERR(ctx, MSPH_ERR_TOOLONG); return (-1); } - SPHO_DEBUG_PRINT("committing\n"); if (fromcbuf_charcpy(ptr->d.s.buf, file->buf, sizeof(file->buf), pos_old, m->matchlen) == -1) { MSPH_ERR(ctx, MSPH_ERR_TOOLONG); return (-1); } - SPHO_DEBUG_PRINT("committing\n"); ptr->d.s.buf[m->matchlen] = '\0'; return (0);