diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp index 1a0023d4b453a..68e12b2fb0016 100644 --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -821,7 +821,6 @@ bool contextAllowsIndex(enum CodeCompletionContext::Kind K) { case CodeCompletionContext::CCC_Symbol: case CodeCompletionContext::CCC_SymbolOrNewName: case CodeCompletionContext::CCC_ObjCClassForwardDecl: - case CodeCompletionContext::CCC_TopLevelOrExpression: return true; case CodeCompletionContext::CCC_OtherWithMacros: case CodeCompletionContext::CCC_DotMemberAccess: diff --git a/clang/include/clang/Frontend/ASTUnit.h b/clang/include/clang/Frontend/ASTUnit.h index c6d0d4d7e9023..b762be1c9b1d6 100644 --- a/clang/include/clang/Frontend/ASTUnit.h +++ b/clang/include/clang/Frontend/ASTUnit.h @@ -77,7 +77,6 @@ class Preprocessor; class PreprocessorOptions; class Sema; class TargetInfo; -class SyntaxOnlyAction; /// \brief Enumerates the available scopes for skipping function bodies. enum class SkipFunctionBodiesScope { None, Preamble, PreambleAndMainFile }; @@ -888,10 +887,6 @@ class ASTUnit { /// \param IncludeBriefComments Whether to include brief documentation within /// the set of code completions returned. /// - /// \param Act If supplied, this argument is used to parse the input file, - /// allowing customized parsing by overriding SyntaxOnlyAction lifecycle - /// methods. - /// /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and /// OwnedBuffers parameters are all disgusting hacks. They will go away. void CodeComplete(StringRef File, unsigned Line, unsigned Column, @@ -902,8 +897,7 @@ class ASTUnit { DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr, FileManager &FileMgr, SmallVectorImpl &StoredDiagnostics, - SmallVectorImpl &OwnedBuffers, - std::unique_ptr Act = nullptr); + SmallVectorImpl &OwnedBuffers); /// Save this translation unit to a file with the given name. /// diff --git a/clang/include/clang/Sema/CodeCompleteConsumer.h b/clang/include/clang/Sema/CodeCompleteConsumer.h index 274eaac819af1..bb4b638050383 100644 --- a/clang/include/clang/Sema/CodeCompleteConsumer.h +++ b/clang/include/clang/Sema/CodeCompleteConsumer.h @@ -336,12 +336,7 @@ class CodeCompletionContext { CCC_Recovery, /// Code completion in a @class forward declaration. - CCC_ObjCClassForwardDecl, - - /// Code completion at a top level, i.e. in a namespace or global scope, - /// but also in expression statements. This is because REPL inputs can be - /// declarations or expression statements. - CCC_TopLevelOrExpression, + CCC_ObjCClassForwardDecl }; using VisitedContextSet = llvm::SmallPtrSet; diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index e5083ddf1847f..04c2faddec5bf 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -13456,9 +13456,7 @@ class Sema final { PCC_ParenthesizedExpression, /// Code completion occurs within a sequence of declaration /// specifiers within a function, method, or block. - PCC_LocalDeclarationSpecifiers, - /// Code completion occurs at top-level in a REPL session - PCC_TopLevelOrExpression, + PCC_LocalDeclarationSpecifiers }; void CodeCompleteModuleImport(SourceLocation ImportLoc, ModuleIdPath Path); diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 98d6c08146d6c..aece72243d17e 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -2008,8 +2008,7 @@ static void CalculateHiddenNames(const CodeCompletionContext &Context, case CodeCompletionContext::CCC_SymbolOrNewName: case CodeCompletionContext::CCC_ParenthesizedExpression: case CodeCompletionContext::CCC_ObjCInterfaceName: - case CodeCompletionContext::CCC_TopLevelOrExpression: - break; + break; case CodeCompletionContext::CCC_EnumTag: case CodeCompletionContext::CCC_UnionTag: @@ -2168,8 +2167,7 @@ void ASTUnit::CodeComplete( std::shared_ptr PCHContainerOps, DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr, FileManager &FileMgr, SmallVectorImpl &StoredDiagnostics, - SmallVectorImpl &OwnedBuffers, - std::unique_ptr Act) { + SmallVectorImpl &OwnedBuffers) { if (!Invocation) return; @@ -2306,9 +2304,8 @@ void ASTUnit::CodeComplete( if (!Clang->getLangOpts().Modules) PreprocessorOpts.DetailedRecord = false; - if (!Act) - Act.reset(new SyntaxOnlyAction); - + std::unique_ptr Act; + Act.reset(new SyntaxOnlyAction); if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { if (llvm::Error Err = Act->Execute()) { consumeError(std::move(Err)); // FIXME this drops errors on the floor. diff --git a/clang/lib/Interpreter/CMakeLists.txt b/clang/lib/Interpreter/CMakeLists.txt index 79d2cba67f54c..d3781fef1bd3d 100644 --- a/clang/lib/Interpreter/CMakeLists.txt +++ b/clang/lib/Interpreter/CMakeLists.txt @@ -13,7 +13,6 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangInterpreter DeviceOffload.cpp - CodeCompletion.cpp IncrementalExecutor.cpp IncrementalParser.cpp Interpreter.cpp diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp index 370bcbfee8b01..9e5cf358700b1 100644 --- a/clang/lib/Interpreter/IncrementalParser.cpp +++ b/clang/lib/Interpreter/IncrementalParser.cpp @@ -11,7 +11,6 @@ //===----------------------------------------------------------------------===// #include "IncrementalParser.h" - #include "clang/AST/DeclContextInternals.h" #include "clang/CodeGen/BackendUtil.h" #include "clang/CodeGen/CodeGenAction.h" @@ -158,11 +157,16 @@ class IncrementalAction : public WrapperFrontendAction { TranslationUnitKind getTranslationUnitKind() override { return TU_Incremental; } - void ExecuteAction() override { CompilerInstance &CI = getCompilerInstance(); assert(CI.hasPreprocessor() && "No PP!"); + // FIXME: Move the truncation aspect of this into Sema, we delayed this till + // here so the source manager would be initialized. + if (hasCodeCompletionSupport() && + !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) + CI.createCodeCompletionConsumer(); + // Use a code completion consumer? CodeCompleteConsumer *CompletionConsumer = nullptr; if (CI.hasCodeCompletionConsumer()) @@ -394,4 +398,5 @@ llvm::StringRef IncrementalParser::GetMangledName(GlobalDecl GD) const { assert(CG); return CG->GetMangledName(GD); } + } // end namespace clang diff --git a/clang/lib/Interpreter/IncrementalParser.h b/clang/lib/Interpreter/IncrementalParser.h index e13b74c7f6594..def5750d16675 100644 --- a/clang/lib/Interpreter/IncrementalParser.h +++ b/clang/lib/Interpreter/IncrementalParser.h @@ -13,9 +13,9 @@ #ifndef LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H #define LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H -#include "clang/AST/GlobalDecl.h" #include "clang/Interpreter/PartialTranslationUnit.h" +#include "clang/AST/GlobalDecl.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" @@ -24,7 +24,7 @@ #include namespace llvm { class LLVMContext; -} // namespace llvm +} namespace clang { class ASTConsumer; diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp index 7968c62cbd3e7..4e1045298537b 100644 --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -11,11 +11,13 @@ // //===----------------------------------------------------------------------===// +#include "clang/Interpreter/Interpreter.h" + #include "DeviceOffload.h" #include "IncrementalExecutor.h" #include "IncrementalParser.h" -#include "InterpreterUtils.h" +#include "InterpreterUtils.h" #include "clang/AST/ASTContext.h" #include "clang/AST/Mangle.h" #include "clang/AST/TypeVisitor.h" @@ -31,7 +33,6 @@ #include "clang/Driver/Tool.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/TextDiagnosticBuffer.h" -#include "clang/Interpreter/Interpreter.h" #include "clang/Interpreter/Value.h" #include "clang/Lex/PreprocessorOptions.h" #include "clang/Sema/Lookup.h" @@ -126,6 +127,7 @@ CreateCI(const llvm::opt::ArgStringList &Argv) { Clang->getFrontendOpts().DisableFree = false; Clang->getCodeGenOpts().DisableFree = false; + return std::move(Clang); } @@ -274,7 +276,6 @@ Interpreter::create(std::unique_ptr CI) { std::unique_ptr(new Interpreter(std::move(CI), Err)); if (Err) return std::move(Err); - auto PTU = Interp->Parse(Runtimes); if (!PTU) return PTU.takeError(); diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp index d4cc04848c4c3..cd7c5dcf275c0 100644 --- a/clang/lib/Parse/ParseDecl.cpp +++ b/clang/lib/Parse/ParseDecl.cpp @@ -18,7 +18,6 @@ #include "clang/Basic/Attributes.h" #include "clang/Basic/CharInfo.h" #include "clang/Basic/TargetInfo.h" -#include "clang/Basic/TokenKinds.h" #include "clang/Parse/ParseDiagnostic.h" #include "clang/Parse/Parser.h" #include "clang/Parse/RAIIObjectsForParser.h" diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp index 09215b8303ecf..9a8c9e3497de9 100644 --- a/clang/lib/Parse/Parser.cpp +++ b/clang/lib/Parse/Parser.cpp @@ -923,16 +923,9 @@ Parser::ParseExternalDeclaration(ParsedAttributes &Attrs, /*IsInstanceMethod=*/std::nullopt, /*ReturnType=*/nullptr); } - - Sema::ParserCompletionContext PCC; - if (CurParsedObjCImpl) { - PCC = Sema::PCC_ObjCImplementation; - } else if (PP.isIncrementalProcessingEnabled()) { - PCC = Sema::PCC_TopLevelOrExpression; - } else { - PCC = Sema::PCC_Namespace; - }; - Actions.CodeCompleteOrdinaryName(getCurScope(), PCC); + Actions.CodeCompleteOrdinaryName( + getCurScope(), + CurParsedObjCImpl ? Sema::PCC_ObjCImplementation : Sema::PCC_Namespace); return nullptr; case tok::kw_import: { Sema::ModuleImportState IS = Sema::ModuleImportState::NotACXX20Module; diff --git a/clang/lib/Sema/CodeCompleteConsumer.cpp b/clang/lib/Sema/CodeCompleteConsumer.cpp index 9caa1a8431e90..202417798712a 100644 --- a/clang/lib/Sema/CodeCompleteConsumer.cpp +++ b/clang/lib/Sema/CodeCompleteConsumer.cpp @@ -51,7 +51,6 @@ bool CodeCompletionContext::wantConstructorResults() const { case CCC_ParenthesizedExpression: case CCC_Symbol: case CCC_SymbolOrNewName: - case CCC_TopLevelOrExpression: return true; case CCC_TopLevel: @@ -170,8 +169,6 @@ StringRef clang::getCompletionKindString(CodeCompletionContext::Kind Kind) { return "Recovery"; case CCKind::CCC_ObjCClassForwardDecl: return "ObjCClassForwardDecl"; - case CCKind::CCC_TopLevelOrExpression: - return "ReplTopLevel"; } llvm_unreachable("Invalid CodeCompletionContext::Kind!"); } diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp index 5440e16841ad8..2a3de571c4c42 100644 --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -225,7 +225,6 @@ class ResultBuilder { case CodeCompletionContext::CCC_ObjCMessageReceiver: case CodeCompletionContext::CCC_ParenthesizedExpression: case CodeCompletionContext::CCC_Statement: - case CodeCompletionContext::CCC_TopLevelOrExpression: case CodeCompletionContext::CCC_Recovery: if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) if (Method->isInstanceMethod()) @@ -1851,7 +1850,6 @@ static void AddFunctionSpecifiers(Sema::ParserCompletionContext CCC, case Sema::PCC_ObjCInstanceVariableList: case Sema::PCC_Expression: case Sema::PCC_Statement: - case Sema::PCC_TopLevelOrExpression: case Sema::PCC_ForInit: case Sema::PCC_Condition: case Sema::PCC_RecoveryInFunction: @@ -1909,7 +1907,6 @@ static bool WantTypesInContext(Sema::ParserCompletionContext CCC, case Sema::PCC_Type: case Sema::PCC_ParenthesizedExpression: case Sema::PCC_LocalDeclarationSpecifiers: - case Sema::PCC_TopLevelOrExpression: return true; case Sema::PCC_Expression: @@ -2222,7 +2219,6 @@ static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC, Scope *S, break; case Sema::PCC_RecoveryInFunction: - case Sema::PCC_TopLevelOrExpression: case Sema::PCC_Statement: { if (SemaRef.getLangOpts().CPlusPlus11) AddUsingAliasResult(Builder, Results); @@ -4212,8 +4208,6 @@ mapCodeCompletionContext(Sema &S, Sema::ParserCompletionContext PCC) { case Sema::PCC_LocalDeclarationSpecifiers: return CodeCompletionContext::CCC_Type; - case Sema::PCC_TopLevelOrExpression: - return CodeCompletionContext::CCC_TopLevelOrExpression; } llvm_unreachable("Invalid ParserCompletionContext!"); @@ -4354,7 +4348,6 @@ void Sema::CodeCompleteOrdinaryName(Scope *S, break; case PCC_Statement: - case PCC_TopLevelOrExpression: case PCC_ParenthesizedExpression: case PCC_Expression: case PCC_ForInit: @@ -4392,7 +4385,6 @@ void Sema::CodeCompleteOrdinaryName(Scope *S, case PCC_ParenthesizedExpression: case PCC_Expression: case PCC_Statement: - case PCC_TopLevelOrExpression: case PCC_RecoveryInFunction: if (S->getFnParent()) AddPrettyFunctionResults(getLangOpts(), Results); diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp index 51741fd1a27ef..535866b8495ef 100644 --- a/clang/tools/clang-repl/ClangRepl.cpp +++ b/clang/tools/clang-repl/ClangRepl.cpp @@ -13,7 +13,6 @@ #include "clang/Basic/Diagnostic.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendDiagnostic.h" -#include "clang/Interpreter/CodeCompletion.h" #include "clang/Interpreter/Interpreter.h" #include "llvm/ExecutionEngine/Orc/LLJIT.h" @@ -71,70 +70,6 @@ static int checkDiagErrors(const clang::CompilerInstance *CI, bool HasError) { return (Errs || HasError) ? EXIT_FAILURE : EXIT_SUCCESS; } -struct ReplListCompleter { - clang::IncrementalCompilerBuilder &CB; - clang::Interpreter &MainInterp; - ReplListCompleter(clang::IncrementalCompilerBuilder &CB, - clang::Interpreter &Interp) - : CB(CB), MainInterp(Interp){}; - - std::vector operator()(llvm::StringRef Buffer, - size_t Pos) const; - std::vector - operator()(llvm::StringRef Buffer, size_t Pos, llvm::Error &ErrRes) const; -}; - -std::vector -ReplListCompleter::operator()(llvm::StringRef Buffer, size_t Pos) const { - auto Err = llvm::Error::success(); - auto res = (*this)(Buffer, Pos, Err); - if (Err) - llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: "); - return res; -} - -std::vector -ReplListCompleter::operator()(llvm::StringRef Buffer, size_t Pos, - llvm::Error &ErrRes) const { - std::vector Comps; - std::vector Results; - - auto CI = CB.CreateCpp(); - if (auto Err = CI.takeError()) { - ErrRes = std::move(Err); - return {}; - } - - size_t Lines = - std::count(Buffer.begin(), std::next(Buffer.begin(), Pos), '\n') + 1; - auto Interp = clang::Interpreter::create(std::move(*CI)); - - if (auto Err = Interp.takeError()) { - // log the error and returns an empty vector; - ErrRes = std::move(Err); - - return {}; - } - - codeComplete( - const_cast((*Interp)->getCompilerInstance()), - Buffer, Lines, Pos + 1, MainInterp.getCompilerInstance(), Results); - - size_t space_pos = Buffer.rfind(" "); - llvm::StringRef Prefix; - if (space_pos == llvm::StringRef::npos) { - Prefix = Buffer; - } else { - Prefix = Buffer.substr(space_pos + 1); - } - - for (auto c : Results) { - if (c.find(Prefix) == 0) - Comps.push_back(llvm::LineEditor::Completion(c.substr(Prefix.size()), c)); - } - return Comps; -} - llvm::ExitOnError ExitOnErr; int main(int argc, const char **argv) { ExitOnErr.setBanner("clang-repl: "); @@ -200,7 +135,6 @@ int main(int argc, const char **argv) { DeviceCI->LoadRequestedPlugins(); std::unique_ptr Interp; - if (CudaEnabled) { Interp = ExitOnErr( clang::Interpreter::createWithCUDA(std::move(CI), std::move(DeviceCI))); @@ -223,8 +157,8 @@ int main(int argc, const char **argv) { if (OptInputs.empty()) { llvm::LineEditor LE("clang-repl"); + // FIXME: Add LE.setListCompleter std::string Input; - LE.setListCompleter(ReplListCompleter(CB, *Interp)); while (std::optional Line = LE.readLine()) { llvm::StringRef L = *Line; L = L.trim(); @@ -236,10 +170,10 @@ int main(int argc, const char **argv) { } Input += L; + if (Input == R"(%quit)") { break; - } - if (Input == R"(%undo)") { + } else if (Input == R"(%undo)") { if (auto Err = Interp->Undo()) { llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: "); HasError = true; diff --git a/clang/tools/libclang/CIndexCodeCompletion.cpp b/clang/tools/libclang/CIndexCodeCompletion.cpp index 196c64e617227..01bad3d625912 100644 --- a/clang/tools/libclang/CIndexCodeCompletion.cpp +++ b/clang/tools/libclang/CIndexCodeCompletion.cpp @@ -11,8 +11,8 @@ // //===----------------------------------------------------------------------===// -#include "CIndexDiagnostic.h" #include "CIndexer.h" +#include "CIndexDiagnostic.h" #include "CLog.h" #include "CXCursor.h" #include "CXSourceLocation.h" @@ -25,7 +25,6 @@ #include "clang/Basic/SourceManager.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/CompilerInstance.h" -#include "clang/Frontend/FrontendActions.h" #include "clang/Sema/CodeCompleteConsumer.h" #include "clang/Sema/Sema.h" #include "llvm/ADT/SmallString.h" @@ -42,6 +41,7 @@ #include #include + #ifdef UDP_CODE_COMPLETION_LOGGER #include "clang/Basic/Version.h" #include @@ -543,7 +543,6 @@ static unsigned long long getContextsForContextKind( case CodeCompletionContext::CCC_PreprocessorExpression: case CodeCompletionContext::CCC_PreprocessorDirective: case CodeCompletionContext::CCC_Attribute: - case CodeCompletionContext::CCC_TopLevelOrExpression: 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) diff --git a/clang/unittests/Interpreter/CMakeLists.txt b/clang/unittests/Interpreter/CMakeLists.txt index 712641afb976d..698494b9897f5 100644 --- a/clang/unittests/Interpreter/CMakeLists.txt +++ b/clang/unittests/Interpreter/CMakeLists.txt @@ -9,7 +9,6 @@ set(LLVM_LINK_COMPONENTS add_clang_unittest(ClangReplInterpreterTests IncrementalProcessingTest.cpp InterpreterTest.cpp - CodeCompletionTest.cpp ) target_link_libraries(ClangReplInterpreterTests PUBLIC clangAST