1 #include "clang/Interpreter/CodeCompletion.h"
2 #include "clang/Frontend/CompilerInstance.h"
3 #include "clang/Interpreter/Interpreter.h"
4 #include "clang/Sema/CodeCompleteConsumer.h"
5 #include "llvm/LineEditor/LineEditor.h"
6 #include "llvm/Support/Error.h"
7 #include "llvm/Support/raw_ostream.h"
9 #include "gmock/gmock.h"
10 #include "gtest/gtest.h"
12 using namespace clang
;
14 auto CB
= clang::IncrementalCompilerBuilder();
16 static std::unique_ptr
<Interpreter
> createInterpreter() {
17 auto CI
= cantFail(CB
.CreateCpp());
18 return cantFail(clang::Interpreter::create(std::move(CI
)));
21 static std::vector
<std::string
> runComp(clang::Interpreter
&MainInterp
,
22 llvm::StringRef Prefix
,
24 auto CI
= CB
.CreateCpp();
25 if (auto Err
= CI
.takeError()) {
26 ErrR
= std::move(Err
);
30 auto Interp
= clang::Interpreter::create(std::move(*CI
));
31 if (auto Err
= Interp
.takeError()) {
32 // log the error and returns an empty vector;
33 ErrR
= std::move(Err
);
38 std::vector
<std::string
> Results
;
39 std::vector
<std::string
> Comps
;
42 const_cast<clang::CompilerInstance
*>((*Interp
)->getCompilerInstance()),
43 Prefix
, /* Lines */ 1, Prefix
.size(), MainInterp
.getCompilerInstance(),
46 for (auto Res
: Results
)
47 if (Res
.find(Prefix
) == 0)
54 TEST(CodeCompletionTest
, DISABLED_Sanity
) {
56 TEST(CodeCompletionTest
, Sanity
) {
58 auto Interp
= createInterpreter();
59 if (auto R
= Interp
->ParseAndExecute("int foo = 12;")) {
60 consumeError(std::move(R
));
63 auto Err
= llvm::Error::success();
64 auto comps
= runComp(*Interp
, "f", Err
);
65 EXPECT_EQ((size_t)2, comps
.size()); // foo and float
66 EXPECT_EQ(comps
[0], std::string("foo"));
67 EXPECT_EQ((bool)Err
, false);
71 TEST(CodeCompletionTest
, DISABLED_SanityNoneValid
) {
73 TEST(CodeCompletionTest
, SanityNoneValid
) {
75 auto Interp
= createInterpreter();
76 if (auto R
= Interp
->ParseAndExecute("int foo = 12;")) {
77 consumeError(std::move(R
));
80 auto Err
= llvm::Error::success();
81 auto comps
= runComp(*Interp
, "babanana", Err
);
82 EXPECT_EQ((size_t)0, comps
.size()); // foo and float
83 EXPECT_EQ((bool)Err
, false);
87 TEST(CodeCompletionTest
, DISABLED_TwoDecls
) {
89 TEST(CodeCompletionTest
, TwoDecls
) {
91 auto Interp
= createInterpreter();
92 if (auto R
= Interp
->ParseAndExecute("int application = 12;")) {
93 consumeError(std::move(R
));
96 if (auto R
= Interp
->ParseAndExecute("int apple = 12;")) {
97 consumeError(std::move(R
));
100 auto Err
= llvm::Error::success();
101 auto comps
= runComp(*Interp
, "app", Err
);
102 EXPECT_EQ((size_t)2, comps
.size());
103 EXPECT_EQ((bool)Err
, false);
106 TEST(CodeCompletionTest
, CompFunDeclsNoError
) {
107 auto Interp
= createInterpreter();
108 auto Err
= llvm::Error::success();
109 auto comps
= runComp(*Interp
, "void app(", Err
);
110 EXPECT_EQ((bool)Err
, false);
113 } // anonymous namespace