summaryrefslogtreecommitdiffstats
path: root/build/clang-plugin/tests/TestAssertWithAssignment.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'build/clang-plugin/tests/TestAssertWithAssignment.cpp')
-rw-r--r--build/clang-plugin/tests/TestAssertWithAssignment.cpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/build/clang-plugin/tests/TestAssertWithAssignment.cpp b/build/clang-plugin/tests/TestAssertWithAssignment.cpp
new file mode 100644
index 000000000..f0f049e4a
--- /dev/null
+++ b/build/clang-plugin/tests/TestAssertWithAssignment.cpp
@@ -0,0 +1,68 @@
+#include "mozilla/MacroArgs.h"
+
+static __attribute__((always_inline)) bool MOZ_AssertAssignmentTest(bool expr) {
+ return expr;
+}
+
+#define MOZ_UNLIKELY(x) (__builtin_expect(!!(x), 0))
+#define MOZ_CRASH() do { } while(0)
+#define MOZ_CHECK_ASSERT_ASSIGNMENT(expr) MOZ_AssertAssignmentTest(!!(expr))
+
+#define MOZ_ASSERT_HELPER1(expr) \
+ do { \
+ if (MOZ_UNLIKELY(!MOZ_CHECK_ASSERT_ASSIGNMENT(expr))) { \
+ MOZ_CRASH();\
+ } \
+ } while(0) \
+
+/* Now the two-argument form. */
+#define MOZ_ASSERT_HELPER2(expr, explain) \
+ do { \
+ if (MOZ_UNLIKELY(!MOZ_CHECK_ASSERT_ASSIGNMENT(expr))) { \
+ MOZ_CRASH();\
+ } \
+ } while(0) \
+
+#define MOZ_RELEASE_ASSERT_GLUE(a, b) a b
+#define MOZ_RELEASE_ASSERT(...) \
+ MOZ_RELEASE_ASSERT_GLUE( \
+ MOZ_PASTE_PREFIX_AND_ARG_COUNT(MOZ_ASSERT_HELPER, __VA_ARGS__), \
+ (__VA_ARGS__))
+
+#define MOZ_ASSERT(...) MOZ_RELEASE_ASSERT(__VA_ARGS__)
+
+void FunctionTest(int p) {
+ MOZ_ASSERT(p = 1); // expected-error {{Forbidden assignment in assert expression}}
+}
+
+void FunctionTest2(int p) {
+ MOZ_ASSERT(((p = 1))); // expected-error {{Forbidden assignment in assert expression}}
+}
+
+void FunctionTest3(int p) {
+ MOZ_ASSERT(p != 3);
+}
+
+class TestOverloading {
+ int value;
+public:
+ explicit TestOverloading(int _val) : value(_val) {}
+ // different operators
+ explicit operator bool() const { return true; }
+ TestOverloading& operator=(const int _val) { value = _val; return *this; }
+
+ int& GetInt() {return value;}
+};
+
+void TestOverloadingFunc() {
+ TestOverloading p(2);
+ int f;
+
+ MOZ_ASSERT(p);
+ MOZ_ASSERT(p = 3); // expected-error {{Forbidden assignment in assert expression}}
+ MOZ_ASSERT(p, "p is not valid");
+ MOZ_ASSERT(p = 3, "p different than 3"); // expected-error {{Forbidden assignment in assert expression}}
+ MOZ_ASSERT(p.GetInt() = 2); // expected-error {{Forbidden assignment in assert expression}}
+ MOZ_ASSERT(p.GetInt() == 2);
+ MOZ_ASSERT(p.GetInt() == 2, f = 3);
+}