summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/cpp11-migrate/UseNullptr/macros.cpp
blob: 7b3c2f0c5b9e4cf14ae9b9dc99725c8bd3a43348 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
// RUN: cpp11-migrate -use-nullptr %t.cpp -- -I %S
// RUN: FileCheck -input-file=%t.cpp %s

#define NULL 0
// CHECK: #define NULL 0

void dummy(int*) {}
void side_effect() {}

#define MACRO_EXPANSION_HAS_NULL \
  void foo() { \
    dummy(0); \
    dummy(NULL); \
    side_effect(); \
  }
  // CHECK: void foo() { \
  // CHECK-NEXT:   dummy(0); \
  // CHECK-NEXT:   dummy(NULL); \

MACRO_EXPANSION_HAS_NULL;
// CHECK: MACRO_EXPANSION_HAS_NULL;
#undef MACRO_EXPANSION_HAS_NULL


void test_macro_expansion1() {
#define MACRO_EXPANSION_HAS_NULL \
  dummy(NULL); \
  side_effect();
  // CHECK: dummy(NULL); \
  // CHECK-NEXT: side_effect();

  MACRO_EXPANSION_HAS_NULL;
  // CHECK: MACRO_EXPANSION_HAS_NULL;

#undef MACRO_EXPANSION_HAS_NULL
}

void test_macro_expansion2() {
#define MACRO_EXPANSION_HAS_NULL \
  dummy(NULL); \
  side_effect();
  // CHECK: dummy(NULL); \
  // CHECK-NEXT: side_effect();

#define OUTER_MACRO \
  MACRO_EXPANSION_HAS_NULL; \
  side_effect();

  OUTER_MACRO;
  // CHECK: OUTER_MACRO;

#undef OUTER_MACRO
#undef MACRO_EXPANSION_HAS_NULL
}

void test_macro_expansion3() {
#define MY_NULL NULL
  // TODO: Eventually we should fix the transform to detect cases like this so
  // that we can replace MY_NULL with nullptr.
  int *p = MY_NULL;
  // CHECK: int *p = MY_NULL;
#undef MY_NULL
}

// Test function-like macros where the parameter to the macro (expression)
// results in a nullptr.
void __dummy_assert_fail() {}

void test_function_like_macro1() {
  // This tests that the CastSequenceVisitor is working properly.
#define my_assert(expr) \
  ((expr) ? static_cast<void>(expr) : __dummy_assert_fail())
  // CHECK: ((expr) ? static_cast<void>(expr) : __dummy_assert_fail())

  int *p;
  my_assert(p != 0);
  // CHECK: my_assert(p != nullptr);
#undef my_assert
}

void test_function_like_macro2() {
  // This tests that the implicit cast is working properly.
#define my_macro(expr) \
  (expr)

  int *p;
  my_macro(p != 0);
  // CHECK: my_macro(p != nullptr);
#undef my_macro
}


OpenPOWER on IntegriCloud