From c2d4a0286e5899f3382bd34f9d285f122753bca5 Mon Sep 17 00:00:00 2001 From: David Steele Date: Tue, 13 Apr 2021 18:06:07 -0400 Subject: [PATCH] Define DEBUG in build.auto.c. Both NDEBUG and DEBUG were used in the code, which was a bit confusing. Define DEBUG in build.auto.c so it is available in all C and header files and stop using NDEBUG. This is preferable to using NDEBUG everywhere since there are multiple DEBUG* defines, e.g. DEBUG_COVERAGE. Note that NDEBUG is still required since it is used by the C libraries. --- src/build.auto.h.in | 7 ++++++- src/common/assert.h | 2 +- src/common/debug.h | 13 +++---------- src/common/error.c | 2 +- src/common/error.h | 2 +- src/common/io/filter/group.h | 2 +- src/common/io/read.intern.h | 2 +- src/common/stackTrace.c | 20 ++++++++++---------- src/common/stackTrace.h | 10 +++++----- test/src/common/harnessConfig.c | 2 ++ test/src/common/harnessDebug.h | 2 +- test/src/common/harnessInfo.c | 2 ++ test/src/common/harnessLog.c | 2 ++ test/src/common/harnessPack.c | 2 ++ test/src/common/harnessPq.c | 2 ++ test/src/common/harnessServer.c | 2 ++ test/src/common/harnessStorage.c | 2 ++ test/src/common/harnessTest.c | 8 +++++--- test/src/module/common/stackTraceTest.c | 2 +- test/src/test.c | 5 +++-- 20 files changed, 53 insertions(+), 38 deletions(-) diff --git a/src/build.auto.h.in b/src/build.auto.h.in index d79d8e10f..d4200ba8e 100644 --- a/src/build.auto.h.in +++ b/src/build.auto.h.in @@ -3,9 +3,14 @@ Build Flags Generated by Configure ***********************************************************************************************************************************/ #include "version.h" -// Are test code and asserts disabled? +// Are test code and asserts disabled? NDEBUG can be confusing to work with because, when debugging, #ifndef NDEBUG must be used for +// #defines, so also #define DEBUG in this case to make #ifdefs simpler. #undef NDEBUG +#ifndef NDEBUG + #define DEBUG +#endif + // Does the compiler provide _Static_assert()? #undef HAVE_STATIC_ASSERT diff --git a/src/common/assert.h b/src/common/assert.h index 35cef2943..27b788d67 100644 --- a/src/common/assert.h +++ b/src/common/assert.h @@ -9,7 +9,7 @@ Assert Routines /*********************************************************************************************************************************** Asserts are used in test code to ensure that certain conditions are true. They are omitted from production builds. ***********************************************************************************************************************************/ -#ifndef NDEBUG +#ifdef DEBUG #define ASSERT(condition) \ do \ { \ diff --git a/src/common/debug.h b/src/common/debug.h index e8cfef740..a2a61fcf6 100644 --- a/src/common/debug.h +++ b/src/common/debug.h @@ -9,13 +9,6 @@ Debug Routines #include "common/type/convert.h" #include "common/type/stringz.h" -/*********************************************************************************************************************************** -NDEBUG indicates to C library routines that debugging is off -- set a more readable flag to use when debugging is on -***********************************************************************************************************************************/ -#ifndef NDEBUG - #define DEBUG -#endif - /*********************************************************************************************************************************** Extern variables that are needed for unit testing ***********************************************************************************************************************************/ @@ -296,13 +289,13 @@ Function Test Macros In debug builds these macros will update the stack trace with function names and parameters but will not log. In production builds all test macros are compiled out (except for return statements). -Ignore DEBUG_TEST_TRACE_MACRO if NDEBUG is defined because the underlying functions that support the macros will not be present. +Ignore DEBUG_TEST_TRACE_MACRO if DEBUG is not defined because the underlying functions that support the macros will not be present. ***********************************************************************************************************************************/ -#ifndef NDEBUG +#ifdef DEBUG #ifdef DEBUG_TEST_TRACE #define DEBUG_TEST_TRACE_MACRO #endif // DEBUG_TEST_TRACE -#endif // NDEBUG +#endif // DEBUG #ifdef DEBUG_TEST_TRACE_MACRO #define FUNCTION_TEST_BEGIN() \ diff --git a/src/common/error.c b/src/common/error.c index b168c66a2..a7570f4dc 100644 --- a/src/common/error.c +++ b/src/common/error.c @@ -27,7 +27,7 @@ struct ErrorType const ErrorType name = {code, #name, &parentType} // Define test error -#ifndef NDEBUG +#ifdef DEBUG ERROR_DEFINE(1, TestError, RuntimeError); #endif diff --git a/src/common/error.h b/src/common/error.h index d23ac8e35..12df0c5d6 100644 --- a/src/common/error.h +++ b/src/common/error.h @@ -54,7 +54,7 @@ typedef struct ErrorType ErrorType; #include "common/error.auto.h" // Declare test error -#ifndef NDEBUG +#ifdef DEBUG ERROR_DECLARE(TestError); #else // Must always be defined since it might be needed to compile (though not used) during profiling diff --git a/src/common/io/filter/group.h b/src/common/io/filter/group.h index eeaffe4dc..e0194932d 100644 --- a/src/common/io/filter/group.h +++ b/src/common/io/filter/group.h @@ -34,7 +34,7 @@ typedef struct IoFilterGroupPub bool inputSame; // Same input required again? bool done; // Is processing done? -#ifndef NDEBUG +#ifdef DEBUG bool opened; // Has the filter set been opened? bool closed; // Has the filter set been closed? #endif diff --git a/src/common/io/read.intern.h b/src/common/io/read.intern.h index 8ca803ab0..e4ef8c569 100644 --- a/src/common/io/read.intern.h +++ b/src/common/io/read.intern.h @@ -43,7 +43,7 @@ typedef struct IoReadPub IoFilterGroup *filterGroup; // IO filters bool eofAll; // Is the read done (read and filters complete)? -#ifndef NDEBUG +#ifdef DEBUG bool opened; // Has the io been opened? bool closed; // Has the io been closed? #endif diff --git a/src/common/stackTrace.c b/src/common/stackTrace.c index 023216ee4..99cc52e60 100644 --- a/src/common/stackTrace.c +++ b/src/common/stackTrace.c @@ -86,7 +86,7 @@ backTraceCallbackError(void *data, const char *msg, int errnum) #endif /**********************************************************************************************************************************/ -#ifndef NDEBUG +#ifdef DEBUG static struct StackTraceTestLocal { @@ -259,15 +259,7 @@ stackTraceParamLog(void) } /**********************************************************************************************************************************/ -#ifdef NDEBUG - -void -stackTracePop(void) -{ - stackTraceLocal.stackSize--; -} - -#else +#ifdef DEBUG void stackTracePop(const char *fileName, const char *functionName, bool test) @@ -285,6 +277,14 @@ stackTracePop(const char *fileName, const char *functionName, bool test) } } +#else + +void +stackTracePop(void) +{ + stackTraceLocal.stackSize--; +} + #endif /*********************************************************************************************************************************** diff --git a/src/common/stackTrace.h b/src/common/stackTrace.h index 90f420113..b1b59b095 100644 --- a/src/common/stackTrace.h +++ b/src/common/stackTrace.h @@ -20,12 +20,12 @@ Macros to access internal functions #define STACK_TRACE_PUSH(logLevel) \ stackTracePush(__FILE__, __func__, logLevel) -#ifdef NDEBUG - #define STACK_TRACE_POP(test) \ - stackTracePop(); -#else +#ifdef DEBUG #define STACK_TRACE_POP(test) \ stackTracePop(__FILE__, __func__, test); +#else + #define STACK_TRACE_POP(test) \ + stackTracePop(); #endif /*********************************************************************************************************************************** @@ -36,7 +36,7 @@ Internal Functions void stackTraceInit(const char *exe); #endif -#ifndef NDEBUG +#ifdef DEBUG // Enable/disable test function logging void stackTraceTestStart(void); void stackTraceTestStop(void); diff --git a/test/src/common/harnessConfig.c b/test/src/common/harnessConfig.c index 4f449c6c0..83752f0b5 100644 --- a/test/src/common/harnessConfig.c +++ b/test/src/common/harnessConfig.c @@ -1,6 +1,8 @@ /*********************************************************************************************************************************** Harness for Loading Test Configurations ***********************************************************************************************************************************/ +#include "build.auto.h" + #include #include #include diff --git a/test/src/common/harnessDebug.h b/test/src/common/harnessDebug.h index 84fcd2fce..2b80b30a6 100644 --- a/test/src/common/harnessDebug.h +++ b/test/src/common/harnessDebug.h @@ -16,7 +16,7 @@ C Debug Harness // Set line numer of the current function in the stack trace. This is used to give more detailed info about which test macro // caused an error. - #ifndef NDEBUG + #ifdef DEBUG #define FUNCTION_HARNESS_STACK_TRACE_LINE_SET(lineNo) \ stackTraceTestFileLineSet((unsigned int)lineNo) #else diff --git a/test/src/common/harnessInfo.c b/test/src/common/harnessInfo.c index 40138bb01..279872c31 100644 --- a/test/src/common/harnessInfo.c +++ b/test/src/common/harnessInfo.c @@ -1,6 +1,8 @@ /*********************************************************************************************************************************** Harness for Loading Test Configurations ***********************************************************************************************************************************/ +#include "build.auto.h" + #include #include "common/assert.h" diff --git a/test/src/common/harnessLog.c b/test/src/common/harnessLog.c index bb9850e9e..025827a8b 100644 --- a/test/src/common/harnessLog.c +++ b/test/src/common/harnessLog.c @@ -1,6 +1,8 @@ /*********************************************************************************************************************************** Log Test Harness ***********************************************************************************************************************************/ +#include "build.auto.h" + #include #include #include diff --git a/test/src/common/harnessPack.c b/test/src/common/harnessPack.c index 4928e525a..ff2af953b 100644 --- a/test/src/common/harnessPack.c +++ b/test/src/common/harnessPack.c @@ -1,6 +1,8 @@ /*********************************************************************************************************************************** Harness for Loading Test Configurations ***********************************************************************************************************************************/ +#include "build.auto.h" + #include "common/assert.h" #include "common/type/convert.h" #include "common/type/pack.h" diff --git a/test/src/common/harnessPq.c b/test/src/common/harnessPq.c index 7ecf6ea96..ee6d80afb 100644 --- a/test/src/common/harnessPq.c +++ b/test/src/common/harnessPq.c @@ -3,6 +3,8 @@ Pq Test Harness ***********************************************************************************************************************************/ #ifndef HARNESS_PQ_REAL +#include "build.auto.h" + #include #include diff --git a/test/src/common/harnessServer.c b/test/src/common/harnessServer.c index 7f4bac849..707c4cab4 100644 --- a/test/src/common/harnessServer.c +++ b/test/src/common/harnessServer.c @@ -1,6 +1,8 @@ /*********************************************************************************************************************************** Server Test Harness ***********************************************************************************************************************************/ +#include "build.auto.h" + #include #include #include diff --git a/test/src/common/harnessStorage.c b/test/src/common/harnessStorage.c index 460b3c667..13f2a7671 100644 --- a/test/src/common/harnessStorage.c +++ b/test/src/common/harnessStorage.c @@ -1,6 +1,8 @@ /*********************************************************************************************************************************** Storage Test Harness ***********************************************************************************************************************************/ +#include "build.auto.h" + #include #include #include diff --git a/test/src/common/harnessTest.c b/test/src/common/harnessTest.c index 69f8988cd..6f0e39bcd 100644 --- a/test/src/common/harnessTest.c +++ b/test/src/common/harnessTest.c @@ -1,6 +1,8 @@ /*********************************************************************************************************************************** C Test Harness ***********************************************************************************************************************************/ +#include "build.auto.h" + #include #include #include @@ -490,7 +492,7 @@ hrnTestResultException(void) if (harnessTestLocal.result.running) { THROW_FMT( -#ifndef NDEBUG +#ifdef DEBUG TestError, #else AssertError, @@ -520,7 +522,7 @@ static void hrnTestResultDiff(const char *actual, const char *expected) if (actual != NULL && expected != NULL && (strstr(actual, "\n") != NULL || strstr(expected, "\n") != NULL)) { THROW_FMT( -#ifndef NDEBUG +#ifdef DEBUG TestError, #else AssertError, @@ -531,7 +533,7 @@ static void hrnTestResultDiff(const char *actual, const char *expected) else { THROW_FMT( -#ifndef NDEBUG +#ifdef DEBUG TestError, #else AssertError, diff --git a/test/src/module/common/stackTraceTest.c b/test/src/module/common/stackTraceTest.c index c3d346302..397ec3b37 100644 --- a/test/src/module/common/stackTraceTest.c +++ b/test/src/module/common/stackTraceTest.c @@ -44,7 +44,7 @@ testRun(void) // ***************************************************************************************************************************** if (testBegin("stackTraceTestStart(), stackTraceTestStop(), and stackTraceTest()")) { -#ifndef NDEBUG +#ifdef DEBUG assert(stackTraceTest()); stackTraceTestStop(); diff --git a/test/src/test.c b/test/src/test.c index c2dcf3d88..86055d3c1 100644 --- a/test/src/test.c +++ b/test/src/test.c @@ -3,6 +3,7 @@ C Test Wrapper This wrapper runs the the C unit tests. ***********************************************************************************************************************************/ +#include "build.auto.h" /*********************************************************************************************************************************** C files to be tested @@ -156,7 +157,7 @@ main(int argListSize, const char *argList[]) CATCH_ANY() { // If a test was running then throw a detailed result exception -#ifndef NDEBUG +#ifdef DEBUG if (!errorInstanceOf(&TestError)) #endif hrnTestResultException(); @@ -186,7 +187,7 @@ main(int argListSize, const char *argList[]) errorName(), errorMessage()); // If not a TestError (which is detailed) then also print the stack trace -#ifndef NDEBUG +#ifdef DEBUG if (!errorInstanceOf(&TestError)) #endif fprintf(stderr, "\nTHROWN AT:\n%s\n", errorStackTrace());