summaryrefslogtreecommitdiffstats
path: root/clang/unittests/Tooling/ASTSelectionTest.cpp
blob: b4e8bc042b89a31fa8c91122fe69b4c8e8a93c86 (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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//===- unittest/Tooling/ASTSelectionTest.cpp ------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "TestVisitor.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Tooling/Refactoring/ASTSelection.h"

using namespace clang;
using namespace tooling;

namespace {

struct FileLocation {
  unsigned Line, Column;

  SourceLocation translate(const SourceManager &SM) {
    return SM.translateLineCol(SM.getMainFileID(), Line, Column);
  }
};

using FileRange = std::pair<FileLocation, FileLocation>;

class SelectionFinderVisitor : public TestVisitor<SelectionFinderVisitor> {
  FileLocation Location;
  Optional<FileRange> SelectionRange;

public:
  Optional<SelectedASTNode> Selection;

  SelectionFinderVisitor(FileLocation Location,
                         Optional<FileRange> SelectionRange)
      : Location(Location), SelectionRange(SelectionRange) {}

  bool VisitTranslationUnitDecl(const TranslationUnitDecl *TU) {
    const ASTContext &Context = TU->getASTContext();
    const SourceManager &SM = Context.getSourceManager();

    SourceRange SelRange;
    if (SelectionRange) {
      SelRange = SourceRange(SelectionRange->first.translate(SM),
                             SelectionRange->second.translate(SM));
    } else {
      SourceLocation Loc = Location.translate(SM);
      SelRange = SourceRange(Loc, Loc);
    }
    Selection = findSelectedASTNodes(Context, SelRange);
    return false;
  }
};

Optional<SelectedASTNode>
findSelectedASTNodes(StringRef Source, FileLocation Location,
                     Optional<FileRange> SelectionRange,
                     SelectionFinderVisitor::Language Language =
                         SelectionFinderVisitor::Lang_CXX11) {
  SelectionFinderVisitor Visitor(Location, SelectionRange);
  EXPECT_TRUE(Visitor.runOver(Source, Language));
  return std::move(Visitor.Selection);
}

void checkNodeImpl(bool IsTypeMatched, const SelectedASTNode &Node,
                   SourceSelectionKind SelectionKind, unsigned NumChildren) {
  ASSERT_TRUE(IsTypeMatched);
  EXPECT_EQ(Node.Children.size(), NumChildren);
  ASSERT_EQ(Node.SelectionKind, SelectionKind);
}

void checkDeclName(const SelectedASTNode &Node, StringRef Name) {
  const auto *ND = Node.Node.get<NamedDecl>();
  EXPECT_TRUE(!!ND);
  ASSERT_EQ(ND->getName(), Name);
}

template <typename T>
const SelectedASTNode &
checkNode(const SelectedASTNode &StmtNode, SourceSelectionKind SelectionKind,
          unsigned NumChildren = 0,
          typename std::enable_if<std::is_base_of<Stmt, T>::value, T>::type
              *StmtOverloadChecker = nullptr) {
  checkNodeImpl(isa<T>(StmtNode.Node.get<Stmt>()), StmtNode, SelectionKind,
                NumChildren);
  return StmtNode;
}

template <typename T>
const SelectedASTNode &
checkNode(const SelectedASTNode &DeclNode, SourceSelectionKind SelectionKind,
          unsigned NumChildren = 0, StringRef Name = "",
          typename std::enable_if<std::is_base_of<Decl, T>::value, T>::type
              *DeclOverloadChecker = nullptr) {
  checkNodeImpl(isa<T>(DeclNode.Node.get<Decl>()), DeclNode, SelectionKind,
                NumChildren);
  if (!Name.empty())
    checkDeclName(DeclNode, Name);
  return DeclNode;
}

struct ForAllChildrenOf {
  const SelectedASTNode &Node;

  static void childKindVerifier(const SelectedASTNode &Node,
                                SourceSelectionKind SelectionKind) {
    for (const SelectedASTNode &Child : Node.Children) {
      ASSERT_EQ(Node.SelectionKind, SelectionKind);
      childKindVerifier(Child, SelectionKind);
    }
  }

public:
  ForAllChildrenOf(const SelectedASTNode &Node) : Node(Node) {}

  void shouldHaveSelectionKind(SourceSelectionKind Kind) {
    childKindVerifier(Node, Kind);
  }
};

ForAllChildrenOf allChildrenOf(const SelectedASTNode &Node) {
  return ForAllChildrenOf(Node);
}

TEST(ASTSelectionFinder, CursorNoSelection) {
  Optional<SelectedASTNode> Node =
      findSelectedASTNodes(" void f() { }", {1, 1}, None);
  EXPECT_FALSE(Node);
}

TEST(ASTSelectionFinder, CursorAtStartOfFunction) {
  Optional<SelectedASTNode> Node =
      findSelectedASTNodes("void f() { }", {1, 1}, None);
  EXPECT_TRUE(Node);
  checkNode<TranslationUnitDecl>(*Node, SourceSelectionKind::None,
                                 /*NumChildren=*/1);
  checkNode<FunctionDecl>(Node->Children[0],
                          SourceSelectionKind::ContainsSelection,
                          /*NumChildren=*/0, /*Name=*/"f");

  // Check that the dumping works.
  std::string DumpValue;
  llvm::raw_string_ostream OS(DumpValue);
  Node->Children[0].dump(OS);
  ASSERT_EQ(OS.str(), "FunctionDecl \"f\" contains-selection\n");
}

TEST(ASTSelectionFinder, RangeNoSelection) {
  {
    Optional<SelectedASTNode> Node = findSelectedASTNodes(
        " void f() { }", {1, 1}, FileRange{{1, 1}, {1, 1}});
    EXPECT_FALSE(Node);
  }
  {
    Optional<SelectedASTNode> Node = findSelectedASTNodes(
        "  void f() { }", {1, 1}, FileRange{{1, 1}, {1, 2}});
    EXPECT_FALSE(Node);
  }
}

TEST(ASTSelectionFinder, EmptyRangeFallbackToCursor) {
  Optional<SelectedASTNode> Node =
      findSelectedASTNodes("void f() { }", {1, 1}, FileRange{{1, 1}, {1, 1}});
  EXPECT_TRUE(Node);
  checkNode<FunctionDecl>(Node->Children[0],
                          SourceSelectionKind::ContainsSelection,
                          /*NumChildren=*/0, /*Name=*/"f");
}

TEST(ASTSelectionFinder, WholeFunctionSelection) {
  StringRef Source = "int f(int x) { return x;\n}\nvoid f2() { }";
  // From 'int' until just after '}':
  {
    auto Node = findSelectedASTNodes(Source, {1, 1}, FileRange{{1, 1}, {2, 2}});
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    const auto &Fn = checkNode<FunctionDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/2, /*Name=*/"f");
    checkNode<ParmVarDecl>(Fn.Children[0],
                           SourceSelectionKind::InsideSelection);
    const auto &Body = checkNode<CompoundStmt>(
        Fn.Children[1], SourceSelectionKind::InsideSelection,
        /*NumChildren=*/1);
    const auto &Return = checkNode<ReturnStmt>(
        Body.Children[0], SourceSelectionKind::InsideSelection,
        /*NumChildren=*/1);
    checkNode<ImplicitCastExpr>(Return.Children[0],
                                SourceSelectionKind::InsideSelection,
                                /*NumChildren=*/1);
    checkNode<DeclRefExpr>(Return.Children[0].Children[0],
                           SourceSelectionKind::InsideSelection);
  }
  // From 'int' until just before '}':
  {
    auto Node = findSelectedASTNodes(Source, {2, 1}, FileRange{{1, 1}, {2, 1}});
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    const auto &Fn = checkNode<FunctionDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/2, /*Name=*/"f");
    const auto &Body = checkNode<CompoundStmt>(
        Fn.Children[1], SourceSelectionKind::ContainsSelectionEnd,
        /*NumChildren=*/1);
    checkNode<ReturnStmt>(Body.Children[0],
                          SourceSelectionKind::InsideSelection,
                          /*NumChildren=*/1);
  }
  // From '{' until just after '}':
  {
    auto Node =
        findSelectedASTNodes(Source, {1, 14}, FileRange{{1, 14}, {2, 2}});
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    const auto &Fn = checkNode<FunctionDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/1, /*Name=*/"f");
    const auto &Body = checkNode<CompoundStmt>(
        Fn.Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/1);
    checkNode<ReturnStmt>(Body.Children[0],
                          SourceSelectionKind::InsideSelection,
                          /*NumChildren=*/1);
  }
  // From 'x' until just after '}':
  {
    auto Node =
        findSelectedASTNodes(Source, {2, 2}, FileRange{{1, 11}, {2, 2}});
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    const auto &Fn = checkNode<FunctionDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/2, /*Name=*/"f");
    checkNode<ParmVarDecl>(Fn.Children[0],
                           SourceSelectionKind::ContainsSelectionStart);
    const auto &Body = checkNode<CompoundStmt>(
        Fn.Children[1], SourceSelectionKind::InsideSelection,
        /*NumChildren=*/1);
    checkNode<ReturnStmt>(Body.Children[0],
                          SourceSelectionKind::InsideSelection,
                          /*NumChildren=*/1);
  }
}

