[llvm] ec05b88 - [lineeditor] Add `setHistorySize()` method for adjusting history size (#110092)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 7 07:18:10 PST 2024


Author: Devajith
Date: 2024-11-07T17:18:07+02:00
New Revision: ec05b883653ba1bb9e92399f78b99a9d9342efc0

URL: https://github.com/llvm/llvm-project/commit/ec05b883653ba1bb9e92399f78b99a9d9342efc0
DIFF: https://github.com/llvm/llvm-project/commit/ec05b883653ba1bb9e92399f78b99a9d9342efc0.diff

LOG: [lineeditor] Add `setHistorySize()` method for adjusting history size (#110092)

This patch adds a `setHistorySize` method to `LineEditor`.

This is particularly useful for tools like `clang-repl`, `clang-query`,
`mlir-query`, and other REPL interfaces, where managing history size
might be needed.

Added: 
    

Modified: 
    llvm/include/llvm/LineEditor/LineEditor.h
    llvm/lib/LineEditor/LineEditor.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/LineEditor/LineEditor.h b/llvm/include/llvm/LineEditor/LineEditor.h
index 29fdf9f85c6fa5..ca0312bf6297f5 100644
--- a/llvm/include/llvm/LineEditor/LineEditor.h
+++ b/llvm/include/llvm/LineEditor/LineEditor.h
@@ -41,6 +41,7 @@ class LineEditor {
 
   void saveHistory();
   void loadHistory();
+  void setHistorySize(int size);
 
   static std::string getDefaultHistoryPath(StringRef ProgName);
 

diff  --git a/llvm/lib/LineEditor/LineEditor.cpp b/llvm/lib/LineEditor/LineEditor.cpp
index d0d138bb1f9d8d..8f667b0238df8d 100644
--- a/llvm/lib/LineEditor/LineEditor.cpp
+++ b/llvm/lib/LineEditor/LineEditor.cpp
@@ -20,6 +20,7 @@
 #endif
 
 using namespace llvm;
+constexpr int DefaultHistorySize = 800;
 
 std::string LineEditor::getDefaultHistoryPath(StringRef ProgName) {
   SmallString<32> Path;
@@ -220,8 +221,8 @@ LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In,
            NULL); // Fix the delete key.
   ::el_set(Data->EL, EL_CLIENTDATA, Data.get());
 
+  setHistorySize(DefaultHistorySize);
   HistEvent HE;
-  ::history(Data->Hist, &HE, H_SETSIZE, 800);
   ::history(Data->Hist, &HE, H_SETUNIQUE, 1);
   loadHistory();
 }
@@ -248,6 +249,11 @@ void LineEditor::loadHistory() {
   }
 }
 
+void LineEditor::setHistorySize(int size) {
+  HistEvent HE;
+  ::history(Data->Hist, &HE, H_SETSIZE, size);
+}
+
 std::optional<std::string> LineEditor::readLine() const {
   // Call el_gets to prompt the user and read the user's input.
   int LineLen = 0;
@@ -291,6 +297,7 @@ LineEditor::~LineEditor() {
 
 void LineEditor::saveHistory() {}
 void LineEditor::loadHistory() {}
+void LineEditor::setHistorySize(int size) {}
 
 std::optional<std::string> LineEditor::readLine() const {
   ::fprintf(Data->Out, "%s", Prompt.c_str());


        


More information about the llvm-commits mailing list