From c0d96ca9d1aacd562042ece956593ddc7a174847 Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Fri, 5 Jan 2024 01:28:24 +0100 Subject: [PATCH] fix(debug): add missing parentheses in ternary expression --- CMakeLists.txt | 47 +++++++++++++++++++++++- src/dcheck_unittest/CMakeLists.txt | 7 ++++ src/dcheck_unittest/glog_dcheck.cc | 58 ++++++++++++++++++++++++++++++ src/glog/logging.h | 2 +- 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 src/dcheck_unittest/CMakeLists.txt create mode 100644 src/dcheck_unittest/glog_dcheck.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index f7c157aef..6790af5cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 3.21) +cmake_minimum_required (VERSION 3.22) project (glog VERSION 0.7.0 DESCRIPTION "C++ implementation of the Google logging module" @@ -908,6 +908,51 @@ if (BUILD_TESTING) -DCMAKE_GENERATOR_TOOLSET=${CMAKE_GENERATOR_TOOLSET} -Dglog_DIR=${glog_BINARY_DIR} ) + + add_test (NAME dcheck_on COMMAND ${CMAKE_CTEST_COMMAND} + --build-config Debug + --build-and-test + "${glog_SOURCE_DIR}/src/dcheck_unittest" + "${glog_BINARY_DIR}/Tests/dcheck_on" + --build-generator ${CMAKE_GENERATOR} + --build-generator-platform "${CMAKE_GENERATOR_PLATFORM}" + --build-generator-toolset "${CMAKE_GENERATOR_TOOLSET}" + --build-makeprogram ${CMAKE_MAKE_PROGRAM} + --build-target glog_dcheck + --build-options + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY=ON + -Dglog_DIR=${glog_BINARY_DIR} + --test-command glog_dcheck + ) + set_tests_properties (dcheck_on PROPERTIES + DISABLED $> + ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$" + PASS_REGULAR_EXPRESSION "Check failure stack trace" + ) + + add_test (NAME dcheck_off COMMAND ${CMAKE_CTEST_COMMAND} + --build-config Release + --build-and-test + "${glog_SOURCE_DIR}/src/dcheck_unittest" + "${glog_BINARY_DIR}/Tests/dcheck_off" + --build-generator ${CMAKE_GENERATOR} + --build-generator-platform "${CMAKE_GENERATOR_PLATFORM}" + --build-generator-toolset "${CMAKE_GENERATOR_TOOLSET}" + --build-makeprogram ${CMAKE_MAKE_PROGRAM} + --build-target glog_dcheck + --build-options + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY=ON + -Dglog_DIR=${glog_BINARY_DIR} + --test-command glog_dcheck + ) + # There should be no output + set_tests_properties (dcheck_off PROPERTIES + DISABLED $> + ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$" + PASS_REGULAR_EXPRESSION "" + ) endif (BUILD_TESTING) install (TARGETS glog diff --git a/src/dcheck_unittest/CMakeLists.txt b/src/dcheck_unittest/CMakeLists.txt new file mode 100644 index 000000000..cd08704f3 --- /dev/null +++ b/src/dcheck_unittest/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required (VERSION 3.16) +project (glog_log_severity LANGUAGES CXX) + +find_package (glog REQUIRED NO_MODULE) + +add_executable (glog_dcheck glog_dcheck.cc) +target_link_libraries (glog_dcheck PRIVATE glog::glog) diff --git a/src/dcheck_unittest/glog_dcheck.cc b/src/dcheck_unittest/glog_dcheck.cc new file mode 100644 index 000000000..291eb53dd --- /dev/null +++ b/src/dcheck_unittest/glog_dcheck.cc @@ -0,0 +1,58 @@ +// Copyright (c) 2024, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Author: Sergiu Deitsch + +#include + +#include + +namespace { +[[noreturn]] void handle_abort() { std::exit(EXIT_SUCCESS); } +} // namespace + +int main(int /*argc*/, char** argv) { + google::InitGoogleLogging(argv[0]); + google::InstallFailureSignalHandler(); + +#if defined(_MSC_VER) + // Avoid presenting an interactive dialog that will cause the test to time + // out. + _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); +#endif // defined(_MSC_VER) + google::InstallFailureFunction(handle_abort); + + DLOG(INFO) << "no output"; + DLOG(WARNING) << "no output"; + DLOG(ERROR) << "no output"; + DLOG(FATAL) << "no output"; + + // Must not fail in release build + DLOG_ASSERT(false); +} diff --git a/src/glog/logging.h b/src/glog/logging.h index f8cb7884f..db87819d9 100644 --- a/src/glog/logging.h +++ b/src/glog/logging.h @@ -1070,7 +1070,7 @@ constexpr LogSeverity GLOG_0 = GLOG_ERROR; true ? (void)0 : google::LogMessageVoidify() & LOG(severity) # define DLOG_ASSERT(condition) \ - static_cast(0), true ? (void)0 : LOG_ASSERT(condition) + static_cast(0), true ? (void)0 : (LOG_ASSERT(condition)) // MSVC warning C4127: conditional expression is constant # define DCHECK(condition) \