Filter: auto-generating enum-to-string

This commit is contained in:
Maria Matejka 2019-02-12 11:35:41 +01:00
parent 041608129a
commit b256f24145
2 changed files with 24 additions and 69 deletions

View file

@ -9,6 +9,7 @@ m4_divert(-1)m4_dnl
#
# Global Diversions:
# 4 enum fi_code
# 5 enum fi_code to string
# 1 struct f_inst_FI_...
# 2 union in struct f_inst
# 3 constructors
@ -30,6 +31,7 @@ m4_define(FID_STRUCT, `FID_ZONE(1, Per-instruction structure)')
m4_define(FID_UNION, `FID_ZONE(2, Union member)')
m4_define(FID_NEW, `FID_ZONE(3, Constructor)')
m4_define(FID_ENUM, `FID_ZONE(4, Code enum)')
m4_define(FID_ENUM_STR, `FID_ZONE(5, Code enum to string)')
m4_define(FID_STRUCT_IN, `m4_divert(101)')
m4_define(FID_NEW_ARGS, `m4_divert(102)')
@ -43,6 +45,8 @@ m4_define(FID_H, `m4_ifelse(TARGET, [[H]], FID_ALL, [[m4_define(FID_CURDIV, m4_d
m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[
FID_ENUM
INST_NAME(),
FID_ENUM_STR
[INST_NAME()] = "INST_NAME()",
FID_STRUCT
struct f_inst_[[]]INST_NAME() {
m4_undivert(101)
@ -124,12 +128,32 @@ FID_C
#include "nest/bird.h"
#include "filter/filter.h"
#include "filter/f-inst.h"
FID_H
/* Filter instruction codes */
enum f_instruction_code {
FID_WR_PUT(4)
};
FID_C
/* Instruction codes to string */
static const char * const f_instruction_name_str[] = {
FID_WR_PUT(5)
};
const char *
f_instruction_name(enum f_instruction_code fi)
{
if (fi < (sizeof(f_instruction_name_str) / sizeof(f_instruction_name_str[0])))
return f_instruction_name_str[fi];
else
bug("Got unknown instruction code: %d", fi);
}
FID_H
/* Per-instruction structures */
FID_WR_PUT(1)

View file

@ -62,75 +62,6 @@ struct filter_state {
void (*bt_assert_hook)(int result, const struct f_line_item *assert);
static const char * const f_instruction_name_str[] = {
/* TODO: Make this better */
[FI_ADD] = "FI_ADD",
[FI_SUBTRACT] = "FI_SUBTRACT",
[FI_MULTIPLY] = "FI_MULTIPLY",
[FI_DIVIDE] = "FI_DIVIDE",
[FI_AND] = "FI_AND",
[FI_OR] = "FI_OR",
[FI_PAIR_CONSTRUCT] = "FI_PAIR_CONSTRUCT",
[FI_EC_CONSTRUCT] = "FI_EC_CONSTRUCT",
[FI_LC_CONSTRUCT] = "FI_LC_CONSTRUCT",
[FI_PATHMASK_CONSTRUCT] = "FI_PATHMASK_CONSTRUCT",
[FI_NEQ] = "FI_NEQ",
[FI_EQ] = "FI_EQ",
[FI_LT] = "FI_LT",
[FI_LTE] = "FI_LTE",
[FI_NOT] = "FI_NOT",
[FI_MATCH] = "FI_MATCH",
[FI_NOT_MATCH] = "FI_NOT_MATCH",
[FI_DEFINED] = "FI_DEFINED",
[FI_TYPE] = "FI_TYPE",
[FI_IS_V4] = "FI_IS_V4",
[FI_SET] = "FI_SET",
[FI_CONSTANT] = "FI_CONSTANT",
[FI_VARIABLE] = "FI_VARIABLE",
[FI_CONSTANT_INDIRECT] = "FI_CONSTANT_INDIRECT",
[FI_PRINT] = "FI_PRINT",
[FI_CONDITION] = "FI_CONDITION",
[FI_PRINT_AND_DIE] = "FI_PRINT_AND_DIE",
[FI_RTA_GET] = "FI_RTA_GET",
[FI_RTA_SET] = "FI_RTA_SET",
[FI_EA_GET] = "FI_EA_GET",
[FI_EA_SET] = "FI_EA_SET",
[FI_EA_UNSET] = "FI_EA_UNSET",
[FI_PREF_GET] = "FI_PREF_GET",
[FI_PREF_SET] = "FI_PREF_SET",
[FI_LENGTH] = "FI_LENGTH",
[FI_SADR_SRC] = "FI_SADR_SRC",
[FI_ROA_MAXLEN] = "FI_ROA_MAXLEN",
[FI_ROA_ASN] = "FI_ROA_ASN",
[FI_IP] = "FI_IP",
[FI_ROUTE_DISTINGUISHER] = "FI_ROUTE_DISTINGUISHER",
[FI_AS_PATH_FIRST] = "FI_AS_PATH_FIRST",
[FI_AS_PATH_LAST] = "FI_AS_PATH_LAST",
[FI_AS_PATH_LAST_NAG] = "FI_AS_PATH_LAST_NAG",
[FI_RETURN] = "FI_RETURN",
[FI_CALL] = "FI_CALL",
[FI_DROP_RESULT] = "FI_DROP_RESULT",
[FI_SWITCH] = "FI_SWITCH",
[FI_IP_MASK] = "FI_IP_MASK",
[FI_PATH_PREPEND] = "FI_PATH_PREPEND",
[FI_CLIST_ADD] = "FI_CLIST_ADD",
[FI_CLIST_DEL] = "FI_CLIST_DEL",
[FI_CLIST_FILTER] = "FI_CLIST_FILTER",
[FI_ROA_CHECK_IMPLICIT] = "FI_ROA_CHECK_IMPLICIT",
[FI_ROA_CHECK_EXPLICIT] = "FI_ROA_CHECK_EXPLICIT",
[FI_FORMAT] = "FI_FORMAT",
[FI_ASSERT] = "FI_ASSERT",
};
const char *
f_instruction_name(enum f_instruction_code fi)
{
if (fi < (sizeof(f_instruction_name_str) / sizeof(f_instruction_name_str[0])))
return f_instruction_name_str[fi];
else
bug("Got unknown instruction code: %d", fi);
}
static inline void f_cache_eattrs(struct filter_state *fs)
{
fs->eattrs = &((*fs->rte)->attrs->eattrs);