[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
capfredf wrote: @vgvassilev there are some issues reported by sanitizers. Let's revert the patch. I will fix those issues and then re-land the patch https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
capfredf wrote: @vgvassilev Thank you very much! https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/vgvassilev closed https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 80962a4daaf20069c56d86f196a06abc91f3474c Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH] [ClangRepl] Add type directed code completion to clang-repl use CodeCompletionContext to implement this feature. --- .../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 9adcdf0dc3afac6..c64aa899759fd87 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 43573fb1a4b8915..01858dfcc90ac5a 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 c40e11b9d1ece0a..c34767c3ef9b87d 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
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
vgvassilev wrote: @capfredf, can you rebase the PR to trigger a rebuild - the test failure seems unrelated to our changes but maybe it was fixed meanwhile. https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/vgvassilev approved this pull request. LGTM! Thank you! https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
capfredf wrote: ping @vgvassilev https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 2e01c0e7974977d5dd13ef2dcab765c4d714f5ce Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH] [ClangRepl] Add type directed code completion to clang-repl use CodeCompletionContext to implement this feature. --- .../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 9adcdf0dc3afac6..c64aa899759fd87 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 43573fb1a4b8915..01858dfcc90ac5a 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 c40e11b9d1ece0a..c34767c3ef9b87d 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
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -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 substract classes are used to create concrete handlers based on +// 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 handleDeclaration(const CodeCompletionResult ) { +auto PreferredType = CCC.getPreferredType(); +if (PreferredType.isNull()) { + Results.push_back(Result.Declaration->getName().str()); + return; +} + +if (auto *VD = dyn_cast(Result.Declaration)) { + auto ArgumentType = VD->getType(); + if (PreferredType->isReferenceType()) { +QualType RT = PreferredType->castAs()->getPointeeType(); +Sema::ReferenceConversions RefConv; +Sema::ReferenceCompareResult RefRelationship = +S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType, + ); +switch (RefRelationship) { +case Sema::Ref_Compatible: +case Sema::Ref_Related: + Results.push_back(VD->getName().str()); + break; +case Sema::Ref_Incompatible: + break; +} + } else if (S.Context.hasSameType(ArgumentType, PreferredType)) { +Results.push_back(VD->getName().str()); + } +} + } + + // converts a Keyword completion result to a completion string, and then + // stores it in Results. + virtual void handleKeyword(const CodeCompletionResult ) { +auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); +// Add keyword to the completion results only if we are in a type-aware +// situation. +if (!CCC.getBaseType().isNull() || !CCC.getPreferredType().isNull()) + return; +if (StringRef(Result.Keyword).startswith(Prefix)) + Results.push_back(Result.Keyword); + } + + // converts a Pattern completion result to a completion string, and then + // stores it in Results. vgvassilev wrote: ```suggestion /// Converts a Pattern completion result to a completion string, and then /// stores it in Results. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -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 substract classes are used to create concrete handlers based on +// 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 handleDeclaration(const CodeCompletionResult ) { +auto PreferredType = CCC.getPreferredType(); +if (PreferredType.isNull()) { + Results.push_back(Result.Declaration->getName().str()); + return; +} + +if (auto *VD = dyn_cast(Result.Declaration)) { + auto ArgumentType = VD->getType(); + if (PreferredType->isReferenceType()) { +QualType RT = PreferredType->castAs()->getPointeeType(); +Sema::ReferenceConversions RefConv; +Sema::ReferenceCompareResult RefRelationship = +S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType, + ); +switch (RefRelationship) { +case Sema::Ref_Compatible: +case Sema::Ref_Related: + Results.push_back(VD->getName().str()); + break; +case Sema::Ref_Incompatible: + break; +} + } else if (S.Context.hasSameType(ArgumentType, PreferredType)) { +Results.push_back(VD->getName().str()); + } +} + } + + // converts a Keyword completion result to a completion string, and then + // stores it in Results. vgvassilev wrote: ```suggestion /// Converts a Keyword completion result to a completion string, and then /// stores it in Results. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -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 substract classes are used to create concrete handlers based on +// 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 handleDeclaration(const CodeCompletionResult ) { +auto PreferredType = CCC.getPreferredType(); +if (PreferredType.isNull()) { + Results.push_back(Result.Declaration->getName().str()); + return; +} + +if (auto *VD = dyn_cast(Result.Declaration)) { + auto ArgumentType = VD->getType(); + if (PreferredType->isReferenceType()) { +QualType RT = PreferredType->castAs()->getPointeeType(); +Sema::ReferenceConversions RefConv; +Sema::ReferenceCompareResult RefRelationship = +S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType, + ); +switch (RefRelationship) { +case Sema::Ref_Compatible: +case Sema::Ref_Related: + Results.push_back(VD->getName().str()); + break; +case Sema::Ref_Incompatible: + break; +} + } else if (S.Context.hasSameType(ArgumentType, PreferredType)) { +Results.push_back(VD->getName().str()); + } +} + } + + // converts a Keyword completion result to a completion string, and then + // stores it in Results. + virtual void handleKeyword(const CodeCompletionResult ) { +auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); +// Add keyword to the completion results only if we are in a type-aware +// situation. +if (!CCC.getBaseType().isNull() || !CCC.getPreferredType().isNull()) + return; +if (StringRef(Result.Keyword).startswith(Prefix)) + Results.push_back(Result.Keyword); + } + + // converts a Pattern completion result to a completion string, and then + // stores it in Results. + virtual void handlePattern(const CodeCompletionResult ) {} + + // converts a Macro completion result to a completion string, and then stores + // it in Results. vgvassilev wrote: ```suggestion /// Converts a Macro completion result to a completion string, and then stores /// it in Results. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -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 substract classes are used to create concrete handlers based on +// 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. vgvassilev wrote: ```suggestion /// Converts a Declaration completion result to a completion string, and then /// stores it in Results. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -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 substract classes are used to create concrete handlers based on +// CodeCompletionContext. vgvassilev wrote: ```suggestion /// 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. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 304c77231a403a8c3eb0b5650a58ebf36655f263 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH] [ClangRepl] Add type directed code completion to clang-repl use CodeCompletionContext to implement this feature. --- .../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 9adcdf0dc3afac6..c64aa899759fd87 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 43573fb1a4b8915..01858dfcc90ac5a 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 c40e11b9d1ece0a..54945fdb7181922 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 substract classes are used to create concrete handlers based on +// 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
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 3393f32af92a7ae37d4046b03ed670b717aee82b Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH] [ClangRepl] Add type directed code completion to clang-repl use CodeCompletionContext to implement this feature. --- .../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| 220 - 6 files changed, 445 insertions(+), 51 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..c64aa899759fd87 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 43573fb1a4b8915..01858dfcc90ac5a 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 c40e11b9d1ece0a..54945fdb7181922 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 substract classes are used to create concrete handlers based on +// 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
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -37,16 +39,15 @@ static std::vector runComp(clang::Interpreter , std::vector Results; std::vector Comps; - - codeComplete( - const_cast((*Interp)->getCompilerInstance()), - Prefix, /* Lines */ 1, Prefix.size(), MainInterp.getCompilerInstance(), - Results); + auto *MainCI = + const_cast((*Interp)->getCompilerInstance()); vgvassilev wrote: Then just drop the `const_cast` we should not need it. https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -37,16 +39,15 @@ static std::vector runComp(clang::Interpreter , std::vector Results; std::vector Comps; - - codeComplete( - const_cast((*Interp)->getCompilerInstance()), - Prefix, /* Lines */ 1, Prefix.size(), MainInterp.getCompilerInstance(), - Results); + auto *MainCI = + const_cast((*Interp)->getCompilerInstance()); capfredf wrote: This won't work. Interp is of `Expected>` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/* + The abstract class CompletionContextHandler contains four interfaces, each of + which handles one type of completion result. + + Its substract classes are used to create concrete handlers based on + CodeCompletionContext. + */ +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. + virtual void handleDeclaration(const CodeCompletionResult ) {} + + // convert a Keyword completion result to a completion string, and then store it in Results. + virtual void handleKeyword(const CodeCompletionResult ) {} + + // convert a Pattern completion result to a completion string, and then store it in Results. + virtual void handlePattern(const CodeCompletionResult ) {} + + // convert a Macro completion result to a completion string, and then store it in Results. + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHandler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHandler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto *ID = Result.Declaration->getIdentifier(); +if (!ID) + return; +if (!isa(Result.Declaration)) + return; +const auto *Fun = cast(Result.Declaration); +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +}; + +class DefaultAccessHandler : public CompletionContextHandler { capfredf wrote: @vgvassilev Do you mean we don't need an "abstract" class? I am all for it. [I am a FP person. I just want to write high-order functions most of the time] https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -37,16 +39,15 @@ static std::vector runComp(clang::Interpreter , std::vector Results; std::vector Comps; - - codeComplete( - const_cast((*Interp)->getCompilerInstance()), - Prefix, /* Lines */ 1, Prefix.size(), MainInterp.getCompilerInstance(), - Results); + auto *MainCI = + const_cast((*Interp)->getCompilerInstance()); vgvassilev wrote: ```suggestion auto *MainCI = Interp->getCompilerInstance(); ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From c3cead275f5fa2f8d3d6c78fd747eae9a3edbd92 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH] [ClangRepl] Add type directed code completion to clang-repl use CodeCompletionContext to implement this feature. --- .../clang/Interpreter/CodeCompletion.h| 25 +- clang/include/clang/Interpreter/Interpreter.h | 1 + clang/lib/Interpreter/CodeCompletion.cpp | 225 -- clang/lib/Interpreter/Interpreter.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 24 +- .../Interpreter/CodeCompletionTest.cpp| 220 - 6 files changed, 448 insertions(+), 51 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..c64aa899759fd87 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 43573fb1a4b8915..01858dfcc90ac5a 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 c40e11b9d1ece0a..f5767de982319e5 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,10 +42,11 @@ 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) {} void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +60,152 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +// The abstract class CompletionContextHandler contains four interfaces, each of +// which handles one type of completion result. +// Its substract classes are used to create concrete handlers based on +// CodeCompletionContext. +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results) {} + + // converts a Declaration completion result to a completion string, and then + // stores it in Results. + virtual void handleDeclaration(const CodeCompletionResult ) {} + + // converts a Keyword completion result to a completion string, and then + // stores it in Results. + virtual void
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -159,29 +308,62 @@ void ExternalSource::completeVisibleDeclsMap( for (auto *DeclCtxt = ParentTUDeclCtxt; DeclCtxt != nullptr; DeclCtxt = DeclCtxt->getPreviousDecl()) { for (auto : DeclCtxt->decls()) { - if (NamedDecl *Decl = llvm::dyn_cast(IDeclContext)) { -if (auto DeclOrErr = Importer->Import(Decl)) { - if (NamedDecl *importedNamedDecl = - llvm::dyn_cast(*DeclOrErr)) { -SetExternalVisibleDeclsForName(ChildDeclContext, - importedNamedDecl->getDeclName(), - importedNamedDecl); - } - -} else { - llvm::consumeError(DeclOrErr.takeError()); -} + if (!llvm::isa(IDeclContext)) +continue; + + NamedDecl *Decl = llvm::cast(IDeclContext); + + auto DeclOrErr = Importer->Import(Decl); + if (!DeclOrErr) { +llvm::consumeError(DeclOrErr.takeError()); +continue; } + + if (!llvm::isa(*DeclOrErr)) +continue; + + NamedDecl *importedNamedDecl = llvm::cast(*DeclOrErr); + + SetExternalVisibleDeclsForName(ChildDeclContext, + importedNamedDecl->getDeclName(), + importedNamedDecl); + + if (!llvm::isa(importedNamedDecl)) +continue; + + auto *Record = +llvm::cast(importedNamedDecl); + + if (auto Err = Importer->ImportDefinition(Decl)) { +consumeError(std::move(Err)); vgvassilev wrote: Likewise. https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/* + The abstract class CompletionContextHandler contains four interfaces, each of + which handles one type of completion result. + + Its substract classes are used to create concrete handlers based on + CodeCompletionContext. + */ +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. + virtual void handleDeclaration(const CodeCompletionResult ) {} + + // convert a Keyword completion result to a completion string, and then store it in Results. + virtual void handleKeyword(const CodeCompletionResult ) {} + + // convert a Pattern completion result to a completion string, and then store it in Results. + virtual void handlePattern(const CodeCompletionResult ) {} + + // convert a Macro completion result to a completion string, and then store it in Results. + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHandler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHandler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto *ID = Result.Declaration->getIdentifier(); +if (!ID) + return; +if (!isa(Result.Declaration)) + return; +const auto *Fun = cast(Result.Declaration); +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +}; + +class DefaultAccessHandler : public CompletionContextHandler { +private: + Sema + +public: + DefaultAccessHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CompletionContextHandler(CCC, Results), S(S) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto PreferredType = CCC.getPreferredType(); +if (PreferredType.isNull()) { + Results.push_back(Result.Declaration->getName().str()); + return; +} + +if (auto *VD = dyn_cast(Result.Declaration)) { + auto ArgumentType = VD->getType(); + if (PreferredType->isReferenceType()) { +QualType RT = PreferredType->castAs()->getPointeeType(); +Sema::ReferenceConversions RefConv; +Sema::ReferenceCompareResult RefRelationship = +S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType, + ); +switch (RefRelationship) { +case Sema::Ref_Compatible: +case Sema::Ref_Related: + Results.push_back(VD->getName().str()); + break; +case Sema::Ref_Incompatible: + break; +} + } else if (S.Context.hasSameType(ArgumentType, PreferredType)) { +Results.push_back(VD->getName().str()); + } +} + } + + void handleKeyword(const CodeCompletionResult ) override { +auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); +// add keyword to the completion results only if we are in a type-aware +// situation. vgvassilev wrote: ```suggestion // Add keyword to the completion results only if we are in a type-aware // situation. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/* + The abstract class CompletionContextHandler contains four interfaces, each of + which handles one type of completion result. + + Its substract classes are used to create concrete handlers based on + CodeCompletionContext. + */ +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. + virtual void handleDeclaration(const CodeCompletionResult ) {} + + // convert a Keyword completion result to a completion string, and then store it in Results. + virtual void handleKeyword(const CodeCompletionResult ) {} + + // convert a Pattern completion result to a completion string, and then store it in Results. + virtual void handlePattern(const CodeCompletionResult ) {} + + // convert a Macro completion result to a completion string, and then store it in Results. + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHandler { vgvassilev wrote: Maybe we can add some documentation here explaining what we do on a high-level as it seems the main part of this PR. https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -159,29 +308,62 @@ void ExternalSource::completeVisibleDeclsMap( for (auto *DeclCtxt = ParentTUDeclCtxt; DeclCtxt != nullptr; DeclCtxt = DeclCtxt->getPreviousDecl()) { for (auto : DeclCtxt->decls()) { - if (NamedDecl *Decl = llvm::dyn_cast(IDeclContext)) { -if (auto DeclOrErr = Importer->Import(Decl)) { - if (NamedDecl *importedNamedDecl = - llvm::dyn_cast(*DeclOrErr)) { -SetExternalVisibleDeclsForName(ChildDeclContext, - importedNamedDecl->getDeclName(), - importedNamedDecl); - } - -} else { - llvm::consumeError(DeclOrErr.takeError()); -} + if (!llvm::isa(IDeclContext)) +continue; + + NamedDecl *Decl = llvm::cast(IDeclContext); + + auto DeclOrErr = Importer->Import(Decl); + if (!DeclOrErr) { +llvm::consumeError(DeclOrErr.takeError()); vgvassilev wrote: Maybe add a comment here why error is fine. https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/* + The abstract class CompletionContextHandler contains four interfaces, each of + which handles one type of completion result. + + Its substract classes are used to create concrete handlers based on + CodeCompletionContext. + */ +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. + virtual void handleDeclaration(const CodeCompletionResult ) {} + + // convert a Keyword completion result to a completion string, and then store it in Results. + virtual void handleKeyword(const CodeCompletionResult ) {} + + // convert a Pattern completion result to a completion string, and then store it in Results. + virtual void handlePattern(const CodeCompletionResult ) {} + + // convert a Macro completion result to a completion string, and then store it in Results. + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHandler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHandler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto *ID = Result.Declaration->getIdentifier(); +if (!ID) + return; +if (!isa(Result.Declaration)) + return; +const auto *Fun = cast(Result.Declaration); +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +}; + +class DefaultAccessHandler : public CompletionContextHandler { vgvassilev wrote: What is the benefit of having a base class? We could merge the implementations of `DefaultAccessHandler` and `DotMemberAccessHandler` into the base class to simplify things. https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/* + The abstract class CompletionContextHandler contains four interfaces, each of + which handles one type of completion result. + + Its substract classes are used to create concrete handlers based on + CodeCompletionContext. + */ +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. + virtual void handleDeclaration(const CodeCompletionResult ) {} + + // convert a Keyword completion result to a completion string, and then store it in Results. + virtual void handleKeyword(const CodeCompletionResult ) {} + + // convert a Pattern completion result to a completion string, and then store it in Results. + virtual void handlePattern(const CodeCompletionResult ) {} + + // convert a Macro completion result to a completion string, and then store it in Results. vgvassilev wrote: ```suggestion // Converts a Macro completion result to a completion string, and then store it in Results. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/* + The abstract class CompletionContextHandler contains four interfaces, each of + which handles one type of completion result. + + Its substract classes are used to create concrete handlers based on + CodeCompletionContext. + */ +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. + virtual void handleDeclaration(const CodeCompletionResult ) {} + + // convert a Keyword completion result to a completion string, and then store it in Results. + virtual void handleKeyword(const CodeCompletionResult ) {} + + // convert a Pattern completion result to a completion string, and then store it in Results. vgvassilev wrote: ```suggestion /// Convert a Pattern completion result to a completion string, and then store it in Results. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -118,6 +256,16 @@ void IncrementalSyntaxOnlyAction::ExecuteAction() { CI.getASTContext().getTranslationUnitDecl()->setHasExternalVisibleStorage( true); + // Load all external decls into current context. Under the hood, it calls vgvassilev wrote: ```suggestion // Load all external decls into current context. Under the hood, it calls ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/* + The abstract class CompletionContextHandler contains four interfaces, each of + which handles one type of completion result. + + Its substract classes are used to create concrete handlers based on + CodeCompletionContext. + */ vgvassilev wrote: ```suggestion /// The abstract class CompletionContextHandler contains four interfaces, each of /// which handles one type of completion result. /// /// Its substract classes are used to create concrete handlers based on /// \c CodeCompletionContext. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { vgvassilev wrote: I cannot seem to find the uses of this class. https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -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 The compiler instance that is used to trigger code completion + +@param Content The string where code completion is triggered. + +@param Line The line number of the code completion point. + +@param Col The column number of the code completion point. + +@param ParentCI The running interpreter compiler instance that provides ASTContexts. + +@param CCResults [out] The completion results. + */ vgvassilev wrote: ```suggestion /// Provides code completion results given a stem. /// /// \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. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/* + The abstract class CompletionContextHandler contains four interfaces, each of + which handles one type of completion result. + + Its substract classes are used to create concrete handlers based on + CodeCompletionContext. + */ +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. vgvassilev wrote: ```suggestion /// Converts a Declaration completion result to a completion string, and then stores it in Results. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/* + The abstract class CompletionContextHandler contains four interfaces, each of + which handles one type of completion result. + + Its substract classes are used to create concrete handlers based on + CodeCompletionContext. + */ +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. + virtual void handleDeclaration(const CodeCompletionResult ) {} + + // convert a Keyword completion result to a completion string, and then store it in Results. vgvassilev wrote: ```suggestion /// Convert a Keyword completion result to a completion string, and then store it in Results. ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 57de74a2f63ee1db8519bc3225b2ccaef2b216c5 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH] [ClangRepl] Add type directed code completion to clang-repl use CodeCompletionContext to implement this feature. --- .../clang/Interpreter/CodeCompletion.h| 25 +- clang/lib/Interpreter/CodeCompletion.cpp | 230 -- clang/tools/clang-repl/ClangRepl.cpp | 25 +- .../Interpreter/CodeCompletionTest.cpp| 220 - 4 files changed, 448 insertions(+), 52 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..0c181b3e0920bad 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 The compiler instance that is used to trigger code completion + +@param Content The string where code completion is triggered. + +@param Line The line number of the code completion point. + +@param Col The column number of the code completion point. + +@param ParentCI 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/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..472c55dd3644ec4 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -10,19 +10,22 @@ // //===--===// -#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" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendActions.h" +#include "clang/Interpreter/CodeCompletion.h" #include "clang/Interpreter/Interpreter.h" #include "clang/Lex/PreprocessorOptions.h" #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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void HandleCodeCompleteResults(class Sema , + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector ) = 0; +}; + 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) {} void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompleter +}; + +/* + The abstract class CompletionContextHandler contains four interfaces, each of + which handles one type of completion result. + + Its substract classes are used to create concrete handlers based on + CodeCompletionContext. + */ +class CompletionContextHandler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector ) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. + virtual void handleDeclaration(const CodeCompletionResult ) {} + + // convert a Keyword completion result to a completion string, and then store it in Results. + virtual void
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 582f652f35d813a86790bc62473a0efc4560ae9a Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/9] [ClangRepl] Type Directed Code Completion --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 189 ++- clang/tools/clang-repl/ClangRepl.cpp | 25 +- .../Interpreter/CodeCompletionTest.cpp| 220 +- 4 files changed, 405 insertions(+), 40 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..0eb493d1b894b39 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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void HandleCodeCompleteResults(class Sema , + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , + ReplCodeCompletion ) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector ) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult ) {} + virtual void handleKeyword(const CodeCompletionResult ) {} + virtual void handlePattern(const CodeCompletionResult ) {} + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} + } +}; + +class DefaultAccessHandler : public CompletionContextHanndler { +private: + Sema + +public: + DefaultAccessHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results), S(S) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto PreferredType =
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 582f652f35d813a86790bc62473a0efc4560ae9a Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/5] [ClangRepl] Type Directed Code Completion --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 189 ++- clang/tools/clang-repl/ClangRepl.cpp | 25 +- .../Interpreter/CodeCompletionTest.cpp| 220 +- 4 files changed, 405 insertions(+), 40 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..0eb493d1b894b39 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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void HandleCodeCompleteResults(class Sema , + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , + ReplCodeCompletion ) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector ) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult ) {} + virtual void handleKeyword(const CodeCompletionResult ) {} + virtual void handlePattern(const CodeCompletionResult ) {} + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} + } +}; + +class DefaultAccessHandler : public CompletionContextHanndler { +private: + Sema + +public: + DefaultAccessHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results), S(S) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto PreferredType =
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 582f652f35d813a86790bc62473a0efc4560ae9a Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/4] [ClangRepl] Type Directed Code Completion --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 189 ++- clang/tools/clang-repl/ClangRepl.cpp | 25 +- .../Interpreter/CodeCompletionTest.cpp| 220 +- 4 files changed, 405 insertions(+), 40 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..0eb493d1b894b39 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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void HandleCodeCompleteResults(class Sema , + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , + ReplCodeCompletion ) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector ) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult ) {} + virtual void handleKeyword(const CodeCompletionResult ) {} + virtual void handlePattern(const CodeCompletionResult ) {} + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} + } +}; + +class DefaultAccessHandler : public CompletionContextHanndler { +private: + Sema + +public: + DefaultAccessHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results), S(S) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto PreferredType =
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 582f652f35d813a86790bc62473a0efc4560ae9a Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/3] [ClangRepl] Type Directed Code Completion --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 189 ++- clang/tools/clang-repl/ClangRepl.cpp | 25 +- .../Interpreter/CodeCompletionTest.cpp| 220 +- 4 files changed, 405 insertions(+), 40 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..0eb493d1b894b39 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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void HandleCodeCompleteResults(class Sema , + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , + ReplCodeCompletion ) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector ) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult ) {} + virtual void handleKeyword(const CodeCompletionResult ) {} + virtual void handlePattern(const CodeCompletionResult ) {} + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} + } +}; + +class DefaultAccessHandler : public CompletionContextHanndler { +private: + Sema + +public: + DefaultAccessHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results), S(S) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto PreferredType =
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 582f652f35d813a86790bc62473a0efc4560ae9a Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/2] [ClangRepl] Type Directed Code Completion --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 189 ++- clang/tools/clang-repl/ClangRepl.cpp | 25 +- .../Interpreter/CodeCompletionTest.cpp| 220 +- 4 files changed, 405 insertions(+), 40 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..0eb493d1b894b39 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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void HandleCodeCompleteResults(class Sema , + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , + ReplCodeCompletion ) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector ) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult ) {} + virtual void handleKeyword(const CodeCompletionResult ) {} + virtual void handlePattern(const CodeCompletionResult ) {} + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} + } +}; + +class DefaultAccessHandler : public CompletionContextHanndler { +private: + Sema + +public: + DefaultAccessHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results), S(S) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto PreferredType =
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { vgvassilev wrote: ```suggestion class CompletionContextHandler { ``` Can we add some documentation, too? https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -166,8 +300,37 @@ void ExternalSource::completeVisibleDeclsMap( SetExternalVisibleDeclsForName(ChildDeclContext, importedNamedDecl->getDeclName(), importedNamedDecl); - } +if (auto *Record = +llvm::dyn_cast(importedNamedDecl)) { + if (auto Err = Importer->ImportDefinition(Decl)) +consumeError(std::move(Err)); + // LLVM_DEBUG(llvm::dbgs() << "\nHello :\n"); + // Record->getTypeForDecl()->dump(); + Record->setHasLoadedFieldsFromExternalStorage(true); + // auto ToOrErr = Importer->Import(->getTypeForDecl()); + // if (!ToOrErr) { + // consumeError(std::move(ToOrErr.takeError())); + // } + LLVM_DEBUG( + llvm::dbgs() + << "\nCXXRecrod : " << Record->getName() << " size(methods): " + << std::distance(Record->method_begin(), Record->method_end()) + << " has def?: " << Record->hasDefinition() + << " # (methods): " + << std::distance(Record->getDefinition()->method_begin(), + Record->getDefinition()->method_end()) + << "\n"); + for (auto *Meth : Record->methods()) { +SetExternalVisibleDeclsForName(ChildDeclContext, + Meth->getDeclName(), Meth); +// if (Meth->getDeclName().isIdentifier()) { +// LLVM_DEBUG(llvm::dbgs() << "CXXRecrod Method: " << +// Meth->getName() << "\n"); vgvassilev wrote: Can we clean up the comments and reduce the indentation levels as suggested in previous comments? https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { vgvassilev wrote: Can we rename that as `CodeCompleter` or similar and add some doxygen-style documentation? https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector ) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult ) {} + virtual void handleKeyword(const CodeCompletionResult ) {} + virtual void handlePattern(const CodeCompletionResult ) {} + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} + } +}; + +class DefaultAccessHandler : public CompletionContextHanndler { +private: + Sema + +public: + DefaultAccessHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results), S(S) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto PreferredType = CCC.getPreferredType(); +if (!PreferredType.isNull()) { + if (auto *VD = dyn_cast(Result.Declaration)) { +auto ArgumentType = VD->getType(); +if (PreferredType->isReferenceType()) { + QualType RT = + PreferredType->castAs()->getPointeeType(); + Sema::ReferenceConversions RefConv; + Sema::ReferenceCompareResult RefRelationship = + S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType, + ); + switch (RefRelationship) { + case Sema::Ref_Compatible: + case Sema::Ref_Related: +Results.push_back(VD->getName().str()); +break; + case Sema::Ref_Incompatible: +break; + } +} else if (S.Context.hasSameType(ArgumentType, PreferredType)) { + Results.push_back(VD->getName().str()); +} + } +} else + Results.push_back(Result.Declaration->getName().str()); + } + + void handleKeyword(const CodeCompletionResult ) override { +auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); +// add keyword to the completion results only if we are in a type-aware +// situation. +if (!CCC.getBaseType().isNull() || !CCC.getPreferredType().isNull()) + return; +if (StringRef(Result.Keyword).startswith(Prefix)) + Results.push_back(Result.Keyword); + } }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { + + // auto& a = S.Context.Idents.get("f1"); + // LLVM_DEBUG(llvm::dbgs() << "\n F11 : " << a.getName() << "\n" ); vgvassilev wrote: ```suggestion ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector ) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult ) {} + virtual void handleKeyword(const CodeCompletionResult ) {} + virtual void handlePattern(const CodeCompletionResult ) {} + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} vgvassilev wrote: ```suggestion if (!Result.Declaration->getIdentifier()) return; if (!isa(Result.Declaration)) return; const auto *Fun = cast(Result.Declaration) if (Fun->getParent()->getCanonicalDecl() == CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " << ID->getName() << "\n"); Results.push_back(ID->getName().str()); } ``` This would help with the indentation, please run clang-format on it. https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -101,6 +223,7 @@ class ExternalSource : public clang::ExternalASTSource { ASTContext , FileManager ); bool FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) override; + // void CompleteType(TagDecl *Tag) override; vgvassilev wrote: ```suggestion ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector ) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult ) {} + virtual void handleKeyword(const CodeCompletionResult ) {} + virtual void handlePattern(const CodeCompletionResult ) {} + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} + } +}; + +class DefaultAccessHandler : public CompletionContextHanndler { +private: + Sema + +public: + DefaultAccessHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results), S(S) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto PreferredType = CCC.getPreferredType(); +if (!PreferredType.isNull()) { vgvassilev wrote: We could use a similar approach here: ```suggestion if (PreferredType.isNull()) return; ... ``` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/vgvassilev commented: Looks like this is heading in a good direction. Thank you! https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/vgvassilev edited https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
capfredf wrote: @vgvassilev https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 582f652f35d813a86790bc62473a0efc4560ae9a Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH] [ClangRepl] Type Directed Code Completion --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 189 ++- clang/tools/clang-repl/ClangRepl.cpp | 25 +- .../Interpreter/CodeCompletionTest.cpp| 220 +- 4 files changed, 405 insertions(+), 40 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..0eb493d1b894b39 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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void HandleCodeCompleteResults(class Sema , + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , + ReplCodeCompletion ) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector ) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult ) {} + virtual void handleKeyword(const CodeCompletionResult ) {} + virtual void handlePattern(const CodeCompletionResult ) {} + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} + } +}; + +class DefaultAccessHandler : public CompletionContextHanndler { +private: + Sema + +public: + DefaultAccessHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results), S(S) {} + void handleDeclaration(const CodeCompletionResult ) override { +auto PreferredType =
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 61bb4c46f6733128d5ec4b511a858f08e86bfd40 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH] [ClangRepl] Type Directed Code Completion Differential Revision: https://reviews.llvm.org/D159128 --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 189 ++- clang/tools/clang-repl/ClangRepl.cpp | 29 +-- .../Interpreter/CodeCompletionTest.cpp| 220 +- 4 files changed, 406 insertions(+), 43 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..0eb493d1b894b39 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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void HandleCodeCompleteResults(class Sema , + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , + ReplCodeCompletion ) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector ) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult ) {} + virtual void handleKeyword(const CodeCompletionResult ) {} + virtual void handlePattern(const CodeCompletionResult ) {} + virtual void handleMacro(const CodeCompletionResult ) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult ) override { +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == +CCC.getBaseType()->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " + << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} + } +}; + +class DefaultAccessHandler : public CompletionContextHanndler { +private: + Sema + +public: + DefaultAccessHandler(Sema , CodeCompletionContext CCC, + std::vector ) + : CompletionContextHanndler(CCC, Results), S(S) {} + void handleDeclaration(const
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf ready_for_review https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 4b371d40d74d6297bdb5ecbe2eae0573e20d0569 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/8] [ClangRepl] Type Directed Code Completion Differential Revision: https://reviews.llvm.org/D159128 --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 147 +++-- clang/lib/Sema/SemaLookup.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 25 +-- .../Interpreter/CodeCompletionTest.cpp| 195 +- 5 files changed, 334 insertions(+), 48 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..9c812c28f677726 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -23,6 +23,9 @@ #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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void + HandleCodeCompleteResults(class Sema , CodeCompletionResult *InResults, +unsigned NumResults, +std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto = InResults[I]; -switch (Result.Kind) { -case CodeCompletionResult::RK_Declaration: - if (auto *ID = Result.Declaration->getIdentifier()) { -Results.push_back(ID->getName().str()); + + // auto& a = S.Context.Idents.get("f1"); + // LLVM_DEBUG(llvm::dbgs() << "\n F11 : " << a.getName() << "\n" ); + auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); + CC.Prefix = Prefix; + switch (Context.getKind()) { + case CodeCompletionContext::CCC_DotMemberAccess: { +// FIXME: check BaseType is dependent, or from a typo +auto BaseType = Context.getBaseType(); +LLVM_DEBUG(llvm::dbgs() << "BaseType : " << BaseType.getAsString() << "\n" ); +for (unsigned I = 0; I < NumResults; ++I) { + auto = InResults[I]; + switch (Result.Kind) { + case CodeCompletionResult::RK_Declaration: +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == BaseType->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} +break; + default: +break; } - break; -case CodeCompletionResult::RK_Keyword: - Results.push_back(Result.Keyword); - break; -default: - break; } +break; } + default: +auto PreferredType = Context.getPreferredType(); +for (unsigned I = 0; I <
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 4b371d40d74d6297bdb5ecbe2eae0573e20d0569 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/6] [ClangRepl] Type Directed Code Completion Differential Revision: https://reviews.llvm.org/D159128 --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 147 +++-- clang/lib/Sema/SemaLookup.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 25 +-- .../Interpreter/CodeCompletionTest.cpp| 195 +- 5 files changed, 334 insertions(+), 48 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..9c812c28f677726 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -23,6 +23,9 @@ #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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void + HandleCodeCompleteResults(class Sema , CodeCompletionResult *InResults, +unsigned NumResults, +std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto = InResults[I]; -switch (Result.Kind) { -case CodeCompletionResult::RK_Declaration: - if (auto *ID = Result.Declaration->getIdentifier()) { -Results.push_back(ID->getName().str()); + + // auto& a = S.Context.Idents.get("f1"); + // LLVM_DEBUG(llvm::dbgs() << "\n F11 : " << a.getName() << "\n" ); + auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); + CC.Prefix = Prefix; + switch (Context.getKind()) { + case CodeCompletionContext::CCC_DotMemberAccess: { +// FIXME: check BaseType is dependent, or from a typo +auto BaseType = Context.getBaseType(); +LLVM_DEBUG(llvm::dbgs() << "BaseType : " << BaseType.getAsString() << "\n" ); +for (unsigned I = 0; I < NumResults; ++I) { + auto = InResults[I]; + switch (Result.Kind) { + case CodeCompletionResult::RK_Declaration: +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == BaseType->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} +break; + default: +break; } - break; -case CodeCompletionResult::RK_Keyword: - Results.push_back(Result.Keyword); - break; -default: - break; } +break; } + default: +auto PreferredType = Context.getPreferredType(); +for (unsigned I = 0; I <
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 4b371d40d74d6297bdb5ecbe2eae0573e20d0569 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/5] [ClangRepl] Type Directed Code Completion Differential Revision: https://reviews.llvm.org/D159128 --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 147 +++-- clang/lib/Sema/SemaLookup.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 25 +-- .../Interpreter/CodeCompletionTest.cpp| 195 +- 5 files changed, 334 insertions(+), 48 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..9c812c28f677726 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -23,6 +23,9 @@ #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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void + HandleCodeCompleteResults(class Sema , CodeCompletionResult *InResults, +unsigned NumResults, +std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto = InResults[I]; -switch (Result.Kind) { -case CodeCompletionResult::RK_Declaration: - if (auto *ID = Result.Declaration->getIdentifier()) { -Results.push_back(ID->getName().str()); + + // auto& a = S.Context.Idents.get("f1"); + // LLVM_DEBUG(llvm::dbgs() << "\n F11 : " << a.getName() << "\n" ); + auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); + CC.Prefix = Prefix; + switch (Context.getKind()) { + case CodeCompletionContext::CCC_DotMemberAccess: { +// FIXME: check BaseType is dependent, or from a typo +auto BaseType = Context.getBaseType(); +LLVM_DEBUG(llvm::dbgs() << "BaseType : " << BaseType.getAsString() << "\n" ); +for (unsigned I = 0; I < NumResults; ++I) { + auto = InResults[I]; + switch (Result.Kind) { + case CodeCompletionResult::RK_Declaration: +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == BaseType->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} +break; + default: +break; } - break; -case CodeCompletionResult::RK_Keyword: - Results.push_back(Result.Keyword); - break; -default: - break; } +break; } + default: +auto PreferredType = Context.getPreferredType(); +for (unsigned I = 0; I <
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff 8586cd5ad8a7fa4f84b5913cbeaf634d68500095 dd3e3e6b8eb3f9f1f740d2a9fc8a62e1964f9153 -- clang/include/clang/Interpreter/CodeCompletion.h clang/lib/Interpreter/CodeCompletion.cpp clang/tools/clang-repl/ClangRepl.cpp clang/unittests/Interpreter/CodeCompletionTest.cpp `` View the diff from clang-format here. ``diff diff --git a/clang/unittests/Interpreter/CodeCompletionTest.cpp b/clang/unittests/Interpreter/CodeCompletionTest.cpp index fba6e0dbcd31..918b4e8167eb 100644 --- a/clang/unittests/Interpreter/CodeCompletionTest.cpp +++ b/clang/unittests/Interpreter/CodeCompletionTest.cpp @@ -287,8 +287,8 @@ TEST(CodeCompletionTest, NestedInvocations) { TEST(CodeCompletionTest, TemplateFunctions) { auto Interp = createInterpreter(); - cantFail(Interp->ParseAndExecute( - "template T id(T a) { return a;} ")); + cantFail( + Interp->ParseAndExecute("template T id(T a) { return a;} ")); cantFail(Interp->ParseAndExecute("int apple = 84;")); { auto Err = llvm::Error::success(); @@ -298,7 +298,8 @@ TEST(CodeCompletionTest, TemplateFunctions) { EXPECT_EQ((bool)Err, false); } - cantFail(Interp->ParseAndExecute("template T pickFirst(T a, T b) { return a;} ")); + cantFail(Interp->ParseAndExecute( + "template T pickFirst(T a, T b) { return a;} ")); cantFail(Interp->ParseAndExecute("char pear = '4';")); { auto Err = llvm::Error::success(); @@ -309,5 +310,4 @@ TEST(CodeCompletionTest, TemplateFunctions) { } } - } // anonymous namespace `` https://github.com/llvm/llvm-project/pull/67349 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 4b371d40d74d6297bdb5ecbe2eae0573e20d0569 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/4] [ClangRepl] Type Directed Code Completion Differential Revision: https://reviews.llvm.org/D159128 --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 147 +++-- clang/lib/Sema/SemaLookup.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 25 +-- .../Interpreter/CodeCompletionTest.cpp| 195 +- 5 files changed, 334 insertions(+), 48 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..9c812c28f677726 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -23,6 +23,9 @@ #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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void + HandleCodeCompleteResults(class Sema , CodeCompletionResult *InResults, +unsigned NumResults, +std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto = InResults[I]; -switch (Result.Kind) { -case CodeCompletionResult::RK_Declaration: - if (auto *ID = Result.Declaration->getIdentifier()) { -Results.push_back(ID->getName().str()); + + // auto& a = S.Context.Idents.get("f1"); + // LLVM_DEBUG(llvm::dbgs() << "\n F11 : " << a.getName() << "\n" ); + auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); + CC.Prefix = Prefix; + switch (Context.getKind()) { + case CodeCompletionContext::CCC_DotMemberAccess: { +// FIXME: check BaseType is dependent, or from a typo +auto BaseType = Context.getBaseType(); +LLVM_DEBUG(llvm::dbgs() << "BaseType : " << BaseType.getAsString() << "\n" ); +for (unsigned I = 0; I < NumResults; ++I) { + auto = InResults[I]; + switch (Result.Kind) { + case CodeCompletionResult::RK_Declaration: +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == BaseType->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} +break; + default: +break; } - break; -case CodeCompletionResult::RK_Keyword: - Results.push_back(Result.Keyword); - break; -default: - break; } +break; } + default: +auto PreferredType = Context.getPreferredType(); +for (unsigned I = 0; I <
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 4b371d40d74d6297bdb5ecbe2eae0573e20d0569 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/2] [ClangRepl] Type Directed Code Completion Differential Revision: https://reviews.llvm.org/D159128 --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 147 +++-- clang/lib/Sema/SemaLookup.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 25 +-- .../Interpreter/CodeCompletionTest.cpp| 195 +- 5 files changed, 334 insertions(+), 48 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..9c812c28f677726 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -23,6 +23,9 @@ #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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void + HandleCodeCompleteResults(class Sema , CodeCompletionResult *InResults, +unsigned NumResults, +std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto = InResults[I]; -switch (Result.Kind) { -case CodeCompletionResult::RK_Declaration: - if (auto *ID = Result.Declaration->getIdentifier()) { -Results.push_back(ID->getName().str()); + + // auto& a = S.Context.Idents.get("f1"); + // LLVM_DEBUG(llvm::dbgs() << "\n F11 : " << a.getName() << "\n" ); + auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); + CC.Prefix = Prefix; + switch (Context.getKind()) { + case CodeCompletionContext::CCC_DotMemberAccess: { +// FIXME: check BaseType is dependent, or from a typo +auto BaseType = Context.getBaseType(); +LLVM_DEBUG(llvm::dbgs() << "BaseType : " << BaseType.getAsString() << "\n" ); +for (unsigned I = 0; I < NumResults; ++I) { + auto = InResults[I]; + switch (Result.Kind) { + case CodeCompletionResult::RK_Declaration: +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == BaseType->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} +break; + default: +break; } - break; -case CodeCompletionResult::RK_Keyword: - Results.push_back(Result.Keyword); - break; -default: - break; } +break; } + default: +auto PreferredType = Context.getPreferredType(); +for (unsigned I = 0; I <
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/67349 >From 90dae115b403ad7019f51154b4b771a2a2997a8f Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH 1/2] [ClangRepl] Type Directed Code Completion Differential Revision: https://reviews.llvm.org/D159128 --- .../clang/Interpreter/CodeCompletion.h| 11 +- clang/lib/Interpreter/CodeCompletion.cpp | 147 +++-- clang/lib/Sema/SemaLookup.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 25 +-- .../Interpreter/CodeCompletionTest.cpp| 195 +- 5 files changed, 334 insertions(+), 48 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..83f665b7a2db944 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,13 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..9c812c28f677726 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -23,6 +23,9 @@ #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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void + HandleCodeCompleteResults(class Sema , CodeCompletionResult *InResults, +unsigned NumResults, +std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto = InResults[I]; -switch (Result.Kind) { -case CodeCompletionResult::RK_Declaration: - if (auto *ID = Result.Declaration->getIdentifier()) { -Results.push_back(ID->getName().str()); + + // auto& a = S.Context.Idents.get("f1"); + // LLVM_DEBUG(llvm::dbgs() << "\n F11 : " << a.getName() << "\n" ); + auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); + CC.Prefix = Prefix; + switch (Context.getKind()) { + case CodeCompletionContext::CCC_DotMemberAccess: { +// FIXME: check BaseType is dependent, or from a typo +auto BaseType = Context.getBaseType(); +LLVM_DEBUG(llvm::dbgs() << "BaseType : " << BaseType.getAsString() << "\n" ); +for (unsigned I = 0; I < NumResults; ++I) { + auto = InResults[I]; + switch (Result.Kind) { + case CodeCompletionResult::RK_Declaration: +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == BaseType->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} +break; + default: +break; } - break; -case CodeCompletionResult::RK_Keyword: - Results.push_back(Result.Keyword); - break; -default: - break; } +break; } + default: +auto PreferredType = Context.getPreferredType(); +for (unsigned I = 0; I <
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
llvmbot wrote: @llvm/pr-subscribers-clang Changes Differential Revision: https://reviews.llvm.org/D159128 --- Patch is 20.30 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/67349.diff 5 Files Affected: - (modified) clang/include/clang/Interpreter/CodeCompletion.h (+7-3) - (modified) clang/lib/Interpreter/CodeCompletion.cpp (+127-20) - (modified) clang/lib/Sema/SemaLookup.cpp (+4) - (modified) clang/tools/clang-repl/ClangRepl.cpp (+7-15) - (modified) clang/unittests/Interpreter/CodeCompletionTest.cpp (+185-10) ``diff diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..d78072cec1a2666 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,12 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..9c812c28f677726 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -23,6 +23,9 @@ #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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void + HandleCodeCompleteResults(class Sema , CodeCompletionResult *InResults, +unsigned NumResults, +std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto = InResults[I]; -switch (Result.Kind) { -case CodeCompletionResult::RK_Declaration: - if (auto *ID = Result.Declaration->getIdentifier()) { -Results.push_back(ID->getName().str()); + + // auto& a = S.Context.Idents.get("f1"); + // LLVM_DEBUG(llvm::dbgs() << "\n F11 : " << a.getName() << "\n" ); + auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); + CC.Prefix = Prefix; + switch (Context.getKind()) { + case CodeCompletionContext::CCC_DotMemberAccess: { +// FIXME: check BaseType is dependent, or from a typo +auto BaseType = Context.getBaseType(); +LLVM_DEBUG(llvm::dbgs() << "BaseType : " << BaseType.getAsString() << "\n" ); +for (unsigned I = 0; I < NumResults; ++I) { + auto = InResults[I]; + switch (Result.Kind) { + case CodeCompletionResult::RK_Declaration: +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == BaseType->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} +break; + default: +break; } - break; -case CodeCompletionResult::RK_Keyword: - Results.push_back(Result.Keyword); - break; -default: - break; } +break; } + default: +auto PreferredType = Context.getPreferredType(); +for (unsigned I = 0; I < NumResults; I++) { + auto = InResults[I]; + switch (Result.Kind) { + case CodeCompletionResult::RK_Declaration:
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
https://github.com/capfredf created https://github.com/llvm/llvm-project/pull/67349 Differential Revision: https://reviews.llvm.org/D159128 >From 0515d9d0901bc00b26787b1774ef003d5f0b8567 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Tue, 29 Aug 2023 11:56:59 -0400 Subject: [PATCH] [ClangRepl] Type Directed Code Completion Differential Revision: https://reviews.llvm.org/D159128 --- .../clang/Interpreter/CodeCompletion.h| 10 +- clang/lib/Interpreter/CodeCompletion.cpp | 147 +++-- clang/lib/Sema/SemaLookup.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 22 +- .../Interpreter/CodeCompletionTest.cpp| 195 +- 5 files changed, 330 insertions(+), 48 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac6..d78072cec1a2666 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,12 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector ); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, const CompilerInstance *ParentCI, +std::vector ); +}; } // namespace clang #endif diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0a..9c812c28f677726 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -23,6 +23,9 @@ #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 { @@ -37,12 +40,22 @@ clang::CodeCompleteOptions getClangCompleteOpts() { return Opts; } +class CodeCompletionSubContext { +public: + virtual ~CodeCompletionSubContext(){}; + virtual void + HandleCodeCompleteResults(class Sema , CodeCompletionResult *InResults, +unsigned NumResults, +std::vector ) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector ) + ReplCompletionConsumer(std::vector , ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema , CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto = InResults[I]; -switch (Result.Kind) { -case CodeCompletionResult::RK_Declaration: - if (auto *ID = Result.Declaration->getIdentifier()) { -Results.push_back(ID->getName().str()); + + // auto& a = S.Context.Idents.get("f1"); + // LLVM_DEBUG(llvm::dbgs() << "\n F11 : " << a.getName() << "\n" ); + auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); + CC.Prefix = Prefix; + switch (Context.getKind()) { + case CodeCompletionContext::CCC_DotMemberAccess: { +// FIXME: check BaseType is dependent, or from a typo +auto BaseType = Context.getBaseType(); +LLVM_DEBUG(llvm::dbgs() << "BaseType : " << BaseType.getAsString() << "\n" ); +for (unsigned I = 0; I < NumResults; ++I) { + auto = InResults[I]; + switch (Result.Kind) { + case CodeCompletionResult::RK_Declaration: +if (auto *ID = Result.Declaration->getIdentifier()) { + if (const auto *Fun = llvm::dyn_cast(Result.Declaration)) { +if (Fun->getParent()->getCanonicalDecl() == BaseType->getAsCXXRecordDecl()->getCanonicalDecl()) { + LLVM_DEBUG(llvm::dbgs() << "[In HandleCodeCompleteDOT] Name : " << ID->getName() << "\n"); + Results.push_back(ID->getName().str()); +} + } +} +break; + default: +break; } - break; -case CodeCompletionResult::RK_Keyword: - Results.push_back(Result.Keyword); - break; -default: - break; } +break; } + default: +auto PreferredType = Context.getPreferredType(); +