[clang] [ClangRepl] Semanic Code Completion (PR #75556)
https://github.com/capfredf ready_for_review https://github.com/llvm/llvm-project/pull/75556 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Semanic Code Completion (PR #75556)
capfredf wrote: @vgvassilev https://github.com/llvm/llvm-project/pull/75556 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Semanic Code Completion (PR #75556)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/75556 >From 17486783ff2eab10a592eef53f33b1298ff29b49 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Wed, 13 Dec 2023 22:07:17 -0500 Subject: [PATCH] [ClangRepl] Semantic Code Completion This patch piggybacks on clang's semantic modules to enable semantic completion. In particular, we use `CodeCompletionContext` to differentiate two types of code completion. We also extract the relevant type information from it. --- .../clang/Interpreter/CodeCompletion.h| 25 +- clang/include/clang/Interpreter/Interpreter.h | 1 + clang/lib/Interpreter/CodeCompletion.cpp | 223 -- clang/lib/Interpreter/Interpreter.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 24 +- .../Interpreter/CodeCompletionTest.cpp| 219 - 6 files changed, 445 insertions(+), 51 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac..c64aa899759fd8 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,27 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompleter { + ReplCodeCompleter() = default; + std::string Prefix; + + /// \param InterpCI [in] The compiler instance that is used to trigger code + /// completion + + /// \param Content [in] The string where code completion is triggered. + + /// \param Line [in] The line number of the code completion point. + + /// \param Col [in] The column number of the code completion point. + + /// \param ParentCI [in] The running interpreter compiler instance that + /// provides ASTContexts. + + /// \param CCResults [out] The completion results. + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index 43573fb1a4b891..01858dfcc90ac5 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -101,6 +101,7 @@ class Interpreter { const ASTContext () const; ASTContext (); const CompilerInstance *getCompilerInstance() const; + CompilerInstance *getCompilerInstance(); llvm::Expected getExecutionEngine(); llvm::Expected Parse(llvm::StringRef Code); diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0..a9789355b2c5f3 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -12,6 +12,7 @@ #include "clang/Interpreter/CodeCompletion.h" #include "clang/AST/ASTImporter.h" +#include "clang/AST/DeclLookups.h" #include "clang/AST/DeclarationName.h" #include "clang/AST/ExternalASTSource.h" #include "clang/Basic/IdentifierTable.h" @@ -23,6 +24,8 @@ #include "clang/Sema/CodeCompleteConsumer.h" #include "clang/Sema/CodeCompleteOptions.h" #include "clang/Sema/Sema.h" +#include "llvm/Support/Debug.h" +#define DEBUG_TYPE "REPLCC" namespace clang { @@ -39,11 +42,15 @@ clang::CodeCompleteOptions getClangCompleteOpts() { class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , + ReplCodeCompleter ) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} + // The entry of handling code completion. When the function is called, we + // create a `Context`-based handler (see classes defined below) to handle each + // completion result. void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -56,26 +63,147 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/// The class CompletionContextHandler contains four interfaces, each of +/// which handles one type of completion result. +/// Its derived classes are used to create concrete handlers based on +/// \c CodeCompletionContext. +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +private: + Sema + +public: + CompletionContextHandler(Sema ,
[clang] [ClangRepl] Semanic Code Completion (PR #75556)
https://github.com/capfredf created https://github.com/llvm/llvm-project/pull/75556 None >From 77b2b39174570f62dec16557b8a539811f64b62c Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Wed, 13 Dec 2023 22:07:17 -0500 Subject: [PATCH 1/2] WIP --- .../clang/Interpreter/CodeCompletion.h| 25 +- clang/include/clang/Interpreter/Interpreter.h | 1 + clang/lib/Interpreter/CodeCompletion.cpp | 222 -- clang/lib/Interpreter/Interpreter.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 24 +- .../Interpreter/CodeCompletionTest.cpp| 219 - 6 files changed, 444 insertions(+), 51 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac..c64aa899759fd8 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,27 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompleter { + ReplCodeCompleter() = default; + std::string Prefix; + + /// \param InterpCI [in] The compiler instance that is used to trigger code + /// completion + + /// \param Content [in] The string where code completion is triggered. + + /// \param Line [in] The line number of the code completion point. + + /// \param Col [in] The column number of the code completion point. + + /// \param ParentCI [in] The running interpreter compiler instance that + /// provides ASTContexts. + + /// \param CCResults [out] The completion results. + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index 43573fb1a4b891..01858dfcc90ac5 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -101,6 +101,7 @@ class Interpreter { const ASTContext () const; ASTContext (); const CompilerInstance *getCompilerInstance() const; + CompilerInstance *getCompilerInstance(); llvm::Expected getExecutionEngine(); llvm::Expected Parse(llvm::StringRef Code); diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0..c34767c3ef9b87 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -12,6 +12,7 @@ #include "clang/Interpreter/CodeCompletion.h" #include "clang/AST/ASTImporter.h" +#include "clang/AST/DeclLookups.h" #include "clang/AST/DeclarationName.h" #include "clang/AST/ExternalASTSource.h" #include "clang/Basic/IdentifierTable.h" @@ -23,6 +24,8 @@ #include "clang/Sema/CodeCompleteConsumer.h" #include "clang/Sema/CodeCompleteOptions.h" #include "clang/Sema/Sema.h" +#include "llvm/Support/Debug.h" +#define DEBUG_TYPE "REPLCC" namespace clang { @@ -39,11 +42,15 @@ clang::CodeCompleteOptions getClangCompleteOpts() { class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , + ReplCodeCompleter ) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} + // The entry of handling code completion. When the function is called, we + // create a `Context`-based handler (see classes defined below) to handle each + // completion result. void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -56,26 +63,146 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/// The class CompletionContextHandler contains four interfaces, each of +/// which handles one type of completion result. +/// Its derived classes are used to create concrete handlers based on +/// \c CodeCompletionContext. +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +private: + Sema + +public: + CompletionContextHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results), S(S) {} + + /// Converts a Declaration completion result to a completion string, and then + /// stores it in Results. + virtual void