diff --git a/clang/include/clang/Interpreter/CodeCompletion.h b/clang/include/clang/Interpreter/CodeCompletion.h --- a/clang/include/clang/Interpreter/CodeCompletion.h +++ b/clang/include/clang/Interpreter/CodeCompletion.h @@ -19,31 +19,6 @@ class Interpreter; class IncrementalCompilerBuilder; -clang::CodeCompleteOptions getClangCompleteOpts(); - -class ReplCompletionConsumer : public CodeCompleteConsumer { -public: - ReplCompletionConsumer(std::vector &Results) - : CodeCompleteConsumer(getClangCompleteOpts()), - CCAllocator(std::make_shared()), - CCTUInfo(CCAllocator), Results(Results){}; - void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, - CodeCompletionResult *InResults, - unsigned NumResults) final; - - clang::CodeCompletionAllocator &getAllocator() override { - return *CCAllocator; - } - - clang::CodeCompletionTUInfo &getCodeCompletionTUInfo() override { - return CCTUInfo; - } - -private: - std::shared_ptr CCAllocator; - CodeCompletionTUInfo CCTUInfo; - std::vector &Results; -}; struct ReplListCompleter { IncrementalCompilerBuilder &CB; diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -39,7 +39,7 @@ class CompilerInstance; class IncrementalExecutor; class IncrementalParser; -class ReplCompletionConsumer; +class CodeCompleteConsumer; /// Create a pre-configured \c CompilerInstance for incremental processing. class IncrementalCompilerBuilder { @@ -82,11 +82,10 @@ // An optional parser for CUDA offloading std::unique_ptr DeviceParser; - std::unique_ptr CConsumer; Interpreter(std::unique_ptr CI, llvm::Error &Err); Interpreter(std::unique_ptr CI, llvm::Error &Err, - std::vector &CompResults, + CodeCompleteConsumer* CConsumer, const CompilerInstance *ParentCI = nullptr); llvm::Error CreateExecutor(); @@ -110,7 +109,7 @@ static llvm::Expected> createForCodeCompletion(IncrementalCompilerBuilder &CB, const CompilerInstance *ParentCI, - std::vector &CompResults); + CodeCompleteConsumer* CConsumer); const ASTContext &getASTContext() const; ASTContext &getASTContext(); diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -13321,7 +13321,7 @@ /// specifiers within a function, method, or block. PCC_LocalDeclarationSpecifiers, /// Code completion occurs at top-level in a REPL session - PCC_ReplTopLevel, + PCC_TopLevelStmtDecl, }; void CodeCompleteModuleImport(SourceLocation ImportLoc, ModuleIdPath Path); diff --git a/clang/lib/Interpreter/CodeCompletion.cpp b/clang/lib/Interpreter/CodeCompletion.cpp --- a/clang/lib/Interpreter/CodeCompletion.cpp +++ b/clang/lib/Interpreter/CodeCompletion.cpp @@ -28,6 +28,32 @@ return Opts; } + +class ReplCompletionConsumer : public CodeCompleteConsumer { +public: + ReplCompletionConsumer(std::vector &Results) + : CodeCompleteConsumer(getClangCompleteOpts()), + CCAllocator(std::make_shared()), + CCTUInfo(CCAllocator), Results(Results){}; + void ProcessCodeCompleteResults(class Sema &S, CodeCompletionContext Context, + CodeCompletionResult *InResults, + unsigned NumResults) final; + + clang::CodeCompletionAllocator &getAllocator() override { + return *CCAllocator; + } + + clang::CodeCompletionTUInfo &getCodeCompletionTUInfo() override { + return CCTUInfo; + } + +private: + std::shared_ptr CCAllocator; + CodeCompletionTUInfo CCTUInfo; + std::vector &Results; +}; + + void ReplCompletionConsumer::ProcessCodeCompleteResults( class Sema &S, CodeCompletionContext Context, CodeCompletionResult *InResults, unsigned NumResults) { @@ -68,12 +94,14 @@ return CompletionStrings; } + std::vector ReplListCompleter::operator()(llvm::StringRef Buffer, size_t Pos) const { std::vector Comps; std::vector Results; + auto *CConsumer = new ReplCompletionConsumer(Results); auto Interp = Interpreter::createForCodeCompletion( - CB, MainInterp.getCompilerInstance(), Results); + CB, MainInterp.getCompilerInstance(), CConsumer); if (auto Err = Interp.takeError()) { // log the error and returns an empty vector; diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp --- a/clang/lib/Interpreter/IncrementalParser.cpp +++ b/clang/lib/Interpreter/IncrementalParser.cpp @@ -331,11 +331,7 @@ auto [FID, SrcLoc] = createSourceFile(SourceName.str(), input); auto FE = CI->getSourceManager().getFileEntryRefForID(FID); - // auto Entry = PP.getFileManager().getFile(DummyFN); - // if (!Entry) { - // std::cout << "Entry invalid \n"; - // return; - // } + if (FE) { PP.SetCodeCompletionPoint(*FE, Line, Col); @@ -372,13 +368,12 @@ // candidates for example SourceLocation NewLoc = SM.getLocForStartOfFile(SM.getMainFileID()); - // Create FileID for the current buffer. - // FileID FID = SM.createFileID(std::move(MB), SrcMgr::C_User, /*LoadedID=*/0, - // /*LoadedOffset=*/0, NewLoc); const clang::FileEntry *FE = SM.getFileManager().getVirtualFile( SourceName.str(), InputSize, 0 /* mod time*/); SM.overrideFileContents(FE, std::move(MB)); + + // Create FileID for the current buffer. FileID FID = SM.createFileID(FE, NewLoc, SrcMgr::C_User); return {FID, NewLoc}; } diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -239,12 +239,11 @@ } Interpreter::Interpreter(std::unique_ptr CI, llvm::Error &Err, - std::vector &CompResults, + CodeCompleteConsumer* CConsumer, const CompilerInstance *ParentCI) { llvm::ErrorAsOutParameter EAO(&Err); auto LLVMCtx = std::make_unique(); TSCtx = std::make_unique(std::move(LLVMCtx)); - auto *CConsumer = new ReplCompletionConsumer(CompResults); CI->setCodeCompletionConsumer(CConsumer); IncrParser = std::make_unique( *this, std::move(CI), *TSCtx->getContext(), Err, ParentCI); @@ -304,7 +303,7 @@ llvm::Expected> Interpreter::createForCodeCompletion( IncrementalCompilerBuilder &CB, const CompilerInstance *ParentCI, - std::vector &CompResults) { + CodeCompleteConsumer* CConsumer) { auto CI = CB.CreateCpp(); if (auto Err = CI.takeError()) { return std::move(Err); @@ -315,12 +314,9 @@ (*CI)->getLangOpts().SpellChecking = false; (*CI)->getLangOpts().DelayedTemplateParsing = false; - auto &FrontendOpts = (*CI)->getFrontendOpts(); - FrontendOpts.CodeCompleteOpts = getClangCompleteOpts(); - llvm::Error Err = llvm::Error::success(); auto Interp = std::unique_ptr( - new Interpreter(std::move(*CI), Err, CompResults, ParentCI)); + new Interpreter(std::move(*CI), Err, CConsumer, ParentCI)); if (Err) return std::move(Err); diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp --- a/clang/lib/Parse/Parser.cpp +++ b/clang/lib/Parse/Parser.cpp @@ -928,7 +928,7 @@ if (CurParsedObjCImpl) { PCC = Sema::PCC_ObjCImplementation; } else if (PP.isIncrementalProcessingEnabled()) { - PCC = Sema::PCC_ReplTopLevel; + PCC = Sema::PCC_TopLevelStmtDecl; } else { PCC = Sema::PCC_Namespace; }; diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -1851,7 +1851,7 @@ case Sema::PCC_ObjCInstanceVariableList: case Sema::PCC_Expression: case Sema::PCC_Statement: - case Sema::PCC_ReplTopLevel: + case Sema::PCC_TopLevelStmtDecl: case Sema::PCC_ForInit: case Sema::PCC_Condition: case Sema::PCC_RecoveryInFunction: @@ -1909,7 +1909,7 @@ case Sema::PCC_Type: case Sema::PCC_ParenthesizedExpression: case Sema::PCC_LocalDeclarationSpecifiers: - case Sema::PCC_ReplTopLevel: + case Sema::PCC_TopLevelStmtDecl: return true; case Sema::PCC_Expression: @@ -2222,7 +2222,7 @@ break; case Sema::PCC_RecoveryInFunction: - case Sema::PCC_ReplTopLevel: + case Sema::PCC_TopLevelStmtDecl: case Sema::PCC_Statement: { if (SemaRef.getLangOpts().CPlusPlus11) AddUsingAliasResult(Builder, Results); @@ -4212,7 +4212,7 @@ case Sema::PCC_LocalDeclarationSpecifiers: return CodeCompletionContext::CCC_Type; - case Sema::PCC_ReplTopLevel: + case Sema::PCC_TopLevelStmtDecl: return CodeCompletionContext::CCC_ReplTopLevel; } @@ -4354,7 +4354,7 @@ break; case PCC_Statement: - case PCC_ReplTopLevel: + case PCC_TopLevelStmtDecl: case PCC_ParenthesizedExpression: case PCC_Expression: case PCC_ForInit: @@ -4392,7 +4392,7 @@ case PCC_ParenthesizedExpression: case PCC_Expression: case PCC_Statement: - case PCC_ReplTopLevel: + case PCC_TopLevelStmtDecl: case PCC_RecoveryInFunction: if (S->getFnParent()) AddPrettyFunctionResults(getLangOpts(), Results); diff --git a/clang/tools/libclang/CIndexCodeCompletion.cpp b/clang/tools/libclang/CIndexCodeCompletion.cpp --- a/clang/tools/libclang/CIndexCodeCompletion.cpp +++ b/clang/tools/libclang/CIndexCodeCompletion.cpp @@ -543,6 +543,7 @@ case CodeCompletionContext::CCC_PreprocessorExpression: case CodeCompletionContext::CCC_PreprocessorDirective: case CodeCompletionContext::CCC_Attribute: + case CodeCompletionContext::CCC_ReplTopLevel: case CodeCompletionContext::CCC_TypeQualifiers: { //Only Clang results should be accepted, so we'll set all of the other //context bits to 0 (i.e. the empty set)