[master] 75acb5cc7 Add disarmed assertions on ctx->method to vgc

Nils Goroll nils.goroll at uplex.de
Sat Jan 30 10:57:08 UTC 2021


commit 75acb5cc7aa73fcd22b150dd1a3b899c6ec9bc44
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Sat Jan 30 11:50:27 2021 +0100

    Add disarmed assertions on ctx->method to vgc
    
    in the course of #3163 we want to add a preamble to VCL subs, which,
    ideally, would perform as many checks based on constants as possible.
    
    This is a first step towards such checks: We add a commented-out
    assert() on ctx->method for built-in subs only. To add the same for
    custom subs, we will need the okmask from #3163, for which I would first
    want to coordinate with phk.
    
    Also, I would want to coordinate with him on arming the assertion, as
    there might be a good reason why assert() is not yet defined for vgc (it
    is already used in unused macros, though).
    
    Having assertions would help humans, flexelint and compilers alike. Until
    armed, the assertion helps humans at least.
    
    The resulting vgc looks like this:
    
    void v_matchproto_(vcl_func_f)
    VGC_function_vcl_backend_error(VRT_CTX)
    {
      // assert(ctx->method == (VCL_MET_BACKEND_ERROR));
    
      /* ... from ('Builtin' Line 165 Pos 23) */

diff --git a/lib/libvcc/vcc_compile.c b/lib/libvcc/vcc_compile.c
index b55de8642..bd227fe2f 100644
--- a/lib/libvcc/vcc_compile.c
+++ b/lib/libvcc/vcc_compile.c
@@ -72,6 +72,25 @@ static const struct method method_tab[] = {
 	{ NULL, 0U, 0}
 };
 
+/*--------------------------------------------------------------------*/
+
+static void
+vcc_vcl_met2c(struct vsb *vsb, unsigned method)
+{
+	int d = 0;
+
+#define VCL_MET_MAC(l,U,t,b)			\
+	if (method & VCL_MET_##U) {		\
+		if (d)				\
+			VSB_putc(vsb, '|');	\
+		VSB_cat(vsb, "VCL_MET_" #U);	\
+		d = 1;				\
+	}
+#include "tbl/vcl_returns.h"
+	AN(d);
+}
+
+
 /*--------------------------------------------------------------------*/
 
 void * v_matchproto_(TlAlloc)
@@ -147,9 +166,20 @@ vcc_NewProc(struct vcc *tl, struct symbol *sym)
 static void
 vcc_EmitProc(struct vcc *tl, struct proc *p)
 {
+	struct vsb *vsbm;
+
 	AZ(VSB_finish(p->cname));
 	AZ(VSB_finish(p->prologue));
 	AZ(VSB_finish(p->body));
+
+	vsbm = VSB_new_auto();
+	if (p->method) {
+		VSB_cat(vsbm, "  // assert(ctx->method == (");
+		vcc_vcl_met2c(vsbm, p->method->bitval);
+		VSB_cat(vsbm, "));\n");
+	}
+	AZ(VSB_finish(vsbm));
+
 	Fh(tl, 1, "vcl_func_f %s;\n", VSB_data(p->cname));
 	/*
 	 * TODO: v_dont_optimize for custom subs called from vcl_init/fini only
@@ -162,10 +192,12 @@ vcc_EmitProc(struct vcc *tl, struct proc *p)
 	   p->method && p->method->bitval & VCL_MET_TASK_H ?
 	   "v_dont_optimize " : "");
 	Fc(tl, 1, "%s(VRT_CTX)\n", VSB_data(p->cname));
-	Fc(tl, 1, "{\n%s\n%s}\n", VSB_data(p->prologue), VSB_data(p->body));
+	Fc(tl, 1, "{\n%s%s\n%s}\n", VSB_data(vsbm), VSB_data(p->prologue),
+	    VSB_data(p->body));
 	VSB_destroy(&p->body);
 	VSB_destroy(&p->prologue);
 	VSB_destroy(&p->cname);
+	VSB_destroy(&vsbm);
 }
 
 /*--------------------------------------------------------------------*/


More information about the varnish-commit mailing list