aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2020-09-23 22:37:28 -0700
committerBruce Hill <bruce@bruce-hill.com>2020-09-23 22:37:28 -0700
commit938ff73730b49488e06677fed8c2980638cbeeca (patch)
treef88eabbb93aabfbb8785b264d77477f3ac4c86a0
parent452c7df023181954d48c5338ba8cb10dbeae1a94 (diff)
More rigorous compile-time checks via __attribute__s
-rw-r--r--compiler.c1
-rw-r--r--compiler.h4
-rw-r--r--file_loader.h4
-rw-r--r--grammar.c2
-rw-r--r--grammar.h5
-rw-r--r--utils.h5
-rw-r--r--vm.c2
-rw-r--r--vm.h4
8 files changed, 23 insertions, 4 deletions
diff --git a/compiler.c b/compiler.c
index 3b0489c..a0ff087 100644
--- a/compiler.c
+++ b/compiler.c
@@ -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;
diff --git a/compiler.h b/compiler.h
index 005eacc..d5b4ff1 100644
--- a/compiler.h
+++ b/compiler.h
@@ -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);
diff --git a/grammar.c b/grammar.c
index ec0f40a..50f2306 100644
--- a/grammar.c
+++ b/grammar.c
@@ -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));
}
diff --git a/grammar.h b/grammar.h
index 0f57616..985f814 100644
--- a/grammar.h
+++ b/grammar.h
@@ -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
diff --git a/utils.h b/utils.h
index 28bfe05..84456a1 100644
--- a/utils.h
+++ b/utils.h
@@ -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
diff --git a/vm.c b/vm.c
index 2fb78be..530cd09 100644
--- a/vm.c
+++ b/vm.c
@@ -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;
diff --git a/vm.h b/vm.h
index e5e82bd..e0e6b5d 100644
--- a/vm.h
+++ b/vm.h
@@ -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