Skip to content

Commit

Permalink
Add common/macro.h for general-purpose macros.
Browse files Browse the repository at this point in the history
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 4a20d44
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 12 deletions.
4 changes: 4 additions & 0 deletions doc/xml/release.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions src/common/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/common/macro.h
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion src/common/type/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion test/src/module/common/debugOffTest.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/***********************************************************************************************************************************
Test Debug Macros and Routines when Disabled
***********************************************************************************************************************************/
#include "common/macro.h"

/***********************************************************************************************************************************
Test Run
Expand All @@ -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");
}

Expand Down
3 changes: 2 additions & 1 deletion test/src/module/common/debugOnTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)");
}

Expand Down

0 comments on commit 4a20d44

Please sign in to comment.