summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/cpp11-migrate/LoopConvert/naming-conflict.cpp
blob: 050f44ef90080447312b62fe46e6b51cc67e8241 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
// RUN: cpp11-migrate -loop-convert %t.cpp -- -I %S/Inputs
// RUN: FileCheck -input-file=%t.cpp %s

#include "structures.h"

#define MAX(a,b) (a > b) ? a : b
#define DEF 5

const int N = 10;
int nums[N];
int sum = 0;

namespace ns {
  struct st {
    int x;
  };
}

void sameNames() {
  int num = 0;
  for (int i = 0; i < N; ++i) {
    printf("Fibonacci number is %d\n", nums[i]);
    sum += nums[i] + 2 + num;
    (void) nums[i];
  }
  // CHECK: for (auto & nums_i : nums)
  // CHECK-NEXT: printf("Fibonacci number is %d\n", nums_i);
  // CHECK-NEXT: sum += nums_i + 2 + num;
  // CHECK-NOT: (void) num;
}

void macroConflict() {
  S MAXs;
  for (S::const_iterator it = MAXs.begin(), e = MAXs.end(); it != e; ++it) {
    printf("s has value %d\n", (*it).x);
    printf("Max of 3 and 5: %d\n", MAX(3,5));
  }
  // CHECK: for (auto & MAXs_it : MAXs)
  // CHECK-NEXT: printf("s has value %d\n", (MAXs_it).x);
  // CHECK-NEXT: printf("Max of 3 and 5: %d\n", MAX(3,5));

  T DEFs;
  for (T::iterator it = DEFs.begin(), e = DEFs.end(); it != e; ++it) {
    if (*it == DEF) {
      printf("I found %d\n", *it);
    }
  }
  // CHECK: for (auto & DEFs_it : DEFs)
  // CHECK-NEXT: if (DEFs_it == DEF) {
  // CHECK-NEXT: printf("I found %d\n", DEFs_it);
}

void keywordConflict() {
  T ints;
  for (T::iterator it = ints.begin(), e = ints.end(); it != e; ++it) {
    *it = 5;
  }
  // CHECK: for (auto & ints_it : ints)
  // CHECK-NEXT: ints_it = 5;

  U __FUNCTION__s;
  for (U::iterator it = __FUNCTION__s.begin(), e = __FUNCTION__s.end();
       it != e; ++it) {
    int __FUNCTION__s_it = (*it).x + 2;
  }
  // CHECK: for (auto & __FUNCTION__s_elem : __FUNCTION__s)
  // CHECK-NEXT: int __FUNCTION__s_it = (__FUNCTION__s_elem).x + 2;
}

void typeConflict() {
  T Vals;
  // Using the name "Val", although it is the name of an existing struct, is
  // safe in this loop since it will only exist within this scope.
  for (T::iterator it = Vals.begin(), e = Vals.end(); it != e; ++it) {
  }
  // CHECK: for (auto & Val : Vals)

  // We cannot use the name "Val" in this loop since there is a reference to
  // it in the body of the loop.
  for (T::iterator it = Vals.begin(), e = Vals.end(); it != e; ++it) {
    *it = sizeof(Val);
  }
  // CHECK: for (auto & Vals_it : Vals)
  // CHECK-NEXT: Vals_it = sizeof(Val);

  typedef struct Val TD;
  U TDs;
  // Naming the variable "TD" within this loop is safe because the typedef
  // was never used within the loop.
  for (U::iterator it = TDs.begin(), e = TDs.end(); it != e; ++it) {
  }
  // CHECK: for (auto & TD : TDs)

  // "TD" cannot be used in this loop since the typedef is being used.
  for (U::iterator it = TDs.begin(), e = TDs.end(); it != e; ++it) {
    TD V;
    V.x = 5;
  }
  // CHECK: for (auto & TDs_it : TDs)
  // CHECK-NEXT: TD V;
  // CHECK-NEXT: V.x = 5;

  using ns::st;
  T sts;
  for (T::iterator it = sts.begin(), e = sts.end(); it != e; ++it) {
    *it = sizeof(st);
  }
  // CHECK: for (auto & sts_it : sts)
  // CHECK-NEXT: sts_it = sizeof(st);
}
OpenPOWER on IntegriCloud