diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index 859824e03..e5f8c2e01 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -31,7 +31,7 @@ jobs: - name: Configure env: - CXXFLAGS: -Wall -Wextra -Wpedantic -Wsign-conversion -Wtautological-compare -Werror ${{env.CXXFLAGS}} + CXXFLAGS: -Wall -Wextra -Wpedantic -Wsign-conversion -Wtautological-compare -Wformat-nonliteral -Wundef -Werror ${{env.CXXFLAGS}} run: | cmake -S . -B build_${{matrix.abi}} \ -DANDROID_ABI=${{matrix.abi}} \ diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index fe0b07a96..c9ac1a4c1 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -67,7 +67,7 @@ jobs: - name: Configure env: - CXXFLAGS: -Wall -Wextra -Wsign-conversion -Wtautological-compare -Werror ${{env.CXXFLAGS}} + CXXFLAGS: -Wall -Wextra -Wsign-conversion -Wtautological-compare -Wformat-nonliteral -Wundef -Werror ${{env.CXXFLAGS}} run: | cmake -S . -B build_${{matrix.build_type}} \ -DBUILD_SHARED_LIBS=${{matrix.lib == 'shared'}} \ diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index be09b97ba..0b20ed270 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -34,7 +34,7 @@ jobs: - name: Configure shell: bash env: - CXXFLAGS: -Wall -Wextra -Wsign-conversion -Wtautological-compare -Werror ${{env.CXXFLAGS}} + CXXFLAGS: -Wall -Wextra -Wsign-conversion -Wtautological-compare -Wformat-nonliteral -Wundef -Werror ${{env.CXXFLAGS}} run: | cmake -S . -B build_${{matrix.build_type}} \ -DCMAKE_CXX_EXTENSIONS=OFF \ diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 2e6ae8cd6..72a26fa68 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -104,7 +104,7 @@ jobs: - name: Configure env: - CXXFLAGS: -Wall -Wextra -Wpedantic -Wsign-conversion -Wtautological-compare -Werror ${{env.CXXFLAGS}} + CXXFLAGS: -Wall -Wextra -Wpedantic -Wsign-conversion -Wtautological-compare -Wformat-nonliteral -Wundef -Werror ${{env.CXXFLAGS}} run: | if [[ ${{matrix.build_type}} == "Debug" ]]; then export CXXFLAGS="--coverage ${CXXFLAGS}" diff --git a/CMakeLists.txt b/CMakeLists.txt index 8cb747724..f166f7b96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,7 +145,11 @@ check_cxx_compiler_flag (-Wunnamed-type-template-args HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS) cmake_push_check_state (RESET) -set (CMAKE_REQUIRED_LIBRARIES Threads::Threads) + +if (Threads_FOUND) + set (CMAKE_REQUIRED_LIBRARIES Threads::Threads) +endif (Threads_FOUND) + check_cxx_symbol_exists (pthread_threadid_np "pthread.h" HAVE_PTHREAD_THREADID_NP) cmake_pop_check_state () diff --git a/src/logging_custom_prefix_unittest.cc b/src/logging_custom_prefix_unittest.cc index ac25f04b0..04a71787a 100644 --- a/src/logging_custom_prefix_unittest.cc +++ b/src/logging_custom_prefix_unittest.cc @@ -371,7 +371,7 @@ void TestRawLogging() { RAW_LOG(ERROR, "%s%d%c%010d%s%1x", foo->c_str(), j, ' ', j, " ", j); RAW_VLOG(0, "foo %d", j); -#ifdef NDEBUG +#if defined(NDEBUG) RAW_LOG(INFO, "foo %d", j); // so that have same stderr to compare #else RAW_DLOG(INFO, "foo %d", j); // test RAW_DLOG in debug mode @@ -393,7 +393,7 @@ void TestRawLogging() { RAW_VLOG(2, "vlog 2 on"); RAW_VLOG(3, "vlog 3 off"); -#ifdef NDEBUG +#if defined(NDEBUG) RAW_DCHECK(1 == 2, " RAW_DCHECK's shouldn't be compiled in normal mode"); #endif @@ -599,7 +599,7 @@ void TestCHECK() { } void TestDCHECK() { -#ifdef NDEBUG +#if defined(NDEBUG) DCHECK( 1 == 2 ) << " DCHECK's shouldn't be compiled in normal mode"; #endif DCHECK( 1 == 1 ); @@ -1228,7 +1228,7 @@ static void MyCheck(bool a, bool b) { TEST(DVLog, Basic) { ScopedMockLog log; -#if NDEBUG +#if defined(NDEBUG) // We are expecting that nothing is logged. EXPECT_CALL(log, Log(_, _, _)).Times(0); #else @@ -1279,7 +1279,7 @@ TEST(TestExitOnDFatal, ToBeOrNotToBe) { // LOG(DFATAL) has severity FATAL if debugging, but is // downgraded to ERROR if not debugging. const LogSeverity severity = -#ifdef NDEBUG +#if defined(NDEBUG) GLOG_ERROR; #else GLOG_FATAL; diff --git a/src/logging_unittest.cc b/src/logging_unittest.cc index 72b892f62..30bad773f 100644 --- a/src/logging_unittest.cc +++ b/src/logging_unittest.cc @@ -348,7 +348,7 @@ void TestRawLogging() { RAW_LOG(ERROR, "%s%d%c%010d%s%1x", foo->c_str(), j, ' ', j, " ", j); RAW_VLOG(0, "foo %d", j); -#ifdef NDEBUG +#if defined(NDEBUG) RAW_LOG(INFO, "foo %d", j); // so that have same stderr to compare #else RAW_DLOG(INFO, "foo %d", j); // test RAW_DLOG in debug mode @@ -370,7 +370,7 @@ void TestRawLogging() { RAW_VLOG(2, "vlog 2 on"); RAW_VLOG(3, "vlog 3 off"); -#ifdef NDEBUG +#if defined(NDEBUG) RAW_DCHECK(1 == 2, " RAW_DCHECK's shouldn't be compiled in normal mode"); #endif @@ -594,7 +594,7 @@ void TestCHECK() { } void TestDCHECK() { -#ifdef NDEBUG +#if defined(NDEBUG) DCHECK( 1 == 2 ) << " DCHECK's shouldn't be compiled in normal mode"; #endif DCHECK( 1 == 1 ); @@ -1316,7 +1316,7 @@ static void MyCheck(bool a, bool b) { TEST(DVLog, Basic) { ScopedMockLog log; -#if NDEBUG +#if defined(NDEBUG) // We are expecting that nothing is logged. EXPECT_CALL(log, Log(_, _, _)).Times(0); #else @@ -1367,7 +1367,7 @@ TEST(TestExitOnDFatal, ToBeOrNotToBe) { // LOG(DFATAL) has severity FATAL if debugging, but is // downgraded to ERROR if not debugging. const LogSeverity severity = -#ifdef NDEBUG +#if defined(NDEBUG) GLOG_ERROR; #else GLOG_FATAL; diff --git a/src/raw_logging.cc b/src/raw_logging.cc index 8cedabd0f..7b0f89ce3 100644 --- a/src/raw_logging.cc +++ b/src/raw_logging.cc @@ -68,6 +68,16 @@ _START_GOOGLE_NAMESPACE_ +#if defined(__GNUC__) +#define GLOG_ATTRIBUTE_FORMAT(archetype, stringIndex, firstToCheck) \ + __attribute__((format(archetype, stringIndex, firstToCheck))) +#define GLOG_ATTRIBUTE_FORMAT_ARG(stringIndex) \ + __attribute__((format_arg(stringIndex))) +#else +#define GLOG_ATTRIBUTE_FORMAT(archetype, stringIndex, firstToCheck) +#define GLOG_ATTRIBUTE_FORMAT_ARG(stringIndex) +#endif + // CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths // that invoke malloc() and getenv() that might acquire some locks. // If this becomes a problem we should reimplement a subset of vsnprintf @@ -76,6 +86,7 @@ _START_GOOGLE_NAMESPACE_ // Helper for RawLog__ below. // *DoRawLog writes to *buf of *size and move them past the written portion. // It returns true iff there was no overflow or error. +GLOG_ATTRIBUTE_FORMAT(printf, 3, 4) static bool DoRawLog(char** buf, size_t* size, const char* format, ...) { va_list ap; va_start(ap, format); @@ -90,7 +101,14 @@ static bool DoRawLog(char** buf, size_t* size, const char* format, ...) { // Helper for RawLog__ below. inline static bool VADoRawLog(char** buf, size_t* size, const char* format, va_list ap) { +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +#endif int n = vsnprintf(*buf, *size, format, ap); +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif if (n < 0 || static_cast(n) > *size) return false; *size -= static_cast(n); *buf += n; @@ -102,6 +120,7 @@ static bool crashed = false; static CrashReason crash_reason; static char crash_buf[kLogBufSize + 1] = { 0 }; // Will end in '\0' +GLOG_ATTRIBUTE_FORMAT(printf, 4, 5) void RawLog__(LogSeverity severity, const char* file, int line, const char* format, ...) { if (!(FLAGS_logtostderr || severity >= FLAGS_stderrthreshold || diff --git a/src/symbolize_unittest.cc b/src/symbolize_unittest.cc index a07ab7935..a50e44aff 100644 --- a/src/symbolize_unittest.cc +++ b/src/symbolize_unittest.cc @@ -71,7 +71,7 @@ static const char *TrySymbolize(void *pc) { #if defined(__GNUC__) && !defined(__OPENCC__) # if __GNUC__ >= 4 # define TEST_WITH_MODERN_GCC -# if __i386__ // always_inline isn't supported for x86_64 with GCC 4.1.0. +# if defined(__i386__) && __i386__ // always_inline isn't supported for x86_64 with GCC 4.1.0. # undef always_inline # define always_inline __attribute__((always_inline)) # define HAVE_ALWAYS_INLINE