diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2020-09-23 22:37:28 -0700 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2020-09-23 22:37:28 -0700 |
| commit | 938ff73730b49488e06677fed8c2980638cbeeca (patch) | |
| tree | f88eabbb93aabfbb8785b264d77477f3ac4c86a0 | |
| parent | 452c7df023181954d48c5338ba8cb10dbeae1a94 (diff) | |
More rigorous compile-time checks via __attribute__s
| -rw-r--r-- | compiler.c | 1 | ||||
| -rw-r--r-- | compiler.h | 4 | ||||
| -rw-r--r-- | file_loader.h | 4 | ||||
| -rw-r--r-- | grammar.c | 2 | ||||
| -rw-r--r-- | grammar.h | 5 | ||||
| -rw-r--r-- | utils.h | 5 | ||||
| -rw-r--r-- | vm.c | 2 | ||||
| -rw-r--r-- | vm.h | 4 |
8 files changed, 23 insertions, 4 deletions
@@ -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; @@ -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 diff --git a/file_loader.h b/file_loader.h index a93c7a3..1634f15 100644 --- a/file_loader.h +++ b/file_loader.h @@ -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); @@ -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)); } @@ -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 @@ -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 @@ -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; @@ -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 |
