blob: 33043b714be1222c53a571a47e6987ece6f2088c [file] [log] [blame]
Devin Coughlin8beac282016-12-19 22:50:31 +00001//RUN: %clang_cc1 -cc1 -std=c++11 -analyze -analyzer-checker=core,apiModeling.google.GTest -analyzer-eagerly-assume %s -verify
2//RUN: %clang_cc1 -cc1 -std=c++11 -analyze -analyzer-checker=core,apiModeling.google.GTest -analyzer-eagerly-assume -DGTEST_VERSION_1_8_AND_LATER=1 %s -verify
3
4// expected-no-diagnostics
5
6namespace std {
7 class string {
8 public:
9 ~string();
10 const char *c_str();
11 };
12}
13
14namespace testing {
15
16class Message { };
17class TestPartResult {
18 public:
19 enum Type {
20 kSuccess,
21 kNonFatalFailure,
22 kFatalFailure
23 };
24};
25
26namespace internal {
27
28class AssertHelper {
29 public:
30 AssertHelper(TestPartResult::Type type, const char* file, int line,
31 const char* message);
32 ~AssertHelper();
33 void operator=(const Message& message) const;
34};
35
36
37template <typename T>
38struct AddReference { typedef T& type; };
39template <typename T>
40struct AddReference<T&> { typedef T& type; };
41template <typename From, typename To>
42class ImplicitlyConvertible {
43 private:
44 static typename AddReference<From>::type MakeFrom();
45 static char Helper(To);
46 static char (&Helper(...))[2];
47 public:
48 static const bool value =
49 sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1;
50};
51template <typename From, typename To>
52const bool ImplicitlyConvertible<From, To>::value;
53template<bool> struct EnableIf;
54template<> struct EnableIf<true> { typedef void type; };
55
56} // end internal
57
58
59class AssertionResult {
60public:
61
62 // The implementation for the copy constructor is not exposed in the
63 // interface.
64 AssertionResult(const AssertionResult& other);
65
66#if defined(GTEST_VERSION_1_8_AND_LATER)
67 template <typename T>
68 explicit AssertionResult(
69 const T& success,
70 typename internal::EnableIf<
71 !internal::ImplicitlyConvertible<T, AssertionResult>::value>::type*
72 /*enabler*/ = 0)
73 : success_(success) {}
74#else
75 explicit AssertionResult(bool success) : success_(success) {}
76#endif
77
78 operator bool() const { return success_; }
79
80 // The actual AssertionResult does not have an explicit destructor, but
81 // it does have a non-trivial member veriable, so we add a destructor here
82 // to force temporary cleanups.
83 ~AssertionResult();
84private:
85
86 bool success_;
87};
88
89namespace internal {
90std::string GetBoolAssertionFailureMessage(
91 const AssertionResult& assertion_result,
92 const char* expression_text,
93 const char* actual_predicate_value,
94 const char* expected_predicate_value);
95} // end internal
96
97} // end testing
98
99#define GTEST_MESSAGE_AT_(file, line, message, result_type) \
100 ::testing::internal::AssertHelper(result_type, file, line, message) \
101 = ::testing::Message()
102
103#define GTEST_MESSAGE_(message, result_type) \
104 GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type)
105
106#define GTEST_FATAL_FAILURE_(message) \
107 return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure)
108
109#define GTEST_NONFATAL_FAILURE_(message) \
110 GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure)
111
112# define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default:
113
114#define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \
115 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
116 if (const ::testing::AssertionResult gtest_ar_ = \
117 ::testing::AssertionResult(expression)) \
118 ; \
119 else \
120 fail(::testing::internal::GetBoolAssertionFailureMessage(\
121 gtest_ar_, text, #actual, #expected).c_str())
122
123#define EXPECT_TRUE(condition) \
124 GTEST_TEST_BOOLEAN_((condition), #condition, false, true, \
125 GTEST_NONFATAL_FAILURE_)
126#define ASSERT_TRUE(condition) \
127 GTEST_TEST_BOOLEAN_((condition), #condition, false, true, \
128 GTEST_FATAL_FAILURE_)
129
130#define ASSERT_FALSE(condition) \
131 GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
132 GTEST_FATAL_FAILURE_)
133
134void testAssertTrue(int *p) {
135 ASSERT_TRUE(p != nullptr);
136 EXPECT_TRUE(1 == *p); // no-warning
137}
138
139void testAssertFalse(int *p) {
140 ASSERT_FALSE(p == nullptr);
141 EXPECT_TRUE(1 == *p); // no-warning
142}