diff options
author | Fred Fu <moonsolo@gmail.com> | 2023-11-23 14:07:51 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-23 21:07:51 +0200 |
commit | 9ebe6e28cdbe97f6c03209b87e91be6b55a8026a (patch) | |
tree | 506a5f287774a55df8ca6abdaecb0919199f0c39 /clang/lib | |
parent | aea7929b0a04c5ea0ac85aba2b85fb58c718626f (diff) | |
download | llvm-9ebe6e28cdbe97f6c03209b87e91be6b55a8026a.zip llvm-9ebe6e28cdbe97f6c03209b87e91be6b55a8026a.tar.gz llvm-9ebe6e28cdbe97f6c03209b87e91be6b55a8026a.tar.bz2 |
Revert "[ClangRepl] Type Directed Code Completion" (#73259)
Reverts llvm/llvm-project#67349
There are some issues with the sanitizers. We will reland once that's fixed.
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Interpreter/CodeCompletion.cpp | 222 | ||||
-rw-r--r-- | clang/lib/Interpreter/Interpreter.cpp | 4 |
2 files changed, 23 insertions, 203 deletions
diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp index c34767c..c40e11b 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<std::string> &Results, - ReplCodeCompleter &CC) + ReplCompletionConsumer(std::vector<std::string> &Results) : CodeCompleteConsumer(getClangCompleteOpts()), CCAllocator(std::make_shared<GlobalCodeCompletionAllocator>()), - 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 @@ private: std::shared_ptr<GlobalCodeCompletionAllocator> CCAllocator; CodeCompletionTUInfo CCTUInfo; std::vector<std::string> &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<std::string> &Results; - -private: - Sema &S; - -public: - CompletionContextHandler(Sema &S, CodeCompletionContext CCC, - std::vector<std::string> &Results) - : CCC(CCC), Results(Results), S(S) {} - - /// Converts a Declaration completion result to a completion string, and then - /// stores it in Results. - virtual void handleDeclaration(const CodeCompletionResult &Result) { - auto PreferredType = CCC.getPreferredType(); - if (PreferredType.isNull()) { - Results.push_back(Result.Declaration->getName().str()); - return; - } - - if (auto *VD = dyn_cast<VarDecl>(Result.Declaration)) { - auto ArgumentType = VD->getType(); - if (PreferredType->isReferenceType()) { - QualType RT = PreferredType->castAs<ReferenceType>()->getPointeeType(); - Sema::ReferenceConversions RefConv; - Sema::ReferenceCompareResult RefRelationship = - S.CompareReferenceRelationship(SourceLocation(), RT, ArgumentType, - &RefConv); - switch (RefRelationship) { - case Sema::Ref_Compatible: - case Sema::Ref_Related: - Results.push_back(VD->getName().str()); - break; - case Sema::Ref_Incompatible: - break; - } - } else if (S.Context.hasSameType(ArgumentType, PreferredType)) { - Results.push_back(VD->getName().str()); - } - } - } - - /// Converts a Keyword completion result to a completion string, and then - /// stores it in Results. - virtual void handleKeyword(const CodeCompletionResult &Result) { - auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); - // Add keyword to the completion results only if we are in a type-aware - // situation. - if (!CCC.getBaseType().isNull() || !CCC.getPreferredType().isNull()) - return; - if (StringRef(Result.Keyword).startswith(Prefix)) - Results.push_back(Result.Keyword); - } - - /// Converts a Pattern completion result to a completion string, and then - /// stores it in Results. - virtual void handlePattern(const CodeCompletionResult &Result) {} - - /// Converts a Macro completion result to a completion string, and then stores - /// it in Results. - virtual void handleMacro(const CodeCompletionResult &Result) {} -}; - -class DotMemberAccessHandler : public CompletionContextHandler { -public: - DotMemberAccessHandler(Sema &S, CodeCompletionContext CCC, - std::vector<std::string> &Results) - : CompletionContextHandler(S, CCC, Results) {} - void handleDeclaration(const CodeCompletionResult &Result) override { - auto *ID = Result.Declaration->getIdentifier(); - if (!ID) - return; - if (!isa<CXXMethodDecl>(Result.Declaration)) - return; - const auto *Fun = cast<CXXMethodDecl>(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()); - } - } - - void handleKeyword(const CodeCompletionResult &Result) override {} }; void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { - - auto Prefix = S.getPreprocessor().getCodeCompletionFilter(); - CC.Prefix = Prefix; - - std::unique_ptr<CompletionContextHandler> CCH; - - // initialize fine-grained code completion handler based on the code - // completion context. - switch (Context.getKind()) { - case CodeCompletionContext::CCC_DotMemberAccess: - CCH.reset(new DotMemberAccessHandler(S, Context, this->Results)); - break; - default: - CCH.reset(new CompletionContextHandler(S, Context, this->Results)); - }; - - for (unsigned I = 0; I < NumResults; I++) { + for (unsigned I = 0; I < NumResults; ++I) { auto &Result = InResults[I]; switch (Result.Kind) { case CodeCompletionResult::RK_Declaration: - if (Result.Hidden) { - break; - } - if (!Result.Declaration->getDeclName().isIdentifier() || - !Result.Declaration->getName().startswith(Prefix)) { - break; + if (auto *ID = Result.Declaration->getIdentifier()) { + Results.push_back(ID->getName().str()); } - CCH->handleDeclaration(Result); break; case CodeCompletionResult::RK_Keyword: - CCH->handleKeyword(Result); - break; - case CodeCompletionResult::RK_Macro: - CCH->handleMacro(Result); + Results.push_back(Result.Keyword); break; - case CodeCompletionResult::RK_Pattern: - CCH->handlePattern(Result); + default: break; } } - - std::sort(Results.begin(), Results.end()); } class IncrementalSyntaxOnlyAction : public SyntaxOnlyAction { @@ -245,16 +118,6 @@ void IncrementalSyntaxOnlyAction::ExecuteAction() { CI.getASTContext().getTranslationUnitDecl()->setHasExternalVisibleStorage( true); - // Load all external decls into current context. Under the hood, it calls - // ExternalSource::completeVisibleDeclsMap, which make all decls on the redecl - // chain visible. - // - // This is crucial to code completion on dot members, since a bound variable - // before "." would be otherwise treated out-of-scope. - // - // clang-repl> Foo f1; - // clang-repl> f1.<tab> - CI.getASTContext().getTranslationUnitDecl()->lookups(); SyntaxOnlyAction::ExecuteAction(); } @@ -271,7 +134,6 @@ ExternalSource::ExternalSource(ASTContext &ChildASTCtxt, FileManager &ChildFM, bool ExternalSource::FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) { - IdentifierTable &ParentIdTable = ParentASTCtxt.Idents; auto ParentDeclName = @@ -297,67 +159,29 @@ void ExternalSource::completeVisibleDeclsMap( for (auto *DeclCtxt = ParentTUDeclCtxt; DeclCtxt != nullptr; DeclCtxt = DeclCtxt->getPreviousDecl()) { for (auto &IDeclContext : DeclCtxt->decls()) { - if (!llvm::isa<NamedDecl>(IDeclContext)) - continue; - - NamedDecl *Decl = llvm::cast<NamedDecl>(IDeclContext); - - auto DeclOrErr = Importer->Import(Decl); - if (!DeclOrErr) { - // if an error happens, it usually means the decl has already been - // imported or the decl is a result of a failed import. But in our - // case, every import is fresh each time code completion is - // triggered. So Import usually doesn't fail. If it does, it just means - // the related decl can't be used in code completion and we can safely - // drop it. - llvm::consumeError(DeclOrErr.takeError()); - continue; - } - - if (!llvm::isa<NamedDecl>(*DeclOrErr)) - continue; - - NamedDecl *importedNamedDecl = llvm::cast<NamedDecl>(*DeclOrErr); - - SetExternalVisibleDeclsForName(ChildDeclContext, - importedNamedDecl->getDeclName(), - importedNamedDecl); - - if (!llvm::isa<CXXRecordDecl>(importedNamedDecl)) - continue; - - auto *Record = llvm::cast<CXXRecordDecl>(importedNamedDecl); - - if (auto Err = Importer->ImportDefinition(Decl)) { - // the same as above - consumeError(std::move(Err)); - continue; + if (NamedDecl *Decl = llvm::dyn_cast<NamedDecl>(IDeclContext)) { + if (auto DeclOrErr = Importer->Import(Decl)) { + if (NamedDecl *importedNamedDecl = + llvm::dyn_cast<NamedDecl>(*DeclOrErr)) { + SetExternalVisibleDeclsForName(ChildDeclContext, + importedNamedDecl->getDeclName(), + importedNamedDecl); + } + + } else { + llvm::consumeError(DeclOrErr.takeError()); + } } - - Record->setHasLoadedFieldsFromExternalStorage(true); - LLVM_DEBUG(llvm::dbgs() - << "\nCXXRecrod : " << Record->getName() << " size(methods): " - << std::distance(Record->method_begin(), Record->method_end()) - << " has def?: " << Record->hasDefinition() - << " # (methods): " - << std::distance(Record->getDefinition()->method_begin(), - Record->getDefinition()->method_end()) - << "\n"); - for (auto *Meth : Record->methods()) - SetExternalVisibleDeclsForName(ChildDeclContext, Meth->getDeclName(), - Meth); } ChildDeclContext->setHasExternalLexicalStorage(false); } } -void ReplCodeCompleter::codeComplete(CompilerInstance *InterpCI, - llvm::StringRef Content, unsigned Line, - unsigned Col, - const CompilerInstance *ParentCI, - std::vector<std::string> &CCResults) { +void codeComplete(CompilerInstance *InterpCI, llvm::StringRef Content, + unsigned Line, unsigned Col, const CompilerInstance *ParentCI, + std::vector<std::string> &CCResults) { auto DiagOpts = DiagnosticOptions(); - auto consumer = ReplCompletionConsumer(CCResults, *this); + auto consumer = ReplCompletionConsumer(CCResults); auto diag = InterpCI->getDiagnosticsPtr(); std::unique_ptr<ASTUnit> AU(ASTUnit::LoadFromCompilerInvocationAction( diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp index c9fcef5..7968c62 100644 --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -319,10 +319,6 @@ const CompilerInstance *Interpreter::getCompilerInstance() const { return IncrParser->getCI(); } -CompilerInstance *Interpreter::getCompilerInstance() { - return IncrParser->getCI(); -} - llvm::Expected<llvm::orc::LLJIT &> Interpreter::getExecutionEngine() { if (!IncrExecutor) { if (auto Err = CreateExecutor()) |