Skip to content

Commit 700b380

Browse files
committed
- CMake: added option to turn fail compilation if warning occurs, and warning level 4 with MSVC.
- Fixed some warnings
1 parent 7b62cea commit 700b380

4 files changed

Lines changed: 29 additions & 8 deletions

File tree

CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ENABLE_TESTING()
44

55
OPTION(JSONCPP_WITH_TESTS "Compile and run JsonCpp test executables" ON)
66
OPTION(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
7+
OPTION(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF)
78

89
# Ensures that CMAKE_BUILD_TYPE is visible in cmake-gui on Unix
910
IF(NOT WIN32)
@@ -61,9 +62,27 @@ MESSAGE(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINO
6162
CONFIGURE_FILE( "${PROJECT_SOURCE_DIR}/src/lib_json/version.h.in"
6263
"${PROJECT_SOURCE_DIR}/include/json/version.h" )
6364

65+
macro(UseCompilationWarningAsError)
66+
if ( MSVC )
67+
# Only enabled in debug because some old versions of VS STL generate
68+
# warnings when compiled in release configuration.
69+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /WX ")
70+
endif( MSVC )
71+
endmacro()
72+
6473
# Include our configuration header
6574
INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/include )
6675

76+
if ( MSVC )
77+
# Only enabled in debug because some old versions of VS STL generate
78+
# unreachable code warning when compiled in release configuration.
79+
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /W4 ")
80+
endif( MSVC )
81+
82+
IF(JSONCPP_WITH_WARNING_AS_ERROR)
83+
UseCompilationWarningAsError()
84+
ENDIF(JSONCPP_WITH_WARNING_AS_ERROR)
85+
6786
# Build the different applications
6887
ADD_SUBDIRECTORY( src )
6988

src/lib_json/json_value.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ Path::makePath( const std::string &path,
18051805

18061806

18071807
void
1808-
Path::addPathInArg( const std::string &path,
1808+
Path::addPathInArg( const std::string &/*path*/,
18091809
const InArgs &in,
18101810
InArgs::const_iterator &itInArg,
18111811
PathArgument::Kind kind )
@@ -1826,8 +1826,8 @@ Path::addPathInArg( const std::string &path,
18261826

18271827

18281828
void
1829-
Path::invalidPath( const std::string &path,
1830-
int location )
1829+
Path::invalidPath( const std::string &/*path*/,
1830+
int /*location*/ )
18311831
{
18321832
// Error: invalid path.
18331833
}

src/test_lib_json/jsontest.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,10 @@ Runner::runCommandLine( int argc, const char *argv[] ) const
480480
}
481481

482482

483-
#if defined(_MSC_VER)
483+
#if defined(_MSC_VER) && defined(_DEBUG)
484484
// Hook MSVCRT assertions to prevent dialog from appearing
485485
static int
486-
msvcrtSilentReportHook( int reportType, char *message, int *returnValue )
486+
msvcrtSilentReportHook( int reportType, char *message, int * /*returnValue*/ )
487487
{
488488
// The default CRT handling of error and assertion is to display
489489
// an error dialog to the user.
@@ -517,9 +517,11 @@ msvcrtSilentReportHook( int reportType, char *message, int *returnValue )
517517
void
518518
Runner::preventDialogOnCrash()
519519
{
520-
#if defined(_MSC_VER)
520+
#if defined(_MSC_VER) && defined(_DEBUG)
521521
// Install a hook to prevent MSVCRT error and assertion from
522-
// popping a dialog.
522+
// popping a dialog
523+
// This function a NO-OP in release configuration
524+
// (which cause warning since msvcrtSilentReportHook is not referenced)
523525
_CrtSetReportHook( &msvcrtSilentReportHook );
524526
#endif // if defined(_MSC_VER)
525527

src/test_lib_json/jsontest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ namespace JsonTest {
193193
if ( static_cast< U >( expected ) != actual )
194194
{
195195
result.addFailure( file, line, expr );
196-
result << "Expected: " << expected << "\n";
196+
result << "Expected: " << static_cast< U >( expected ) << "\n";
197197
result << "Actual : " << actual;
198198
}
199199
return result;

0 commit comments

Comments
 (0)