Skip to content

Commit

Permalink
CHECK support for nullptr (fixes google#341)
Browse files Browse the repository at this point in the history
This allows CHECK_NE(foo, nullptr) to compile and produces "nullptr" for the
string representation of nullptr.
  • Loading branch information
bretmckee authored and sergiud committed Apr 8, 2021
1 parent a79416b commit be3f421
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ std::atomic<int> i;
int main() { }
" HAVE_CXX11_ATOMIC)

check_cxx_source_compiles ("
#include <cstddef>
void foo(std::nullptr_t) {}
int main(void) { foo(nullptr); }
" HAVE_CXX11_NULLPTR_T)

if (WITH_TLS)
# Cygwin does not support the thread attribute. Don't bother.
if (HAVE_GCC_TLS)
Expand Down Expand Up @@ -381,6 +387,12 @@ else (HAVE_USING_OPERATOR)
set (ac_cv_cxx_using_operator 0)
endif (HAVE_USING_OPERATOR)

if (HAVE_CXX11_NULLPTR_T)
set (ac_cv_cxx11_nullptr_t 1)
else (HAVE_CXX11_NULLPTR_T)
set (ac_cv_cxx11_nullptr_t 0)
endif (HAVE_CXX11_NULLPTR_T)

if (HAVE_EXECINFO_H)
set (HAVE_STACKTRACE 1)
endif (HAVE_EXECINFO_H)
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Abhishek Dasgupta <abhi2743@gmail.com>
Abhishek Parmar <abhishek@orng.net>
Andrew Schwartzmeyer <andrew@schwartzmeyer.com>
Andy Ying <andy@trailofbits.com>
Bret McKee <bretmckee@google.com>
Brian Silverman <bsilver16384@gmail.com>
Fumitoshi Ukai <ukai@google.com>
Guillaume Dumont <dumont.guillaume@gmail.com>
Expand Down
8 changes: 8 additions & 0 deletions src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#define _LOGGING_H_

#include <cerrno>
#include <cstddef>
#include <cstring>
#include <ctime>
#include <iosfwd>
Expand Down Expand Up @@ -673,6 +674,13 @@ void MakeCheckOpValueString(std::ostream* os, const signed char& v);
template <> GOOGLE_GLOG_DLL_DECL
void MakeCheckOpValueString(std::ostream* os, const unsigned char& v);

// This is required because nullptr is only present in c++ 11 and later.
#if @ac_cv_cxx11_nullptr_t@
// Provide printable value for nullptr_t
template <> GOOGLE_GLOG_DLL_DECL
void MakeCheckOpValueString(std::ostream* os, const std::nullptr_t& v);
#endif

// Build the error message string. Specify no inlining for code size.
template <typename T1, typename T2>
std::string* MakeCheckOpString(const T1& v1, const T2& v2, const char* exprtext)
Expand Down
7 changes: 7 additions & 0 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,13 @@ void MakeCheckOpValueString(std::ostream* os, const unsigned char& v) {
}
}

#if __cplusplus >= 201103L
template <>
void MakeCheckOpValueString(std::ostream* os, const std::nullptr_t& v) {
(*os) << "nullptr";
}
#endif

void InitGoogleLogging(const char* argv0) {
glog_internal_namespace_::InitGoogleLoggingUtilities(argv0);
}
Expand Down

0 comments on commit be3f421

Please sign in to comment.