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 // Low-level types and utilities for porting Google Mock to various
31 // platforms.  All macros ending with _ and symbols defined in an
32 // internal namespace are subject to change without notice.  Code
33 // outside Google Mock MUST NOT USE THEM DIRECTLY.  Macros that don't
34 // end with _ are part of Google Mock's public API and can be used by
35 // code outside Google Mock.
36 
37 // IWYU pragma: private, include "gmock/gmock.h"
38 // IWYU pragma: friend gmock/.*
39 
40 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_
41 #define GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_
42 
43 #include <assert.h>
44 #include <stdlib.h>
45 #include <cstdint>
46 #include <iostream>
47 
48 // Most of the utilities needed for porting Google Mock are also
49 // required for Google Test and are defined in gtest-port.h.
50 //
51 // Note to maintainers: to reduce code duplication, prefer adding
52 // portability utilities to Google Test's gtest-port.h instead of
53 // here, as Google Mock depends on Google Test.  Only add a utility
54 // here if it's truly specific to Google Mock.
55 
56 #include "gmock/internal/custom/gmock-port.h"
57 #include "gtest/internal/gtest-port.h"
58 
59 #ifdef GTEST_HAS_ABSL
60 #include "absl/flags/declare.h"
61 #include "absl/flags/flag.h"
62 #endif
63 
64 // For MS Visual C++, check the compiler version. At least VS 2015 is
65 // required to compile Google Mock.
66 #if defined(_MSC_VER) && _MSC_VER < 1900
67 #error "At least Visual C++ 2015 (14.0) is required to compile Google Mock."
68 #endif
69 
70 // Macro for referencing flags.  This is public as we want the user to
71 // use this syntax to reference Google Mock flags.
72 #define GMOCK_FLAG_NAME_(name) gmock_##name
73 #define GMOCK_FLAG(name) FLAGS_gmock_##name
74 
75 // Pick a command line flags implementation.
76 #ifdef GTEST_HAS_ABSL
77 
78 // Macros for defining flags.
79 #define GMOCK_DEFINE_bool_(name, default_val, doc) \
80   ABSL_FLAG(bool, GMOCK_FLAG_NAME_(name), default_val, doc)
81 #define GMOCK_DEFINE_int32_(name, default_val, doc) \
82   ABSL_FLAG(int32_t, GMOCK_FLAG_NAME_(name), default_val, doc)
83 #define GMOCK_DEFINE_string_(name, default_val, doc) \
84   ABSL_FLAG(std::string, GMOCK_FLAG_NAME_(name), default_val, doc)
85 
86 // Macros for declaring flags.
87 #define GMOCK_DECLARE_bool_(name) \
88   ABSL_DECLARE_FLAG(bool, GMOCK_FLAG_NAME_(name))
89 #define GMOCK_DECLARE_int32_(name) \
90   ABSL_DECLARE_FLAG(int32_t, GMOCK_FLAG_NAME_(name))
91 #define GMOCK_DECLARE_string_(name) \
92   ABSL_DECLARE_FLAG(std::string, GMOCK_FLAG_NAME_(name))
93 
94 #define GMOCK_FLAG_GET(name) ::absl::GetFlag(GMOCK_FLAG(name))
95 #define GMOCK_FLAG_SET(name, value) \
96   (void)(::absl::SetFlag(&GMOCK_FLAG(name), value))
97 
98 #else  // GTEST_HAS_ABSL
99 
100 // Macros for defining flags.
101 #define GMOCK_DEFINE_bool_(name, default_val, doc)  \
102   namespace testing {                               \
103   GTEST_API_ bool GMOCK_FLAG(name) = (default_val); \
104   }                                                 \
105   static_assert(true, "no-op to require trailing semicolon")
106 #define GMOCK_DEFINE_int32_(name, default_val, doc)    \
107   namespace testing {                                  \
108   GTEST_API_ int32_t GMOCK_FLAG(name) = (default_val); \
109   }                                                    \
110   static_assert(true, "no-op to require trailing semicolon")
111 #define GMOCK_DEFINE_string_(name, default_val, doc)         \
112   namespace testing {                                        \
113   GTEST_API_ ::std::string GMOCK_FLAG(name) = (default_val); \
114   }                                                          \
115   static_assert(true, "no-op to require trailing semicolon")
116 
117 // Macros for declaring flags.
118 #define GMOCK_DECLARE_bool_(name)          \
119   namespace testing {                      \
120   GTEST_API_ extern bool GMOCK_FLAG(name); \
121   }                                        \
122   static_assert(true, "no-op to require trailing semicolon")
123 #define GMOCK_DECLARE_int32_(name)            \
124   namespace testing {                         \
125   GTEST_API_ extern int32_t GMOCK_FLAG(name); \
126   }                                           \
127   static_assert(true, "no-op to require trailing semicolon")
128 #define GMOCK_DECLARE_string_(name)                 \
129   namespace testing {                               \
130   GTEST_API_ extern ::std::string GMOCK_FLAG(name); \
131   }                                                 \
132   static_assert(true, "no-op to require trailing semicolon")
133 
134 #define GMOCK_FLAG_GET(name) ::testing::GMOCK_FLAG(name)
135 #define GMOCK_FLAG_SET(name, value) (void)(::testing::GMOCK_FLAG(name) = value)
136 
137 #endif  // GTEST_HAS_ABSL
138 
139 #endif  // GOOGLEMOCK_INCLUDE_GMOCK_INTERNAL_GMOCK_PORT_H_
140