TEST(ASTSelectionFinder, MultipleFunctionSelection) {
  StringRef Source = R"(void f0() {
}
void f1() { }
void f2() { }
void f3() { }
)";
  auto SelectedF1F2 = [](Optional<SelectedASTNode> Node) {
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 2u);
    checkNode<FunctionDecl>(Node->Children[0],
                            SourceSelectionKind::InsideSelection,
                            /*NumChildren=*/1, /*Name=*/"f1");
    checkNode<FunctionDecl>(Node->Children[1],
                            SourceSelectionKind::InsideSelection,
                            /*NumChildren=*/1, /*Name=*/"f2");
  };
  // Just after '}' of f0 and just before 'void' of f3:
  SelectedF1F2(findSelectedASTNodes(Source, {2, 2}, FileRange{{2, 2}, {5, 1}}));
  // Just before 'void' of f1 and just after '}' of f2:
  SelectedF1F2(
      findSelectedASTNodes(Source, {3, 1}, FileRange{{3, 1}, {4, 14}}));
}

TEST(ASTSelectionFinder, MultipleStatementSelection) {
  StringRef Source = R"(void f(int x, int y) {
  int z = x;
  f(2, 3);
  if (x == 0) {
    return;
  }
  x = 1;
  return;
})";
  // From 'f(2,3)' until just before 'x = 1;':
  {
    auto Node = findSelectedASTNodes(Source, {3, 2}, FileRange{{3, 2}, {7, 1}});
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    const auto &Fn = checkNode<FunctionDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/1, /*Name=*/"f");
    const auto &Body = checkNode<CompoundStmt>(
        Fn.Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/2);
    allChildrenOf(checkNode<CallExpr>(Body.Children[0],
                                      SourceSelectionKind::InsideSelection,
                                      /*NumChildren=*/3))
        .shouldHaveSelectionKind(SourceSelectionKind::InsideSelection);
    allChildrenOf(checkNode<IfStmt>(Body.Children[1],
                                    SourceSelectionKind::InsideSelection,
                                    /*NumChildren=*/2))
        .shouldHaveSelectionKind(SourceSelectionKind::InsideSelection);
  }
  // From 'f(2,3)' until just before ';' in 'x = 1;':
  {
    auto Node = findSelectedASTNodes(Source, {3, 2}, FileRange{{3, 2}, {7, 8}});
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    const auto &Fn = checkNode<FunctionDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/1, /*Name=*/"f");
    const auto &Body = checkNode<CompoundStmt>(
        Fn.Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/3);
    checkNode<CallExpr>(Body.Children[0], SourceSelectionKind::InsideSelection,
                        /*NumChildren=*/3);
    checkNode<IfStmt>(Body.Children[1], SourceSelectionKind::InsideSelection,
                      /*NumChildren=*/2);
    checkNode<BinaryOperator>(Body.Children[2],
                              SourceSelectionKind::InsideSelection,
                              /*NumChildren=*/2);
  }
  // From the middle of 'int z = 3' until the middle of 'x = 1;':
  {
    auto Node =
        findSelectedASTNodes(Source, {2, 10}, FileRange{{2, 10}, {7, 5}});
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    const auto &Fn = checkNode<FunctionDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/1, /*Name=*/"f");
    const auto &Body = checkNode<CompoundStmt>(
        Fn.Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/4);
    checkNode<DeclStmt>(Body.Children[0],
                        SourceSelectionKind::ContainsSelectionStart,
                        /*NumChildren=*/1);
    checkNode<CallExpr>(Body.Children[1], SourceSelectionKind::InsideSelection,
                        /*NumChildren=*/3);
    checkNode<IfStmt>(Body.Children[2], SourceSelectionKind::InsideSelection,
                      /*NumChildren=*/2);
    checkNode<BinaryOperator>(Body.Children[3],
                              SourceSelectionKind::ContainsSelectionEnd,
                              /*NumChildren=*/1);
  }
}

