]> git.proxmox.com Git - rustc.git/blame - src/llvm/utils/unittest/googletest/src/gtest-port.cc
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / utils / unittest / googletest / src / gtest-port.cc
CommitLineData
223e47cc
LB
1// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32#include "gtest/internal/gtest-port.h"
33
34#include <limits.h>
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38
39#if GTEST_OS_WINDOWS_MOBILE
40# include <windows.h> // For TerminateProcess()
41#elif GTEST_OS_WINDOWS
42# include <io.h>
43# include <sys/stat.h>
44#else
45# include <unistd.h>
46#endif // GTEST_OS_WINDOWS_MOBILE
47
48#if GTEST_OS_MAC
49# include <mach/mach_init.h>
50# include <mach/task.h>
51# include <mach/vm_map.h>
52#endif // GTEST_OS_MAC
53
54#include "gtest/gtest-spi.h"
55#include "gtest/gtest-message.h"
56#include "gtest/internal/gtest-internal.h"
57#include "gtest/internal/gtest-string.h"
58
59// Indicates that this translation unit is part of Google Test's
60// implementation. It must come before gtest-internal-inl.h is
61// included, or there will be a compiler error. This trick is to
62// prevent a user from accidentally including gtest-internal-inl.h in
63// his code.
64#define GTEST_IMPLEMENTATION_ 1
1a4d82fc 65#include "src/gtest-internal-inl.h"
223e47cc
LB
66#undef GTEST_IMPLEMENTATION_
67
68namespace testing {
69namespace internal {
70
71#if defined(_MSC_VER) || defined(__BORLANDC__)
72// MSVC and C++Builder do not provide a definition of STDERR_FILENO.
73const int kStdOutFileno = 1;
74const int kStdErrFileno = 2;
75#else
76const int kStdOutFileno = STDOUT_FILENO;
77const int kStdErrFileno = STDERR_FILENO;
78#endif // _MSC_VER
79
80#if GTEST_OS_MAC
81
82// Returns the number of threads running in the process, or 0 to indicate that
83// we cannot detect it.
84size_t GetThreadCount() {
85 const task_t task = mach_task_self();
86 mach_msg_type_number_t thread_count;
87 thread_act_array_t thread_list;
88 const kern_return_t status = task_threads(task, &thread_list, &thread_count);
89 if (status == KERN_SUCCESS) {
90 // task_threads allocates resources in thread_list and we need to free them
91 // to avoid leaks.
92 vm_deallocate(task,
93 reinterpret_cast<vm_address_t>(thread_list),
94 sizeof(thread_t) * thread_count);
95 return static_cast<size_t>(thread_count);
96 } else {
97 return 0;
98 }
99}
100
101#else
102
103size_t GetThreadCount() {
104 // There's no portable way to detect the number of threads, so we just
105 // return 0 to indicate that we cannot detect it.
106 return 0;
107}
108
109#endif // GTEST_OS_MAC
110
111#if GTEST_USES_POSIX_RE
112
113// Implements RE. Currently only needed for death tests.
114
115RE::~RE() {
116 if (is_valid_) {
117 // regfree'ing an invalid regex might crash because the content
118 // of the regex is undefined. Since the regex's are essentially
119 // the same, one cannot be valid (or invalid) without the other
120 // being so too.
121 regfree(&partial_regex_);
122 regfree(&full_regex_);
123 }
124 free(const_cast<char*>(pattern_));
125}
126
127// Returns true iff regular expression re matches the entire str.
128bool RE::FullMatch(const char* str, const RE& re) {
129 if (!re.is_valid_) return false;
130
131 regmatch_t match;
132 return regexec(&re.full_regex_, str, 1, &match, 0) == 0;
133}
134
135// Returns true iff regular expression re matches a substring of str
136// (including str itself).
137bool RE::PartialMatch(const char* str, const RE& re) {
138 if (!re.is_valid_) return false;
139
140 regmatch_t match;
141 return regexec(&re.partial_regex_, str, 1, &match, 0) == 0;
142}
143
144// Initializes an RE from its string representation.
145void RE::Init(const char* regex) {
146 pattern_ = posix::StrDup(regex);
147
148 // Reserves enough bytes to hold the regular expression used for a
149 // full match.
150 const size_t full_regex_len = strlen(regex) + 10;
151 char* const full_pattern = new char[full_regex_len];
152
153 snprintf(full_pattern, full_regex_len, "^(%s)$", regex);
154 is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0;
155 // We want to call regcomp(&partial_regex_, ...) even if the
156 // previous expression returns false. Otherwise partial_regex_ may
157 // not be properly initialized can may cause trouble when it's
158 // freed.
159 //
160 // Some implementation of POSIX regex (e.g. on at least some
161 // versions of Cygwin) doesn't accept the empty string as a valid
162 // regex. We change it to an equivalent form "()" to be safe.
163 if (is_valid_) {
164 const char* const partial_regex = (*regex == '\0') ? "()" : regex;
165 is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0;
166 }
167 EXPECT_TRUE(is_valid_)
168 << "Regular expression \"" << regex
169 << "\" is not a valid POSIX Extended regular expression.";
170
171 delete[] full_pattern;
172}
173
174#elif GTEST_USES_SIMPLE_RE
175
176// Returns true iff ch appears anywhere in str (excluding the
177// terminating '\0' character).
178bool IsInSet(char ch, const char* str) {
179 return ch != '\0' && strchr(str, ch) != NULL;
180}
181
182// Returns true iff ch belongs to the given classification. Unlike
183// similar functions in <ctype.h>, these aren't affected by the
184// current locale.
185bool IsAsciiDigit(char ch) { return '0' <= ch && ch <= '9'; }
186bool IsAsciiPunct(char ch) {
187 return IsInSet(ch, "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~");
188}
189bool IsRepeat(char ch) { return IsInSet(ch, "?*+"); }
190bool IsAsciiWhiteSpace(char ch) { return IsInSet(ch, " \f\n\r\t\v"); }
191bool IsAsciiWordChar(char ch) {
192 return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') ||
193 ('0' <= ch && ch <= '9') || ch == '_';
194}
195
196// Returns true iff "\\c" is a supported escape sequence.
197bool IsValidEscape(char c) {
198 return (IsAsciiPunct(c) || IsInSet(c, "dDfnrsStvwW"));
199}
200
201// Returns true iff the given atom (specified by escaped and pattern)
202// matches ch. The result is undefined if the atom is invalid.
203bool AtomMatchesChar(bool escaped, char pattern_char, char ch) {
204 if (escaped) { // "\\p" where p is pattern_char.
205 switch (pattern_char) {
206 case 'd': return IsAsciiDigit(ch);
207 case 'D': return !IsAsciiDigit(ch);
208 case 'f': return ch == '\f';
209 case 'n': return ch == '\n';
210 case 'r': return ch == '\r';
211 case 's': return IsAsciiWhiteSpace(ch);
212 case 'S': return !IsAsciiWhiteSpace(ch);
213 case 't': return ch == '\t';
214 case 'v': return ch == '\v';
215 case 'w': return IsAsciiWordChar(ch);
216 case 'W': return !IsAsciiWordChar(ch);
217 }
218 return IsAsciiPunct(pattern_char) && pattern_char == ch;
219 }
220
221 return (pattern_char == '.' && ch != '\n') || pattern_char == ch;
222}
223
224// Helper function used by ValidateRegex() to format error messages.
225String FormatRegexSyntaxError(const char* regex, int index) {
226 return (Message() << "Syntax error at index " << index
227 << " in simple regular expression \"" << regex << "\": ").GetString();
228}
229
230// Generates non-fatal failures and returns false if regex is invalid;
231// otherwise returns true.
232bool ValidateRegex(const char* regex) {
233 if (regex == NULL) {
234 // TODO(wan@google.com): fix the source file location in the
235 // assertion failures to match where the regex is used in user
236 // code.
237 ADD_FAILURE() << "NULL is not a valid simple regular expression.";
238 return false;
239 }
240
241 bool is_valid = true;
242
243 // True iff ?, *, or + can follow the previous atom.
244 bool prev_repeatable = false;
245 for (int i = 0; regex[i]; i++) {
246 if (regex[i] == '\\') { // An escape sequence
247 i++;
248 if (regex[i] == '\0') {
249 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
250 << "'\\' cannot appear at the end.";
251 return false;
252 }
253
254 if (!IsValidEscape(regex[i])) {
255 ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)
256 << "invalid escape sequence \"\\" << regex[i] << "\".";
257 is_valid = false;
258 }
259 prev_repeatable = true;
260 } else { // Not an escape sequence.
261 const char ch = regex[i];
262
263 if (ch == '^' && i > 0) {
264 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
265 << "'^' can only appear at the beginning.";
266 is_valid = false;
267 } else if (ch == '$' && regex[i + 1] != '\0') {
268 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
269 << "'$' can only appear at the end.";
270 is_valid = false;
271 } else if (IsInSet(ch, "()[]{}|")) {
272 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
273 << "'" << ch << "' is unsupported.";
274 is_valid = false;
275 } else if (IsRepeat(ch) && !prev_repeatable) {
276 ADD_FAILURE() << FormatRegexSyntaxError(regex, i)
277 << "'" << ch << "' can only follow a repeatable token.";
278 is_valid = false;
279 }
280
281 prev_repeatable = !IsInSet(ch, "^$?*+");
282 }
283 }
284
285 return is_valid;
286}
287
288// Matches a repeated regex atom followed by a valid simple regular
289// expression. The regex atom is defined as c if escaped is false,
290// or \c otherwise. repeat is the repetition meta character (?, *,
291// or +). The behavior is undefined if str contains too many
292// characters to be indexable by size_t, in which case the test will
293// probably time out anyway. We are fine with this limitation as
294// std::string has it too.
295bool MatchRepetitionAndRegexAtHead(
296 bool escaped, char c, char repeat, const char* regex,
297 const char* str) {
298 const size_t min_count = (repeat == '+') ? 1 : 0;
299 const size_t max_count = (repeat == '?') ? 1 :
300 static_cast<size_t>(-1) - 1;
301 // We cannot call numeric_limits::max() as it conflicts with the
302 // max() macro on Windows.
303
304 for (size_t i = 0; i <= max_count; ++i) {
305 // We know that the atom matches each of the first i characters in str.
306 if (i >= min_count && MatchRegexAtHead(regex, str + i)) {
307 // We have enough matches at the head, and the tail matches too.
308 // Since we only care about *whether* the pattern matches str
309 // (as opposed to *how* it matches), there is no need to find a
310 // greedy match.
311 return true;
312 }
313 if (str[i] == '\0' || !AtomMatchesChar(escaped, c, str[i]))
314 return false;
315 }
316 return false;
317}
318
319// Returns true iff regex matches a prefix of str. regex must be a
320// valid simple regular expression and not start with "^", or the
321// result is undefined.
322bool MatchRegexAtHead(const char* regex, const char* str) {
323 if (*regex == '\0') // An empty regex matches a prefix of anything.
324 return true;
325
326 // "$" only matches the end of a string. Note that regex being
327 // valid guarantees that there's nothing after "$" in it.
328 if (*regex == '$')
329 return *str == '\0';
330
331 // Is the first thing in regex an escape sequence?
332 const bool escaped = *regex == '\\';
333 if (escaped)
334 ++regex;
335 if (IsRepeat(regex[1])) {
336 // MatchRepetitionAndRegexAtHead() calls MatchRegexAtHead(), so
337 // here's an indirect recursion. It terminates as the regex gets
338 // shorter in each recursion.
339 return MatchRepetitionAndRegexAtHead(
340 escaped, regex[0], regex[1], regex + 2, str);
341 } else {
342 // regex isn't empty, isn't "$", and doesn't start with a
343 // repetition. We match the first atom of regex with the first
344 // character of str and recurse.
345 return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) &&
346 MatchRegexAtHead(regex + 1, str + 1);
347 }
348}
349
350// Returns true iff regex matches any substring of str. regex must be
351// a valid simple regular expression, or the result is undefined.
352//
353// The algorithm is recursive, but the recursion depth doesn't exceed
354// the regex length, so we won't need to worry about running out of
355// stack space normally. In rare cases the time complexity can be
356// exponential with respect to the regex length + the string length,
357// but usually it's must faster (often close to linear).
358bool MatchRegexAnywhere(const char* regex, const char* str) {
359 if (regex == NULL || str == NULL)
360 return false;
361
362 if (*regex == '^')
363 return MatchRegexAtHead(regex + 1, str);
364
365 // A successful match can be anywhere in str.
366 do {
367 if (MatchRegexAtHead(regex, str))
368 return true;
369 } while (*str++ != '\0');
370 return false;
371}
372
373// Implements the RE class.
374
375RE::~RE() {
376 free(const_cast<char*>(pattern_));
377 free(const_cast<char*>(full_pattern_));
378}
379
380// Returns true iff regular expression re matches the entire str.
381bool RE::FullMatch(const char* str, const RE& re) {
382 return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str);
383}
384
385// Returns true iff regular expression re matches a substring of str
386// (including str itself).
387bool RE::PartialMatch(const char* str, const RE& re) {
388 return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str);
389}
390
391// Initializes an RE from its string representation.
392void RE::Init(const char* regex) {
393 pattern_ = full_pattern_ = NULL;
394 if (regex != NULL) {
395 pattern_ = posix::StrDup(regex);
396 }
397
398 is_valid_ = ValidateRegex(regex);
399 if (!is_valid_) {
400 // No need to calculate the full pattern when the regex is invalid.
401 return;
402 }
403
404 const size_t len = strlen(regex);
405 // Reserves enough bytes to hold the regular expression used for a
406 // full match: we need space to prepend a '^', append a '$', and
407 // terminate the string with '\0'.
408 char* buffer = static_cast<char*>(malloc(len + 3));
409 full_pattern_ = buffer;
410
411 if (*regex != '^')
412 *buffer++ = '^'; // Makes sure full_pattern_ starts with '^'.
413
414 // We don't use snprintf or strncpy, as they trigger a warning when
415 // compiled with VC++ 8.0.
416 memcpy(buffer, regex, len);
417 buffer += len;
418
419 if (len == 0 || regex[len - 1] != '$')
420 *buffer++ = '$'; // Makes sure full_pattern_ ends with '$'.
421
422 *buffer = '\0';
423}
424
425#endif // GTEST_USES_POSIX_RE
426
427const char kUnknownFile[] = "unknown file";
428
429// Formats a source file path and a line number as they would appear
430// in an error message from the compiler used to compile this code.
431GTEST_API_ ::std::string FormatFileLocation(const char* file, int line) {
432 const char* const file_name = file == NULL ? kUnknownFile : file;
433
434 if (line < 0) {
435 return String::Format("%s:", file_name).c_str();
436 }
437#ifdef _MSC_VER
438 return String::Format("%s(%d):", file_name, line).c_str();
439#else
440 return String::Format("%s:%d:", file_name, line).c_str();
441#endif // _MSC_VER
442}
443
444// Formats a file location for compiler-independent XML output.
445// Although this function is not platform dependent, we put it next to
446// FormatFileLocation in order to contrast the two functions.
447// Note that FormatCompilerIndependentFileLocation() does NOT append colon
448// to the file location it produces, unlike FormatFileLocation().
449GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(
450 const char* file, int line) {
451 const char* const file_name = file == NULL ? kUnknownFile : file;
452
453 if (line < 0)
454 return file_name;
455 else
456 return String::Format("%s:%d", file_name, line).c_str();
457}
458
459
460GTestLog::GTestLog(GTestLogSeverity severity, const char* file, int line)
461 : severity_(severity) {
462 const char* const marker =
463 severity == GTEST_INFO ? "[ INFO ]" :
464 severity == GTEST_WARNING ? "[WARNING]" :
465 severity == GTEST_ERROR ? "[ ERROR ]" : "[ FATAL ]";
466 GetStream() << ::std::endl << marker << " "
467 << FormatFileLocation(file, line).c_str() << ": ";
468}
469
470// Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.
471GTestLog::~GTestLog() {
472 GetStream() << ::std::endl;
473 if (severity_ == GTEST_FATAL) {
474 fflush(stderr);
475 posix::Abort();
476 }
477}
478// Disable Microsoft deprecation warnings for POSIX functions called from
479// this class (creat, dup, dup2, and close)
480#ifdef _MSC_VER
481# pragma warning(push)
482# pragma warning(disable: 4996)
483#endif // _MSC_VER
484
485#if GTEST_HAS_STREAM_REDIRECTION
486
487// Object that captures an output stream (stdout/stderr).
488class CapturedStream {
489 public:
490 // The ctor redirects the stream to a temporary file.
491 CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) {
492
493# if GTEST_OS_WINDOWS
494 char temp_dir_path[MAX_PATH + 1] = { '\0' }; // NOLINT
495 char temp_file_path[MAX_PATH + 1] = { '\0' }; // NOLINT
496
497 ::GetTempPathA(sizeof(temp_dir_path), temp_dir_path);
498 const UINT success = ::GetTempFileNameA(temp_dir_path,
499 "gtest_redir",
500 0, // Generate unique file name.
501 temp_file_path);
502 GTEST_CHECK_(success != 0)
503 << "Unable to create a temporary file in " << temp_dir_path;
504 const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE);
505 GTEST_CHECK_(captured_fd != -1) << "Unable to open temporary file "
506 << temp_file_path;
507 filename_ = temp_file_path;
508#elif GTEST_OS_LINUX_ANDROID
509 char name_template[] = "/sdcard/captured_stderr.XXXXXX";
510 const int captured_fd = mkstemp(name_template);
511 filename_ = name_template;
512# else
513 // There's no guarantee that a test has write access to the
514 // current directory, so we create the temporary file in the /tmp
515 // directory instead.
516 char name_template[] = "/tmp/captured_stream.XXXXXX";
517 const int captured_fd = mkstemp(name_template);
518 filename_ = name_template;
519# endif // GTEST_OS_WINDOWS
520 fflush(NULL);
521 dup2(captured_fd, fd_);
522 close(captured_fd);
523 }
524
525 ~CapturedStream() {
526 remove(filename_.c_str());
527 }
528
529 String GetCapturedString() {
530 if (uncaptured_fd_ != -1) {
531 // Restores the original stream.
532 fflush(NULL);
533 dup2(uncaptured_fd_, fd_);
534 close(uncaptured_fd_);
535 uncaptured_fd_ = -1;
536 }
537
538 FILE* const file = posix::FOpen(filename_.c_str(), "r");
539 const String content = ReadEntireFile(file);
540 posix::FClose(file);
541 return content;
542 }
543
544 private:
545 // Reads the entire content of a file as a String.
546 static String ReadEntireFile(FILE* file);
547
548 // Returns the size (in bytes) of a file.
549 static size_t GetFileSize(FILE* file);
550
551 const int fd_; // A stream to capture.
552 int uncaptured_fd_;
553 // Name of the temporary file holding the stderr output.
554 ::std::string filename_;
555
556 GTEST_DISALLOW_COPY_AND_ASSIGN_(CapturedStream);
557};
558
559// Returns the size (in bytes) of a file.
560size_t CapturedStream::GetFileSize(FILE* file) {
561 fseek(file, 0, SEEK_END);
562 return static_cast<size_t>(ftell(file));
563}
564
565// Reads the entire content of a file as a string.
566String CapturedStream::ReadEntireFile(FILE* file) {
567 const size_t file_size = GetFileSize(file);
568 char* const buffer = new char[file_size];
569
570 size_t bytes_last_read = 0; // # of bytes read in the last fread()
571 size_t bytes_read = 0; // # of bytes read so far
572
573 fseek(file, 0, SEEK_SET);
574
575 // Keeps reading the file until we cannot read further or the
576 // pre-determined file size is reached.
577 do {
578 bytes_last_read = fread(buffer+bytes_read, 1, file_size-bytes_read, file);
579 bytes_read += bytes_last_read;
580 } while (bytes_last_read > 0 && bytes_read < file_size);
581
582 const String content(buffer, bytes_read);
583 delete[] buffer;
584
585 return content;
586}
587
588# ifdef _MSC_VER
589# pragma warning(pop)
590# endif // _MSC_VER
591
592static CapturedStream* g_captured_stderr = NULL;
593static CapturedStream* g_captured_stdout = NULL;
594
595// Starts capturing an output stream (stdout/stderr).
596void CaptureStream(int fd, const char* stream_name, CapturedStream** stream) {
597 if (*stream != NULL) {
598 GTEST_LOG_(FATAL) << "Only one " << stream_name
599 << " capturer can exist at a time.";
600 }
601 *stream = new CapturedStream(fd);
602}
603
604// Stops capturing the output stream and returns the captured string.
605String GetCapturedStream(CapturedStream** captured_stream) {
606 const String content = (*captured_stream)->GetCapturedString();
607
608 delete *captured_stream;
609 *captured_stream = NULL;
610
611 return content;
612}
613
614// Starts capturing stdout.
615void CaptureStdout() {
616 CaptureStream(kStdOutFileno, "stdout", &g_captured_stdout);
617}
618
619// Starts capturing stderr.
620void CaptureStderr() {
621 CaptureStream(kStdErrFileno, "stderr", &g_captured_stderr);
622}
623
624// Stops capturing stdout and returns the captured string.
625String GetCapturedStdout() { return GetCapturedStream(&g_captured_stdout); }
626
627// Stops capturing stderr and returns the captured string.
628String GetCapturedStderr() { return GetCapturedStream(&g_captured_stderr); }
629
630#endif // GTEST_HAS_STREAM_REDIRECTION
631
632#if GTEST_HAS_DEATH_TEST
633
634// A copy of all command line arguments. Set by InitGoogleTest().
635::std::vector<String> g_argvs;
636
637// Returns the command line as a vector of strings.
638const ::std::vector<String>& GetArgvs() { return g_argvs; }
639
640#endif // GTEST_HAS_DEATH_TEST
641
642#if GTEST_OS_WINDOWS_MOBILE
643namespace posix {
644void Abort() {
645 DebugBreak();
646 TerminateProcess(GetCurrentProcess(), 1);
647}
648} // namespace posix
649#endif // GTEST_OS_WINDOWS_MOBILE
650
651// Returns the name of the environment variable corresponding to the
652// given flag. For example, FlagToEnvVar("foo") will return
653// "GTEST_FOO" in the open-source version.
654static String FlagToEnvVar(const char* flag) {
655 const String full_flag =
656 (Message() << GTEST_FLAG_PREFIX_ << flag).GetString();
657
658 Message env_var;
659 for (size_t i = 0; i != full_flag.length(); i++) {
660 env_var << ToUpper(full_flag.c_str()[i]);
661 }
662
663 return env_var.GetString();
664}
665
666// Parses 'str' for a 32-bit signed integer. If successful, writes
667// the result to *value and returns true; otherwise leaves *value
668// unchanged and returns false.
669bool ParseInt32(const Message& src_text, const char* str, Int32* value) {
670 // Parses the environment variable as a decimal integer.
671 char* end = NULL;
672 const long long_value = strtol(str, &end, 10); // NOLINT
673
674 // Has strtol() consumed all characters in the string?
675 if (*end != '\0') {
676 // No - an invalid character was encountered.
677 Message msg;
678 msg << "WARNING: " << src_text
679 << " is expected to be a 32-bit integer, but actually"
680 << " has value \"" << str << "\".\n";
681 printf("%s", msg.GetString().c_str());
682 fflush(stdout);
683 return false;
684 }
685
686 // Is the parsed value in the range of an Int32?
687 const Int32 result = static_cast<Int32>(long_value);
688 if (long_value == LONG_MAX || long_value == LONG_MIN ||
689 // The parsed value overflows as a long. (strtol() returns
690 // LONG_MAX or LONG_MIN when the input overflows.)
691 result != long_value
692 // The parsed value overflows as an Int32.
693 ) {
694 Message msg;
695 msg << "WARNING: " << src_text
696 << " is expected to be a 32-bit integer, but actually"
697 << " has value " << str << ", which overflows.\n";
698 printf("%s", msg.GetString().c_str());
699 fflush(stdout);
700 return false;
701 }
702
703 *value = result;
704 return true;
705}
706
707// Reads and returns the Boolean environment variable corresponding to
708// the given flag; if it's not set, returns default_value.
709//
710// The value is considered true iff it's not "0".
711bool BoolFromGTestEnv(const char* flag, bool default_value) {
712 const String env_var = FlagToEnvVar(flag);
713 const char* const string_value = posix::GetEnv(env_var.c_str());
714 return string_value == NULL ?
715 default_value : strcmp(string_value, "0") != 0;
716}
717
718// Reads and returns a 32-bit integer stored in the environment
719// variable corresponding to the given flag; if it isn't set or
720// doesn't represent a valid 32-bit integer, returns default_value.
721Int32 Int32FromGTestEnv(const char* flag, Int32 default_value) {
722 const String env_var = FlagToEnvVar(flag);
723 const char* const string_value = posix::GetEnv(env_var.c_str());
724 if (string_value == NULL) {
725 // The environment variable is not set.
726 return default_value;
727 }
728
729 Int32 result = default_value;
730 if (!ParseInt32(Message() << "Environment variable " << env_var,
731 string_value, &result)) {
732 printf("The default value %s is used.\n",
733 (Message() << default_value).GetString().c_str());
734 fflush(stdout);
735 return default_value;
736 }
737
738 return result;
739}
740
741// Reads and returns the string environment variable corresponding to
742// the given flag; if it's not set, returns default_value.
743const char* StringFromGTestEnv(const char* flag, const char* default_value) {
744 const String env_var = FlagToEnvVar(flag);
745 const char* const value = posix::GetEnv(env_var.c_str());
746 return value == NULL ? default_value : value;
747}
748
1a4d82fc
JJ
749// Pin the vtables to this file.
750#if GTEST_HAS_PTHREAD
751ThreadWithParamBase::~ThreadWithParamBase() {}
752ThreadLocalValueHolderBase::~ThreadLocalValueHolderBase() {}
753#endif
754TestFactoryBase::~TestFactoryBase() {}
755
223e47cc
LB
756} // namespace internal
757} // namespace testing
1a4d82fc
JJ
758
759// Pin the vtable to this file.
760#if !GTEST_NO_LLVM_RAW_OSTREAM
761namespace llvm {
762void convertible_fwd_ostream::anchor() {}
763}
764#endif