Static analysis part 1

This commit is contained in:
Bruce Hill 2021-01-18 09:15:25 -08:00
parent 65141b2402
commit 3ededef53d
9 changed files with 20 additions and 17 deletions

View File

@ -27,6 +27,9 @@ leaktest:
make G=-ggdb O=-O0 EXTRA=-DDEBUG_HEAP clean bp
valgrind --leak-check=full ./bp -l -g grammars/bp.bp -p Grammar grammars/bp.bp
splint:
splint +posixlib $(CFILES) bp.c
install: $(NAME)
mkdir -p -m 755 "$(PREFIX)/share/man/man1" "$(PREFIX)/bin" "$(SYSCONFDIR)/xdg/$(NAME)"
cp -r grammars/* "$(SYSCONFDIR)/xdg/$(NAME)/"
@ -42,4 +45,4 @@ uninstall:
[ "$$confirm" != n ] && rm -rf ~/.config/$(NAME); \
fi
.PHONY: all clean install uninstall
.PHONY: all clean install uninstall leaktest splint

8
bp.c
View File

@ -65,10 +65,10 @@ static enum {
} mode = MODE_NORMAL;
// If a filename is put here, it will be deleted if a signal is received
const char *in_use_tempfile = NULL;
static const char *in_use_tempfile = NULL;
// Used for user input/output that doesn't interfere with unix pipeline
FILE *tty_out = NULL, *tty_in = NULL;
static FILE *tty_out = NULL, *tty_in = NULL;
//
// Helper function to reduce code duplication
@ -616,8 +616,8 @@ int main(int argc, char *argv[])
}
if (mode == MODE_JSON) printf("]\n");
if (tty_out) fclose(tty_out);
if (tty_in) fclose(tty_in);
if (tty_out) { fclose(tty_out); tty_out = NULL; }
if (tty_in) { fclose(tty_in); tty_in = NULL; }
#ifdef DEBUG_HEAP
// This code frees up all residual heap-allocated memory. Since the program

View File

@ -18,6 +18,8 @@
__attribute__((nonnull))
static void populate_lines(file_t *f);
__attribute__((pure, nonnull))
static size_t get_char_number(file_t *f, const char *p);
//
// In the file object, populate the `lines` array with pointers to the
@ -185,7 +187,7 @@ size_t get_line_number(file_t *f, const char *p)
//
// Given a pointer, determine which character offset within the line it points to.
//
size_t get_char_number(file_t *f, const char *p)
static size_t get_char_number(file_t *f, const char *p)
{
size_t linenum = get_line_number(f, p);
return 1 + (size_t)(p - f->lines[linenum-1]);

View File

@ -30,8 +30,6 @@ void destroy_file(file_t **f);
__attribute__((pure, nonnull))
size_t get_line_number(file_t *f, const char *p);
__attribute__((pure, nonnull))
size_t get_char_number(file_t *f, const char *p);
__attribute__((pure, nonnull))
const char *get_line(file_t *f, size_t line_number);
__attribute__((nonnull(1,2,3), format(printf,5,6)))
void fprint_line(FILE *dest, file_t *f, const char *start, const char *end, const char *fmt, ...);

View File

@ -33,6 +33,7 @@ static match_t *unused_matches = NULL;
static match_t *in_use_matches = NULL;
#endif
static match_t *new_match(void);
__attribute__((nonnull, pure))
static inline const char *next_char(file_t *f, const char *str);
__attribute__((nonnull))
@ -41,6 +42,8 @@ __attribute__((nonnull))
static match_t *get_capture_by_num(match_t *m, int *n);
__attribute__((nonnull, pure))
static match_t *get_capture_by_name(match_t *m, const char *name);
__attribute__((hot, nonnull(2,3,4)))
static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, unsigned int flags);
//
// Return the location of the next character or UTF8 codepoint.
@ -144,7 +147,7 @@ match_t *next_match(def_t *defs, file_t *f, match_t *prev, pat_t *pat, unsigned
// match object, or NULL if no match is found.
// The returned value should be free()'d to avoid memory leaking.
//
match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, unsigned int ignorecase)
static match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, unsigned int ignorecase)
{
switch (pat->type) {
case BP_LEFTRECURSION: {
@ -574,7 +577,7 @@ match_t *get_capture(match_t *m, const char **id)
//
// Return a match object which can be used (may be allocated or recycled).
//
match_t *new_match(void)
static match_t *new_match(void)
{
match_t *m;

View File

@ -10,11 +10,8 @@
__attribute__((nonnull(2,4)))
match_t *next_match(def_t *defs, file_t *f, match_t *prev, pat_t *pat, unsigned int flags);
__attribute__((hot, nonnull(2,3,4)))
match_t *match(def_t *defs, file_t *f, const char *str, pat_t *pat, unsigned int flags);
__attribute__((nonnull))
match_t *get_capture(match_t *m, const char **id);
match_t *new_match(void);
__attribute__((nonnull))
void recycle_if_unused(match_t **at_m);
#ifdef DEBUG_HEAP

View File

@ -21,6 +21,8 @@ __attribute__((nonnull))
static pat_t *_bp_simplepattern(file_t *f, const char *str);
__attribute__((nonnull(1,2,3,6)))
static pat_t *new_range(file_t *f, const char *start, const char *end, ssize_t min, ssize_t max, pat_t *repeating, pat_t *sep);
__attribute__((nonnull(1,2)))
static pat_t *bp_simplepattern(file_t *f, const char *str);
//
// Allocate a new pattern for this file (ensuring it will be automatically
@ -149,7 +151,7 @@ pat_t *chain_together(file_t *f, pat_t *first, pat_t *second)
//
// Wrapper for _bp_simplepattern() that expands any postfix operators
//
pat_t *bp_simplepattern(file_t *f, const char *str)
static pat_t *bp_simplepattern(file_t *f, const char *str)
{
pat_t *pat = _bp_simplepattern(f, str);
if (pat == NULL) return pat;

View File

@ -10,8 +10,6 @@
__attribute__((nonnull))
pat_t *new_pat(file_t *f, const char *start, enum pattype_e type);
__attribute__((nonnull(1,2)))
pat_t *bp_simplepattern(file_t *f, const char *str);
__attribute__((nonnull(1,2)))
pat_t *bp_stringpattern(file_t *f, const char *str);
__attribute__((nonnull(1,2)))
pat_t *bp_replacement(file_t *f, pat_t *replacepat, const char *replacement);

View File

@ -265,7 +265,7 @@ static const char *context_after(printer_t *pr, const char *pos)
//
// Print the text of a match (no context).
//
void _print_match(FILE *out, printer_t *pr, match_t *m)
static void _print_match(FILE *out, printer_t *pr, match_t *m)
{
pr->pos = m->start;
if (m->pat->type == BP_REPLACE) {