aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Interpreter/CodeCompletionTest.cpp
blob: 23cfc469695d2885067c2529c12c9a3c620be5ce (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
//===- unittests/Interpreter/CodeCompletionTest.cpp -----------------------===//
//
// 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 "InterpreterTestFixture.h"

#include "clang/Interpreter/CodeCompletion.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Interpreter/Interpreter.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/CodeCompleteConsumer.h"
#include "clang/Sema/Sema.h"
#include "llvm/LineEditor/LineEditor.h"
#include "llvm/Support/raw_ostream.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace clang;
namespace {
auto CB = clang::IncrementalCompilerBuilder();

class CodeCompletionTest : public InterpreterTestBase {
public:
  std::unique_ptr<clang::Interpreter> Interp;

  void SetUp() override {
    if (!HostSupportsJIT())
      GTEST_SKIP();
    std::unique_ptr<CompilerInstance> CI = cantFail(CB.CreateCpp());
    this->Interp = cantFail(clang::Interpreter::create(std::move(CI)));
  }

  std::vector<std::string> runComp(llvm::StringRef Input, llvm::Error &ErrR) {
    auto ComplCI = CB.CreateCpp();
    if (auto Err = ComplCI.takeError()) {
      ErrR = std::move(Err);
      return {};
    }

    auto ComplInterp = clang::Interpreter::create(std::move(*ComplCI));
    if (auto Err = ComplInterp.takeError()) {
      ErrR = std::move(Err);
      return {};
    }

    std::vector<std::string> Results;
    std::vector<std::string> Comps;
    auto *ParentCI = this->Interp->getCompilerInstance();
    auto *MainCI = (*ComplInterp)->getCompilerInstance();
    auto CC = ReplCodeCompleter();
    CC.codeComplete(MainCI, Input, /* Lines */ 1, Input.size() + 1, ParentCI,
                    Results);

    for (auto Res : Results)
      if (Res.find(CC.Prefix) == 0)
        Comps.push_back(Res);
    return Comps;
  }
};

TEST_F(CodeCompletionTest, Sanity) {
  cantFail(Interp->Parse("int foo = 12;"));
  auto Err = llvm::Error::success();
  auto comps = runComp("f", Err);
  EXPECT_EQ((size_t)2, comps.size()); // float and foo
  EXPECT_EQ(comps[0], std::string("float"));
  EXPECT_EQ(comps[1], std::string("foo"));
  EXPECT_EQ((bool)Err, false);
}

TEST_F(CodeCompletionTest, SanityNoneValid) {
  cantFail(Interp->Parse("int foo = 12;"));
  auto Err = llvm::Error::success();
  auto comps = runComp("babanana", Err);
  EXPECT_EQ((size_t)0, comps.size()); // foo and float
  EXPECT_EQ((bool)Err, false);
}

TEST_F(CodeCompletionTest, TwoDecls) {
  cantFail(Interp->Parse("int application = 12;"));
  cantFail(Interp->Parse("int apple = 12;"));
  auto Err = llvm::Error::success();
  auto comps = runComp("app", Err);
  EXPECT_EQ((size_t)2, comps.size());
  EXPECT_EQ((bool)Err, false);
}

TEST_F(CodeCompletionTest, CompFunDeclsNoError) {
  auto Err = llvm::Error::success();
  auto comps = runComp("void app(", Err);
  EXPECT_EQ((bool)Err, false);
}

TEST_F(CodeCompletionTest, TypedDirected) {
  cantFail(Interp->Parse("int application = 12;"));
  cantFail(Interp->Parse("char apple = '2';"));
  cantFail(Interp->Parse("void add(int &SomeInt){}"));
  {
    auto Err = llvm::Error::success();
    auto comps = runComp(std::string("add("), Err);
    EXPECT_EQ((size_t)1, comps.size());
    EXPECT_EQ((bool)Err, false);
  }

  cantFail(Interp->Parse("int banana = 42;"));

  {
    auto Err = llvm::Error::success();
    auto comps = runComp(std::string("add("), Err);
    EXPECT_EQ((size_t)2, comps.size());
    EXPECT_EQ(comps[0], "application");
    EXPECT_EQ(comps[1], "banana");
    EXPECT_EQ((bool)Err, false);
  }

  {
    auto Err = llvm::Error::success();
    auto comps = runComp(std::string("add(b"), Err);
    EXPECT_EQ((size_t)1, comps.size());
    EXPECT_EQ(comps[0], "banana");
    EXPECT_EQ((bool)Err, false);
  }
}

TEST_F(CodeCompletionTest, SanityClasses) {
  cantFail(Interp->Parse("struct Apple{};"));
  cantFail(Interp->Parse("void takeApple(Apple &a1){}"));
  cantFail(Interp->Parse("Apple a1;"));
  cantFail(Interp->Parse("void takeAppleCopy(Apple a1){}"));

  {
    auto Err = llvm::Error::success();
    auto comps = runComp("takeApple(", Err);
    EXPECT_EQ((size_t)1, comps.size());
    EXPECT_EQ(comps[0], std::string("a1"));
    EXPECT_EQ((bool)Err, false);
  }
  {
    auto Err = llvm::Error::success();
    auto comps = runComp(std::string("takeAppleCopy("), Err);
    EXPECT_EQ((size_t)1, comps.size());
    EXPECT_EQ(comps[0], std::string("a1"));
    EXPECT_EQ((bool)Err, false);
  }
}

TEST_F(CodeCompletionTest, SubClassing) {
  cantFail(Interp->Parse("struct Fruit {};"));
  cantFail(Interp->Parse("struct Apple : Fruit{};"));
  cantFail(Interp->Parse("void takeFruit(Fruit &f){}"));
  cantFail(Interp->Parse("Apple a1;"));
  cantFail(Interp->Parse("Fruit f1;"));
  auto Err = llvm::Error::success();
  auto comps = runComp(std::string("takeFruit("), Err);
  EXPECT_EQ((size_t)2, comps.size());
  EXPECT_EQ(comps[0], std::string("a1"));
  EXPECT_EQ(comps[1], std::string("f1"));
  EXPECT_EQ((bool)Err, false);
}

TEST_F(CodeCompletionTest, MultipleArguments) {
  cantFail(Interp->Parse("int foo = 42;"));
  cantFail(Interp->Parse("char fowl = 'A';"));
  cantFail(Interp->Parse("void takeTwo(int &a, char b){}"));
  auto Err = llvm::Error::success();
  auto comps = runComp(std::string("takeTwo(foo,  "), Err);
  EXPECT_EQ((size_t)1, comps.size());
  EXPECT_EQ(comps[0], std::string("fowl"));
  EXPECT_EQ((bool)Err, false);
}

TEST_F(CodeCompletionTest, Methods) {
  cantFail(Interp->Parse(
      "struct Foo{int add(int a){return 42;} int par(int b){return 42;}};"));
  cantFail(Interp->Parse("Foo f1;"));

  auto Err = llvm::Error::success();
  auto comps = runComp(std::string("f1."), Err);
  EXPECT_EQ((size_t)2, comps.size());
  EXPECT_EQ(comps[0], std::string("add"));
  EXPECT_EQ(comps[1], std::string("par"));
  EXPECT_EQ((bool)Err, false);
}

TEST_F(CodeCompletionTest, MethodsInvocations) {
  cantFail(Interp->Parse(
      "struct Foo{int add(int a){return 42;} int par(int b){return 42;}};"));
  cantFail(Interp->Parse("Foo f1;"));
  cantFail(Interp->Parse("int a = 84;"));

  auto Err = llvm::Error::success();
  auto comps = runComp(std::string("f1.add("), Err);
  EXPECT_EQ((size_t)1, comps.size());
  EXPECT_EQ(comps[0], std::string("a"));
  EXPECT_EQ((bool)Err, false);
}

TEST_F(CodeCompletionTest, NestedInvocations) {
  cantFail(Interp->Parse(
      "struct Foo{int add(int a){return 42;} int par(int b){return 42;}};"));
  cantFail(Interp->Parse("Foo f1;"));
  cantFail(Interp->Parse("int a = 84;"));
  cantFail(Interp->Parse("int plus(int a, int b) { return a + b; }"));

  auto Err = llvm::Error::success();
  auto comps = runComp(std::string("plus(42, f1.add("), Err);
  EXPECT_EQ((size_t)1, comps.size());
  EXPECT_EQ(comps[0], std::string("a"));
  EXPECT_EQ((bool)Err, false);
}

TEST_F(CodeCompletionTest, TemplateFunctions) {
  cantFail(
      Interp->Parse("template <typename T> T id(T a) { return a;} "));
  cantFail(Interp->Parse("int apple = 84;"));
  {
    auto Err = llvm::Error::success();
    auto comps = runComp(std::string("id<int>("), Err);
    EXPECT_EQ((size_t)1, comps.size());
    EXPECT_EQ(comps[0], std::string("apple"));
    EXPECT_EQ((bool)Err, false);
  }

  cantFail(Interp->Parse(
      "template <typename T> T pickFirst(T a, T b) { return a;} "));
  cantFail(Interp->Parse("char pear = '4';"));
  {
    auto Err = llvm::Error::success();
    auto comps = runComp(std::string("pickFirst(apple, "), Err);
    EXPECT_EQ((size_t)1, comps.size());
    EXPECT_EQ(comps[0], std::string("apple"));
    EXPECT_EQ((bool)Err, false);
  }
}

} // anonymous namespace