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