File: extract-repeated-expr-initiate.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (109 lines) | stat: -rw-r--r-- 4,837 bytes parent folder | download
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

struct AClass {
  int method();
  int method2();
};
struct AWrapperClass {
  AClass &object();
};

void takesClass(AWrapperClass &wrapper) {
  wrapper.object().method();
  wrapper.object().method();
  wrapper.object().method2();
}
// CHECK1: Initiated the 'extract-repeated-expr-into-var' action at [[@LINE-4]]:3
// CHECK2: Initiated the 'extract-repeated-expr-into-var' action at [[@LINE-4]]:3
// CHECK3: Initiated the 'extract-repeated-expr-into-var' action at [[@LINE-4]]:3

// Suggest extracting 'wrapper.object()'
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:11:3-19 %s | FileCheck --check-prefix=CHECK1 %s
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:12:3-19 %s | FileCheck --check-prefix=CHECK2 %s
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:13:3-19 %s | FileCheck --check-prefix=CHECK3 %s

// CHECK-NO: Failed to initiate the refactoring action!

// Avoid suggesting extraction of 'wrapper.object().method2()'
// RUN: not clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:13:20-30 %s 2>&1 | FileCheck --check-prefix=CHECK-NO %s

// RUN: clang-refactor-test list-actions -at=%s:11:11 %s | FileCheck --check-prefix=CHECK-ACTION %s
// CHECK-ACTION: Extract Repeated Expression

AClass &returnsReference(int x);
AClass &returnsAndTakesFunctionPointer(AClass& (*)(int));

void checkReferenceCall() {
  returnsReference(0).method();
  returnsReference(0).method2();
  returnsAndTakesFunctionPointer(returnsReference).method();
  returnsAndTakesFunctionPointer(returnsReference).method2();
}
// CHECK4: Initiated the 'extract-repeated-expr-into-var' action at [[@LINE-5]]:3
// CHECK5: Initiated the 'extract-repeated-expr-into-var' action at [[@LINE-4]]:3
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:36:3-22 %s | FileCheck --check-prefix=CHECK4 %s
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:38:3-51 %s | FileCheck --check-prefix=CHECK5 %s
// RUN: not clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:36:23-32 -in=%s:37:23-32 -in=%s:38:52-61 %s 2>&1 | FileCheck --check-prefix=CHECK-NO %s

struct WithFields {
  int x, y;
};
struct WithFieldsOperators {
  WithFields *operator ->();
  WithFields &operator ()();

  const WithFields &operator [](int x) const;
  WithFields &operator [](int x);
};

void checkOperatorCalls(WithFieldsOperators &op, int id) {
  op[id].x;
  op[id].y;
// CHECK6: Initiated the 'extract-repeated-expr-into-var' action at [[@LINE-2]]:3
  op().x;
  op().x;
// CHECK7: Initiated the 'extract-repeated-expr-into-var' action at [[@LINE-2]]:3
  op->x;
  op->x;
}
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:59:3-9 %s | FileCheck --check-prefix=CHECK6 %s
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:62:3-7 %s | FileCheck --check-prefix=CHECK7 %s
// RUN: not clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:59:10-12 -in=%s:62:8-10 -in=%s:65:3-9 %s 2>&1 | FileCheck --check-prefix=CHECK-NO %s

struct AWrapperClass2 {
  AClass *object() const;
};

void checkPointerType(AWrapperClass *object, AWrapperClass2 *object2) {
  object->object().method();
  if (object) {
    object->object().method2();
  }
// CHECK8: Initiated the 'extract-repeated-expr-into-var' action at [[@LINE-4]]:3
  object2->object()->method();
  int m = object2->object()->method2();
// CHECK9: Initiated the 'extract-repeated-expr-into-var' action at [[@LINE-2]]:3
}
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:77:3-19 %s | FileCheck --check-prefix=CHECK8 %s
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -in=%s:82:3-20 %s | FileCheck --check-prefix=CHECK9 %s

struct ConstVsNonConst {
  int field;
  void constMethod() const;
  void method();
};

struct ConstVsNonConstWrapper {
  const ConstVsNonConst &object() const;
  ConstVsNonConst &object();
};

void checkFoo(ConstVsNonConstWrapper &object) {
  object.object().constMethod();
  object.object().method();
}
// CHECK10: Initiated the 'extract-repeated-expr-into-var' action at [[@LINE-3]]:3
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -at=%s:101:3 %s | FileCheck --check-prefix=CHECK10 %s

// Check that the action can be initiate using a selection:
// RUN: clang-refactor-test initiate -action extract-repeated-expr-into-var -selected=%s:11:3-11:19 -selected=%s:11:15-11:19 -selected=%s:11:3-11:17 -selected=%s:11:15-11:18 %s | FileCheck --check-prefix=CHECK1 %s
// RUN: not clang-refactor-test initiate -action extract-repeated-expr-into-var -selected=%s:11:3-11:22 -selected=%s:11:1-13:30 %s 2>&1 | FileCheck --check-prefix=CHECK-NO %s