TEST(ASTSelectionFinder, SelectionInFunctionInObjCImplementation) {
  StringRef Source = R"(
@interface I
@end
@implementation I

int notSelected() { }

int selected(int x) {
  return x;
}

@end
@implementation I(Cat)

void catF() { }

@end

void outerFunction() { }
)";
  // Just the 'x' expression in 'selected':
  {
    auto Node =
        findSelectedASTNodes(Source, {9, 10}, FileRange{{9, 10}, {9, 11}},
                             SelectionFinderVisitor::Lang_OBJC);
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    const auto &Impl = checkNode<ObjCImplementationDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/1, /*Name=*/"I");
    const auto &Fn = checkNode<FunctionDecl>(
        Impl.Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/1, /*Name=*/"selected");
    allChildrenOf(Fn).shouldHaveSelectionKind(
        SourceSelectionKind::ContainsSelection);
  }
  // The entire 'catF':
  {
    auto Node =
        findSelectedASTNodes(Source, {15, 1}, FileRange{{15, 1}, {15, 16}},
                             SelectionFinderVisitor::Lang_OBJC);
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    const auto &Impl = checkNode<ObjCCategoryImplDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/1, /*Name=*/"Cat");
    const auto &Fn = checkNode<FunctionDecl>(
        Impl.Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/1, /*Name=*/"catF");
    allChildrenOf(Fn).shouldHaveSelectionKind(
        SourceSelectionKind::ContainsSelection);
  }
  // From the line before 'selected' to the line after 'catF':
  {
    auto Node =
        findSelectedASTNodes(Source, {16, 1}, FileRange{{7, 1}, {16, 1}},
                             SelectionFinderVisitor::Lang_OBJC);
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 2u);
    const auto &Impl = checkNode<ObjCImplementationDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelectionStart,
        /*NumChildren=*/1, /*Name=*/"I");
    const auto &Selected = checkNode<FunctionDecl>(
        Impl.Children[0], SourceSelectionKind::InsideSelection,
        /*NumChildren=*/2, /*Name=*/"selected");
    allChildrenOf(Selected).shouldHaveSelectionKind(
        SourceSelectionKind::InsideSelection);
    const auto &Cat = checkNode<ObjCCategoryImplDecl>(
        Node->Children[1], SourceSelectionKind::ContainsSelectionEnd,
        /*NumChildren=*/1, /*Name=*/"Cat");
    const auto &CatF = checkNode<FunctionDecl>(
        Cat.Children[0], SourceSelectionKind::InsideSelection,
        /*NumChildren=*/1, /*Name=*/"catF");
    allChildrenOf(CatF).shouldHaveSelectionKind(
        SourceSelectionKind::InsideSelection);
  }
  // Just the 'outer' function:
  {
    auto Node =
        findSelectedASTNodes(Source, {19, 1}, FileRange{{19, 1}, {19, 25}},
                             SelectionFinderVisitor::Lang_OBJC);
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    checkNode<FunctionDecl>(Node->Children[0],
                            SourceSelectionKind::ContainsSelection,
                            /*NumChildren=*/1, /*Name=*/"outerFunction");
  }
}

