tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestAssertWithAssignment.cpp (1941B)


      1 #include "mozilla/MacroArgs.h"
      2 
      3 static __attribute__((always_inline)) bool MOZ_AssertAssignmentTest(bool expr) {
      4  return expr;
      5 }
      6 
      7 #define MOZ_UNLIKELY(x) (__builtin_expect(!!(x), 0))
      8 #define MOZ_CRASH() do { } while(0)
      9 #define MOZ_CHECK_ASSERT_ASSIGNMENT(expr) MOZ_AssertAssignmentTest(!!(expr))
     10 
     11 #define MOZ_ASSERT_HELPER1(expr) \
     12  do { \
     13    if (MOZ_UNLIKELY(!MOZ_CHECK_ASSERT_ASSIGNMENT(expr))) { \
     14      MOZ_CRASH();\
     15    } \
     16  } while(0) \
     17 
     18 /* Now the two-argument form. */
     19 #define MOZ_ASSERT_HELPER2(expr, explain) \
     20  do { \
     21    if (MOZ_UNLIKELY(!MOZ_CHECK_ASSERT_ASSIGNMENT(expr))) { \
     22      MOZ_CRASH();\
     23    } \
     24  } while(0) \
     25 
     26 #define MOZ_RELEASE_ASSERT_GLUE(a, b) a b
     27 #define MOZ_RELEASE_ASSERT(...) \
     28  MOZ_RELEASE_ASSERT_GLUE( \
     29    MOZ_PASTE_PREFIX_AND_ARG_COUNT(MOZ_ASSERT_HELPER, __VA_ARGS__), \
     30    (__VA_ARGS__))
     31 
     32 #define MOZ_ASSERT(...) MOZ_RELEASE_ASSERT(__VA_ARGS__)
     33 
     34 void FunctionTest(int p) {
     35  MOZ_ASSERT(p = 1); // expected-error {{Forbidden assignment in assert expression}}
     36 }
     37 
     38 void FunctionTest2(int p) {
     39  MOZ_ASSERT(((p = 1)));  // expected-error {{Forbidden assignment in assert expression}}
     40 }
     41 
     42 void FunctionTest3(int p) {
     43  MOZ_ASSERT(p != 3);
     44 }
     45 
     46 class TestOverloading {
     47  int value;
     48 public:
     49  explicit TestOverloading(int _val) : value(_val) {}
     50  // different operators
     51  explicit operator bool() const { return true; }
     52  TestOverloading& operator=(const int _val) { value = _val; return *this; }
     53 
     54  int& GetInt() {return value;}
     55 };
     56 
     57 void TestOverloadingFunc() {
     58  TestOverloading p(2);
     59  int f;
     60 
     61  MOZ_ASSERT(p);
     62  MOZ_ASSERT(p = 3); // expected-error {{Forbidden assignment in assert expression}}
     63  MOZ_ASSERT(p, "p is not valid");
     64  MOZ_ASSERT(p = 3, "p different than 3"); // expected-error {{Forbidden assignment in assert expression}}
     65  MOZ_ASSERT(p.GetInt() = 2); // expected-error {{Forbidden assignment in assert expression}}
     66  MOZ_ASSERT(p.GetInt() == 2);
     67  MOZ_ASSERT(p.GetInt() == 2, f = 3);
     68 }