From 8e967debeae0cda07fddd5253d799a795caf5f69 Mon Sep 17 00:00:00 2001 From: ykiko Date: Fri, 4 Oct 2024 17:29:49 +0800 Subject: [PATCH] Add test for CodeCompletion. --- tests/Feature/CodeCompletion.cpp | 78 ++++++++++++++++++++++++++++ tests/Source/CodeCompletion/test.cpp | 6 +++ tests/Source/CodeCompletion/test1.h | 1 + tests/Source/CodeCompletion/test2.h | 3 ++ 4 files changed, 88 insertions(+) create mode 100644 tests/Feature/CodeCompletion.cpp create mode 100644 tests/Source/CodeCompletion/test.cpp create mode 100644 tests/Source/CodeCompletion/test1.h create mode 100644 tests/Source/CodeCompletion/test2.h diff --git a/tests/Feature/CodeCompletion.cpp b/tests/Feature/CodeCompletion.cpp new file mode 100644 index 00000000..c3d95d96 --- /dev/null +++ b/tests/Feature/CodeCompletion.cpp @@ -0,0 +1,78 @@ +#include +#include + +namespace { + +using namespace clice; + +std::vector compileArgs = { + "clang++", + "-std=c++20", + "/home/ykiko/C++/clice2/tests/Source/CodeCompletion/test.cpp", + "-resource-dir", + "/home/ykiko/C++/clice2/build/lib/clang/20", +}; + +class CodeCompletionConsumer : public clang::CodeCompleteConsumer { +public: + void ProcessCodeCompleteResults(clang::Sema& S, + clang::CodeCompletionContext Context, + clang::CodeCompletionResult* Results, + unsigned NumResults) override { + for(unsigned i = 0; i < NumResults; ++i) { + auto str = + Results[i].CreateCodeCompletionString(S, Context, getAllocator(), getCodeCompletionTUInfo(), true); + llvm::outs() << str->getAsString() << "\n"; + } + } + + void ProcessOverloadCandidates(clang::Sema& S, + unsigned CurrentArg, + OverloadCandidate* Candidates, + unsigned NumCandidates, + clang::SourceLocation OpenParLoc, + bool Braced) override {} + + clang::CodeCompletionAllocator& getAllocator() override { + return *Allocator; + }; + + clang::CodeCompletionTUInfo& getCodeCompletionTUInfo() override { + return TUInfo; + } + + CodeCompletionConsumer() : + clang::CodeCompleteConsumer(clang::CodeCompleteOptions()), + Allocator(std::make_shared()), TUInfo(Allocator) {} + +private: + std::shared_ptr Allocator; + clang::CodeCompletionTUInfo TUInfo; +}; + +TEST(clice, CodeCompletion) { + auto invocation = clang::createInvocation(compileArgs, {}); + + auto& opt = invocation->getFrontendOpts().CodeCompletionAt; + opt.FileName = "/home/ykiko/C++/clice2/tests/Source/CodeCompletion/test2.h"; + opt.Line = 2; + opt.Column = 7; + + auto instance = createInstance(std::move(invocation)); + instance->setCodeCompletionConsumer(new CodeCompletionConsumer()); + auto action = std::make_unique(); + + if(!action->BeginSourceFile(*instance, instance->getFrontendOpts().Inputs[0])) { + llvm::errs() << "Failed to begin source file\n"; + std::terminate(); + } + + if(auto error = action->Execute()) { + llvm::errs() << "Failed to execute action: " << error << "\n"; + std::terminate(); + } + + instance->getASTContext().getTranslationUnitDecl()->dump(); +} + +} // namespace diff --git a/tests/Source/CodeCompletion/test.cpp b/tests/Source/CodeCompletion/test.cpp new file mode 100644 index 00000000..dad189e4 --- /dev/null +++ b/tests/Source/CodeCompletion/test.cpp @@ -0,0 +1,6 @@ +#include "test1.h" +#include "test2.h" + +int main() { + return 0; +} diff --git a/tests/Source/CodeCompletion/test1.h b/tests/Source/CodeCompletion/test1.h new file mode 100644 index 00000000..c8620b64 --- /dev/null +++ b/tests/Source/CodeCompletion/test1.h @@ -0,0 +1 @@ +void foo(); diff --git a/tests/Source/CodeCompletion/test2.h b/tests/Source/CodeCompletion/test2.h new file mode 100644 index 00000000..79a15557 --- /dev/null +++ b/tests/Source/CodeCompletion/test2.h @@ -0,0 +1,3 @@ +inline void bar() { + fo +}