Imported Upstream version 1.14.0
[platform/upstream/gtest.git] / googletest / src / gtest-port.cc
index d797fe4..2aaf2bc 100644 (file)
 #include <cstdint>
 #include <fstream>
 #include <memory>
+#include <ostream>
+#include <string>
+#include <utility>
+#include <vector>
 
-#if GTEST_OS_WINDOWS
+#ifdef GTEST_OS_WINDOWS
 #include <io.h>
 #include <sys/stat.h>
 #include <windows.h>
 #include <unistd.h>
 #endif  // GTEST_OS_WINDOWS
 
-#if GTEST_OS_MAC
+#ifdef GTEST_OS_MAC
 #include <mach/mach_init.h>
 #include <mach/task.h>
 #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
+#if defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) ||   \
+    defined(GTEST_OS_GNU_KFREEBSD) || defined(GTEST_OS_NETBSD) || \
+    defined(GTEST_OS_OPENBSD)
 #include <sys/sysctl.h>
-#if GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD
+#if defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \
+    defined(GTEST_OS_GNU_KFREEBSD)
 #include <sys/user.h>
 #endif
 #endif
 
-#if GTEST_OS_QNX
+#ifdef GTEST_OS_QNX
 #include <devctl.h>
 #include <fcntl.h>
 #include <sys/procfs.h>
 #endif  // GTEST_OS_QNX
 
-#if GTEST_OS_AIX
+#ifdef GTEST_OS_AIX
 #include <procinfo.h>
 #include <sys/types.h>
 #endif  // GTEST_OS_AIX
 
-#if GTEST_OS_FUCHSIA
+#ifdef GTEST_OS_FUCHSIA
 #include <zircon/process.h>
 #include <zircon/syscalls.h>
 #endif  // GTEST_OS_FUCHSIA
