Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / unittests / Tooling / RecursiveASTVisitorTests / DeclRefExpr.cpp
blobadc972e1c3d9130cbfb87c29d29fc8b73dce5b6b
1 //===- unittest/Tooling/RecursiveASTVisitorTests/DeclRefExpr.cpp ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "TestVisitor.h"
11 using namespace clang;
13 namespace {
15 class DeclRefExprVisitor : public ExpectedLocationVisitor<DeclRefExprVisitor> {
16 public:
17 DeclRefExprVisitor() : ShouldVisitImplicitCode(false) {}
19 bool shouldVisitImplicitCode() const { return ShouldVisitImplicitCode; }
21 void setShouldVisitImplicitCode(bool NewValue) {
22 ShouldVisitImplicitCode = NewValue;
25 bool VisitDeclRefExpr(DeclRefExpr *Reference) {
26 Match(Reference->getNameInfo().getAsString(), Reference->getLocation());
27 return true;
30 private:
31 bool ShouldVisitImplicitCode;
34 TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArguments) {
35 DeclRefExprVisitor Visitor;
36 Visitor.ExpectMatch("x", 2, 3);
37 EXPECT_TRUE(Visitor.runOver(
38 "void x(); template <void (*T)()> class X {};\nX<x> y;"));
41 TEST(RecursiveASTVisitor, VisitsCXXForRangeStmtRange) {
42 DeclRefExprVisitor Visitor;
43 Visitor.ExpectMatch("x", 2, 25);
44 Visitor.ExpectMatch("x", 2, 30);
45 EXPECT_TRUE(Visitor.runOver(
46 "int x[5];\n"
47 "void f() { for (int i : x) { x[0] = 1; } }",
48 DeclRefExprVisitor::Lang_CXX11));
51 TEST(RecursiveASTVisitor, VisitsCallExpr) {
52 DeclRefExprVisitor Visitor;
53 Visitor.ExpectMatch("x", 1, 22);
54 EXPECT_TRUE(Visitor.runOver(
55 "void x(); void y() { x(); }"));
58 TEST(RecursiveASTVisitor, VisitsExplicitLambdaCaptureInit) {
59 DeclRefExprVisitor Visitor;
60 Visitor.ExpectMatch("i", 1, 20);
61 EXPECT_TRUE(Visitor.runOver(
62 "void f() { int i; [i]{}; }",
63 DeclRefExprVisitor::Lang_CXX11));
66 TEST(RecursiveASTVisitor, VisitsUseOfImplicitLambdaCapture) {
67 DeclRefExprVisitor Visitor;
68 Visitor.ExpectMatch("i", 1, 24);
69 EXPECT_TRUE(Visitor.runOver(
70 "void f() { int i; [=]{ i; }; }",
71 DeclRefExprVisitor::Lang_CXX11));
74 TEST(RecursiveASTVisitor, VisitsImplicitLambdaCaptureInit) {
75 DeclRefExprVisitor Visitor;
76 Visitor.setShouldVisitImplicitCode(true);
77 // We're expecting "i" to be visited twice: once for the initialization expr
78 // for the captured variable "i" outside of the lambda body, and again for
79 // the use of "i" inside the lambda.
80 Visitor.ExpectMatch("i", 1, 20, /*Times=*/1);
81 Visitor.ExpectMatch("i", 1, 24, /*Times=*/1);
82 EXPECT_TRUE(Visitor.runOver(
83 "void f() { int i; [=]{ i; }; }",
84 DeclRefExprVisitor::Lang_CXX11));
87 TEST(RecursiveASTVisitor, VisitsLambdaInitCaptureInit) {
88 DeclRefExprVisitor Visitor;
89 Visitor.ExpectMatch("i", 1, 24);
90 EXPECT_TRUE(Visitor.runOver(
91 "void f() { int i; [a = i + 1]{}; }",
92 DeclRefExprVisitor::Lang_CXX14));
95 /* FIXME: According to Richard Smith this is a bug in the AST.
96 TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArgumentsInInstantiation) {
97 DeclRefExprVisitor Visitor;
98 Visitor.ExpectMatch("x", 3, 43);
99 EXPECT_TRUE(Visitor.runOver(
100 "template <typename T> void x();\n"
101 "template <void (*T)()> class X {};\n"
102 "template <typename T> class Y : public X< x<T> > {};\n"
103 "Y<int> y;"));
107 TEST(RecursiveASTVisitor, VisitsExtension) {
108 DeclRefExprVisitor Visitor;
109 Visitor.ExpectMatch("s", 1, 24);
110 EXPECT_TRUE(Visitor.runOver(
111 "int s = __extension__ (s);\n"));
114 TEST(RecursiveASTVisitor, VisitsCopyExprOfBlockDeclCapture) {
115 DeclRefExprVisitor Visitor;
116 Visitor.ExpectMatch("x", 3, 24);
117 EXPECT_TRUE(Visitor.runOver("void f(int(^)(int)); \n"
118 "void g() { \n"
119 " f([&](int x){ return x; }); \n"
120 "}",
121 DeclRefExprVisitor::Lang_OBJCXX11));
124 } // end anonymous namespace