diff --git a/include/Compiler/Compiler.h b/include/Compiler/Compiler.h index 92689d5b..d13ad2d7 100644 --- a/include/Compiler/Compiler.h +++ b/include/Compiler/Compiler.h @@ -108,6 +108,8 @@ struct CompliationParams { /// Information about reuse PCM(name, path). llvm::SmallVector> pcms; + uint32_t line = 0, column = 0; + void addPCH(const PCHInfo& info) { pch = info.path; bounds = info.bounds(); @@ -121,16 +123,12 @@ struct CompliationParams { /// Build AST from given file path and content. If pch or pcm provided, apply them to the compiler. /// Note this function will not check whether we need to update the PCH or PCM, caller should check /// their reusability and update in time. -llvm::Expected buildAST(CompliationParams& params); +llvm::Expected compile(CompliationParams& params); -llvm::Expected buildPCH(CompliationParams& params, PCHInfo& out); +llvm::Expected compile(CompliationParams& params, PCHInfo& out); -llvm::Expected buildPCM(CompliationParams& params, PCMInfo& out); +llvm::Expected compile(CompliationParams& params, PCMInfo& out); -llvm::Expected codeCompleteAt(CompliationParams& params, - uint32_t line, - uint32_t column, - llvm::StringRef file, - clang::CodeCompleteConsumer* consumer); +llvm::Expected compile(CompliationParams& params, clang::CodeCompleteConsumer* consumer); } // namespace clice diff --git a/src/Compiler/Compiler.cpp b/src/Compiler/Compiler.cpp index ba283db4..9ef8e1d7 100644 --- a/src/Compiler/Compiler.cpp +++ b/src/Compiler/Compiler.cpp @@ -115,7 +115,7 @@ llvm::Expected ExecuteAction(std::unique_ptr i } // namespace -llvm::Expected buildAST(CompliationParams& params) { +llvm::Expected compile(CompliationParams& params) { auto instance = createInstance(params.args); auto buffer = llvm::MemoryBuffer::getMemBufferCopy(params.content); @@ -126,7 +126,7 @@ llvm::Expected buildAST(CompliationParams& params) { return ExecuteAction(std::move(instance), clang::frontend::ActionKind::ParseSyntaxOnly); } -llvm::Expected buildPCH(CompliationParams& params, PCHInfo& out) { +llvm::Expected compile(CompliationParams& params, PCHInfo& out) { auto instance = createInstance(params.args); clang::PreambleBounds bounds = {0, false}; @@ -164,7 +164,7 @@ llvm::Expected buildPCH(CompliationParams& params, PCHInfo& out) { } } -llvm::Expected buildPCM(CompliationParams& params, PCMInfo& out) { +llvm::Expected compile(CompliationParams& params, PCMInfo& out) { auto instance = createInstance(params.args); /// Set options to generate PCM. @@ -186,17 +186,13 @@ llvm::Expected buildPCM(CompliationParams& params, PCMInfo& out) { } } -llvm::Expected codeCompleteAt(CompliationParams& params, - uint32_t line, - uint32_t column, - llvm::StringRef file, - clang::CodeCompleteConsumer* consumer) { +llvm::Expected compile(CompliationParams& params, clang::CodeCompleteConsumer* consumer) { auto instance = createInstance(params.args); /// Set options to run code completion. - instance->getFrontendOpts().CodeCompletionAt.Line = line; - instance->getFrontendOpts().CodeCompletionAt.Column = column; - instance->getFrontendOpts().CodeCompletionAt.FileName = file; + instance->getFrontendOpts().CodeCompletionAt.FileName = params.path.str(); + instance->getFrontendOpts().CodeCompletionAt.Line = params.line; + instance->getFrontendOpts().CodeCompletionAt.Column = params.column; instance->setCodeCompletionConsumer(consumer); auto buffer = llvm::MemoryBuffer::getMemBufferCopy(params.content); diff --git a/src/Feature/CodeCompletion.cpp b/src/Feature/CodeCompletion.cpp index a4f5ee0c..b2440f4b 100644 --- a/src/Feature/CodeCompletion.cpp +++ b/src/Feature/CodeCompletion.cpp @@ -208,8 +208,11 @@ proto::CompletionResult codeCompletion(CompliationParams& params, proto::CompletionResult completions; auto consumer = new CodeCompletionCollector(completions, line, column, params.content); - auto info = codeCompleteAt(params, line, column, file, consumer); - if(info) { + params.path = file; + params.line = line; + params.column = column; + + if(auto info = compile(params, consumer)) { for(auto& item: completions) {} return completions; } else { diff --git a/src/Server/Scheduler.cpp b/src/Server/Scheduler.cpp index 747e2cb4..6982cd3f 100644 --- a/src/Server/Scheduler.cpp +++ b/src/Server/Scheduler.cpp @@ -36,7 +36,7 @@ async::promise Scheduler::updatePCH(llvm::StringRef filepath, } } - if(auto info = buildPCH(params, pch); !info) { + if(auto info = compile(params, pch); !info) { log::fatal("Failed to build PCH for {0}, because {1}", filepath.str(), info.takeError()); @@ -109,7 +109,7 @@ async::promise Scheduler::buildAST(llvm::StringRef filepath, llvm::StringR /// the `pchs` map in this task, beacuse it is called in thread pool which will result in /// data race. So temporarily copy the `pch` here. There must be a better way to solve this /// problem. - auto info = clice::buildAST(params); + auto info = clice::compile(params); if(!info) { log::fatal("Failed to build AST for {0}", filepath); } diff --git a/unittests/Compiler/Compiler.cpp b/unittests/Compiler/Compiler.cpp index 4ab49d22..519d0c70 100644 --- a/unittests/Compiler/Compiler.cpp +++ b/unittests/Compiler/Compiler.cpp @@ -29,7 +29,7 @@ int main(){ params.content = code; params.args = compileArgs; - auto info = buildAST(params); + auto info = compile(params); ASSERT_TRUE(bool(info)); } @@ -69,11 +69,11 @@ int main(){ params.args = compileArgs; PCHInfo pch; - ASSERT_TRUE(bool(clice::buildPCH(params, pch))); + ASSERT_TRUE(bool(clice::compile(params, pch))); params.addPCH(pch); - auto ast = buildAST(params); + auto ast = compile(params); ASSERT_TRUE(bool(ast)); } @@ -110,7 +110,7 @@ export int foo() { params.args = compileArgs; PCMInfo pcm; - ASSERT_TRUE(bool(clice::buildPCM(params, pcm))); + ASSERT_TRUE(bool(clice::compile(params, pcm))); ASSERT_EQ(pcm.name, "A"); const char* code2 = R"cpp( @@ -133,7 +133,7 @@ int main(){ params.args = compileArgs; params.addPCM(pcm); - auto info = buildAST(params); + auto info = compile(params); ASSERT_TRUE(bool(info)); } @@ -153,9 +153,11 @@ export int foo = 1; params.path = "main.cppm"; params.content = code; params.args = compileArgs; + params.line = 3; + params.column = 10; auto consumer = new clang::PrintingCodeCompleteConsumer({}, llvm::outs()); - auto info = codeCompleteAt(params, 3, 10, "main.cppm", consumer); + auto info = compile(params, consumer); ASSERT_TRUE(bool(info)); /// TODO: add tests in the case of PCH, PCM and override file. diff --git a/unittests/Compiler/Resolver.cpp b/unittests/Compiler/Resolver.cpp index b197604b..060ac68d 100644 --- a/unittests/Compiler/Resolver.cpp +++ b/unittests/Compiler/Resolver.cpp @@ -21,7 +21,7 @@ struct TemplateResolverTester : public clang::RecursiveASTVisitorinfo = std::move(*info); } else { llvm::errs() << "Failed to build AST\n";