@@ -90,7 +96,7 @@
 namespace testing {
 namespace internal {
 
-#if GTEST_OS_LINUX || GTEST_OS_GNU_HURD
+#if defined(GTEST_OS_LINUX) || defined(GTEST_OS_GNU_HURD)
 
 namespace {
 template <typename T>
@@ -113,7 +119,7 @@ size_t GetThreadCount() {
   return ReadProcFileField<size_t>(filename, 19);
 }
 
-#elif GTEST_OS_MAC
+#elif defined(GTEST_OS_MAC)
 
 size_t GetThreadCount() {
   const task_t task = mach_task_self();
@@ -131,20 +137,20 @@ size_t GetThreadCount() {
   }
 }
 
-#elif GTEST_OS_DRAGONFLY || GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD || \
-    GTEST_OS_NETBSD
+#elif defined(GTEST_OS_DRAGONFLY) || defined(GTEST_OS_FREEBSD) || \
+    defined(GTEST_OS_GNU_KFREEBSD) || defined(GTEST_OS_NETBSD)
 
-#if GTEST_OS_NETBSD
+#ifdef GTEST_OS_NETBSD
 #undef KERN_PROC
 #define KERN_PROC KERN_PROC2
 #define kinfo_proc kinfo_proc2
 #endif
 
-#if GTEST_OS_DRAGONFLY
+#ifdef GTEST_OS_DRAGONFLY
 #define KP_NLWP(kp) (kp.kp_nthreads)
-#elif GTEST_OS_FREEBSD || GTEST_OS_GNU_KFREEBSD
+#elif defined(GTEST_OS_FREEBSD) || defined(GTEST_OS_GNU_KFREEBSD)
 #define KP_NLWP(kp) (kp.ki_numthreads)
-#elif GTEST_OS_NETBSD
+#elif defined(GTEST_OS_NETBSD)
 #define KP_NLWP(kp) (kp.p_nlwps)
 #endif
 
@@ -156,7 +162,7 @@ size_t GetThreadCount() {
     KERN_PROC,
     KERN_PROC_PID,
     getpid(),
-#if GTEST_OS_NETBSD
+#ifdef GTEST_OS_NETBSD
     sizeof(struct kinfo_proc),
     1,
 #endif
@@ -169,7 +175,7 @@ size_t GetThreadCount() {
   }
   return static_cast<size_t>(KP_NLWP(info));
 }
-#elif GTEST_OS_OPENBSD
+#elif defined(GTEST_OS_OPENBSD)
 
 // Returns the number of threads running in the process, or 0 to indicate that
 // we cannot detect it.
@@ -193,8 +199,8 @@ size_t GetThreadCount() {
   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)) {
+  std::vector<struct kinfo_proc> info(mib[5]);
+  if (sysctl(mib, miblen, info.data(), &size, NULL, 0)) {
     return 0;
   }
 
@@ -206,7 +212,7 @@ size_t GetThreadCount() {
   return nthreads;
 }
 
-#elif GTEST_OS_QNX
+#elif defined(GTEST_OS_QNX)
 
 // Returns the number of threads running in the process, or 0 to indicate that
 // we cannot detect it.
@@ -226,7 +232,7 @@ size_t GetThreadCount() {
   }
 }
 
-#elif GTEST_OS_AIX
+#elif defined(GTEST_OS_AIX)
 
 size_t GetThreadCount() {
   struct procentry64 entry;
@@ -239,7 +245,7 @@ size_t GetThreadCount() {
   }
 }
 
-#elif GTEST_OS_FUCHSIA
+#elif defined(GTEST_OS_FUCHSIA)
 
 size_t GetThreadCount() {
   int dummy_buffer;
@@ -264,7 +270,7 @@ size_t GetThreadCount() {
 
 #endif  // GTEST_OS_LINUX
 
-#if GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS
+#if defined(GTEST_IS_THREADSAFE) && defined(GTEST_OS_WINDOWS)
 
 AutoHandle::AutoHandle() : handle_(INVALID_HANDLE_VALUE) {}
 
@@ -655,7 +661,7 @@ void ThreadLocalRegistry::OnThreadLocalDestroyed(
 
 #endif  // GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS
 
-#if GTEST_USES_POSIX_RE
+#ifdef GTEST_USES_POSIX_RE
 
 // Implements RE.  Currently only needed for death tests.
 
@@ -668,7 +674,6 @@ RE::~RE() {
     regfree(&partial_regex_);
     regfree(&full_regex_);
   }
-  free(const_cast<char*>(pattern_));
 }
 
 // Returns true if and only if regular expression re matches the entire str.
@@ -690,7 +695,7 @@ bool RE::PartialMatch(const char* str, const RE& re) {
 
 // Initializes an RE from its string representation.
 void RE::Init(const char* regex) {
-  pattern_ = posix::StrDup(regex);
+  pattern_ = regex;
 
   // Reserves enough bytes to hold the regular expression used for a
   // full match.
@@ -718,7 +723,7 @@ void RE::Init(const char* regex) {
   delete[] full_pattern;
 }
 
-#elif GTEST_USES_SIMPLE_RE
+#elif defined(GTEST_USES_SIMPLE_RE)
 
 // Returns true if and only if ch appears anywhere in str (excluding the
 // terminating '\0' character).
@@ -920,27 +925,26 @@ bool MatchRegexAnywhere(const char* regex, const char* str) {
 
 // Implements the RE class.
 
-RE::~RE() {
-  free(const_cast<char*>(pattern_));
-  free(const_cast<char*>(full_pattern_));
-}
+RE::~RE() = default;
 
 // 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);
+  return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_.c_str(), str);
 }
 
 // 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);
+  return re.is_valid_ && MatchRegexAnywhere(re.pattern_.c_str(), str);
 }
 
 // Initializes an RE from its string representation.
 void RE::Init(const char* regex) {
-  pattern_ = full_pattern_ = nullptr;
+  full_pattern_.clear();
+  pattern_.clear();
+
   if (regex != nullptr) {
-    pattern_ = posix::StrDup(regex);
+    pattern_ = regex;
   }
 
   is_valid_ = ValidateRegex(regex);
@@ -949,25 +953,19 @@ void RE::Init(const char* regex) {
     return;
   }
 
-  const size_t len = strlen(regex);
   // Reserves enough bytes to hold the regular expression used for a
-  // full match: we need space to prepend a '^', append a '$', and
-  // terminate the string with '\0'.
-  char* buffer = static_cast<char*>(malloc(len + 3));
-  full_pattern_ = buffer;
+  // full match: we need space to prepend a '^' and append a '$'.
+  full_pattern_.reserve(pattern_.size() + 2);
 
-  if (*regex != '^')
-    *buffer++ = '^';  // Makes sure full_pattern_ starts with '^'.
-
-  // We don't use snprintf or strncpy, as they trigger a warning when
-  // compiled with VC++ 8.0.
-  memcpy(buffer, regex, len);
-  buffer += len;
+  if (pattern_.empty() || pattern_.front() != '^') {
+    full_pattern_.push_back('^');  // Makes sure full_pattern_ starts with '^'.
+  }
 
-  if (len == 0 || regex[len - 1] != '$')
-    *buffer++ = '$';  // Makes sure full_pattern_ ends with '$'.
+  full_pattern_.append(pattern_);
 
-  *buffer = '\0';
+  if (pattern_.empty() || pattern_.back() != '$') {
+    full_pattern_.push_back('$');  // Makes sure full_pattern_ ends with '$'.
+  }
 }
 
 #endif  // GTEST_USES_POSIX_RE
@@ -1035,7 +1033,7 @@ class CapturedStream {
  public:
   // The ctor redirects the stream to a temporary file.
   explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) {
-#if GTEST_OS_WINDOWS
+#ifdef GTEST_OS_WINDOWS
     char temp_dir_path[MAX_PATH + 1] = {'\0'};   // NOLINT
     char temp_file_path[MAX_PATH + 1] = {'\0'};  // NOLINT
 
@@ -1054,7 +1052,7 @@ class CapturedStream {
     // directory, so we create the temporary file in a temporary directory.
     std::string name_template;
 
-#if GTEST_OS_LINUX_ANDROID
+#ifdef GTEST_OS_LINUX_ANDROID
     // Note: Android applications are expected to call the framework's
     // Context.getExternalStorageDirectory() method through JNI to get
     // the location of the world-writable SD Card directory. However,
@@ -1067,7 +1065,7 @@ class CapturedStream {
     // '/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
+#elif defined(GTEST_OS_IOS)
     char user_temp_dir[PATH_MAX + 1];
 
     // Documented alternative to NSTemporaryDirectory() (for obtaining creating
@@ -1227,7 +1225,7 @@ std::string ReadEntireFile(FILE* file) {
   return content;
 }
 
-#if GTEST_HAS_DEATH_TEST
+#ifdef GTEST_HAS_DEATH_TEST
 static const std::vector<std::string>* g_injected_test_argvs =
     nullptr;  // Owned.
 
@@ -1254,7 +1252,7 @@ void ClearInjectableArgvs() {
 }
 #endif  // GTEST_HAS_DEATH_TEST
 
-#if GTEST_OS_WINDOWS_MOBILE
+#ifdef GTEST_OS_WINDOWS_MOBILE
 namespace posix {
 void Abort() {
   DebugBreak();