[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompletion &CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion &CC; +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector &Results; + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector &Results) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult &Result) {} + virtual void handleKeyword(const CodeCompletionResult &Result) {} + virtual void handlePattern(const CodeCompletionResult &Result) {} + virtual void handleMacro(const CodeCompletionResult &Result) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector &Results) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult &Result) 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 &S; + +public: + DefaultAccessHandler(Sema &S, CodeCompletionContext CCC, + std::vector &Results) + : Comple
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompletion &CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion &CC; +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector &Results; + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector &Results) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult &Result) {} + virtual void handleKeyword(const CodeCompletionResult &Result) {} + virtual void handlePattern(const CodeCompletionResult &Result) {} + virtual void handleMacro(const CodeCompletionResult &Result) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector &Results) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult &Result) 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 &S; + +public: + DefaultAccessHandler(Sema &S, CodeCompletionContext CCC, + std::vector &Results) + : Comple
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompletion &CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion &CC; +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector &Results; + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector &Results) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult &Result) {} + virtual void handleKeyword(const CodeCompletionResult &Result) {} + virtual void handlePattern(const CodeCompletionResult &Result) {} + virtual void handleMacro(const CodeCompletionResult &Result) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector &Results) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult &Result) 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 &S; + +public: + DefaultAccessHandler(Sema &S, CodeCompletionContext CCC, + std::vector &Results) + : Comple
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompletion &CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion &CC; +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector &Results; + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector &Results) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult &Result) {} + virtual void handleKeyword(const CodeCompletionResult &Result) {} + virtual void handlePattern(const CodeCompletionResult &Result) {} + virtual void handleMacro(const CodeCompletionResult &Result) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector &Results) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult &Result) 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 &S; + +public: + DefaultAccessHandler(Sema &S, CodeCompletionContext CCC, + std::vector &Results) + : Comple
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompletion &CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion &CC; +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector &Results; + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector &Results) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult &Result) {} + virtual void handleKeyword(const CodeCompletionResult &Result) {} + virtual void handlePattern(const CodeCompletionResult &Result) {} + virtual void handleMacro(const CodeCompletionResult &Result) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector &Results) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult &Result) 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 &S; + +public: + DefaultAccessHandler(Sema &S, CodeCompletionContext CCC, + std::vector &Results) + : Comple
[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 &CCResults); +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 &CCResults); +}; } // 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 &S, + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompleter &CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +/* + 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 &Results; + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector &Results) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. + virtual void handleDeclaration(const CodeCompletionResult &Result) {} + + // convert a Keyword compl
[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 &CCResults); +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 &CCResults); +}; } // 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 &getASTContext() const; ASTContext &getASTContext(); 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 &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompleter &CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +60,152 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +// 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 &Results; + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector &Results) + : CCC(CCC), Results(Results) {} + + // converts a Declaration completion result to a completion string, and then + // stores it in Results. + virtual void handleDeclaration(const CodeCompletionResult &Result) {} + + // converts a Keyword comp
[clang] [ClangRepl] Type Directed Code Completion (PR #67349)
@@ -56,26 +69,151 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +/* + 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 &Results; + +public: + CompletionContextHandler(CodeCompletionContext CCC, + std::vector &Results) + : CCC(CCC), Results(Results) {} + + // convert a Declaration completion result to a completion string, and then store it in Results. + virtual void handleDeclaration(const CodeCompletionResult &Result) {} + + // convert a Keyword completion result to a completion string, and then store it in Results. + virtual void handleKeyword(const CodeCompletionResult &Result) {} + + // convert a Pattern completion result to a completion string, and then store it in Results. + virtual void handlePattern(const CodeCompletionResult &Result) {} + + // convert a Macro completion result to a completion string, and then store it in Results. + virtual void handleMacro(const CodeCompletionResult &Result) {} +}; + +class DotMemberAccessHandler : public CompletionContextHandler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector &Results) + : CompletionContextHandler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult &Result) 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 &MainInterp, 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)
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 &CCResults); +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 &CCResults); +}; } // 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 &getASTContext() const; ASTContext &getASTContext(); 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 &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompleter &CC) : 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 &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -56,26 +63,146 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +// 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 &Results; + +private: + Sema &S; + +public: + CompletionContextHandler(Sema &S, CodeCompletionContext CCC, + std:
[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 &CCResults); +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 &CCResults); +}; } // 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 &getASTContext() const; ASTContext &getASTContext(); 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 &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompleter &CC) : 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 &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -56,26 +63,146 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +// 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 &Results; + +private: + Sema &S; + +public: + CompletionContextHandler(Sema &S, CodeCompletionContext CCC, + std:
[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 &CCResults); +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 &CCResults); +}; } // 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 &getASTContext() const; ASTContext &getASTContext(); 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 &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompleter &CC) : 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 &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -56,26 +63,146 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +/// 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 &Results; + +private: + Sema &S; + +public: + CompletionContextHandler(Sema &S, CodeCompletionContext CCC, +
[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)
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] Semanic Code Completion (PR #75556)
https://github.com/capfredf created https://github.com/llvm/llvm-project/pull/75556 None >From 77b2b39174570f62dec16557b8a539811f64b62c Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Wed, 13 Dec 2023 22:07:17 -0500 Subject: [PATCH 1/2] WIP --- .../clang/Interpreter/CodeCompletion.h| 25 +- clang/include/clang/Interpreter/Interpreter.h | 1 + clang/lib/Interpreter/CodeCompletion.cpp | 222 -- clang/lib/Interpreter/Interpreter.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 24 +- .../Interpreter/CodeCompletionTest.cpp| 219 - 6 files changed, 444 insertions(+), 51 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac..c64aa899759fd8 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,27 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector &CCResults); +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 &CCResults); +}; } // namespace clang #endif diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index 43573fb1a4b891..01858dfcc90ac5 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -101,6 +101,7 @@ class Interpreter { const ASTContext &getASTContext() const; ASTContext &getASTContext(); const CompilerInstance *getCompilerInstance() const; + CompilerInstance *getCompilerInstance(); llvm::Expected getExecutionEngine(); llvm::Expected Parse(llvm::StringRef Code); diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0..c34767c3ef9b87 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -12,6 +12,7 @@ #include "clang/Interpreter/CodeCompletion.h" #include "clang/AST/ASTImporter.h" +#include "clang/AST/DeclLookups.h" #include "clang/AST/DeclarationName.h" #include "clang/AST/ExternalASTSource.h" #include "clang/Basic/IdentifierTable.h" @@ -23,6 +24,8 @@ #include "clang/Sema/CodeCompleteConsumer.h" #include "clang/Sema/CodeCompleteOptions.h" #include "clang/Sema/Sema.h" +#include "llvm/Support/Debug.h" +#define DEBUG_TYPE "REPLCC" namespace clang { @@ -39,11 +42,15 @@ clang::CodeCompleteOptions getClangCompleteOpts() { class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompleter &CC) : 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 &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -56,26 +63,146 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +/// 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 &Results; + +private: + Sema &S; + +public: + CompletionContextHandler(Sema &S, CodeCompletionContext CCC, + std::vector &Results) + : CCC(CCC), Results(Results), S(S) {} + + /// Converts a Declaration compl
[clang] [ClangRepl] Semanic Code Completion (PR #75556)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/75556 >From 17486783ff2eab10a592eef53f33b1298ff29b49 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Wed, 13 Dec 2023 22:07:17 -0500 Subject: [PATCH] [ClangRepl] Semantic Code Completion This patch piggybacks on clang's semantic modules to enable semantic completion. In particular, we use `CodeCompletionContext` to differentiate two types of code completion. We also extract the relevant type information from it. --- .../clang/Interpreter/CodeCompletion.h| 25 +- clang/include/clang/Interpreter/Interpreter.h | 1 + clang/lib/Interpreter/CodeCompletion.cpp | 223 -- clang/lib/Interpreter/Interpreter.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 24 +- .../Interpreter/CodeCompletionTest.cpp| 219 - 6 files changed, 445 insertions(+), 51 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac..c64aa899759fd8 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,27 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector &CCResults); +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 &CCResults); +}; } // namespace clang #endif diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index 43573fb1a4b891..01858dfcc90ac5 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -101,6 +101,7 @@ class Interpreter { const ASTContext &getASTContext() const; ASTContext &getASTContext(); const CompilerInstance *getCompilerInstance() const; + CompilerInstance *getCompilerInstance(); llvm::Expected getExecutionEngine(); llvm::Expected Parse(llvm::StringRef Code); diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0..a9789355b2c5f3 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -12,6 +12,7 @@ #include "clang/Interpreter/CodeCompletion.h" #include "clang/AST/ASTImporter.h" +#include "clang/AST/DeclLookups.h" #include "clang/AST/DeclarationName.h" #include "clang/AST/ExternalASTSource.h" #include "clang/Basic/IdentifierTable.h" @@ -23,6 +24,8 @@ #include "clang/Sema/CodeCompleteConsumer.h" #include "clang/Sema/CodeCompleteOptions.h" #include "clang/Sema/Sema.h" +#include "llvm/Support/Debug.h" +#define DEBUG_TYPE "REPLCC" namespace clang { @@ -39,11 +42,15 @@ clang::CodeCompleteOptions getClangCompleteOpts() { class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompleter &CC) : 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 &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -56,26 +63,147 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +/// 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; + s
[clang] [ClangRepl] Semanic Code Completion (PR #75556)
capfredf wrote: @vgvassilev https://github.com/llvm/llvm-project/pull/75556 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Semanic Code Completion (PR #75556)
https://github.com/capfredf ready_for_review https://github.com/llvm/llvm-project/pull/75556 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Reland Semanic Code Completion (PR #75556)
https://github.com/capfredf edited https://github.com/llvm/llvm-project/pull/75556 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Reland Semanic Code Completion (PR #75556)
capfredf wrote: @vgvassilev Sure. https://github.com/llvm/llvm-project/pull/75556 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Reland Semanic Code Completion (PR #75556)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/75556 >From bade781901b95458b0195952d0879f60c1320607 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Wed, 13 Dec 2023 22:07:17 -0500 Subject: [PATCH] [ClangRepl] Reland Semantic Code Completion This patch contains changes from 002d471a4a3cd8b429e4ca7c84fd54a642e50e4c, in addition to a bug fix that added a virtual destructor to `CompletionContextHandler` The original changes in the orginal commit piggybacks on clang's semantic modules to enable semantic completion. In particular, we use `CodeCompletionContext` to differentiate two types of code completion. We also extract the relevant type information from it. --- .../clang/Interpreter/CodeCompletion.h| 25 +- clang/include/clang/Interpreter/Interpreter.h | 1 + clang/lib/Interpreter/CodeCompletion.cpp | 223 -- clang/lib/Interpreter/Interpreter.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 24 +- .../Interpreter/CodeCompletionTest.cpp| 219 - 6 files changed, 445 insertions(+), 51 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac..c64aa899759fd8 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,27 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector &CCResults); +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 &CCResults); +}; } // namespace clang #endif diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index 43573fb1a4b891..01858dfcc90ac5 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -101,6 +101,7 @@ class Interpreter { const ASTContext &getASTContext() const; ASTContext &getASTContext(); const CompilerInstance *getCompilerInstance() const; + CompilerInstance *getCompilerInstance(); llvm::Expected getExecutionEngine(); llvm::Expected Parse(llvm::StringRef Code); diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0..a9789355b2c5f3 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -12,6 +12,7 @@ #include "clang/Interpreter/CodeCompletion.h" #include "clang/AST/ASTImporter.h" +#include "clang/AST/DeclLookups.h" #include "clang/AST/DeclarationName.h" #include "clang/AST/ExternalASTSource.h" #include "clang/Basic/IdentifierTable.h" @@ -23,6 +24,8 @@ #include "clang/Sema/CodeCompleteConsumer.h" #include "clang/Sema/CodeCompleteOptions.h" #include "clang/Sema/Sema.h" +#include "llvm/Support/Debug.h" +#define DEBUG_TYPE "REPLCC" namespace clang { @@ -39,11 +42,15 @@ clang::CodeCompleteOptions getClangCompleteOpts() { class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompleter &CC) : 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 &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -56,26 +63,147 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +/// The class CompletionContextHandler contains four interfaces, each of +/// which handles one type o
[clang] [ClangRepl] Reland Semanic Code Completion (PR #75556)
https://github.com/capfredf updated https://github.com/llvm/llvm-project/pull/75556 >From 50d9369cb5144676060a3a6fa0298b3b4a903193 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Wed, 13 Dec 2023 22:07:17 -0500 Subject: [PATCH] [ClangRepl] Reland Semantic Code Completion This patch contains changes of 002d471a4a3cd8b429e4ca7c84fd54a642e50e4c, in addition to a bug fix that added a virtual destructor to `CompletionContextHandler` The original changes in the orginal commit piggybacks on clang's semantic modules to enable semantic completion. In particular, we use `CodeCompletionContext` to differentiate two types of code completion. We also extract the relevant type information from it. --- .../clang/Interpreter/CodeCompletion.h| 25 +- clang/include/clang/Interpreter/Interpreter.h | 1 + clang/lib/Interpreter/CodeCompletion.cpp | 223 -- clang/lib/Interpreter/Interpreter.cpp | 4 + clang/tools/clang-repl/ClangRepl.cpp | 24 +- .../Interpreter/CodeCompletionTest.cpp| 219 - 6 files changed, 445 insertions(+), 51 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index 9adcdf0dc3afac..c64aa899759fd8 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,8 +23,27 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, - unsigned Line, unsigned Col, const CompilerInstance *ParentCI, - std::vector &CCResults); +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 &CCResults); +}; } // namespace clang #endif diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index 43573fb1a4b891..01858dfcc90ac5 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -101,6 +101,7 @@ class Interpreter { const ASTContext &getASTContext() const; ASTContext &getASTContext(); const CompilerInstance *getCompilerInstance() const; + CompilerInstance *getCompilerInstance(); llvm::Expected getExecutionEngine(); llvm::Expected Parse(llvm::StringRef Code); diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c40e11b9d1ece0..a9789355b2c5f3 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -12,6 +12,7 @@ #include "clang/Interpreter/CodeCompletion.h" #include "clang/AST/ASTImporter.h" +#include "clang/AST/DeclLookups.h" #include "clang/AST/DeclarationName.h" #include "clang/AST/ExternalASTSource.h" #include "clang/Basic/IdentifierTable.h" @@ -23,6 +24,8 @@ #include "clang/Sema/CodeCompleteConsumer.h" #include "clang/Sema/CodeCompleteOptions.h" #include "clang/Sema/Sema.h" +#include "llvm/Support/Debug.h" +#define DEBUG_TYPE "REPLCC" namespace clang { @@ -39,11 +42,15 @@ clang::CodeCompleteOptions getClangCompleteOpts() { class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompleter &CC) : 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 &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -56,26 +63,147 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +/// The class CompletionContextHandler contains four interfaces, each of +/// which handles one type of
[clang] [ClangRepl] Reland Semanic Code Completion (PR #75556)
https://github.com/capfredf edited https://github.com/llvm/llvm-project/pull/75556 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] Reland Semanic Code Completion (PR #75556)
capfredf wrote: @vgvassilev the commit message in the original commit isn't very useful. To my recollection, I wrote more detailed message than `Differential Revision: https://reviews.llvm.org/D159128`, but I can't find it now. The original development branch has been deleted. So I rewrote the message. https://github.com/llvm/llvm-project/pull/75556 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [ClangRepl] 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 &CCResults); +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 &CCResults); +}; } // 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 &getASTContext() const; ASTContext &getASTContext(); 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 &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompleter &CC) : 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 &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -56,26 +63,146 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompleter &CC; +}; + +/// 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 &Results; + +private: + Sema &S; + +public: + CompletionContextHandler(Sema &S, CodeCompletionContext CCC, +
[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)
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] Revert "[ClangRepl] Type Directed Code Completion" (PR #73259)
https://github.com/capfredf created https://github.com/llvm/llvm-project/pull/73259 Reverts llvm/llvm-project#67349 >From ad46f098f845c6a67cce0229dd402bd8cf31ac16 Mon Sep 17 00:00:00 2001 From: Fred Fu Date: Thu, 23 Nov 2023 14:06:49 -0500 Subject: [PATCH] Revert "[ClangRepl] Type Directed Code Completion (#67349)" This reverts commit 002d471a4a3cd8b429e4ca7c84fd54a642e50e4c. --- .../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, 51 insertions(+), 444 deletions(-) diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h index c64aa899759fd87..9adcdf0dc3afac6 100644 --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -23,27 +23,8 @@ namespace clang { class CodeCompletionResult; class CompilerInstance; -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 &CCResults); -}; +void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, + unsigned Line, unsigned Col, const CompilerInstance *ParentCI, + std::vector &CCResults); } // namespace clang #endif diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index 01858dfcc90ac5a..43573fb1a4b8915 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -101,7 +101,6 @@ class Interpreter { const ASTContext &getASTContext() const; ASTContext &getASTContext(); 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 c34767c3ef9b87d..c40e11b9d1ece0a 100644 --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -12,7 +12,6 @@ #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" @@ -24,8 +23,6 @@ #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 { @@ -42,15 +39,11 @@ clang::CodeCompleteOptions getClangCompleteOpts() { class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results, - ReplCodeCompleter &CC) + ReplCompletionConsumer(std::vector &Results) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results), CC(CC) {} +CCTUInfo(CCAllocator), Results(Results){}; - // 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 &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) final; @@ -63,146 +56,26 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; - ReplCodeCompleter &CC; -}; - -/// 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 &Results; - -private: - Sema &S; - -public: - CompletionContextHandler(Sema &S, CodeCompleti
[clang] Revert "[ClangRepl] Type Directed Code Completion" (PR #73259)
capfredf wrote: @vgvassilev https://github.com/llvm/llvm-project/pull/73259 ___ 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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, CodeCompletionResult *InResults, +unsigned NumResults, +std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto &Result = 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 &Result = 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 Prefer
[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 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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompletion &CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion &CC; +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector &Results; + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector &Results) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult &Result) {} + virtual void handleKeyword(const CodeCompletionResult &Result) {} + virtual void handlePattern(const CodeCompletionResult &Result) {} + virtual void handleMacro(const CodeCompletionResult &Result) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector &Results) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult &Result) 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 &S; + +public: + DefaultAccessHandler(Sema &S, CodeCompletionContext CCC, +
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, + CodeCompletionResult *InResults, + unsigned NumResults, + std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, + ReplCodeCompletion &CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) {} void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,135 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion &CC; +}; + +class CompletionContextHanndler { +protected: + CodeCompletionContext CCC; + std::vector &Results; + +public: + CompletionContextHanndler(CodeCompletionContext CCC, +std::vector &Results) + : CCC(CCC), Results(Results) {} + virtual void handleDeclaration(const CodeCompletionResult &Result) {} + virtual void handleKeyword(const CodeCompletionResult &Result) {} + virtual void handlePattern(const CodeCompletionResult &Result) {} + virtual void handleMacro(const CodeCompletionResult &Result) {} +}; + +class DotMemberAccessHandler : public CompletionContextHanndler { +public: + DotMemberAccessHandler(CodeCompletionContext CCC, + std::vector &Results) + : CompletionContextHanndler(CCC, Results) {} + void handleDeclaration(const CodeCompletionResult &Result) 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 &S; + +public: + DefaultAccessHandler(Sema &S, CodeCompletionContext CCC, + std::vector &Results) + : Completion
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, CodeCompletionResult *InResults, +unsigned NumResults, +std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto &Result = 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 &Result = 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;
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, CodeCompletionResult *InResults, +unsigned NumResults, +std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto &Result = 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 &Result = 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 Prefer
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, CodeCompletionResult *InResults, +unsigned NumResults, +std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto &Result = 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 &Result = 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 Prefer
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, CodeCompletionResult *InResults, +unsigned NumResults, +std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto &Result = 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 &Result = 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 Prefer
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, CodeCompletionResult *InResults, +unsigned NumResults, +std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto &Result = 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 &Result = 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 Prefer
[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 &CCResults); +struct ReplCodeCompletion { + ReplCodeCompletion() = default; + std::string Prefix; + void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, +unsigned Line, unsigned Col, +const CompilerInstance *ParentCI, +std::vector &CCResults); +}; } // 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 &S, CodeCompletionResult *InResults, +unsigned NumResults, +std::vector &Results) = 0; +}; + class ReplCompletionConsumer : public CodeCompleteConsumer { public: - ReplCompletionConsumer(std::vector &Results) + ReplCompletionConsumer(std::vector &Results, ReplCodeCompletion& CC) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared()), -CCTUInfo(CCAllocator), Results(Results){}; +CCTUInfo(CCAllocator), Results(Results), CC(CC) { + } void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, @@ -56,26 +69,90 @@ class ReplCompletionConsumer : public CodeCompleteConsumer { std::shared_ptr CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector &Results; + ReplCodeCompletion& CC; }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - for (unsigned I = 0; I < NumResults; ++I) { -auto &Result = 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 &Result = 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 Prefer