1
+ // ===- unittests/Interpreter/CodeCompletionTest.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
+ // ===----------------------------------------------------------------------===//
8
+ //
9
+ // Unit tests for Clang's Interpreter library.
10
+ //
11
+ // ===----------------------------------------------------------------------===//
12
+
13
+ #include " InterpreterTestFixture.h"
14
+
1
15
#include " clang/Interpreter/CodeCompletion.h"
2
16
#include " clang/Frontend/CompilerInstance.h"
3
17
#include " clang/Interpreter/Interpreter.h"
12
26
#include " gmock/gmock.h"
13
27
#include " gtest/gtest.h"
14
28
15
- #if defined(_AIX) || defined(__MVS__)
16
- #define CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
17
- #endif
18
-
19
29
using namespace clang ;
20
30
namespace {
21
31
auto CB = clang::IncrementalCompilerBuilder();
22
32
23
- static std::unique_ptr<Interpreter> createInterpreter () {
24
- auto CI = cantFail (CB.CreateCpp ());
25
- return cantFail (clang::Interpreter::create (std::move (CI)));
26
- }
27
-
28
- static std::vector<std::string> runComp (clang::Interpreter &MainInterp,
29
- llvm::StringRef Input,
30
- llvm::Error &ErrR) {
31
- auto CI = CB.CreateCpp ();
32
- if (auto Err = CI.takeError ()) {
33
- ErrR = std::move (Err);
34
- return {};
35
- }
36
-
37
- auto Interp = clang::Interpreter::create (std::move (*CI));
38
- if (auto Err = Interp.takeError ()) {
39
- // log the error and returns an empty vector;
40
- ErrR = std::move (Err);
41
-
42
- return {};
33
+ class CodeCompletionTest : public InterpreterTestBase {
34
+ public:
35
+ std::unique_ptr<CompilerInstance> CI;
36
+ std::unique_ptr<Interpreter> Interp;
37
+
38
+ CodeCompletionTest ()
39
+ : CI(cantFail(CB.CreateCpp())),
40
+ Interp (cantFail(clang::Interpreter::create(std::move(CI)))) {}
41
+
42
+ std::vector<std::string> runComp (llvm::StringRef Input, llvm::Error &ErrR) {
43
+ auto ComplCI = CB.CreateCpp ();
44
+ if (auto Err = ComplCI.takeError ()) {
45
+ ErrR = std::move (Err);
46
+ return {};
47
+ }
48
+
49
+ auto ComplInterp = clang::Interpreter::create (std::move (*ComplCI));
50
+ if (auto Err = ComplInterp.takeError ()) {
51
+ ErrR = std::move (Err);
52
+ return {};
53
+ }
54
+
55
+ std::vector<std::string> Results;
56
+ std::vector<std::string> Comps;
57
+ auto *ParentCI = this ->Interp ->getCompilerInstance ();
58
+ auto *MainCI = (*ComplInterp)->getCompilerInstance ();
59
+ auto CC = ReplCodeCompleter ();
60
+ CC.codeComplete (MainCI, Input, /* Lines */ 1 , Input.size () + 1 , ParentCI,
61
+ Results);
62
+
63
+ for (auto Res : Results)
64
+ if (Res.find (CC.Prefix ) == 0 )
65
+ Comps.push_back (Res);
66
+ return Comps;
43
67
}
68
+ };
44
69
45
- std::vector<std::string> Results;
46
- std::vector<std::string> Comps;
47
- auto *MainCI = (*Interp)->getCompilerInstance ();
48
- auto CC = ReplCodeCompleter ();
49
- CC.codeComplete (MainCI, Input, /* Lines */ 1 , Input.size () + 1 ,
50
- MainInterp.getCompilerInstance (), Results);
51
-
52
- for (auto Res : Results)
53
- if (Res.find (CC.Prefix ) == 0 )
54
- Comps.push_back (Res);
55
- return Comps;
56
- }
57
-
58
- static bool HostSupportsJit () {
59
- auto J = llvm::orc::LLJITBuilder ().create ();
60
- if (J)
61
- return true ;
62
- LLVMConsumeError (llvm::wrap (J.takeError ()));
63
- return false ;
64
- }
65
-
66
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
67
- TEST (CodeCompletionTest, DISABLED_Sanity) {
68
- #else
69
- TEST (CodeCompletionTest, Sanity) {
70
- #endif
71
- if (!HostSupportsJit ())
72
- GTEST_SKIP ();
73
- auto Interp = createInterpreter ();
70
+ TEST_F (CodeCompletionTest, Sanity) {
74
71
cantFail (Interp->Parse (" int foo = 12;" ));
75
72
auto Err = llvm::Error::success ();
76
- auto comps = runComp (*Interp, " f" , Err);
73
+ auto comps = runComp (" f" , Err);
77
74
EXPECT_EQ ((size_t )2 , comps.size ()); // float and foo
78
75
EXPECT_EQ (comps[0 ], std::string (" float" ));
79
76
EXPECT_EQ (comps[1 ], std::string (" foo" ));
80
77
EXPECT_EQ ((bool )Err, false );
81
78
}
82
79
83
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
84
- TEST (CodeCompletionTest, DISABLED_SanityNoneValid) {
85
- #else
86
- TEST (CodeCompletionTest, SanityNoneValid) {
87
- #endif
88
- if (!HostSupportsJit ())
89
- GTEST_SKIP ();
90
- auto Interp = createInterpreter ();
80
+ TEST_F (CodeCompletionTest, SanityNoneValid) {
91
81
cantFail (Interp->Parse (" int foo = 12;" ));
92
82
auto Err = llvm::Error::success ();
93
- auto comps = runComp (*Interp, " babanana" , Err);
83
+ auto comps = runComp (" babanana" , Err);
94
84
EXPECT_EQ ((size_t )0 , comps.size ()); // foo and float
95
85
EXPECT_EQ ((bool )Err, false );
96
86
}
97
87
98
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
99
- TEST (CodeCompletionTest, DISABLED_TwoDecls) {
100
- #else
101
- TEST (CodeCompletionTest, TwoDecls) {
102
- #endif
103
- if (!HostSupportsJit ())
104
- GTEST_SKIP ();
105
- auto Interp = createInterpreter ();
88
+ TEST_F (CodeCompletionTest, TwoDecls) {
106
89
cantFail (Interp->Parse (" int application = 12;" ));
107
90
cantFail (Interp->Parse (" int apple = 12;" ));
108
91
auto Err = llvm::Error::success ();
109
- auto comps = runComp (*Interp, " app" , Err);
92
+ auto comps = runComp (" app" , Err);
110
93
EXPECT_EQ ((size_t )2 , comps.size ());
111
94
EXPECT_EQ ((bool )Err, false );
112
95
}
113
96
114
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
115
- TEST (CodeCompletionTest, DISABLED_CompFunDeclsNoError) {
116
- #else
117
- TEST (CodeCompletionTest, CompFunDeclsNoError) {
118
- #endif
119
- if (!HostSupportsJit ())
120
- GTEST_SKIP ();
121
- auto Interp = createInterpreter ();
97
+ TEST_F (CodeCompletionTest, CompFunDeclsNoError) {
122
98
auto Err = llvm::Error::success ();
123
- auto comps = runComp (*Interp, " void app(" , Err);
99
+ auto comps = runComp (" void app(" , Err);
124
100
EXPECT_EQ ((bool )Err, false );
125
101
}
126
102
127
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
128
- TEST (CodeCompletionTest, DISABLED_TypedDirected) {
129
- #else
130
- TEST (CodeCompletionTest, TypedDirected) {
131
- #endif
132
- if (!HostSupportsJit ())
133
- GTEST_SKIP ();
134
- auto Interp = createInterpreter ();
103
+ TEST_F (CodeCompletionTest, TypedDirected) {
135
104
cantFail (Interp->Parse (" int application = 12;" ));
136
105
cantFail (Interp->Parse (" char apple = '2';" ));
137
106
cantFail (Interp->Parse (" void add(int &SomeInt){}" ));
138
107
{
139
108
auto Err = llvm::Error::success ();
140
- auto comps = runComp (*Interp, std::string (" add(" ), Err);
109
+ auto comps = runComp (std::string (" add(" ), Err);
141
110
EXPECT_EQ ((size_t )1 , comps.size ());
142
111
EXPECT_EQ ((bool )Err, false );
143
112
}
@@ -146,7 +115,7 @@ TEST(CodeCompletionTest, TypedDirected) {
146
115
147
116
{
148
117
auto Err = llvm::Error::success ();
149
- auto comps = runComp (*Interp, std::string (" add(" ), Err);
118
+ auto comps = runComp (std::string (" add(" ), Err);
150
119
EXPECT_EQ ((size_t )2 , comps.size ());
151
120
EXPECT_EQ (comps[0 ], " application" );
152
121
EXPECT_EQ (comps[1 ], " banana" );
@@ -155,156 +124,107 @@ TEST(CodeCompletionTest, TypedDirected) {
155
124
156
125
{
157
126
auto Err = llvm::Error::success ();
158
- auto comps = runComp (*Interp, std::string (" add(b" ), Err);
127
+ auto comps = runComp (std::string (" add(b" ), Err);
159
128
EXPECT_EQ ((size_t )1 , comps.size ());
160
129
EXPECT_EQ (comps[0 ], " banana" );
161
130
EXPECT_EQ ((bool )Err, false );
162
131
}
163
132
}
164
133
165
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
166
- TEST (CodeCompletionTest, DISABLED_SanityClasses) {
167
- #else
168
- TEST (CodeCompletionTest, SanityClasses) {
169
- #endif
170
- if (!HostSupportsJit ())
171
- GTEST_SKIP ();
172
- auto Interp = createInterpreter ();
134
+ TEST_F (CodeCompletionTest, SanityClasses) {
173
135
cantFail (Interp->Parse (" struct Apple{};" ));
174
136
cantFail (Interp->Parse (" void takeApple(Apple &a1){}" ));
175
137
cantFail (Interp->Parse (" Apple a1;" ));
176
138
cantFail (Interp->Parse (" void takeAppleCopy(Apple a1){}" ));
177
139
178
140
{
179
141
auto Err = llvm::Error::success ();
180
- auto comps = runComp (*Interp, " takeApple(" , Err);
142
+ auto comps = runComp (" takeApple(" , Err);
181
143
EXPECT_EQ ((size_t )1 , comps.size ());
182
144
EXPECT_EQ (comps[0 ], std::string (" a1" ));
183
145
EXPECT_EQ ((bool )Err, false );
184
146
}
185
147
{
186
148
auto Err = llvm::Error::success ();
187
- auto comps = runComp (*Interp, std::string (" takeAppleCopy(" ), Err);
149
+ auto comps = runComp (std::string (" takeAppleCopy(" ), Err);
188
150
EXPECT_EQ ((size_t )1 , comps.size ());
189
151
EXPECT_EQ (comps[0 ], std::string (" a1" ));
190
152
EXPECT_EQ ((bool )Err, false );
191
153
}
192
154
}
193
155
194
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
195
- TEST (CodeCompletionTest, DISABLED_SubClassing) {
196
- #else
197
- TEST (CodeCompletionTest, SubClassing) {
198
- #endif
199
- if (!HostSupportsJit ())
200
- GTEST_SKIP ();
201
- auto Interp = createInterpreter ();
156
+ TEST_F (CodeCompletionTest, SubClassing) {
202
157
cantFail (Interp->Parse (" struct Fruit {};" ));
203
158
cantFail (Interp->Parse (" struct Apple : Fruit{};" ));
204
159
cantFail (Interp->Parse (" void takeFruit(Fruit &f){}" ));
205
160
cantFail (Interp->Parse (" Apple a1;" ));
206
161
cantFail (Interp->Parse (" Fruit f1;" ));
207
162
auto Err = llvm::Error::success ();
208
- auto comps = runComp (*Interp, std::string (" takeFruit(" ), Err);
163
+ auto comps = runComp (std::string (" takeFruit(" ), Err);
209
164
EXPECT_EQ ((size_t )2 , comps.size ());
210
165
EXPECT_EQ (comps[0 ], std::string (" a1" ));
211
166
EXPECT_EQ (comps[1 ], std::string (" f1" ));
212
167
EXPECT_EQ ((bool )Err, false );
213
168
}
214
169
215
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
216
- TEST (CodeCompletionTest, DISABLED_MultipleArguments) {
217
- #else
218
- TEST (CodeCompletionTest, MultipleArguments) {
219
- #endif
220
- if (!HostSupportsJit ())
221
- GTEST_SKIP ();
222
- auto Interp = createInterpreter ();
170
+ TEST_F (CodeCompletionTest, MultipleArguments) {
223
171
cantFail (Interp->Parse (" int foo = 42;" ));
224
172
cantFail (Interp->Parse (" char fowl = 'A';" ));
225
173
cantFail (Interp->Parse (" void takeTwo(int &a, char b){}" ));
226
174
auto Err = llvm::Error::success ();
227
- auto comps = runComp (*Interp, std::string (" takeTwo(foo, " ), Err);
175
+ auto comps = runComp (std::string (" takeTwo(foo, " ), Err);
228
176
EXPECT_EQ ((size_t )1 , comps.size ());
229
177
EXPECT_EQ (comps[0 ], std::string (" fowl" ));
230
178
EXPECT_EQ ((bool )Err, false );
231
179
}
232
180
233
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
234
- TEST (CodeCompletionTest, DISABLED_Methods) {
235
- #else
236
- TEST (CodeCompletionTest, Methods) {
237
- #endif
238
- if (!HostSupportsJit ())
239
- GTEST_SKIP ();
240
- auto Interp = createInterpreter ();
181
+ TEST_F (CodeCompletionTest, Methods) {
241
182
cantFail (Interp->Parse (
242
183
" struct Foo{int add(int a){return 42;} int par(int b){return 42;}};" ));
243
184
cantFail (Interp->Parse (" Foo f1;" ));
244
185
245
186
auto Err = llvm::Error::success ();
246
- auto comps = runComp (*Interp, std::string (" f1." ), Err);
187
+ auto comps = runComp (std::string (" f1." ), Err);
247
188
EXPECT_EQ ((size_t )2 , comps.size ());
248
189
EXPECT_EQ (comps[0 ], std::string (" add" ));
249
190
EXPECT_EQ (comps[1 ], std::string (" par" ));
250
191
EXPECT_EQ ((bool )Err, false );
251
192
}
252
193
253
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
254
- TEST (CodeCompletionTest, DISABLED_MethodsInvocations) {
255
- #else
256
- TEST (CodeCompletionTest, MethodsInvocations) {
257
- #endif
258
- if (!HostSupportsJit ())
259
- GTEST_SKIP ();
260
- auto Interp = createInterpreter ();
194
+ TEST_F (CodeCompletionTest, MethodsInvocations) {
261
195
cantFail (Interp->Parse (
262
196
" struct Foo{int add(int a){return 42;} int par(int b){return 42;}};" ));
263
197
cantFail (Interp->Parse (" Foo f1;" ));
264
198
cantFail (Interp->Parse (" int a = 84;" ));
265
199
266
200
auto Err = llvm::Error::success ();
267
- auto comps = runComp (*Interp, std::string (" f1.add(" ), Err);
201
+ auto comps = runComp (std::string (" f1.add(" ), Err);
268
202
EXPECT_EQ ((size_t )1 , comps.size ());
269
203
EXPECT_EQ (comps[0 ], std::string (" a" ));
270
204
EXPECT_EQ ((bool )Err, false );
271
205
}
272
206
273
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
274
- TEST (CodeCompletionTest, DISABLED_NestedInvocations) {
275
- #else
276
- TEST (CodeCompletionTest, NestedInvocations) {
277
- #endif
278
- if (!HostSupportsJit ())
279
- GTEST_SKIP ();
280
- auto Interp = createInterpreter ();
207
+ TEST_F (CodeCompletionTest, NestedInvocations) {
281
208
cantFail (Interp->Parse (
282
209
" struct Foo{int add(int a){return 42;} int par(int b){return 42;}};" ));
283
210
cantFail (Interp->Parse (" Foo f1;" ));
284
211
cantFail (Interp->Parse (" int a = 84;" ));
285
212
cantFail (Interp->Parse (" int plus(int a, int b) { return a + b; }" ));
286
213
287
214
auto Err = llvm::Error::success ();
288
- auto comps = runComp (*Interp, std::string (" plus(42, f1.add(" ), Err);
215
+ auto comps = runComp (std::string (" plus(42, f1.add(" ), Err);
289
216
EXPECT_EQ ((size_t )1 , comps.size ());
290
217
EXPECT_EQ (comps[0 ], std::string (" a" ));
291
218
EXPECT_EQ ((bool )Err, false );
292
219
}
293
220
294
- #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
295
- TEST (CodeCompletionTest, DISABLED_TemplateFunctions) {
296
- #else
297
- TEST (CodeCompletionTest, TemplateFunctions) {
298
- #endif
299
- if (!HostSupportsJit ())
300
- GTEST_SKIP ();
301
- auto Interp = createInterpreter ();
221
+ TEST_F (CodeCompletionTest, TemplateFunctions) {
302
222
cantFail (
303
223
Interp->Parse (" template <typename T> T id(T a) { return a;} " ));
304
224
cantFail (Interp->Parse (" int apple = 84;" ));
305
225
{
306
226
auto Err = llvm::Error::success ();
307
- auto comps = runComp (*Interp, std::string (" id<int>(" ), Err);
227
+ auto comps = runComp (std::string (" id<int>(" ), Err);
308
228
EXPECT_EQ ((size_t )1 , comps.size ());
309
229
EXPECT_EQ (comps[0 ], std::string (" apple" ));
310
230
EXPECT_EQ ((bool )Err, false );
@@ -315,7 +235,7 @@ TEST(CodeCompletionTest, TemplateFunctions) {
315
235
cantFail (Interp->Parse (" char pear = '4';" ));
316
236
{
317
237
auto Err = llvm::Error::success ();
318
- auto comps = runComp (*Interp, std::string (" pickFirst(apple, " ), Err);
238
+ auto comps = runComp (std::string (" pickFirst(apple, " ), Err);
319
239
EXPECT_EQ ((size_t )1 , comps.size ());
320
240
EXPECT_EQ (comps[0 ], std::string (" apple" ));
321
241
EXPECT_EQ ((bool )Err, false );
0 commit comments