summaryrefslogtreecommitdiffstats
path: root/build/clang-plugin/tests/TestAssertWithAssignment.cpp
blob: f0f049e4a393e0a3ebdf64be04075e472dfd90da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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);
}