From ba1a4f78cb276ef613628afef9b0ac3859b89c8a Mon Sep 17 00:00:00 2001 From: Richard Brooksby Date: Wed, 19 Feb 2014 11:55:14 +0000 Subject: [PATCH] Declaring result codes using a relational macro, so that tests can print more meaningful output, and to eliminate redundant code. Copied from Perforce Change: 184382 ServerID: perforce.ravenbrook.com --- mps/code/mpmtypes.h | 17 +---------------- mps/code/mps.h | 30 +++++++++++++++++------------- mps/code/mpsi.c | 11 ----------- mps/code/testlib.c | 34 +++++++++++++++++++++++----------- 4 files changed, 41 insertions(+), 51 deletions(-) diff --git a/mps/code/mpmtypes.h b/mps/code/mpmtypes.h index f7d5a86513f..e7d01fc2e9e 100644 --- a/mps/code/mpmtypes.h +++ b/mps/code/mpmtypes.h @@ -371,23 +371,8 @@ enum { /* .result-codes: Result Codes -- see */ -/* These definitions must match . */ -/* This is checked by . */ -/* Changing this list entails changing the list in */ -/* and the check in */ -enum { - ResOK = 0, /* MPS_RES_OK */ - ResFAIL, /* MPS_RES_FAIL */ - ResRESOURCE, /* MPS_RES_RESOURCE */ - ResMEMORY, /* MPS_RES_MEMORY */ - ResLIMIT, /* MPS_RES_LIMIT */ - /* note "LIMIT" does _not_ have usual end-of-enum meaning -rhsk */ - ResUNIMPL, /* MPS_RES_UNIMPL */ - ResIO, /* MPS_RES_IO */ - ResCOMMIT_LIMIT, /* MPS_RES_COMMIT_LIMIT */ - ResPARAM /* MPS_RES_PARAM */ -}; +_mps_ENUM_DEF(_mps_RES_ENUM, Res) /* TraceStates -- see */ diff --git a/mps/code/mps.h b/mps/code/mps.h index e155651c189..f8751d01372 100644 --- a/mps/code/mps.h +++ b/mps/code/mps.h @@ -77,21 +77,25 @@ typedef mps_word_t mps_clock_t; /* processor time */ typedef mps_word_t mps_label_t; /* telemetry label */ /* Result Codes */ -/* .result-codes: Keep in sync with */ -/* and the check in */ -enum { - MPS_RES_OK = 0, /* success (always zero) */ - MPS_RES_FAIL, /* unspecified failure */ - MPS_RES_RESOURCE, /* unable to obtain resources */ - MPS_RES_MEMORY, /* unable to obtain memory */ - MPS_RES_LIMIT, /* limitation reached */ - MPS_RES_UNIMPL, /* unimplemented facility */ - MPS_RES_IO, /* system I/O error */ - MPS_RES_COMMIT_LIMIT, /* arena commit limit exceeded */ - MPS_RES_PARAM /* illegal user parameter value */ -}; +#define _mps_RES_ENUM(R, X) \ + R(X, OK, "success (always zero)") \ + R(X, FAIL, "unspecified failure") \ + R(X, RESOURCE, "unable to obtain resources") \ + R(X, MEMORY, "unable to obtain memory") \ + R(X, LIMIT, "limitation reached") \ + R(X, UNIMPL, "unimplemented facility") \ + R(X, IO, "system I/O error") \ + R(X, COMMIT_LIMIT, "arena commit limit exceeded") \ + R(X, PARAM, "illegal user parameter value") +#define _mps_ENUM_DEF_ROW(prefix, ident, doc) prefix##ident, +#define _mps_ENUM_DEF(REL, prefix) \ + enum { \ + REL(_mps_ENUM_DEF_ROW, prefix) \ + _mps_##prefix##LIMIT \ + }; +_mps_ENUM_DEF(_mps_RES_ENUM, MPS_RES_) /* Format and Root Method Types */ /* see design.mps.root-interface */ diff --git a/mps/code/mpsi.c b/mps/code/mpsi.c index 8aa7876a939..eb7fdb89558 100644 --- a/mps/code/mpsi.c +++ b/mps/code/mpsi.c @@ -72,18 +72,7 @@ SRCID(mpsi, "$Id$"); static Bool mpsi_check(void) { - /* .check.rc: Check that external and internal result codes match. */ - /* See and . */ - /* Also see .check.enum.cast. */ CHECKL(COMPATTYPE(mps_res_t, Res)); - CHECKL((int)MPS_RES_OK == (int)ResOK); - CHECKL((int)MPS_RES_FAIL == (int)ResFAIL); - CHECKL((int)MPS_RES_RESOURCE == (int)ResRESOURCE); - CHECKL((int)MPS_RES_MEMORY == (int)ResMEMORY); - CHECKL((int)MPS_RES_LIMIT == (int)ResLIMIT); - CHECKL((int)MPS_RES_UNIMPL == (int)ResUNIMPL); - CHECKL((int)MPS_RES_IO == (int)ResIO); - CHECKL((int)MPS_RES_COMMIT_LIMIT == (int)ResCOMMIT_LIMIT); /* Check that external and internal message types match. */ /* See and */ diff --git a/mps/code/testlib.c b/mps/code/testlib.c index e9dd48e050b..2bed23b0f6f 100644 --- a/mps/code/testlib.c +++ b/mps/code/testlib.c @@ -326,6 +326,17 @@ void rnd_state_set_v2(unsigned long seed0_v2) } +/* res_strings -- human readable MPS result codes */ + +static struct { + const char *ident; + const char *doc; +} res_strings[] = { +#define RES_STRINGS_ROW(X, ident, doc) {#ident, #doc}, +_mps_RES_ENUM(RES_STRINGS_ROW, X) +}; + + /* verror -- die with message */ void verror(const char *format, va_list args) @@ -359,26 +370,27 @@ void error(const char *format, ...) } -/* die -- Test a return code, and exit on error */ - -void die(mps_res_t res, const char *s) -{ - if (res != MPS_RES_OK) { - error("\n%s: %d\n", s, res); - } -} - - /* die_expect -- Test a return code, and exit on unexpected result */ void die_expect(mps_res_t res, mps_res_t expected, const char *s) { if (res != expected) { - error("\n%s: %d\n", s, res); + if (0 <= res && (unsigned)res < sizeof(res_strings) / sizeof(res_strings[0])) + error("\n%s: %s: %s\n", s, res_strings[res].ident, res_strings[res].doc); + else + error("\n%s: %d: unknown result code\n", s, res); } } +/* die -- Test a return code, and exit on error */ + +void die(mps_res_t res, const char *s) +{ + die_expect(res, MPS_RES_OK, s); +} + + /* cdie -- Test a C boolean, and exit on error */ void cdie(int res, const char *s)