File: ExtractVariableTests.cpp

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,496,180 kB
  • sloc: cpp: 5,593,972; ansic: 986,872; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,538; xml: 953; cs: 573; fortran: 567
file content (300 lines) | stat: -rw-r--r-- 9,362 bytes parent folder | download | duplicates (6)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//===-- ExtractVariableTests.cpp --------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "TestTU.h"
#include "TweakTesting.h"
#include "gmock/gmock-matchers.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace clang {
namespace clangd {
namespace {

TWEAK_TEST(ExtractVariable);

TEST_F(ExtractVariableTest, Test) {
  const char *AvailableCases = R"cpp(
    int xyz(int a = 1) {
      struct T {
        int bar(int a = 1);
        int z;
      } t;
      // return statement
      return [[[[t.b[[a]]r]](t.z)]];
    }
    void f() {
      int a = [[5 +]] [[4 * [[[[xyz]]()]]]];
      // multivariable initialization
      if(1)
        int x = [[1]], y = [[a + 1]], a = [[1]], z = a + 1;
      // if without else
      if([[1]])
        a = [[1]] + 1;
      // if with else
      if(a < [[3]])
        if(a == [[4]])
          a = [[5]] + 1;
        else
          a = [[5]] + 1;
      else if (a < [[4]])
        a = [[4]] + 1;
      else
        a = [[5]] + 1;
      // for loop
      for(a = [[1]] + 1; a > [[[[3]] + [[4]]]]; a++)
        a = [[2]] + 1;
      // while
      while(a < [[1]])
        a = [[1]] + 1;
      // do while
      do
        a = [[1]] + 1;
      while(a < [[3]]);
    }
  )cpp";
  EXPECT_AVAILABLE(AvailableCases);

  ExtraArgs = {"-xc"};
  const char *AvailableButC = R"cpp(
    void foo() {
      int x = [[1]];
    })cpp";
  EXPECT_UNAVAILABLE(AvailableButC);
  ExtraArgs = {};