TEST(ASTSelectionFinder, FunctionInObjCImplementationCarefulWithEarlyExit) {
  StringRef Source = R"(
@interface I
@end
@implementation I

void selected() {
}

- (void) method { }

@end
)";
  // Just 'selected'
  {
    auto Node = findSelectedASTNodes(Source, {6, 1}, FileRange{{6, 1}, {7, 2}},
                                     SelectionFinderVisitor::Lang_OBJC);
    EXPECT_TRUE(Node);
    EXPECT_EQ(Node->Children.size(), 1u);
    const auto &Impl = checkNode<ObjCImplementationDecl>(
        Node->Children[0], SourceSelectionKind::ContainsSelection,
        /*NumChildren=*/1, /*Name=*/"I");
    checkNode<FunctionDecl>(Impl.Children[0],
                            SourceSelectionKind::ContainsSelection,
                            /*NumChildren=*/1, /*Name=*/"selected");
  }
}

TEST(ASTSelectionFinder, AvoidImplicitDeclarations) {
  StringRef Source = R"(
struct Copy {
  int x;
};
void foo() {
  Copy x;
  Copy y = x;
}
)";
  // The entire struct 'Copy':
  auto Node = findSelectedASTNodes(Source, {2, 1}, FileRange{{2, 1}, {4, 3}});
  EXPECT_TRUE(Node);
  EXPECT_EQ(Node->Children.size(), 1u);
  const auto &Record = checkNode<CXXRecordDecl>(
      Node->Children[0], SourceSelectionKind::InsideSelection,
      /*NumChildren=*/1, /*Name=*/"Copy");
  checkNode<FieldDecl>(Record.Children[0],
                       SourceSelectionKind::InsideSelection);
}

} // end anonymous namespace
OpenPOWER on IntegriCloud