Skip to content
Permalink
Browse files

Add common/macro.h for general-purpose macros.

Add GLUE() macro which is useful for creating identifiers.

Move MACRO_TO_STR() here and rename it STRINGIFY().  This appears to be the standard name for this type of macro and it is also an awesome name.
  • Loading branch information...
dwsteele committed May 3, 2019
1 parent 32ca27a commit 4a20d44c6b118143f1c1607bacac0d4e268f6ffe
@@ -63,6 +63,10 @@
<p>Improve type safety of interfaces and drivers.</p>
</release-item>

<release-item>
<p>Add <file>common/macro.h</file> for general-purpose macros.</p>
</release-item>

<release-item>
<p>Various <code>Buffer</code> improvements.</p>
</release-item>
@@ -352,7 +352,7 @@ common/type/keyValue.o: common/type/keyValue.c build.auto.h common/assert.h comm
common/type/list.o: common/type/list.c build.auto.h common/assert.h common/debug.h common/error.auto.h common/error.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/list.h common/type/string.h
$(CC) $(CFLAGS) $(CMAKE) -c common/type/list.c -o common/type/list.o

common/type/string.o: common/type/string.c build.auto.h common/assert.h common/debug.h common/error.auto.h common/error.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/string.h
common/type/string.o: common/type/string.c build.auto.h common/assert.h common/debug.h common/error.auto.h common/error.h common/logLevel.h common/macro.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/string.h
$(CC) $(CFLAGS) $(CMAKE) -c common/type/string.c -o common/type/string.o

common/type/stringList.o: common/type/stringList.c build.auto.h common/assert.h common/debug.h common/error.auto.h common/error.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/list.h common/type/string.h common/type/stringList.h common/type/variant.h common/type/variantList.h
@@ -25,14 +25,6 @@ Extern variables that are needed for unit testing
static
#endif

/***********************************************************************************************************************************
Convert a macro to a string
***********************************************************************************************************************************/
#define MACRO_TO_STR_INNER(macro) \
#macro
#define MACRO_TO_STR(macro) \
MACRO_TO_STR_INNER(macro)

/***********************************************************************************************************************************
Base function debugging macros
@@ -0,0 +1,24 @@
/***********************************************************************************************************************************
General Macros
***********************************************************************************************************************************/
#ifndef COMMON_MACRO_H
#define COMMON_MACRO_H

/***********************************************************************************************************************************
Convert the parameter to a zero-terminated string
This is useful for converting non-string types (e.g. int) to strings for inclusion in messages.
***********************************************************************************************************************************/
#define STRINGIFY_HELPER(param) #param
#define STRINGIFY(param) STRINGIFY_HELPER(param)

/***********************************************************************************************************************************
Glue together a string/macro and another string//macro
Useful for creating function names when one or both of the macro parameter needs to be converted to a macro before concatenating.
common/object.h has numerous examples of this.
***********************************************************************************************************************************/
#define GLUE_HELPER(param1, param2) param1##param2
#define GLUE(param1, param2) GLUE_HELPER(param1, param2)

#endif
@@ -10,6 +10,7 @@ String Handler
#include <string.h>

#include "common/debug.h"
#include "common/macro.h"
#include "common/memContext.h"
#include "common/type/string.h"

@@ -39,7 +40,7 @@ Maximum size of a string
do \
{ \
if ((size) > STRING_SIZE_MAX) \
THROW(AssertError, "string size must be <= " MACRO_TO_STR(STRING_SIZE_MAX) " bytes"); \
THROW(AssertError, "string size must be <= " STRINGIFY(STRING_SIZE_MAX) " bytes"); \
} \
while(0)

@@ -1,6 +1,7 @@
/***********************************************************************************************************************************
Test Debug Macros and Routines when Disabled
***********************************************************************************************************************************/
#include "common/macro.h"

/***********************************************************************************************************************************
Test Run
@@ -25,7 +26,7 @@ testRun(void)
// *****************************************************************************************************************************
if (testBegin("DEBUG_UNIT_EXTERN"))
{
const char *debugUnitExtern = MACRO_TO_STR(DEBUG_UNIT_EXTERN);
const char *debugUnitExtern = STRINGIFY(DEBUG_UNIT_EXTERN);
TEST_RESULT_STR(debugUnitExtern, "static", "DEBUG_UNIT_EXTERN is static");
}

@@ -4,6 +4,7 @@ Test Debug Macros and Routines
#include "common/log.h"

#include "common/harnessLog.h"
#include "common/macro.h"

static void
testFunction3(void)
@@ -108,7 +109,7 @@ testRun(void)
// *****************************************************************************************************************************
if (testBegin("DEBUG_UNIT_EXTERN"))
{
const char *debugUnitExtern = MACRO_TO_STR(DEBUG_UNIT_EXTERN);
const char *debugUnitExtern = STRINGIFY(DEBUG_UNIT_EXTERN);
TEST_RESULT_STR(debugUnitExtern, "", "DEBUG_UNIT_EXTERN is blank (extern)");
}

0 comments on commit 4a20d44

Please sign in to comment.
You can’t perform that action at this time.