Skip to content

Commit

Permalink
Reland "[clang-repl] support code completion at a REPL."
Browse files Browse the repository at this point in the history
Original commit message:
"
This patch enabled code completion for ClangREPL. The feature was built upon
three existing Clang components: a list completer for LineEditor, a
CompletionConsumer from SemaCodeCompletion, and the ASTUnit::codeComplete method.
The first component serves as the main entry point of handling interactive inputs.

Because a completion point for a compiler instance has to be unchanged once it
is set, an incremental compiler instance is created for each code
completion. Such a compiler instance carries over AST context source from the
main interpreter compiler in order to obtain declarations or bindings from
previous input in the same REPL session.

The most important API codeComplete in Interpreter/CodeCompletion is a thin
wrapper that calls with ASTUnit::codeComplete with necessary arguments, such as
a code completion point and a ReplCompletionConsumer, which communicates
completion results from SemaCodeCompletion back to the list completer for the
REPL.

In addition, PCC_TopLevelOrExpression and CCC_TopLevelOrExpression` top levels
were added so that SemaCodeCompletion can treat top level statements like
expression statements at the REPL. For example,

clang-repl> int foo = 42;
clang-repl> f<tab>

From a parser's persective, the cursor is at a top level. If we used code
completion without any changes, PCC_Namespace would be supplied to
Sema::CodeCompleteOrdinaryName, and thus the completion results would not
include foo.

Currently, the way we use PCC_TopLevelOrExpression and
CCC_TopLevelOrExpression is no different from the way we use PCC_Statement
and CCC_Statement respectively.

Differential revision: https://reviews.llvm.org/D154382
"

The new patch also fixes clangd and several memory issues that the bots reported.
  • Loading branch information
capfredf authored and vgvassilev committed Aug 28, 2023
1 parent f0e2a5e commit 5ab25a4
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 28 deletions.
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/CodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ 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:
Expand Down
8 changes: 7 additions & 1 deletion clang/include/clang/Frontend/ASTUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ 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 };
Expand Down Expand Up @@ -887,6 +888,10 @@ 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,
Expand All @@ -897,7 +902,8 @@ class ASTUnit {
DiagnosticsEngine &Diag, LangOptions &LangOpts,
SourceManager &SourceMgr, FileManager &FileMgr,
SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers);
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
std::unique_ptr<SyntaxOnlyAction> Act = nullptr);

/// Save this translation unit to a file with the given name.
///
Expand Down
7 changes: 6 additions & 1 deletion clang/include/clang/Sema/CodeCompleteConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,12 @@ class CodeCompletionContext {
CCC_Recovery,

/// Code completion in a @class forward declaration.
CCC_ObjCClassForwardDecl
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,
};

using VisitedContextSet = llvm::SmallPtrSet<DeclContext *, 8>;
Expand Down
4 changes: 3 additions & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -13456,7 +13456,9 @@ class Sema final {
PCC_ParenthesizedExpression,
/// Code completion occurs within a sequence of declaration
/// specifiers within a function, method, or block.
PCC_LocalDeclarationSpecifiers
PCC_LocalDeclarationSpecifiers,
/// Code completion occurs at top-level in a REPL session
PCC_TopLevelOrExpression,
};

void CodeCompleteModuleImport(SourceLocation ImportLoc, ModuleIdPath Path);
Expand Down
11 changes: 7 additions & 4 deletions clang/lib/Frontend/ASTUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2008,7 +2008,8 @@ static void CalculateHiddenNames(const CodeCompletionContext &Context,
case CodeCompletionContext::CCC_SymbolOrNewName:
case CodeCompletionContext::CCC_ParenthesizedExpression:
case CodeCompletionContext::CCC_ObjCInterfaceName:
break;
case CodeCompletionContext::CCC_TopLevelOrExpression:
break;

case CodeCompletionContext::CCC_EnumTag:
case CodeCompletionContext::CCC_UnionTag:
Expand Down Expand Up @@ -2167,7 +2168,8 @@ void ASTUnit::CodeComplete(
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr,
FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics,
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) {
SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers,
std::unique_ptr<SyntaxOnlyAction> Act) {
if (!Invocation)
return;

Expand Down Expand Up @@ -2304,8 +2306,9 @@ void ASTUnit::CodeComplete(
if (!Clang->getLangOpts().Modules)
PreprocessorOpts.DetailedRecord = false;

std::unique_ptr<SyntaxOnlyAction> Act;
Act.reset(new SyntaxOnlyAction);
if (!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.
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(LLVM_LINK_COMPONENTS

add_clang_library(clangInterpreter
DeviceOffload.cpp
CodeCompletion.cpp
IncrementalExecutor.cpp
IncrementalParser.cpp
Interpreter.cpp
Expand Down
9 changes: 2 additions & 7 deletions clang/lib/Interpreter/IncrementalParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "IncrementalParser.h"

#include "clang/AST/DeclContextInternals.h"
#include "clang/CodeGen/BackendUtil.h"
#include "clang/CodeGen/CodeGenAction.h"
Expand Down Expand Up @@ -157,16 +158,11 @@ 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())
Expand Down Expand Up @@ -398,5 +394,4 @@ llvm::StringRef IncrementalParser::GetMangledName(GlobalDecl GD) const {
assert(CG);
return CG->GetMangledName(GD);
}

} // end namespace clang
4 changes: 2 additions & 2 deletions clang/lib/Interpreter/IncrementalParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -24,7 +24,7 @@
#include <memory>
namespace llvm {
class LLVMContext;
}
} // namespace llvm

namespace clang {
class ASTConsumer;
Expand Down
7 changes: 3 additions & 4 deletions clang/lib/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
//
//===----------------------------------------------------------------------===//

#include "clang/Interpreter/Interpreter.h"

#include "DeviceOffload.h"
#include "IncrementalExecutor.h"
#include "IncrementalParser.h"

#include "InterpreterUtils.h"

#include "clang/AST/ASTContext.h"
#include "clang/AST/Mangle.h"
#include "clang/AST/TypeVisitor.h"
Expand All @@ -33,6 +31,7 @@
#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"
Expand Down Expand Up @@ -127,7 +126,6 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {

Clang->getFrontendOpts().DisableFree = false;
Clang->getCodeGenOpts().DisableFree = false;

return std::move(Clang);
}

Expand Down Expand Up @@ -276,6 +274,7 @@ Interpreter::create(std::unique_ptr<CompilerInstance> CI) {
std::unique_ptr<Interpreter>(new Interpreter(std::move(CI), Err));
if (Err)
return std::move(Err);

auto PTU = Interp->Parse(Runtimes);
if (!PTU)
return PTU.takeError();
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#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"
Expand Down
13 changes: 10 additions & 3 deletions clang/lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,9 +923,16 @@ Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
/*IsInstanceMethod=*/std::nullopt,
/*ReturnType=*/nullptr);
}
Actions.CodeCompleteOrdinaryName(
getCurScope(),
CurParsedObjCImpl ? Sema::PCC_ObjCImplementation : Sema::PCC_Namespace);

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);
return nullptr;
case tok::kw_import: {
Sema::ModuleImportState IS = Sema::ModuleImportState::NotACXX20Module;
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/CodeCompleteConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ bool CodeCompletionContext::wantConstructorResults() const {
case CCC_ParenthesizedExpression:
case CCC_Symbol:
case CCC_SymbolOrNewName:
case CCC_TopLevelOrExpression:
return true;

case CCC_TopLevel:
Expand Down Expand Up @@ -169,6 +170,8 @@ 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!");
}
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Sema/SemaCodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ 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())
Expand Down Expand Up @@ -1850,6 +1851,7 @@ 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:
Expand Down Expand Up @@ -1907,6 +1909,7 @@ 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:
Expand Down Expand Up @@ -2219,6 +2222,7 @@ 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);
Expand Down Expand Up @@ -4208,6 +4212,8 @@ 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!");
Expand Down Expand Up @@ -4348,6 +4354,7 @@ void Sema::CodeCompleteOrdinaryName(Scope *S,
break;

case PCC_Statement:
case PCC_TopLevelOrExpression:
case PCC_ParenthesizedExpression:
case PCC_Expression:
case PCC_ForInit:
Expand Down Expand Up @@ -4385,6 +4392,7 @@ 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);
Expand Down

0 comments on commit 5ab25a4

Please sign in to comment.