]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Interpreter/CommandHistory.cpp
Merge ^/head r316992 through r317215.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Interpreter / CommandHistory.cpp
1 //===-- CommandHistory.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include <inttypes.h>
11
12 #include "lldb/Interpreter/CommandHistory.h"
13
14 using namespace lldb;
15 using namespace lldb_private;
16
17 CommandHistory::CommandHistory() : m_mutex(), m_history() {}
18
19 CommandHistory::~CommandHistory() {}
20
21 size_t CommandHistory::GetSize() const {
22   std::lock_guard<std::recursive_mutex> guard(m_mutex);
23   return m_history.size();
24 }
25
26 bool CommandHistory::IsEmpty() const {
27   std::lock_guard<std::recursive_mutex> guard(m_mutex);
28   return m_history.empty();
29 }
30
31 llvm::Optional<llvm::StringRef>
32 CommandHistory::FindString(llvm::StringRef input_str) const {
33   std::lock_guard<std::recursive_mutex> guard(m_mutex);
34   if (input_str.size() < 2)
35     return llvm::None;
36
37   if (input_str[0] != g_repeat_char)
38     return llvm::None;
39
40   if (input_str[1] == g_repeat_char) {
41     if (m_history.empty())
42       return llvm::None;
43     return llvm::StringRef(m_history.back());
44   }
45
46   input_str = input_str.drop_front();
47
48   size_t idx = 0;
49   if (input_str.front() == '-') {
50     if (input_str.drop_front(2).getAsInteger(0, idx))
51       return llvm::None;
52     if (idx >= m_history.size())
53       return llvm::None;
54     idx = m_history.size() - idx;
55   } else {
56     if (input_str.drop_front().getAsInteger(0, idx))
57       return llvm::None;
58     if (idx >= m_history.size())
59       return llvm::None;
60   }
61
62   return llvm::StringRef(m_history[idx]);
63 }
64
65 llvm::StringRef CommandHistory::GetStringAtIndex(size_t idx) const {
66   std::lock_guard<std::recursive_mutex> guard(m_mutex);
67   if (idx < m_history.size())
68     return m_history[idx];
69   return "";
70 }
71
72 llvm::StringRef CommandHistory::operator[](size_t idx) const {
73   return GetStringAtIndex(idx);
74 }
75
76 llvm::StringRef CommandHistory::GetRecentmostString() const {
77   std::lock_guard<std::recursive_mutex> guard(m_mutex);
78   if (m_history.empty())
79     return "";
80   return m_history.back();
81 }
82
83 void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) {
84   std::lock_guard<std::recursive_mutex> guard(m_mutex);
85   if (reject_if_dupe) {
86     if (!m_history.empty()) {
87       if (str == m_history.back())
88         return;
89     }
90   }
91   m_history.push_back(str);
92 }
93
94 void CommandHistory::Clear() {
95   std::lock_guard<std::recursive_mutex> guard(m_mutex);
96   m_history.clear();
97 }
98
99 void CommandHistory::Dump(Stream &stream, size_t start_idx,
100                           size_t stop_idx) const {
101   std::lock_guard<std::recursive_mutex> guard(m_mutex);
102   stop_idx = std::min(stop_idx + 1, m_history.size());
103   for (size_t counter = start_idx; counter < stop_idx; counter++) {
104     const std::string hist_item = m_history[counter];
105     if (!hist_item.empty()) {
106       stream.Indent();
107       stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());
108     }
109   }
110 }