|
@@ -26,22 +26,26 @@
|
|
|
// 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: [email protected] (Zhanyong Wan)
|
|
|
+
|
|
|
|
|
|
#include "gtest/internal/gtest-port.h"
|
|
|
|
|
|
#include <limits.h>
|
|
|
-#include <stdlib.h>
|
|
|
#include <stdio.h>
|
|
|
+#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
+#include <cstdint>
|
|
|
#include <fstream>
|
|
|
+#include <memory>
|
|
|
|
|
|
#if GTEST_OS_WINDOWS
|
|
|
# include <windows.h>
|
|
|
# include <io.h>
|
|
|
# include <sys/stat.h>
|
|
|
# include <map> // Used in ThreadLocal.
|
|
|
+# ifdef _MSC_VER
|
|
|
+# include <crtdbg.h>
|
|
|
+# endif // _MSC_VER
|
|
|
#else
|
|
|
# include <unistd.h>
|
|
|
#endif // GTEST_OS_WINDOWS
|
|
@@ -52,6 +56,14 @@
|
|
|
# include <mach/vm_map.h>
|
|
|
#endif // GTEST_OS_MAC
|
|
|
|
|
|
+#if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \
|
|
|
+ GTEST_OS_NETBSD || GTEST_OS_OPENBSD
|
|
|
+# include <sys/sysctl.h>
|
|
|
+# if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD
|
|
|
+# include <sys/user.h>
|
|
|
+# endif
|
|
|
+#endif
|
|
|
+
|
|
|
#if GTEST_OS_QNX
|
|
|
# include <devctl.h>
|
|
|
# include <fcntl.h>
|
|
@@ -63,19 +75,16 @@
|
|
|
# include <sys/types.h>
|
|
|
#endif // GTEST_OS_AIX
|
|
|
|
|
|
+#if GTEST_OS_FUCHSIA
|
|
|
+# include <zircon/process.h>
|
|
|
+# include <zircon/syscalls.h>
|
|
|
+#endif // GTEST_OS_FUCHSIA
|
|
|
+
|
|
|
#include "gtest/gtest-spi.h"
|
|
|
#include "gtest/gtest-message.h"
|
|
|
#include "gtest/internal/gtest-internal.h"
|
|
|
#include "gtest/internal/gtest-string.h"
|
|
|
-
|
|
|
-// Indicates that this translation unit is part of Google Test's
|
|
|
-// implementation. It must come before gtest-internal-inl.h is
|
|
|
-// included, or there will be a compiler error. This trick exists to
|
|
|
-// prevent the accidental inclusion of gtest-internal-inl.h in the
|
|
|
-// user's code.
|
|
|
-#define GTEST_IMPLEMENTATION_ 1
|
|
|
#include "src/gtest-internal-inl.h"
|
|
|
-#undef GTEST_IMPLEMENTATION_
|
|
|
|
|
|
namespace testing {
|
|
|
namespace internal {
|
|
@@ -93,7 +102,7 @@ const int kStdErrFileno = STDERR_FILENO;
|
|
|
|
|
|
namespace {
|
|
|
template <typename T>
|
|
|
-T ReadProcFileField(const string& filename, int field) {
|
|
|
+T ReadProcFileField(const std::string& filename, int field) {
|
|
|
std::string dummy;
|
|
|
std::ifstream file(filename.c_str());
|
|
|
while (field-- > 0) {
|
|
@@ -107,9 +116,9 @@ T ReadProcFileField(const string& filename, int field) {
|
|
|
|
|
|
// Returns the number of active threads, or 0 when there is an error.
|
|
|
size_t GetThreadCount() {
|
|
|
- const string filename =
|
|
|
+ const std::string filename =
|
|
|
(Message() << "/proc/" << getpid() << "/stat").GetString();
|
|
|
- return ReadProcFileField<int>(filename, 19);
|
|
|
+ return ReadProcFileField<size_t>(filename, 19);
|
|
|
}
|
|
|
|
|
|
#elif GTEST_OS_MAC
|
|
@@ -131,6 +140,82 @@ size_t GetThreadCount() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+#elif GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \
|
|
|
+ GTEST_OS_NETBSD
|
|
|
+
|
|
|
+#if GTEST_OS_NETBSD
|
|
|
+#undef KERN_PROC
|
|
|
+#define KERN_PROC KERN_PROC2
|
|
|
+#define kinfo_proc kinfo_proc2
|
|
|
+#endif
|
|
|
+
|
|
|
+#if GTEST_OS_DRAGONFLY
|
|
|
+#define KP_NLWP(kp) (kp.kp_nthreads)
|
|
|
+#elif GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD
|
|
|
+#define KP_NLWP(kp) (kp.ki_numthreads)
|
|
|
+#elif GTEST_OS_NETBSD
|
|
|
+#define KP_NLWP(kp) (kp.p_nlwps)
|
|
|
+#endif
|
|
|
+
|
|
|
+// Returns the number of threads running in the process, or 0 to indicate that
|
|
|
+// we cannot detect it.
|
|
|
+size_t GetThreadCount() {
|
|
|
+ int mib[] = {
|
|
|
+ CTL_KERN,
|
|
|
+ KERN_PROC,
|
|
|
+ KERN_PROC_PID,
|
|
|
+ getpid(),
|
|
|
+#if GTEST_OS_NETBSD
|
|
|
+ sizeof(struct kinfo_proc),
|
|
|
+ 1,
|
|
|
+#endif
|
|
|
+ };
|
|
|
+ u_int miblen = sizeof(mib) / sizeof(mib[0]);
|
|
|
+ struct kinfo_proc info;
|
|
|
+ size_t size = sizeof(info);
|
|
|
+ if (sysctl(mib, miblen, &info, &size, NULL, 0)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return static_cast<size_t>(KP_NLWP(info));
|
|
|
+}
|
|
|
+#elif GTEST_OS_OPENBSD
|
|
|
+
|
|
|
+// Returns the number of threads running in the process, or 0 to indicate that
|
|
|
+// we cannot detect it.
|
|
|
+size_t GetThreadCount() {
|
|
|
+ int mib[] = {
|
|
|
+ CTL_KERN,
|
|
|
+ KERN_PROC,
|
|
|
+ KERN_PROC_PID | KERN_PROC_SHOW_THREADS,
|
|
|
+ getpid(),
|
|
|
+ sizeof(struct kinfo_proc),
|
|
|
+ 0,
|
|
|
+ };
|
|
|
+ u_int miblen = sizeof(mib) / sizeof(mib[0]);
|
|
|
+
|
|
|
+ // get number of structs
|
|
|
+ size_t size;
|
|
|
+ if (sysctl(mib, miblen, NULL, &size, NULL, 0)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ mib[5] = static_cast<int>(size / static_cast<size_t>(mib[4]));
|
|
|
+
|
|
|
+ // populate array of structs
|
|
|
+ struct kinfo_proc info[mib[5]];
|
|
|
+ if (sysctl(mib, miblen, &info, &size, NULL, 0)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // exclude empty members
|
|
|
+ size_t nthreads = 0;
|
|
|
+ for (size_t i = 0; i < size / static_cast<size_t>(mib[4]); i++) {
|
|
|
+ if (info[i].p_tid != -1)
|
|
|
+ nthreads++;
|
|
|
+ }
|
|
|
+ return nthreads;
|
|
|
+}
|
|
|
+
|
|
|
#elif GTEST_OS_QNX
|
|
|
|
|
|
// Returns the number of threads running in the process, or 0 to indicate that
|
|
@@ -142,7 +227,7 @@ size_t GetThreadCount() {
|
|
|
}
|
|
|
procfs_info process_info;
|
|
|
const int status =
|
|
|
- devctl(fd, DCMD_PROC_INFO, &process_info, sizeof(process_info), NULL);
|
|
|
+ devctl(fd, DCMD_PROC_INFO, &process_info, sizeof(process_info), nullptr);
|
|
|
close(fd);
|
|
|
if (status == EOK) {
|
|
|
return static_cast<size_t>(process_info.num_threads);
|
|
@@ -156,7 +241,7 @@ size_t GetThreadCount() {
|
|
|
size_t GetThreadCount() {
|
|
|
struct procentry64 entry;
|
|
|
pid_t pid = getpid();
|
|
|
- int status = getprocs64(&entry, sizeof(entry), NULL, 0, &pid, 1);
|
|
|
+ int status = getprocs64(&entry, sizeof(entry), nullptr, 0, &pid, 1);
|
|
|
if (status == 1) {
|
|
|
return entry.pi_thcount;
|
|
|
} else {
|
|
@@ -164,6 +249,25 @@ size_t GetThreadCount() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+#elif GTEST_OS_FUCHSIA
|
|
|
+
|
|
|
+size_t GetThreadCount() {
|
|
|
+ int dummy_buffer;
|
|
|
+ size_t avail;
|
|
|
+ zx_status_t status = zx_object_get_info(
|
|
|
+ zx_process_self(),
|
|
|
+ ZX_INFO_PROCESS_THREADS,
|
|
|
+ &dummy_buffer,
|
|
|
+ 0,
|
|
|
+ nullptr,
|
|
|
+ &avail);
|
|
|
+ if (status == ZX_OK) {
|
|
|
+ return avail;
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
#else
|
|
|
|
|
|
size_t GetThreadCount() {
|
|
@@ -177,7 +281,7 @@ size_t GetThreadCount() {
|
|
|
#if GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS
|
|
|
|
|
|
void SleepMilliseconds(int n) {
|
|
|
- ::Sleep(n);
|
|
|
+ ::Sleep(static_cast<DWORD>(n));
|
|
|
}
|
|
|
|
|
|
AutoHandle::AutoHandle()
|
|
@@ -215,15 +319,15 @@ void AutoHandle::Reset(HANDLE handle) {
|
|
|
bool AutoHandle::IsCloseable() const {
|
|
|
// Different Windows APIs may use either of these values to represent an
|
|
|
// invalid handle.
|
|
|
- return handle_ != NULL && handle_ != INVALID_HANDLE_VALUE;
|
|
|
+ return handle_ != nullptr && handle_ != INVALID_HANDLE_VALUE;
|
|
|
}
|
|
|
|
|
|
Notification::Notification()
|
|
|
- : event_(::CreateEvent(NULL, // Default security attributes.
|
|
|
- TRUE, // Do not reset automatically.
|
|
|
- FALSE, // Initially unset.
|
|
|
- NULL)) { // Anonymous event.
|
|
|
- GTEST_CHECK_(event_.Get() != NULL);
|
|
|
+ : event_(::CreateEvent(nullptr, // Default security attributes.
|
|
|
+ TRUE, // Do not reset automatically.
|
|
|
+ FALSE, // Initially unset.
|
|
|
+ nullptr)) { // Anonymous event.
|
|
|
+ GTEST_CHECK_(event_.Get() != nullptr);
|
|
|
}
|
|
|
|
|
|
void Notification::Notify() {
|
|
@@ -246,13 +350,10 @@ Mutex::Mutex()
|
|
|
Mutex::~Mutex() {
|
|
|
// Static mutexes are leaked intentionally. It is not thread-safe to try
|
|
|
// to clean them up.
|
|
|
- // TODO(yukawa): Switch to Slim Reader/Writer (SRW) Locks, which requires
|
|
|
- // nothing to clean it up but is available only on Vista and later.
|
|
|
- // http://msdn.microsoft.com/en-us/library/windows/desktop/aa904937.aspx
|
|
|
if (type_ == kDynamic) {
|
|
|
::DeleteCriticalSection(critical_section_);
|
|
|
delete critical_section_;
|
|
|
- critical_section_ = NULL;
|
|
|
+ critical_section_ = nullptr;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -279,6 +380,41 @@ void Mutex::AssertHeld() {
|
|
|
<< "The current thread is not holding the mutex @" << this;
|
|
|
}
|
|
|
|
|
|
+namespace {
|
|
|
+
|
|
|
+#ifdef _MSC_VER
|
|
|
+// Use the RAII idiom to flag mem allocs that are intentionally never
|
|
|
+// deallocated. The motivation is to silence the false positive mem leaks
|
|
|
+// that are reported by the debug version of MS's CRT which can only detect
|
|
|
+// if an alloc is missing a matching deallocation.
|
|
|
+// Example:
|
|
|
+// MemoryIsNotDeallocated memory_is_not_deallocated;
|
|
|
+// critical_section_ = new CRITICAL_SECTION;
|
|
|
+//
|
|
|
+class MemoryIsNotDeallocated
|
|
|
+{
|
|
|
+ public:
|
|
|
+ MemoryIsNotDeallocated() : old_crtdbg_flag_(0) {
|
|
|
+ old_crtdbg_flag_ = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
|
|
|
+ // Set heap allocation block type to _IGNORE_BLOCK so that MS debug CRT
|
|
|
+ // doesn't report mem leak if there's no matching deallocation.
|
|
|
+ _CrtSetDbgFlag(old_crtdbg_flag_ & ~_CRTDBG_ALLOC_MEM_DF);
|
|
|
+ }
|
|
|
+
|
|
|
+ ~MemoryIsNotDeallocated() {
|
|
|
+ // Restore the original _CRTDBG_ALLOC_MEM_DF flag
|
|
|
+ _CrtSetDbgFlag(old_crtdbg_flag_);
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+ int old_crtdbg_flag_;
|
|
|
+
|
|
|
+ GTEST_DISALLOW_COPY_AND_ASSIGN_(MemoryIsNotDeallocated);
|
|
|
+};
|
|
|
+#endif // _MSC_VER
|
|
|
+
|
|
|
+} // namespace
|
|
|
+
|
|
|
// Initializes owner_thread_id_ and critical_section_ in static mutexes.
|
|
|
void Mutex::ThreadSafeLazyInit() {
|
|
|
// Dynamic mutexes are initialized in the constructor.
|
|
@@ -289,7 +425,13 @@ void Mutex::ThreadSafeLazyInit() {
|
|
|
// If critical_section_init_phase_ was 0 before the exchange, we
|
|
|
// are the first to test it and need to perform the initialization.
|
|
|
owner_thread_id_ = 0;
|
|
|
- critical_section_ = new CRITICAL_SECTION;
|
|
|
+ {
|
|
|
+ // Use RAII to flag that following mem alloc is never deallocated.
|
|
|
+#ifdef _MSC_VER
|
|
|
+ MemoryIsNotDeallocated memory_is_not_deallocated;
|
|
|
+#endif // _MSC_VER
|
|
|
+ critical_section_ = new CRITICAL_SECTION;
|
|
|
+ }
|
|
|
::InitializeCriticalSection(critical_section_);
|
|
|
// Updates the critical_section_init_phase_ to 2 to signal
|
|
|
// initialization complete.
|
|
@@ -328,17 +470,16 @@ class ThreadWithParamSupport : public ThreadWithParamBase {
|
|
|
Notification* thread_can_start) {
|
|
|
ThreadMainParam* param = new ThreadMainParam(runnable, thread_can_start);
|
|
|
DWORD thread_id;
|
|
|
- // TODO(yukawa): Consider to use _beginthreadex instead.
|
|
|
HANDLE thread_handle = ::CreateThread(
|
|
|
- NULL, // Default security.
|
|
|
- 0, // Default stack size.
|
|
|
+ nullptr, // Default security.
|
|
|
+ 0, // Default stack size.
|
|
|
&ThreadWithParamSupport::ThreadMain,
|
|
|
- param, // Parameter to ThreadMainStatic
|
|
|
- 0x0, // Default creation flags.
|
|
|
+ param, // Parameter to ThreadMainStatic
|
|
|
+ 0x0, // Default creation flags.
|
|
|
&thread_id); // Need a valid pointer for the call to work under Win98.
|
|
|
- GTEST_CHECK_(thread_handle != NULL) << "CreateThread failed with error "
|
|
|
- << ::GetLastError() << ".";
|
|
|
- if (thread_handle == NULL) {
|
|
|
+ GTEST_CHECK_(thread_handle != nullptr)
|
|
|
+ << "CreateThread failed with error " << ::GetLastError() << ".";
|
|
|
+ if (thread_handle == nullptr) {
|
|
|
delete param;
|
|
|
}
|
|
|
return thread_handle;
|
|
@@ -350,15 +491,15 @@ class ThreadWithParamSupport : public ThreadWithParamBase {
|
|
|
: runnable_(runnable),
|
|
|
thread_can_start_(thread_can_start) {
|
|
|
}
|
|
|
- scoped_ptr<Runnable> runnable_;
|
|
|
+ std::unique_ptr<Runnable> runnable_;
|
|
|
// Does not own.
|
|
|
Notification* thread_can_start_;
|
|
|
};
|
|
|
|
|
|
static DWORD WINAPI ThreadMain(void* ptr) {
|
|
|
// Transfers ownership.
|
|
|
- scoped_ptr<ThreadMainParam> param(static_cast<ThreadMainParam*>(ptr));
|
|
|
- if (param->thread_can_start_ != NULL)
|
|
|
+ std::unique_ptr<ThreadMainParam> param(static_cast<ThreadMainParam*>(ptr));
|
|
|
+ if (param->thread_can_start_ != nullptr)
|
|
|
param->thread_can_start_->WaitForNotification();
|
|
|
param->runnable_->Run();
|
|
|
return 0;
|
|
@@ -397,6 +538,9 @@ class ThreadLocalRegistryImpl {
|
|
|
// Returns a value that can be used to identify the thread from other threads.
|
|
|
static ThreadLocalValueHolderBase* GetValueOnCurrentThread(
|
|
|
const ThreadLocalBase* thread_local_instance) {
|
|
|
+#ifdef _MSC_VER
|
|
|
+ MemoryIsNotDeallocated memory_is_not_deallocated;
|
|
|
+#endif // _MSC_VER
|
|
|
DWORD current_thread = ::GetCurrentThreadId();
|
|
|
MutexLock lock(&mutex_);
|
|
|
ThreadIdToThreadLocals* const thread_to_thread_locals =
|
|
@@ -416,7 +560,7 @@ class ThreadLocalRegistryImpl {
|
|
|
thread_local_values
|
|
|
.insert(std::make_pair(
|
|
|
thread_local_instance,
|
|
|
- linked_ptr<ThreadLocalValueHolderBase>(
|
|
|
+ std::shared_ptr<ThreadLocalValueHolderBase>(
|
|
|
thread_local_instance->NewValueForCurrentThread())))
|
|
|
.first;
|
|
|
}
|
|
@@ -425,7 +569,7 @@ class ThreadLocalRegistryImpl {
|
|
|
|
|
|
static void OnThreadLocalDestroyed(
|
|
|
const ThreadLocalBase* thread_local_instance) {
|
|
|
- std::vector<linked_ptr<ThreadLocalValueHolderBase> > value_holders;
|
|
|
+ std::vector<std::shared_ptr<ThreadLocalValueHolderBase> > value_holders;
|
|
|
// Clean up the ThreadLocalValues data structure while holding the lock, but
|
|
|
// defer the destruction of the ThreadLocalValueHolderBases.
|
|
|
{
|
|
@@ -453,7 +597,7 @@ class ThreadLocalRegistryImpl {
|
|
|
|
|
|
static void OnThreadExit(DWORD thread_id) {
|
|
|
GTEST_CHECK_(thread_id != 0) << ::GetLastError();
|
|
|
- std::vector<linked_ptr<ThreadLocalValueHolderBase> > value_holders;
|
|
|
+ std::vector<std::shared_ptr<ThreadLocalValueHolderBase> > value_holders;
|
|
|
// Clean up the ThreadIdToThreadLocals data structure while holding the
|
|
|
// lock, but defer the destruction of the ThreadLocalValueHolderBases.
|
|
|
{
|
|
@@ -480,7 +624,8 @@ class ThreadLocalRegistryImpl {
|
|
|
private:
|
|
|
// In a particular thread, maps a ThreadLocal object to its value.
|
|
|
typedef std::map<const ThreadLocalBase*,
|
|
|
- linked_ptr<ThreadLocalValueHolderBase> > ThreadLocalValues;
|
|
|
+ std::shared_ptr<ThreadLocalValueHolderBase> >
|
|
|
+ ThreadLocalValues;
|
|
|
// Stores all ThreadIdToThreadLocals having values in a thread, indexed by
|
|
|
// thread's ID.
|
|
|
typedef std::map<DWORD, ThreadLocalValues> ThreadIdToThreadLocals;
|
|
@@ -495,18 +640,17 @@ class ThreadLocalRegistryImpl {
|
|
|
HANDLE thread = ::OpenThread(SYNCHRONIZE | THREAD_QUERY_INFORMATION,
|
|
|
FALSE,
|
|
|
thread_id);
|
|
|
- GTEST_CHECK_(thread != NULL);
|
|
|
- // We need to to pass a valid thread ID pointer into CreateThread for it
|
|
|
+ GTEST_CHECK_(thread != nullptr);
|
|
|
+ // We need to pass a valid thread ID pointer into CreateThread for it
|
|
|
// to work correctly under Win98.
|
|
|
DWORD watcher_thread_id;
|
|
|
HANDLE watcher_thread = ::CreateThread(
|
|
|
- NULL, // Default security.
|
|
|
- 0, // Default stack size
|
|
|
+ nullptr, // Default security.
|
|
|
+ 0, // Default stack size
|
|
|
&ThreadLocalRegistryImpl::WatcherThreadFunc,
|
|
|
reinterpret_cast<LPVOID>(new ThreadIdAndHandle(thread_id, thread)),
|
|
|
- CREATE_SUSPENDED,
|
|
|
- &watcher_thread_id);
|
|
|
- GTEST_CHECK_(watcher_thread != NULL);
|
|
|
+ CREATE_SUSPENDED, &watcher_thread_id);
|
|
|
+ GTEST_CHECK_(watcher_thread != nullptr);
|
|
|
// Give the watcher thread the same priority as ours to avoid being
|
|
|
// blocked by it.
|
|
|
::SetThreadPriority(watcher_thread,
|
|
@@ -531,7 +675,10 @@ class ThreadLocalRegistryImpl {
|
|
|
// Returns map of thread local instances.
|
|
|
static ThreadIdToThreadLocals* GetThreadLocalsMapLocked() {
|
|
|
mutex_.AssertHeld();
|
|
|
- static ThreadIdToThreadLocals* map = new ThreadIdToThreadLocals;
|
|
|
+#ifdef _MSC_VER
|
|
|
+ MemoryIsNotDeallocated memory_is_not_deallocated;
|
|
|
+#endif // _MSC_VER
|
|
|
+ static ThreadIdToThreadLocals* map = new ThreadIdToThreadLocals();
|
|
|
return map;
|
|
|
}
|
|
|
|
|
@@ -541,8 +688,8 @@ class ThreadLocalRegistryImpl {
|
|
|
static Mutex thread_map_mutex_;
|
|
|
};
|
|
|
|
|
|
-Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex);
|
|
|
-Mutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex);
|
|
|
+Mutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex); // NOLINT
|
|
|
+Mutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex); // NOLINT
|
|
|
|
|
|
ThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread(
|
|
|
const ThreadLocalBase* thread_local_instance) {
|
|
@@ -573,7 +720,7 @@ RE::~RE() {
|
|
|
free(const_cast<char*>(pattern_));
|
|
|
}
|
|
|
|
|
|
-// Returns true iff regular expression re matches the entire str.
|
|
|
+// Returns true if and only if regular expression re matches the entire str.
|
|
|
bool RE::FullMatch(const char* str, const RE& re) {
|
|
|
if (!re.is_valid_) return false;
|
|
|
|
|
@@ -581,8 +728,8 @@ bool RE::FullMatch(const char* str, const RE& re) {
|
|
|
return regexec(&re.full_regex_, str, 1, &match, 0) == 0;
|
|
|
}
|
|
|
|
|
|
-// Returns true iff regular expression re matches a substring of str
|
|
|
-// (including str itself).
|
|
|
+// Returns true if and only if regular expression re matches a substring of
|
|
|
+// str (including str itself).
|
|
|
bool RE::PartialMatch(const char* str, const RE& re) {
|
|
|
if (!re.is_valid_) return false;
|
|
|
|
|
@@ -622,14 +769,14 @@ void RE::Init(const char* regex) {
|
|
|
|
|
|
#elif GTEST_USES_SIMPLE_RE
|
|
|
|
|
|
-// Returns true iff ch appears anywhere in str (excluding the
|
|
|
+// Returns true if and only if ch appears anywhere in str (excluding the
|
|
|
// terminating '\0' character).
|
|
|
bool IsInSet(char ch, const char* str) {
|
|
|
- return ch != '\0' && strchr(str, ch) != NULL;
|
|
|
+ return ch != '\0' && strchr(str, ch) != nullptr;
|
|
|
}
|
|
|
|
|
|
-// Returns true iff ch belongs to the given classification. Unlike
|
|
|
-// similar functions in <ctype.h>, these aren't affected by the
|
|
|
+// Returns true if and only if ch belongs to the given classification.
|
|
|
+// Unlike similar functions in <ctype.h>, these aren't affected by the
|
|
|
// current locale.
|
|
|
bool IsAsciiDigit(char ch) { return '0' <= ch && ch <= '9'; }
|
|
|
bool IsAsciiPunct(char ch) {
|
|
@@ -642,13 +789,13 @@ bool IsAsciiWordChar(char ch) {
|
|
|
('0' <= ch && ch <= '9') || ch == '_';
|
|
|
}
|
|
|
|
|
|
-// Returns true iff "\\c" is a supported escape sequence.
|
|
|
+// Returns true if and only if "\\c" is a supported escape sequence.
|
|
|
bool IsValidEscape(char c) {
|
|
|
return (IsAsciiPunct(c) || IsInSet(c, "dDfnrsStvwW"));
|
|
|
}
|
|
|
|
|
|
-// Returns true iff the given atom (specified by escaped and pattern)
|
|
|
-// matches ch. The result is undefined if the atom is invalid.
|
|
|
+// Returns true if and only if the given atom (specified by escaped and
|
|
|
+// pattern) matches ch. The result is undefined if the atom is invalid.
|
|
|
bool AtomMatchesChar(bool escaped, char pattern_char, char ch) {
|
|
|
if (escaped) { // "\\p" where p is pattern_char.
|
|
|
switch (pattern_char) {
|
|
@@ -671,7 +818,7 @@ bool AtomMatchesChar(bool escaped, char pattern_char, char ch) {
|
|
|
}
|
|
|
|
|
|
// Helper function used by ValidateRegex() to format error messages.
|
|
|
-std::string FormatRegexSyntaxError(const char* regex, int index) {
|
|
|
+static std::string FormatRegexSyntaxError(const char* regex, int index) {
|
|
|
return (Message() << "Syntax error at index " << index
|
|
|
<< " in simple regular expression \"" << regex << "\": ").GetString();
|
|
|
}
|
|
@@ -679,17 +826,14 @@ std::string FormatRegexSyntaxError(const char* regex, int index) {
|
|
|
// Generates non-fatal failures and returns false if regex is invalid;
|
|
|
// otherwise returns true.
|
|
|
bool ValidateRegex(const char* regex) {
|
|
|
- if (regex == NULL) {
|
|
|
- // TODO([email protected]): fix the source file location in the
|
|
|
- // assertion failures to match where the regex is used in user
|
|
|
- // code.
|
|
|
+ if (regex == nullptr) {
|
|
|
ADD_FAILURE() << "NULL is not a valid simple regular expression.";
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
bool is_valid = true;
|
|
|
|
|
|
- // True iff ?, *, or + can follow the previous atom.
|
|
|
+ // True if and only if ?, *, or + can follow the previous atom.
|
|
|
bool prev_repeatable = false;
|
|
|
for (int i = 0; regex[i]; i++) {
|
|
|
if (regex[i] == '\\') { // An escape sequence
|
|
@@ -765,8 +909,8 @@ bool MatchRepetitionAndRegexAtHead(
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-// Returns true iff regex matches a prefix of str. regex must be a
|
|
|
-// valid simple regular expression and not start with "^", or the
|
|
|
+// Returns true if and only if regex matches a prefix of str. regex must
|
|
|
+// be a valid simple regular expression and not start with "^", or the
|
|
|
// result is undefined.
|
|
|
bool MatchRegexAtHead(const char* regex, const char* str) {
|
|
|
if (*regex == '\0') // An empty regex matches a prefix of anything.
|
|
@@ -796,8 +940,8 @@ bool MatchRegexAtHead(const char* regex, const char* str) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// Returns true iff regex matches any substring of str. regex must be
|
|
|
-// a valid simple regular expression, or the result is undefined.
|
|
|
+// Returns true if and only if regex matches any substring of str. regex must
|
|
|
+// be a valid simple regular expression, or the result is undefined.
|
|
|
//
|
|
|
// The algorithm is recursive, but the recursion depth doesn't exceed
|
|
|
// the regex length, so we won't need to worry about running out of
|
|
@@ -805,8 +949,7 @@ bool MatchRegexAtHead(const char* regex, const char* str) {
|
|
|
// exponential with respect to the regex length + the string length,
|
|
|
// but usually it's must faster (often close to linear).
|
|
|
bool MatchRegexAnywhere(const char* regex, const char* str) {
|
|
|
- if (regex == NULL || str == NULL)
|
|
|
- return false;
|
|
|
+ if (regex == nullptr || str == nullptr) return false;
|
|
|
|
|
|
if (*regex == '^')
|
|
|
return MatchRegexAtHead(regex + 1, str);
|
|
@@ -826,21 +969,21 @@ RE::~RE() {
|
|
|
free(const_cast<char*>(full_pattern_));
|
|
|
}
|
|
|
|
|
|
-// Returns true iff regular expression re matches the entire str.
|
|
|
+// Returns true if and only if regular expression re matches the entire str.
|
|
|
bool RE::FullMatch(const char* str, const RE& re) {
|
|
|
return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str);
|
|
|
}
|
|
|
|
|
|
-// Returns true iff regular expression re matches a substring of str
|
|
|
-// (including str itself).
|
|
|
+// Returns true if and only if regular expression re matches a substring of
|
|
|
+// str (including str itself).
|
|
|
bool RE::PartialMatch(const char* str, const RE& re) {
|
|
|
return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str);
|
|
|
}
|
|
|
|
|
|
// Initializes an RE from its string representation.
|
|
|
void RE::Init(const char* regex) {
|
|
|
- pattern_ = full_pattern_ = NULL;
|
|
|
- if (regex != NULL) {
|
|
|
+ pattern_ = full_pattern_ = nullptr;
|
|
|
+ if (regex != nullptr) {
|
|
|
pattern_ = posix::StrDup(regex);
|
|
|
}
|
|
|
|
|
@@ -878,7 +1021,7 @@ const char kUnknownFile[] = "unknown file";
|
|
|
// Formats a source file path and a line number as they would appear
|
|
|
// in an error message from the compiler used to compile this code.
|
|
|
GTEST_API_ ::std::string FormatFileLocation(const char* file, int line) {
|
|
|
- const std::string file_name(file == NULL ? kUnknownFile : file);
|
|
|
+ const std::string file_name(file == nullptr ? kUnknownFile : file);
|
|
|
|
|
|
if (line < 0) {
|
|
|
return file_name + ":";
|
|
@@ -897,7 +1040,7 @@ GTEST_API_ ::std::string FormatFileLocation(const char* file, int line) {
|
|
|
// to the file location it produces, unlike FormatFileLocation().
|
|
|
GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(
|
|
|
const char* file, int line) {
|
|
|
- const std::string file_name(file == NULL ? kUnknownFile : file);
|
|
|
+ const std::string file_name(file == nullptr ? kUnknownFile : file);
|
|
|
|
|
|
if (line < 0)
|
|
|
return file_name;
|
|
@@ -923,10 +1066,11 @@ GTestLog::~GTestLog() {
|
|
|
posix::Abort();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
// Disable Microsoft deprecation warnings for POSIX functions called from
|
|
|
// this class (creat, dup, dup2, and close)
|
|
|
-GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996)
|
|
|
-GTEST_DISABLE_CLANG_DEPRECATED_WARNINGS_PUSH_()
|
|
|
+GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
|
|
|
+
|
|
|
#if GTEST_HAS_STREAM_REDIRECTION
|
|
|
|
|
|
// Object that captures an output stream (stdout/stderr).
|
|
@@ -951,9 +1095,9 @@ class CapturedStream {
|
|
|
filename_ = temp_file_path;
|
|
|
# else
|
|
|
// There's no guarantee that a test has write access to the current
|
|
|
- // directory, so we create the temporary file in the /tmp directory
|
|
|
- // instead. We use /tmp on most systems, and /sdcard on Android.
|
|
|
- // That's because Android doesn't have /tmp.
|
|
|
+ // directory, so we create the temporary file in a temporary directory.
|
|
|
+ std::string name_template;
|
|
|
+
|
|
|
# if GTEST_OS_LINUX_ANDROID
|
|
|
// Note: Android applications are expected to call the framework's
|
|
|
// Context.getExternalStorageDirectory() method through JNI to get
|
|
@@ -963,20 +1107,51 @@ class CapturedStream {
|
|
|
// code as part of a regular standalone executable, which doesn't
|
|
|
// run in a Dalvik process (e.g. when running it through 'adb shell').
|
|
|
//
|
|
|
- // The location /sdcard is directly accessible from native code
|
|
|
- // and is the only location (unofficially) supported by the Android
|
|
|
- // team. It's generally a symlink to the real SD Card mount point
|
|
|
- // which can be /mnt/sdcard, /mnt/sdcard0, /system/media/sdcard, or
|
|
|
- // other OEM-customized locations. Never rely on these, and always
|
|
|
- // use /sdcard.
|
|
|
- char name_template[] = "/sdcard/gtest_captured_stream.XXXXXX";
|
|
|
+ // The location /data/local/tmp is directly accessible from native code.
|
|
|
+ // '/sdcard' and other variants cannot be relied on, as they are not
|
|
|
+ // guaranteed to be mounted, or may have a delay in mounting.
|
|
|
+ name_template = "/data/local/tmp/";
|
|
|
+# elif GTEST_OS_IOS
|
|
|
+ char user_temp_dir[PATH_MAX + 1];
|
|
|
+
|
|
|
+ // Documented alternative to NSTemporaryDirectory() (for obtaining creating
|
|
|
+ // a temporary directory) at
|
|
|
+ // https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/RaceConditions.html#//apple_ref/doc/uid/TP40002585-SW10
|
|
|
+ //
|
|
|
+ // _CS_DARWIN_USER_TEMP_DIR (as well as _CS_DARWIN_USER_CACHE_DIR) is not
|
|
|
+ // documented in the confstr() man page at
|
|
|
+ // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/confstr.3.html#//apple_ref/doc/man/3/confstr
|
|
|
+ // but are still available, according to the WebKit patches at
|
|
|
+ // https://trac.webkit.org/changeset/262004/webkit
|
|
|
+ // https://trac.webkit.org/changeset/263705/webkit
|
|
|
+ //
|
|
|
+ // The confstr() implementation falls back to getenv("TMPDIR"). See
|
|
|
+ // https://opensource.apple.com/source/Libc/Libc-1439.100.3/gen/confstr.c.auto.html
|
|
|
+ ::confstr(_CS_DARWIN_USER_TEMP_DIR, user_temp_dir, sizeof(user_temp_dir));
|
|
|
+
|
|
|
+ name_template = user_temp_dir;
|
|
|
+ if (name_template.back() != GTEST_PATH_SEP_[0])
|
|
|
+ name_template.push_back(GTEST_PATH_SEP_[0]);
|
|
|
# else
|
|
|
- char name_template[] = "/tmp/captured_stream.XXXXXX";
|
|
|
-# endif // GTEST_OS_LINUX_ANDROID
|
|
|
- const int captured_fd = mkstemp(name_template);
|
|
|
- filename_ = name_template;
|
|
|
+ name_template = "/tmp/";
|
|
|
+# endif
|
|
|
+ name_template.append("gtest_captured_stream.XXXXXX");
|
|
|
+
|
|
|
+ // mkstemp() modifies the string bytes in place, and does not go beyond the
|
|
|
+ // string's length. This results in well-defined behavior in C++17.
|
|
|
+ //
|
|
|
+ // The const_cast is needed below C++17. The constraints on std::string
|
|
|
+ // implementations in C++11 and above make assumption behind the const_cast
|
|
|
+ // fairly safe.
|
|
|
+ const int captured_fd = ::mkstemp(const_cast<char*>(name_template.data()));
|
|
|
+ if (captured_fd == -1) {
|
|
|
+ GTEST_LOG_(WARNING)
|
|
|
+ << "Failed to create tmp file " << name_template
|
|
|
+ << " for test; does the test have access to the /tmp directory?";
|
|
|
+ }
|
|
|
+ filename_ = std::move(name_template);
|
|
|
# endif // GTEST_OS_WINDOWS
|
|
|
- fflush(NULL);
|
|
|
+ fflush(nullptr);
|
|
|
dup2(captured_fd, fd_);
|
|
|
close(captured_fd);
|
|
|
}
|
|
@@ -988,13 +1163,17 @@ class CapturedStream {
|
|
|
std::string GetCapturedString() {
|
|
|
if (uncaptured_fd_ != -1) {
|
|
|
// Restores the original stream.
|
|
|
- fflush(NULL);
|
|
|
+ fflush(nullptr);
|
|
|
dup2(uncaptured_fd_, fd_);
|
|
|
close(uncaptured_fd_);
|
|
|
uncaptured_fd_ = -1;
|
|
|
}
|
|
|
|
|
|
FILE* const file = posix::FOpen(filename_.c_str(), "r");
|
|
|
+ if (file == nullptr) {
|
|
|
+ GTEST_LOG_(FATAL) << "Failed to open tmp file " << filename_
|
|
|
+ << " for capturing stream.";
|
|
|
+ }
|
|
|
const std::string content = ReadEntireFile(file);
|
|
|
posix::FClose(file);
|
|
|
return content;
|
|
@@ -1009,15 +1188,15 @@ class CapturedStream {
|
|
|
GTEST_DISALLOW_COPY_AND_ASSIGN_(CapturedStream);
|
|
|
};
|
|
|
|
|
|
-GTEST_DISABLE_MSC_WARNINGS_POP_()
|
|
|
-GTEST_DISABLE_CLANG_WARNINGS_POP_()
|
|
|
+GTEST_DISABLE_MSC_DEPRECATED_POP_()
|
|
|
|
|
|
-static CapturedStream* g_captured_stderr = NULL;
|
|
|
-static CapturedStream* g_captured_stdout = NULL;
|
|
|
+static CapturedStream* g_captured_stderr = nullptr;
|
|
|
+static CapturedStream* g_captured_stdout = nullptr;
|
|
|
|
|
|
// Starts capturing an output stream (stdout/stderr).
|
|
|
-void CaptureStream(int fd, const char* stream_name, CapturedStream** stream) {
|
|
|
- if (*stream != NULL) {
|
|
|
+static void CaptureStream(int fd, const char* stream_name,
|
|
|
+ CapturedStream** stream) {
|
|
|
+ if (*stream != nullptr) {
|
|
|
GTEST_LOG_(FATAL) << "Only one " << stream_name
|
|
|
<< " capturer can exist at a time.";
|
|
|
}
|
|
@@ -1025,11 +1204,11 @@ void CaptureStream(int fd, const char* stream_name, CapturedStream** stream) {
|
|
|
}
|
|
|
|
|
|
// Stops capturing the output stream and returns the captured string.
|
|
|
-std::string GetCapturedStream(CapturedStream** captured_stream) {
|
|
|
+static std::string GetCapturedStream(CapturedStream** captured_stream) {
|
|
|
const std::string content = (*captured_stream)->GetCapturedString();
|
|
|
|
|
|
delete *captured_stream;
|
|
|
- *captured_stream = NULL;
|
|
|
+ *captured_stream = nullptr;
|
|
|
|
|
|
return content;
|
|
|
}
|
|
@@ -1056,23 +1235,9 @@ std::string GetCapturedStderr() {
|
|
|
|
|
|
#endif // GTEST_HAS_STREAM_REDIRECTION
|
|
|
|
|
|
-std::string TempDir() {
|
|
|
-#if GTEST_OS_WINDOWS_MOBILE
|
|
|
- return "\\temp\\";
|
|
|
-#elif GTEST_OS_WINDOWS
|
|
|
- const char* temp_dir = posix::GetEnv("TEMP");
|
|
|
- if (temp_dir == NULL || temp_dir[0] == '\0')
|
|
|
- return "\\temp\\";
|
|
|
- else if (temp_dir[strlen(temp_dir) - 1] == '\\')
|
|
|
- return temp_dir;
|
|
|
- else
|
|
|
- return std::string(temp_dir) + "\\";
|
|
|
-#elif GTEST_OS_LINUX_ANDROID
|
|
|
- return "/sdcard/";
|
|
|
-#else
|
|
|
- return "/tmp/";
|
|
|
-#endif // GTEST_OS_WINDOWS_MOBILE
|
|
|
-}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
size_t GetFileSize(FILE* file) {
|
|
|
fseek(file, 0, SEEK_END);
|
|
@@ -1102,22 +1267,30 @@ std::string ReadEntireFile(FILE* file) {
|
|
|
}
|
|
|
|
|
|
#if GTEST_HAS_DEATH_TEST
|
|
|
+static const std::vector<std::string>* g_injected_test_argvs =
|
|
|
+ nullptr; // Owned.
|
|
|
|
|
|
-static const ::std::vector<testing::internal::string>* g_injected_test_argvs =
|
|
|
- NULL; // Owned.
|
|
|
-
|
|
|
-void SetInjectableArgvs(const ::std::vector<testing::internal::string>* argvs) {
|
|
|
- if (g_injected_test_argvs != argvs)
|
|
|
- delete g_injected_test_argvs;
|
|
|
- g_injected_test_argvs = argvs;
|
|
|
-}
|
|
|
-
|
|
|
-const ::std::vector<testing::internal::string>& GetInjectableArgvs() {
|
|
|
- if (g_injected_test_argvs != NULL) {
|
|
|
+std::vector<std::string> GetInjectableArgvs() {
|
|
|
+ if (g_injected_test_argvs != nullptr) {
|
|
|
return *g_injected_test_argvs;
|
|
|
}
|
|
|
return GetArgvs();
|
|
|
}
|
|
|
+
|
|
|
+void SetInjectableArgvs(const std::vector<std::string>* new_argvs) {
|
|
|
+ if (g_injected_test_argvs != new_argvs) delete g_injected_test_argvs;
|
|
|
+ g_injected_test_argvs = new_argvs;
|
|
|
+}
|
|
|
+
|
|
|
+void SetInjectableArgvs(const std::vector<std::string>& new_argvs) {
|
|
|
+ SetInjectableArgvs(
|
|
|
+ new std::vector<std::string>(new_argvs.begin(), new_argvs.end()));
|
|
|
+}
|
|
|
+
|
|
|
+void ClearInjectableArgvs() {
|
|
|
+ delete g_injected_test_argvs;
|
|
|
+ g_injected_test_argvs = nullptr;
|
|
|
+}
|
|
|
#endif // GTEST_HAS_DEATH_TEST
|
|
|
|
|
|
#if GTEST_OS_WINDOWS_MOBILE
|
|
@@ -1147,9 +1320,9 @@ static std::string FlagToEnvVar(const char* flag) {
|
|
|
// Parses 'str' for a 32-bit signed integer. If successful, writes
|
|
|
// the result to *value and returns true; otherwise leaves *value
|
|
|
// unchanged and returns false.
|
|
|
-bool ParseInt32(const Message& src_text, const char* str, Int32* value) {
|
|
|
+bool ParseInt32(const Message& src_text, const char* str, int32_t* value) {
|
|
|
// Parses the environment variable as a decimal integer.
|
|
|
- char* end = NULL;
|
|
|
+ char* end = nullptr;
|
|
|
const long long_value = strtol(str, &end, 10); // NOLINT
|
|
|
|
|
|
// Has strtol() consumed all characters in the string?
|
|
@@ -1164,13 +1337,13 @@ bool ParseInt32(const Message& src_text, const char* str, Int32* value) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- // Is the parsed value in the range of an Int32?
|
|
|
- const Int32 result = static_cast<Int32>(long_value);
|
|
|
+ // Is the parsed value in the range of an int32_t?
|
|
|
+ const auto result = static_cast<int32_t>(long_value);
|
|
|
if (long_value == LONG_MAX || long_value == LONG_MIN ||
|
|
|
// The parsed value overflows as a long. (strtol() returns
|
|
|
// LONG_MAX or LONG_MIN when the input overflows.)
|
|
|
result != long_value
|
|
|
- // The parsed value overflows as an Int32.
|
|
|
+ // The parsed value overflows as an int32_t.
|
|
|
) {
|
|
|
Message msg;
|
|
|
msg << "WARNING: " << src_text
|
|
@@ -1188,32 +1361,33 @@ bool ParseInt32(const Message& src_text, const char* str, Int32* value) {
|
|
|
// Reads and returns the Boolean environment variable corresponding to
|
|
|
// the given flag; if it's not set, returns default_value.
|
|
|
//
|
|
|
-// The value is considered true iff it's not "0".
|
|
|
+// The value is considered true if and only if it's not "0".
|
|
|
bool BoolFromGTestEnv(const char* flag, bool default_value) {
|
|
|
#if defined(GTEST_GET_BOOL_FROM_ENV_)
|
|
|
return GTEST_GET_BOOL_FROM_ENV_(flag, default_value);
|
|
|
-#endif // defined(GTEST_GET_BOOL_FROM_ENV_)
|
|
|
+#else
|
|
|
const std::string env_var = FlagToEnvVar(flag);
|
|
|
const char* const string_value = posix::GetEnv(env_var.c_str());
|
|
|
- return string_value == NULL ?
|
|
|
- default_value : strcmp(string_value, "0") != 0;
|
|
|
+ return string_value == nullptr ? default_value
|
|
|
+ : strcmp(string_value, "0") != 0;
|
|
|
+#endif // defined(GTEST_GET_BOOL_FROM_ENV_)
|
|
|
}
|
|
|
|
|
|
// Reads and returns a 32-bit integer stored in the environment
|
|
|
// variable corresponding to the given flag; if it isn't set or
|
|
|
// doesn't represent a valid 32-bit integer, returns default_value.
|
|
|
-Int32 Int32FromGTestEnv(const char* flag, Int32 default_value) {
|
|
|
+int32_t Int32FromGTestEnv(const char* flag, int32_t default_value) {
|
|
|
#if defined(GTEST_GET_INT32_FROM_ENV_)
|
|
|
return GTEST_GET_INT32_FROM_ENV_(flag, default_value);
|
|
|
-#endif // defined(GTEST_GET_INT32_FROM_ENV_)
|
|
|
+#else
|
|
|
const std::string env_var = FlagToEnvVar(flag);
|
|
|
const char* const string_value = posix::GetEnv(env_var.c_str());
|
|
|
- if (string_value == NULL) {
|
|
|
+ if (string_value == nullptr) {
|
|
|
// The environment variable is not set.
|
|
|
return default_value;
|
|
|
}
|
|
|
|
|
|
- Int32 result = default_value;
|
|
|
+ int32_t result = default_value;
|
|
|
if (!ParseInt32(Message() << "Environment variable " << env_var,
|
|
|
string_value, &result)) {
|
|
|
printf("The default value %s is used.\n",
|
|
@@ -1223,37 +1397,36 @@ Int32 Int32FromGTestEnv(const char* flag, Int32 default_value) {
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
+#endif // defined(GTEST_GET_INT32_FROM_ENV_)
|
|
|
+}
|
|
|
+
|
|
|
+// As a special case for the 'output' flag, if GTEST_OUTPUT is not
|
|
|
+// set, we look for XML_OUTPUT_FILE, which is set by the Bazel build
|
|
|
+// system. The value of XML_OUTPUT_FILE is a filename without the
|
|
|
+// "xml:" prefix of GTEST_OUTPUT.
|
|
|
+// Note that this is meant to be called at the call site so it does
|
|
|
+// not check that the flag is 'output'
|
|
|
+// In essence this checks an env variable called XML_OUTPUT_FILE
|
|
|
+// and if it is set we prepend "xml:" to its value, if it not set we return ""
|
|
|
+std::string OutputFlagAlsoCheckEnvVar(){
|
|
|
+ std::string default_value_for_output_flag = "";
|
|
|
+ const char* xml_output_file_env = posix::GetEnv("XML_OUTPUT_FILE");
|
|
|
+ if (nullptr != xml_output_file_env) {
|
|
|
+ default_value_for_output_flag = std::string("xml:") + xml_output_file_env;
|
|
|
+ }
|
|
|
+ return default_value_for_output_flag;
|
|
|
}
|
|
|
|
|
|
// Reads and returns the string environment variable corresponding to
|
|
|
// the given flag; if it's not set, returns default_value.
|
|
|
-std::string StringFromGTestEnv(const char* flag, const char* default_value) {
|
|
|
+const char* StringFromGTestEnv(const char* flag, const char* default_value) {
|
|
|
#if defined(GTEST_GET_STRING_FROM_ENV_)
|
|
|
return GTEST_GET_STRING_FROM_ENV_(flag, default_value);
|
|
|
-#endif // defined(GTEST_GET_STRING_FROM_ENV_)
|
|
|
+#else
|
|
|
const std::string env_var = FlagToEnvVar(flag);
|
|
|
- const char* value = posix::GetEnv(env_var.c_str());
|
|
|
- if (value != NULL) {
|
|
|
- return value;
|
|
|
- }
|
|
|
-
|
|
|
- // As a special case for the 'output' flag, if GTEST_OUTPUT is not
|
|
|
- // set, we look for XML_OUTPUT_FILE, which is set by the Bazel build
|
|
|
- // system. The value of XML_OUTPUT_FILE is a filename without the
|
|
|
- // "xml:" prefix of GTEST_OUTPUT.
|
|
|
- //
|
|
|
- // The net priority order after flag processing is thus:
|
|
|
- // --gtest_output command line flag
|
|
|
- // GTEST_OUTPUT environment variable
|
|
|
- // XML_OUTPUT_FILE environment variable
|
|
|
- // 'default_value'
|
|
|
- if (strcmp(flag, "output") == 0) {
|
|
|
- value = posix::GetEnv("XML_OUTPUT_FILE");
|
|
|
- if (value != NULL) {
|
|
|
- return std::string("xml:") + value;
|
|
|
- }
|
|
|
- }
|
|
|
- return default_value;
|
|
|
+ const char* const value = posix::GetEnv(env_var.c_str());
|
|
|
+ return value == nullptr ? default_value : value;
|
|
|
+#endif // defined(GTEST_GET_STRING_FROM_ENV_)
|
|
|
}
|
|
|
|
|
|
} // namespace internal
|