Selaa lähdekoodia

add UA_DEBUG option, make assertions dependent on that

Julius Pfrommer 6 vuotta sitten
vanhempi
commit
b536580822

+ 6 - 0
CMakeLists.txt

@@ -104,6 +104,12 @@ option(UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
 mark_as_advanced(UA_ENABLE_UNIT_TEST_FAILURE_HOOKS)
 
 # Build options for debugging
+option(UA_DEBUG "Enable assertions and additional functionality that should not be included in release builds" OFF)
+mark_as_advanced(UA_DEBUG)
+if(CMAKE_BUILD_TYPE STREQUAL "Debug")
+    set(UA_DEBUG ON)
+endif()
+
 option(UA_DEBUG_DUMP_PKGS "Dump every package received by the server as hexdump format" OFF)
 mark_as_advanced(UA_DEBUG_DUMP_PKGS)
 

+ 11 - 0
doc/building.rst

@@ -201,6 +201,17 @@ be visible in the cmake GUIs.
 **UA_ENABLE_NONSTANDARD_UDP**
    Enable udp extension
 
+UA_DEBUG_* group
+^^^^^^^^^^^^^^^^
+
+This group contains build options mainly useful for development of the library itself.
+
+**UA_DEBUG**
+   Enable assertions and additional definitions not intended for production builds
+
+**UA_DEBUG_DUMP_PKGS**
+   Dump every package received by the server as hexdump format
+
 Building a shared library
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 

+ 11 - 3
include/ua_config.h.in

@@ -40,6 +40,7 @@ extern "C" {
 #cmakedefine UA_ENABLE_UNIT_TEST_FAILURE_HOOKS
 
 /* Options for Debugging */
+#cmakedefine UA_DEBUG
 #cmakedefine UA_DEBUG_DUMP_PKGS
 
 /**
@@ -64,9 +65,16 @@ extern "C" {
 
 /**
  * Assertions
- * ---------- */
-#include <assert.h>
-#define UA_assert(ignore) assert(ignore)
+ * ----------
+ * The assert macro is disabled by defining NDEBUG. It is often forgotten to
+ * include -DNDEBUG in the compiler flags when using the single-file release. So
+ * we make assertions dependent on the UA_DEBUG definition handled by CMake. */
+#ifdef UA_DEBUG
+# include <assert.h>
+# define UA_assert(ignore) assert(ignore)
+#else
+# define UA_assert(ignore)
+#endif
 
 /* Outputs an error message at compile time if the assert fails.
  * Example usage:

+ 1 - 1
tests/check_discovery.c

@@ -219,7 +219,7 @@ FindAndCheck(const UA_String expectedUris[], size_t expectedUrisSize,
 
     // only the discovery server is expected
     ck_assert_uint_eq(applicationDescriptionArraySize, expectedUrisSize);
-    assert(applicationDescriptionArray != NULL);
+    ck_assert(applicationDescriptionArray != NULL);
 
     for(size_t i = 0; i < expectedUrisSize; ++i) {
         ck_assert(UA_String_equal(&applicationDescriptionArray[i].applicationUri,

+ 4 - 4
tests/check_server_readspeed.c

@@ -29,7 +29,7 @@ int main(int argc, char** argv) {
     UA_StatusCode retval = UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
                                                      parentReferenceNodeId, myIntegerName,
                                                      UA_NODEID_NULL, attr, NULL, NULL);
-    assert(retval == UA_STATUSCODE_GOOD);
+    UA_assert(retval == UA_STATUSCODE_GOOD);
 
     UA_ReadRequest request;
     UA_ReadRequest_init(&request);
@@ -50,7 +50,7 @@ int main(int argc, char** argv) {
     UA_Byte *pos = request_msg.data;
     const UA_Byte *end = &request_msg.data[request_msg.length];
     retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_READREQUEST], &pos, &end, NULL, NULL);
-    assert(retval == UA_STATUSCODE_GOOD);
+    UA_assert(retval == UA_STATUSCODE_GOOD);
 
     UA_Byte *resp_pos = response_msg.data;
     const UA_Byte *resp_end = &response_msg.data[response_msg.length];
@@ -64,14 +64,14 @@ int main(int argc, char** argv) {
     for(int i = 0; i < 1000000; i++) {
         size_t offset = 0;
         retval |= UA_decodeBinary(&request_msg, &offset, &rq, &UA_TYPES[UA_TYPES_READREQUEST], 0, NULL);
-        assert(retval == UA_STATUSCODE_GOOD);
+        UA_assert(retval == UA_STATUSCODE_GOOD);
 
         UA_ReadResponse_init(&rr);
         Service_Read(server, &adminSession, &rq, &rr);
 
         resp_pos = response_msg.data;
         retval |= UA_encodeBinary(&rr, &UA_TYPES[UA_TYPES_READRESPONSE], &resp_pos, &resp_end, NULL, NULL);
-        assert(retval == UA_STATUSCODE_GOOD);
+        UA_assert(retval == UA_STATUSCODE_GOOD);
 
         UA_ReadRequest_deleteMembers(&rq);
         UA_ReadResponse_deleteMembers(&rr);

+ 1 - 1
tests/check_types_builtin.c

@@ -414,7 +414,7 @@ START_TEST(UA_Variant_decodeWithOutArrayFlagSetShallSetVTAndAllocateMemoryForArr
     ck_assert_int_eq((uintptr_t)dst.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT32]); 
     ck_assert_int_eq(dst.arrayLength, 0);
     ck_assert_int_ne((uintptr_t)dst.data, 0);
-    assert(dst.data != NULL); /* repeat the previous argument so that clang-analyzer is happy */
+    UA_assert(dst.data != NULL); /* repeat the previous argument so that clang-analyzer is happy */
     ck_assert_int_eq(*(UA_Int32 *)dst.data, 255);
     // finally
     UA_Variant_deleteMembers(&dst);