1 //RUN: %clang_analyze_cc1 -cc1 -std=c++11 -analyzer-checker=core,apiModeling.google.GTest,debug.ExprInspection %s -verify
2 //RUN: %clang_analyze_cc1 -cc1 -std=c++11 -analyzer-checker=core,apiModeling.google.GTest,debug.ExprInspection -DGTEST_VERSION_1_8_AND_LATER=1 %s -verify
4 void clang_analyzer_eval(int);
5 void clang_analyzer_warnIfReached();
18 class TestPartResult
{
31 AssertHelper(TestPartResult::Type type
, const char* file
, int line
,
34 void operator=(const Message
& message
) const;
39 struct AddReference
{ typedef T
& type
; };
41 struct AddReference
<T
&> { typedef T
& type
; };
42 template <typename From
, typename To
>
43 class ImplicitlyConvertible
{
45 static typename AddReference
<From
>::type
MakeFrom();
46 static char Helper(To
);
47 static char (&Helper(...))[2];
49 static const bool value
=
50 sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1;
52 template <typename From
, typename To
>
53 const bool ImplicitlyConvertible
<From
, To
>::value
;
54 template<bool> struct EnableIf
;
55 template<> struct EnableIf
<true> { typedef void type
; };
60 class AssertionResult
{
63 // The implementation for the copy constructor is not exposed in the
65 AssertionResult(const AssertionResult
& other
);
67 #if defined(GTEST_VERSION_1_8_AND_LATER)
69 explicit AssertionResult(
71 typename
internal::EnableIf
<
72 !internal::ImplicitlyConvertible
<T
, AssertionResult
>::value
>::type
*
74 : success_(success
) {}
76 explicit AssertionResult(bool success
) : success_(success
) {}
79 operator bool() const { return success_
; }
81 // The actual AssertionResult does not have an explicit destructor, but
82 // it does have a non-trivial member veriable, so we add a destructor here
83 // to force temporary cleanups.
91 std::string
GetBoolAssertionFailureMessage(
92 const AssertionResult
& assertion_result
,
93 const char* expression_text
,
94 const char* actual_predicate_value
,
95 const char* expected_predicate_value
);
100 #define GTEST_MESSAGE_AT_(file, line, message, result_type) \
101 ::testing::internal::AssertHelper(result_type, file, line, message) \
102 = ::testing::Message()
104 #define GTEST_MESSAGE_(message, result_type) \
105 GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type)
107 #define GTEST_FATAL_FAILURE_(message) \
108 return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure)
110 #define GTEST_NONFATAL_FAILURE_(message) \
111 GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure)
113 # define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default:
115 #define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \
116 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
117 if (const ::testing::AssertionResult gtest_ar_ = \
118 ::testing::AssertionResult(expression)) \
121 fail(::testing::internal::GetBoolAssertionFailureMessage(\
122 gtest_ar_, text, #actual, #expected).c_str())
124 #define EXPECT_TRUE(condition) \
125 GTEST_TEST_BOOLEAN_((condition), #condition, false, true, \
126 GTEST_NONFATAL_FAILURE_)
127 #define ASSERT_TRUE(condition) \
128 GTEST_TEST_BOOLEAN_((condition), #condition, false, true, \
129 GTEST_FATAL_FAILURE_)
131 #define ASSERT_FALSE(condition) \
132 GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
133 GTEST_FATAL_FAILURE_)
135 void testAssertTrue(int *p
) {
136 ASSERT_TRUE(p
!= nullptr);
137 EXPECT_TRUE(1 == *p
); // no-warning
140 void testAssertFalse(int *p
) {
141 ASSERT_FALSE(p
== nullptr);
142 EXPECT_TRUE(1 == *p
); // no-warning
145 void testConstrainState(int p
) {
148 clang_analyzer_eval(p
== 7); // expected-warning {{TRUE}}
151 clang_analyzer_warnIfReached(); // no-warning
154 void testAssertSymbolicPtr(const bool *b
) {
155 ASSERT_TRUE(*b
); // no-crash
157 clang_analyzer_eval(*b
); // expected-warning{{TRUE}}
160 void testAssertSymbolicRef(const bool &b
) {
161 ASSERT_TRUE(b
); // no-crash
163 clang_analyzer_eval(b
); // expected-warning{{TRUE}}