1 //RUN: %clang_cc1 -cc1 -std=c++11 -analyze -analyzer-checker=core,apiModeling.google.GTest,debug.ExprInspection -analyzer-eagerly-assume %s -verify 2 //RUN: %clang_cc1 -cc1 -std=c++11 -analyze -analyzer-checker=core,apiModeling.google.GTest,debug.ExprInspection -analyzer-eagerly-assume -DGTEST_VERSION_1_8_AND_LATER=1 %s -verify 3 //RUN: %clang_cc1 -cc1 -std=c++11 -analyze -analyzer-checker=core,apiModeling.google.GTest,debug.ExprInspection -analyzer-eagerly-assume -analyzer-config cfg-temporary-dtors=true %s -verify 4 5 void clang_analyzer_eval(int); 6 void clang_analyzer_warnIfReached(); 7 8 namespace std { 9 class string { 10 public: 11 ~string(); 12 const char *c_str(); 13 }; 14 } 15 16 namespace testing { 17 18 class Message { }; 19 class TestPartResult { 20 public: 21 enum Type { 22 kSuccess, 23 kNonFatalFailure, 24 kFatalFailure 25 }; 26 }; 27 28 namespace internal { 29 30 class 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 39 template <typename T> 40 struct AddReference { typedef T& type; }; 41 template <typename T> 42 struct AddReference<T&> { typedef T& type; }; 43 template <typename From, typename To> 44 class 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 }; 53 template <typename From, typename To> 54 const bool ImplicitlyConvertible<From, To>::value; 55 template<bool> struct EnableIf; 56 template<> struct EnableIf<true> { typedef void type; }; 57 58 } // end internal 59 60 61 class AssertionResult { 62 public: 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(); 86 private: 87 88 bool success_; 89 }; 90 91 namespace internal { 92 std::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 136 void testAssertTrue(int *p) { 137 ASSERT_TRUE(p != nullptr); 138 EXPECT_TRUE(1 == *p); // no-warning 139 } 140 141 void testAssertFalse(int *p) { 142 ASSERT_FALSE(p == nullptr); 143 EXPECT_TRUE(1 == *p); // no-warning 144 } 145 146 void 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 } 154