More rigorous compile-time checks via __attribute__s

This commit is contained in:
Bruce Hill 2020-09-23 22:37:28 -07:00
parent 452c7df023
commit 938ff73730
8 changed files with 23 additions and 4 deletions

View File

@ -434,7 +434,6 @@ vm_op_t *bpeg_stringpattern(file_t *f, const char *str)
*/
vm_op_t *bpeg_replacement(vm_op_t *pat, const char *replacement)
{
check(pat, "Null pattern used in replacement");
vm_op_t *op = calloc(sizeof(vm_op_t), 1);
op->op = VM_REPLACE;
op->start = pat->start;

View File

@ -9,9 +9,13 @@
#include "types.h"
#include "file_loader.h"
__attribute__((nonnull(2)))
vm_op_t *bpeg_simplepattern(file_t *f, const char *str);
__attribute__((nonnull(2)))
vm_op_t *bpeg_stringpattern(file_t *f, const char *str);
__attribute__((nonnull(1,2)))
vm_op_t *bpeg_replacement(vm_op_t *pat, const char *replacement);
__attribute__((nonnull(2)))
vm_op_t *bpeg_pattern(file_t *f, const char *str);
#endif

View File

@ -14,9 +14,13 @@ typedef struct {
} file_t;
file_t *load_file(const char *filename);
__attribute__((nonnull))
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);
void fprint_line(FILE *dest, file_t *f, const char *start, const char *end, const char *msg);

View File

@ -33,7 +33,6 @@ void add_def(grammar_t *g, file_t *f, const char *src, const char *name, vm_op_t
*/
vm_op_t *load_grammar(grammar_t *g, file_t *f)
{
check(f, "Null file provided");
vm_op_t *ret = NULL;
const char *src = f->contents;
src = after_spaces(src);
@ -81,7 +80,6 @@ vm_op_t *lookup(grammar_t *g, const char *name)
void push_backref(grammar_t *g, const char *name, match_t *capture)
{
check(capture, "No capture provided");
if (g->backrefcount >= g->backrefcapacity) {
g->backrefs = realloc(g->backrefs, sizeof(g->backrefs[0])*(g->backrefcapacity += 32));
}

View File

@ -11,10 +11,15 @@
#include "types.h"
grammar_t *new_grammar(void);
__attribute__((nonnull(1,3,4,5)))
void add_def(grammar_t *g, file_t *f, const char *src, const char *name, vm_op_t *op);
__attribute__((nonnull))
void push_backref(grammar_t *g, const char *name, match_t *capture);
__attribute__((nonnull))
void pop_backrefs(grammar_t *g, size_t count);
__attribute__((nonnull))
vm_op_t *load_grammar(grammar_t *g, file_t *f);
__attribute__((pure, nonnull))
vm_op_t *lookup(grammar_t *g, const char *name);
#endif

View File

@ -17,10 +17,15 @@
#define check(cond, ...) do { if (!(cond)) { fprintf(stderr, __VA_ARGS__); fwrite("\n", 1, 1, stderr); _exit(1); } } while(0)
#define debug(...) do { if (verbose) fprintf(stderr, __VA_ARGS__); } while(0)
__attribute__((nonnull))
char unescapechar(const char *escaped, const char **end);
__attribute__((pure, nonnull, returns_nonnull))
const char *after_name(const char *str);
__attribute__((pure, nonnull, returns_nonnull))
const char *after_spaces(const char *str);
__attribute__((nonnull))
int matchchar(const char **str, char c);
__attribute__((nonnull))
size_t unescape_string(char *dest, const char *src, size_t bufsize);
#endif

2
vm.c
View File

@ -45,7 +45,7 @@ const char *opcode_name(enum VMOpcode o)
*/
void destroy_match(match_t **m)
{
if (!m || !*m) return;
if (!*m) return;
destroy_match(&((*m)->child));
destroy_match(&((*m)->nextsibling));
*m = NULL;

4
vm.h
View File

@ -12,9 +12,13 @@
#include "types.h"
const char *opcode_name(enum VMOpcode o);
__attribute__((hot, nonnull))
match_t *match(grammar_t *g, file_t *f, const char *str, vm_op_t *op, unsigned int flags);
__attribute__((nonnull))
void destroy_match(match_t **m);
__attribute__((nonnull))
void print_pattern(vm_op_t *op);
__attribute__((nonnull))
void print_match(file_t *f, match_t *m);
#endif