Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clang/docs/ClangRepl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ Comments:
clang-repl> // Comments in Clang-Repl
clang-repl> /* Comments in Clang-Repl */

Help:
=====

.. code-block:: text

clang-repl>%help

Undo:
=====

.. code-block:: text

clang-repl>%undo


Closure or Termination:
=======================
Expand Down
15 changes: 14 additions & 1 deletion clang/tools/clang-repl/ClangRepl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "llvm/Support/ManagedStatic.h" // llvm_shutdown
#include "llvm/Support/Signals.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Host.h"
#include <optional>

Expand Down Expand Up @@ -185,7 +186,7 @@ struct ReplListCompleter {
clang::Interpreter &MainInterp;
ReplListCompleter(clang::IncrementalCompilerBuilder &CB,
clang::Interpreter &Interp)
: CB(CB), MainInterp(Interp){};
: CB(CB), MainInterp(Interp) {};

std::vector<llvm::LineEditor::Completion> operator()(llvm::StringRef Buffer,
size_t Pos) const;
Expand Down Expand Up @@ -347,7 +348,15 @@ int main(int argc, const char **argv) {
}
}

// if we add more % commands, there should be better architecture than this
const char *help_output = "%help\tlist clang-repl %commands\n"
"%undo\tundo the previous input\n"
"%quit\texit clang-repl\n";
const char *help_prompt = "type %help to list clang-repl commands\n";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mention that printing this causes a bunch of tests to fail, which is to be expected but probably a sign we should leave this feature out of this PR. It may be something the maintainer would rather not have, and fixing all the tests in this PR will add a lot of things for us to review.

What you've done is good though, that's what I was thinking of.

I didn't mention it before but gdb does this:

$ gdb
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1
<...>
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) q
$ gdb --quiet
(gdb) q

Also clang-repl may want to detect if it's outputting to an interactive terminal or not. Which I did not think of before.

(GDB does not, it expects you to use --quiet)

So yeah, leave this bit out so we don't distract from the rest, maybe come back to it in future.


llvm::raw_ostream &OS = llvm::outs();
if (OptInputs.empty()) {
OS << help_prompt;
llvm::LineEditor LE("clang-repl");
std::string Input;
LE.setListCompleter(ReplListCompleter(CB, *Interp));
Expand All @@ -370,6 +379,10 @@ int main(int argc, const char **argv) {
if (Input == R"(%undo)") {
if (auto Err = Interp->Undo())
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
} else if (Input == R"(%help)") {
OS << help_output << '\n';
} else if (Input[0] == '%') { // make sure this is evaluated last
OS << "Invalid % command: \"" << Input << "\". " << help_prompt;
} else if (Input.rfind("%lib ", 0) == 0) {
if (auto Err = Interp->LoadDynamicLibrary(Input.data() + 5))
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
Expand Down
Loading