[Lldb-commits] [lldb] r371922 - [lldb] Code cleanup: FormattersContainer.h: Use range-based for loops.

2019-09-14 Thread Jan Kratochvil via lldb-commits
Author: jankratochvil
Date: Sat Sep 14 08:46:51 2019
New Revision: 371922

URL: http://llvm.org/viewvc/llvm-project?rev=371922&view=rev
Log:
[lldb] Code cleanup: FormattersContainer.h: Use range-based for loops.

Suggested for an other loop by Pavel Labath in:
https://reviews.llvm.org/D66654#inline-605808

Modified:
lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h

Modified: lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h?rev=371922&r1=371921&r2=371922&view=diff
==
--- lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h (original)
+++ lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h Sat Sep 14 
08:46:51 2019
@@ -114,10 +114,9 @@ public:
   void ForEach(ForEachCallback callback) {
 if (callback) {
   std::lock_guard guard(m_map_mutex);
-  MapIterator pos, end = m_map.end();
-  for (pos = m_map.begin(); pos != end; pos++) {
-const KeyType &type = pos->first;
-if (!callback(type, pos->second))
+  for (const auto &pos : m_map) {
+const KeyType &type = pos.first;
+if (!callback(type, pos.second))
   break;
   }
 }
@@ -295,11 +294,10 @@ protected:
 RegularExpression *dummy) {
 llvm::StringRef key_str = key.GetStringRef();
 std::lock_guard guard(m_format_map.mutex());
-MapIterator pos, end = m_format_map.map().end();
-for (pos = m_format_map.map().begin(); pos != end; pos++) {
-  const RegularExpression ®ex = pos->first;
+for (const auto &pos : m_format_map.map()) {
+  const RegularExpression ®ex = pos.first;
   if (regex.Execute(key_str)) {
-value = pos->second;
+value = pos.second;
 return true;
   }
 }
@@ -309,11 +307,10 @@ protected:
   bool GetExact_Impl(ConstString key, MapValueType &value,
  RegularExpression *dummy) {
 std::lock_guard guard(m_format_map.mutex());
-MapIterator pos, end = m_format_map.map().end();
-for (pos = m_format_map.map().begin(); pos != end; pos++) {
-  const RegularExpression ®ex = pos->first;
+for (const auto &pos : m_format_map.map()) {
+  const RegularExpression ®ex = pos.first;
   if (regex.GetText() == key.GetStringRef()) {
-value = pos->second;
+value = pos.second;
 return true;
   }
 }


___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D66654: 2/2: Process formatters in reverse-chronological order

2019-09-14 Thread Jan Kratochvil via Phabricator via lldb-commits
jankratochvil marked 9 inline comments as done.
jankratochvil added inline comments.



Comment at: lldb/include/lldb/DataFormatters/FormattersContainer.h:83
+Delete(name);
+m_map.push_back(std::make_pair(std::move(name), std::move(entry)));
 if (listener)

labath wrote:
> m_map.emplace_back, maybe?
Yes, not sure why I forgot here.



Comment at: lldb/include/lldb/DataFormatters/FormattersContainer.h:109
 std::lock_guard guard(m_map_mutex);
-MapIterator iter = m_map.find(name);
-if (iter == m_map.end())
-  return false;
-entry = iter->second;
-return true;
+for (MapIterator iter = m_map.begin(); iter != m_map.end(); ++iter)
+  if (iter->first == name) {

labath wrote:
> range-based for
OK, other existing loops updated in: rL371922



Comment at: lldb/include/lldb/DataFormatters/FormattersContainer.h:168
+  typedef KeyType MapKeyType;
+  typedef std::shared_ptr MapValueType;
   typedef typename BackEndType::ForEachCallback ForEachCallback;

labath wrote:
> Is this still needed, or is that a leftover from the MapVector attempt?
Still needed.



Comment at: lldb/include/lldb/DataFormatters/FormattersContainer.h:291-292
+// Patterns are matched in reverse-chronological order.
+MapReverseIterator pos, end = m_format_map.map().rend();
+for (pos = m_format_map.map().rbegin(); pos != end; pos++) {
   const RegularExpression ®ex = pos->first;

labath wrote:
> consider: `for(const auto &entry: llvm::reverse(m_format_map))`
Nice, thanks, used.


Repository:
  rLLDB LLDB

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66654/new/

https://reviews.llvm.org/D66654



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D66654: 2/2: Process formatters in reverse-chronological order

2019-09-14 Thread Jan Kratochvil via Phabricator via lldb-commits
jankratochvil updated this revision to Diff 220219.
jankratochvil marked 4 inline comments as done.

Repository:
  rLLDB LLDB

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66654/new/

https://reviews.llvm.org/D66654

Files:
  lldb/include/lldb/DataFormatters/FormattersContainer.h
  lldb/include/lldb/Utility/RegularExpression.h
  
lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-advanced/TestDataFormatterAdv.py

Index: lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-advanced/TestDataFormatterAdv.py
===
--- lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-advanced/TestDataFormatterAdv.py
+++ lldb/packages/Python/lldbsuite/test/functionalities/data-formatter/data-formatter-advanced/TestDataFormatterAdv.py
@@ -136,6 +136,13 @@
 self.expect("frame variable int_array",
 substrs=['1,2'])
 
+# Test the patterns are matched in reverse-chronological order.
+self.runCmd(
+'type summary add --summary-string \"${var[2-3]}\" "int []"')
+
+self.expect("frame variable int_array",
+substrs=['3,4'])
+
 self.runCmd("type summary clear")
 
 self.runCmd("type summary add -c -x \"i_am_cool \[[0-9]\]\"")
Index: lldb/include/lldb/Utility/RegularExpression.h
===
--- lldb/include/lldb/Utility/RegularExpression.h
+++ lldb/include/lldb/Utility/RegularExpression.h
@@ -78,10 +78,6 @@
   /// otherwise.
   llvm::Error GetError() const;
 
-  bool operator<(const RegularExpression &rhs) const {
-return GetText() < rhs.GetText();
-  }
-
   bool operator==(const RegularExpression &rhs) const {
 return GetText() == rhs.GetText();
   }
Index: lldb/include/lldb/DataFormatters/FormattersContainer.h
===
--- lldb/include/lldb/DataFormatters/FormattersContainer.h
+++ lldb/include/lldb/DataFormatters/FormattersContainer.h
@@ -65,7 +65,7 @@
 template  class FormatMap {
 public:
   typedef typename ValueType::SharedPointer ValueSP;
-  typedef std::map MapType;
+  typedef std::vector> MapType;
   typedef typename MapType::iterator MapIterator;
   typedef std::function ForEachCallback;
 
@@ -79,20 +79,22 @@
   entry->GetRevision() = 0;
 
 std::lock_guard guard(m_map_mutex);
-m_map[std::move(name)] = entry;
+Delete(name);
+m_map.emplace_back(std::move(name), std::move(entry));
 if (listener)
   listener->Changed();
   }
 
   bool Delete(const KeyType &name) {
 std::lock_guard guard(m_map_mutex);
-MapIterator iter = m_map.find(name);
-if (iter == m_map.end())
-  return false;
-m_map.erase(iter);
-if (listener)
-  listener->Changed();
-return true;
+for (MapIterator iter = m_map.begin(); iter != m_map.end(); ++iter)
+  if (iter->first == name) {
+m_map.erase(iter);
+if (listener)
+  listener->Changed();
+return true;
+  }
+return false;
   }
 
   void Clear() {
@@ -104,11 +106,12 @@
 
   bool Get(const KeyType &name, ValueSP &entry) {
 std::lock_guard guard(m_map_mutex);
-MapIterator iter = m_map.find(name);
-if (iter == m_map.end())
-  return false;
-entry = iter->second;
-return true;
+for (const auto &pos : m_map)
+  if (pos.first == name) {
+entry = pos.second;
+return true;
+  }
+return false;
   }
 
   void ForEach(ForEachCallback callback) {
@@ -126,29 +129,17 @@
 
   ValueSP GetValueAtIndex(size_t index) {
 std::lock_guard guard(m_map_mutex);
-MapIterator iter = m_map.begin();
-MapIterator end = m_map.end();
-while (index > 0) {
-  iter++;
-  index--;
-  if (end == iter)
-return ValueSP();
-}
-return iter->second;
+if (index >= m_map.size())
+  return ValueSP();
+return m_map[index].second;
   }
 
   // If caller holds the mutex we could return a reference without copy ctor.
   KeyType GetKeyAtIndex(size_t index) {
 std::lock_guard guard(m_map_mutex);
-MapIterator iter = m_map.begin();
-MapIterator end = m_map.end();
-while (index > 0) {
-  iter++;
-  index--;
-  if (end == iter)
-return KeyType();
-}
-return iter->first;
+if (index >= m_map.size())
+  return {};
+return m_map[index].first;
   }
 
 protected:
@@ -171,8 +162,8 @@
 public:
   typedef typename BackEndType::MapType MapType;
   typedef typename MapType::iterator MapIterator;
-  typedef typename MapType::key_type MapKeyType;
-  typedef typename MapType::mapped_type MapValueType;
+  typedef KeyType MapKeyType;
+  typedef std::shared_ptr MapValueType;
   typedef typename BackEndType::ForEachCallback ForEachCallback;
   typedef typename std::shared_ptr>
   SharedPointer;
@@ -294,7 +285,8 @@
 Reg

[Lldb-commits] [PATCH] D67589: Fix crash on SBCommandReturnObject & assignment

2019-09-14 Thread Jan Kratochvil via Phabricator via lldb-commits
jankratochvil created this revision.
jankratochvil added reviewers: labath, JDevlieghere.
jankratochvil added a project: LLDB.
Herald added a subscriber: abidh.

I was writing an SB API client and it was crashing on:

  bool DoExecute(SBDebugger dbg, char **command, SBCommandReturnObject &result) 
{
result = subcommand(dbg, "help");

That is because `SBCommandReturnObject &result` gets initialized inside LLDB by:

  bool DoExecute(Args &command, CommandReturnObject &result) override {
// std::unique_ptr gets initialized here from `&result`!!!
SBCommandReturnObject sb_return(&result);
DoExecute(...);
sb_return.Release();

This is somehow a most simple fix. I do not like it much.
I think there could be two implementations for superclass 
`SBCommandReturnObject` for internal storage vs. external storage but that 
looked overcomplicated to me.
(Also sure there is missing a move-assignment operator.)


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D67589

Files:
  lldb/include/lldb/API/SBCommandReturnObject.h
  lldb/packages/Python/lldbsuite/test/api/command-return-object/Makefile
  
lldb/packages/Python/lldbsuite/test/api/command-return-object/TestSBCommandReturnObject.py
  lldb/packages/Python/lldbsuite/test/api/command-return-object/main.cpp
  lldb/scripts/Python/python-wrapper.swig
  lldb/source/API/SBCommandInterpreter.cpp
  lldb/source/API/SBCommandReturnObject.cpp

Index: lldb/source/API/SBCommandReturnObject.cpp
===
--- lldb/source/API/SBCommandReturnObject.cpp
+++ lldb/source/API/SBCommandReturnObject.cpp
@@ -18,6 +18,11 @@
 using namespace lldb;
 using namespace lldb_private;
 
+lldb_private::CommandReturnObject &
+lldb_private::SBCommandReturnObject_ref(SBCommandReturnObject &sb_cmd) {
+  return sb_cmd.ref();
+}
+
 SBCommandReturnObject::SBCommandReturnObject()
 : m_opaque_up(new CommandReturnObject()) {
   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCommandReturnObject);
@@ -31,21 +36,8 @@
   m_opaque_up = clone(rhs.m_opaque_up);
 }
 
-SBCommandReturnObject::SBCommandReturnObject(CommandReturnObject *ptr)
-: m_opaque_up(ptr) {
-  LLDB_RECORD_CONSTRUCTOR(SBCommandReturnObject,
-  (lldb_private::CommandReturnObject *), ptr);
-}
-
 SBCommandReturnObject::~SBCommandReturnObject() = default;
 
-CommandReturnObject *SBCommandReturnObject::Release() {
-  LLDB_RECORD_METHOD_NO_ARGS(lldb_private::CommandReturnObject *,
- SBCommandReturnObject, Release);
-
-  return LLDB_RECORD_RESULT(m_opaque_up.release());
-}
-
 const SBCommandReturnObject &SBCommandReturnObject::
 operator=(const SBCommandReturnObject &rhs) {
   LLDB_RECORD_METHOD(
@@ -338,10 +330,6 @@
   LLDB_REGISTER_CONSTRUCTOR(SBCommandReturnObject, ());
   LLDB_REGISTER_CONSTRUCTOR(SBCommandReturnObject,
 (const lldb::SBCommandReturnObject &));
-  LLDB_REGISTER_CONSTRUCTOR(SBCommandReturnObject,
-(lldb_private::CommandReturnObject *));
-  LLDB_REGISTER_METHOD(lldb_private::CommandReturnObject *,
-   SBCommandReturnObject, Release, ());
   LLDB_REGISTER_METHOD(
   const lldb::SBCommandReturnObject &,
   SBCommandReturnObject, operator=,(const lldb::SBCommandReturnObject &));
Index: lldb/source/API/SBCommandInterpreter.cpp
===
--- lldb/source/API/SBCommandInterpreter.cpp
+++ lldb/source/API/SBCommandInterpreter.cpp
@@ -162,12 +162,13 @@
 
 protected:
   bool DoExecute(Args &command, CommandReturnObject &result) override {
-SBCommandReturnObject sb_return(&result);
+SBCommandReturnObject sb_return;
+std::swap(result, SBCommandReturnObject_ref(sb_return));
 SBCommandInterpreter sb_interpreter(&m_interpreter);
 SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this());
 bool ret = m_backend->DoExecute(
 debugger_sb, (char **)command.GetArgumentVector(), sb_return);
-sb_return.Release();
+std::swap(result, SBCommandReturnObject_ref(sb_return));
 return ret;
   }
   std::shared_ptr m_backend;
Index: lldb/scripts/Python/python-wrapper.swig
===
--- lldb/scripts/Python/python-wrapper.swig
+++ lldb/scripts/Python/python-wrapper.swig
@@ -650,30 +650,6 @@
 return sb_ptr;
 }
 
-// Currently, SBCommandReturnObjectReleaser wraps a unique pointer to an
-// lldb_private::CommandReturnObject. This means that the destructor for the
-// SB object will deallocate its contained CommandReturnObject. Because that
-// object is used as the real return object for Python-based commands, we want
-// it to stay around. Thus, we release the unique pointer before returning from
-// LLDBSwigPythonCallCommand, and to guarantee that the release will occur no
-// matter how we exit from the function, we have a releaser object whose
-// destructor does the right thing for us
-class S