From 05b97c473aa2d382f9a34c94a36604fd0db849fc Mon Sep 17 00:00:00 2001
From: Niels Lohmann <mail@nlohmann.me>
Date: Fri, 25 Aug 2017 20:12:21 +0200
Subject: [PATCH] :construction_worker: added flags for Valgrind and Clang
 sanitizer

---
 .travis.yml         | 12 +++---------
 test/CMakeLists.txt | 27 ++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index b59b7a51..0a203066 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -29,34 +29,28 @@ matrix:
   include:
 
   # Valgrind
-
   - os: linux
     compiler: gcc
     env:
       - COMPILER=g++-4.9
-      - SPECIAL=valgrind
+      - CMAKE_OPTIONS=-DJSON_Valgrind=ON
     addons:
       apt:
         sources: ['ubuntu-toolchain-r-test']
         packages: [g++-4.9, valgrind]
-    after_success:
-      - make check TEST_PREFIX="valgrind --error-exitcode=1 --leak-check=full " TEST_PATTERN=""
 
   # clang sanitizer
   - os: linux
     compiler: clang
     env:
       - COMPILER=clang++-5.0
-      - SPECIAL=sanitizer
+      - CMAKE_OPTIONS=-DJSON_Sanitizer=ON
     addons:
       apt:
         sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-5.0']
         packages: ['g++-6', 'clang-5.0']
-    after_success:
-      - make clang_sanitize -j4
 
   # cppcheck
-
   - os: linux
     compiler: gcc
     env:
@@ -289,7 +283,7 @@ script:
 
   # compile and execute unit tests
   - mkdir -p build && cd build
-  - cmake .. && cmake --build . --config Release -- -j4
+  - cmake .. ${CMAKE_OPTIONS} && cmake --build . --config Release -- -j4
   - ctest -C Release -V
   - cd ..
 
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 428bfbae..8624b6bf 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,3 +1,19 @@
+option(JSON_Sanitizer "Build with Clang Sanitizer" OFF)
+option(JSON_Valgrind "Execute tests with Valgrind" OFF)
+
+if(JSON_Sanitizer)
+    if(NOT MSVC)
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O2 -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer")
+    endif()
+endif()
+
+if(JSON_Valgrind)
+    find_program(CMAKE_MEMORYCHECK_COMMAND valgrind)
+    set(MEMORYCHECK_COMMAND_OPTIONS "--error-exitcode=1 --leak-check=full")
+    set(memcheck_command "${CMAKE_MEMORYCHECK_COMMAND} ${CMAKE_MEMORYCHECK_COMMAND_OPTIONS}")
+    separate_arguments(memcheck_command)
+endif()
+
 #############################################################################
 # Catch library with the main function to speed up build
 #############################################################################
@@ -35,7 +51,7 @@ foreach(file ${files})
     target_link_libraries(${testcase} ${JSON_TARGET_NAME})
 
     if(NOT MSVC)
-        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated -Wno-float-equal")
+        set_target_properties(${testcase} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated -Wno-float-equal")
     endif()
 
     include(cotire OPTIONAL)
@@ -54,4 +70,13 @@ foreach(file ${files})
       WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
     )
     set_tests_properties("${testcase}_all" PROPERTIES LABELS "all")
+
+    if(JSON_Valgrind)
+        add_test(NAME "${testcase}_valgrind"
+          COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${testcase}
+          WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+        )
+        set_tests_properties("${testcase}_valgrind" PROPERTIES LABELS "valgrind")
+    endif()
+
 endforeach()