[PATCH] D158629: [ClangRepl] support code completion at the repl

Fred Fu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 23 08:37:05 PDT 2023


capfredf created this revision.
Herald added subscribers: ChuanqiXu, kadircet, arphaman.
Herald added a project: All.
capfredf requested review of this revision.
Herald added projects: clang, clang-tools-extra.
Herald added a subscriber: cfe-commits.

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.
with the first component serving 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.

Previous Differential Revision: https://reviews.llvm.org/D154382


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158629

Files:
  clang-tools-extra/clangd/CodeComplete.cpp
  clang/include/clang/Frontend/ASTUnit.h
  clang/include/clang/Interpreter/CodeCompletion.h
  clang/include/clang/Sema/CodeCompleteConsumer.h
  clang/include/clang/Sema/Sema.h
  clang/lib/Frontend/ASTUnit.cpp
  clang/lib/Interpreter/CMakeLists.txt
  clang/lib/Interpreter/CodeCompletion.cpp
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/lib/Interpreter/IncrementalParser.h
  clang/lib/Interpreter/Interpreter.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/Parser.cpp
  clang/lib/Sema/CodeCompleteConsumer.cpp
  clang/lib/Sema/SemaCodeComplete.cpp
  clang/test/CodeCompletion/incrememal-mode-completion-no-error.cpp
  clang/test/CodeCompletion/incremental-top-level.cpp
  clang/tools/clang-repl/ClangRepl.cpp
  clang/tools/libclang/CIndexCodeCompletion.cpp
  clang/unittests/Interpreter/CMakeLists.txt
  clang/unittests/Interpreter/CodeCompletionTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158629.552733.patch
Type: text/x-patch
Size: 32125 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230823/a6d38845/attachment-0001.bin>


More information about the cfe-commits mailing list