  const char *NoCrashCases = R"cpp(
    // error-ok: broken code, but shouldn't crash
    template<typename T, typename ...Args>
    struct Test<T, Args...> {
    Test(const T &v) :val[[(^]]) {}
      T val;
    };
  )cpp";
  EXPECT_UNAVAILABLE(NoCrashCases);

  const char *UnavailableCases = R"cpp(
    int xyz(int a = [[1]]) {
      struct T {
        int bar(int a = [[1]]);
        int z = [[1]];
      } t;
      return [[t]].bar([[[[t]].z]]);
    }
    void v() { return; }
    // function default argument
    void f(int b = [[1]]) {
      // empty selection
      int a = ^1 ^+ ^2;
      // void expressions
      auto i = new int, j = new int;
      [[[[delete i]], delete j]];
      [[v]]();
      // if
      if(1)
        int x = 1, y = a + 1, a = 1, z = [[a + 1]];
      if(int a = 1)
        if([[a + 1]] == 4)
          a = [[[[a]] +]] 1;
      // for loop
      for(int a = 1, b = 2, c = 3; a > [[b + c]]; [[a++]])
        a = [[a + 1]];
      // lambda
      auto lamb = [&[[a]], &[[b]]](int r = [[1]]) {return 1;};
      // assignment
      xyz([[a = 5]]);
      xyz([[a *= 5]]);
      // Variable DeclRefExpr
      a = [[b]];
      a = [[xyz()]];
      // statement expression
      [[xyz()]];
      while (a)
        [[++a]];
      // label statement
      goto label;
      label:
        a = [[1]];
    }
  )cpp";
  EXPECT_UNAVAILABLE(UnavailableCases);

  // vector of pairs of input and output strings
  const std::vector<std::pair<std::string, std::string>> InputOutputs = {
      // extraction from variable declaration/assignment
      {R"cpp(void varDecl() {
                   int a = 5 * (4 + (3 [[- 1)]]);
                 })cpp",
       R"cpp(void varDecl() {
                   auto placeholder = (3 - 1); int a = 5 * (4 + placeholder);
                 })cpp"},
      // FIXME: extraction from switch case
      /*{R"cpp(void f(int a) {
               if(1)
                 while(a < 1)
                   switch (1) {
                       case 1:
                         a = [[1 + 2]];
                         break;
                       default:
                         break;
                   }
             })cpp",
       R"cpp(void f(int a) {
               auto placeholder = 1 + 2; if(1)
                 while(a < 1)
                   switch (1) {
                       case 1:
                         a = placeholder;
                         break;
                       default:
                         break;
                   }
             })cpp"},*/
      // Macros
      {R"cpp(#define PLUS(x) x++
                 void f(int a) {
                   int y = PLUS([[1+a]]);
                 })cpp",
       /*FIXME: It should be extracted like this.
        R"cpp(#define PLUS(x) x++
              void f(int a) {
                auto placeholder = 1+a; int y = PLUS(placeholder);
              })cpp"},*/
       R"cpp(#define PLUS(x) x++
                 void f(int a) {
                   auto placeholder = PLUS(1+a); int y = placeholder;
                 })cpp"},
      // ensure InsertionPoint isn't inside a macro
      {R"cpp(#define LOOP(x) while (1) {a = x;}
                 void f(int a) {
                   if(1)
                    LOOP(5 + [[3]])
                 })cpp",
       R"cpp(#define LOOP(x) while (1) {a = x;}
                 void f(int a) {
                   auto placeholder = 3; if(1)
                    LOOP(5 + placeholder)
                 })cpp"},
      {R"cpp(#define LOOP(x) do {x;} while(1);
                 void f(int a) {
                   if(1)
                    LOOP(5 + [[3]])
                 })cpp",
       R"cpp(#define LOOP(x) do {x;} while(1);
                 void f(int a) {
                   auto placeholder = 3; if(1)
                    LOOP(5 + placeholder)
                 })cpp"},
      // attribute testing
      {R"cpp(void f(int a) {
                    [ [gsl::suppress("type")] ] for (;;) a = [[1]] + 1;
                 })cpp",
       R"cpp(void f(int a) {
                    auto placeholder = 1; [ [gsl::suppress("type")] ] for (;;) a = placeholder + 1;
                 })cpp"},
      // MemberExpr
      {R"cpp(class T {
                   T f() {
                     return [[T().f()]].f();
                   }
                 };)cpp",
       R"cpp(class T {
                   T f() {
                     auto placeholder = T().f(); return placeholder.f();
                   }
                 };)cpp"},
      // Function DeclRefExpr
      {R"cpp(int f() {
                   return [[f]]();
                 })cpp",
       R"cpp(int f() {
                   auto placeholder = f(); return placeholder;
                 })cpp"},
      // FIXME: Wrong result for \[\[clang::uninitialized\]\] int b = [[1]];
      // since the attr is inside the DeclStmt and the bounds of
      // DeclStmt don't cover the attribute.

      // Binary subexpressions
      {R"cpp(void f() {
                   int x = 1 + [[2 + 3 + 4]] + 5;
                 })cpp",
       R"cpp(void f() {
                   auto placeholder = 2 + 3 + 4; int x = 1 + placeholder + 5;
                 })cpp"},
      {R"cpp(void f() {
                   int x = [[1 + 2 + 3]] + 4 + 5;
                 })cpp",
       R"cpp(void f() {
                   auto placeholder = 1 + 2 + 3; int x = placeholder + 4 + 5;
                 })cpp"},
      {R"cpp(void f() {
                   int x = 1 + 2 + [[3 + 4 + 5]];
                 })cpp",
       R"cpp(void f() {
                   auto placeholder = 3 + 4 + 5; int x = 1 + 2 + placeholder;
                 })cpp"},
      // Non-associative operations have no special support
      {R"cpp(void f() {
                   int x = 1 - [[2 - 3 - 4]] - 5;
                 })cpp",
       R"cpp(void f() {
                   auto placeholder = 1 - 2 - 3 - 4; int x = placeholder - 5;
                 })cpp"},
      // A mix of associative operators isn't associative.
      {R"cpp(void f() {
                   int x = 0 + 1 * [[2 + 3]] * 4 + 5;
                 })cpp",
       R"cpp(void f() {
                   auto placeholder = 1 * 2 + 3 * 4; int x = 0 + placeholder + 5;
                 })cpp"},
      // Overloaded operators are supported, we assume associativity
      // as if they were built-in.
      {R"cpp(struct S {
                   S(int);
                 };
                 S operator+(S, S);

                 void f() {
                   S x = S(1) + [[S(2) + S(3) + S(4)]] + S(5);
                 })cpp",
       R"cpp(struct S {
                   S(int);
                 };
                 S operator+(S, S);

                 void f() {
                   auto placeholder = S(2) + S(3) + S(4); S x = S(1) + placeholder + S(5);
                 })cpp"},
      // Don't try to analyze across macro boundaries
      // FIXME: it'd be nice to do this someday (in a safe way)
      {R"cpp(#define ECHO(X) X
                 void f() {
                   int x = 1 + [[ECHO(2 + 3) + 4]] + 5;
                 })cpp",
       R"cpp(#define ECHO(X) X
                 void f() {
                   auto placeholder = 1 + ECHO(2 + 3) + 4; int x = placeholder + 5;
                 })cpp"},
      {R"cpp(#define ECHO(X) X
                 void f() {
                   int x = 1 + [[ECHO(2) + ECHO(3) + 4]] + 5;
                 })cpp",
       R"cpp(#define ECHO(X) X
                 void f() {
                   auto placeholder = 1 + ECHO(2) + ECHO(3) + 4; int x = placeholder + 5;
                 })cpp"},
  };
  for (const auto &IO : InputOutputs) {
    EXPECT_EQ(IO.second, apply(IO.first)) << IO.first;
  }
}

} // namespace
} // namespace clangd
} // namespace clang