jingham created this revision.
jingham added a reviewer: JDevlieghere.
Herald added a project: All.
jingham requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

When I added containers it seemed like it would be easier to accidentally 
overwrite extant commands, so I required a --overwrite option before "command 
script add" will overwrite an extant command.  That still strikes me as a good 
idea for users of packages of commands, but when you are developing the 
commands it's annoying since it means you have to change the "command script 
add" commands in your package while iteratively adding them during development. 
 So this patch adds a setting to turn off the overwrite requirement.  That way 
you can set this setting while developing your package, then turn it off again 
when you're acting as a consumer of packages.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122680

Files:
  lldb/include/lldb/Interpreter/CommandInterpreter.h
  lldb/source/Commands/CommandObjectCommands.cpp
  lldb/source/Interpreter/CommandInterpreter.cpp
  lldb/source/Interpreter/InterpreterProperties.td
  lldb/test/API/commands/command/container/TestContainerCommands.py

Index: lldb/test/API/commands/command/container/TestContainerCommands.py
===================================================================
--- lldb/test/API/commands/command/container/TestContainerCommands.py
+++ lldb/test/API/commands/command/container/TestContainerCommands.py
@@ -57,8 +57,9 @@
         self.expect("test-multi test-multi-sub welcome friend", "Test command works",
                     substrs=["Hello friend, welcome to LLDB"])
 
-        # Make sure overwriting works, first the leaf command:
-        # We should not be able to remove extant commands by default:
+        # Make sure overwriting works on the leaf command.  First using the
+        # explicit option so we should not be able to remove extant commands by default:
+
         self.expect("command script add -c welcome.WelcomeCommand2 test-multi test-multi-sub welcome",
                     "overwrite command w/o -o",
                     substrs=["cannot add command: sub-command already exists"], error=True)
@@ -67,9 +68,15 @@
         # Make sure we really did overwrite:
         self.expect("test-multi test-multi-sub welcome friend", "Used the new command class",
                     substrs=["Hello friend, welcome again to LLDB"])
-
         self.expect("apropos welcome", "welcome should show up in apropos", substrs=["A docstring for the second Welcome"])
         
+        # Now switch the default and make sure we can now delete w/o the overwrite option:
+        self.runCmd("settings set interpreter.require-overwrite 0")
+        self.runCmd("command script add -c welcome.WelcomeCommand test-multi test-multi-sub welcome")
+        # Make sure we really did overwrite:
+        self.expect("test-multi test-multi-sub welcome friend", "Used the new command class",
+                    substrs=["Hello friend, welcome to LLDB"])
+        
         # Make sure we give good errors when the input is wrong:
         self.expect("command script delete test-mult test-multi-sub welcome", "Delete script command - wrong first path component",
                     substrs=["'test-mult' not found"], error=True)
Index: lldb/source/Interpreter/InterpreterProperties.td
===================================================================
--- lldb/source/Interpreter/InterpreterProperties.td
+++ lldb/source/Interpreter/InterpreterProperties.td
@@ -36,4 +36,8 @@
     Global,
     DefaultTrue,
     Desc<"If true, LLDB will repeat the previous command if no command was passed to the interpreter. If false, LLDB won't repeat the previous command but only return a new prompt.">;
+  def RequireCommandOverwrite: Property<"require-overwrite", "Boolean">,
+    Global,
+    DefaultTrue,
+    Desc<"If true, require --overwrite in 'command script add' before overwriting existing user commands.">;
 }
Index: lldb/source/Interpreter/CommandInterpreter.cpp
===================================================================
--- lldb/source/Interpreter/CommandInterpreter.cpp
+++ lldb/source/Interpreter/CommandInterpreter.cpp
@@ -244,6 +244,12 @@
       nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
 }
 
+bool CommandInterpreter::GetRequireCommandOverwrite() const {
+  const uint32_t idx = ePropertyRequireCommandOverwrite;
+  return m_collection_sp->GetPropertyAtIndexAsBoolean(
+      nullptr, idx, g_interpreter_properties[idx].default_uint_value != 0);
+}
+
 void CommandInterpreter::Initialize() {
   LLDB_SCOPED_TIMER();
 
Index: lldb/source/Commands/CommandObjectCommands.cpp
===================================================================
--- lldb/source/Commands/CommandObjectCommands.cpp
+++ lldb/source/Commands/CommandObjectCommands.cpp
@@ -1445,7 +1445,7 @@
           m_short_help = std::string(option_arg);
         break;
       case 'o':
-        m_overwrite = true;
+        m_overwrite_lazy = eLazyBoolYes;
         break;
       case 's':
         m_synchronicity =
@@ -1467,7 +1467,7 @@
       m_class_name.clear();
       m_funct_name.clear();
       m_short_help.clear();
-      m_overwrite = false;
+      m_overwrite_lazy = eLazyBoolCalculate;
       m_synchronicity = eScriptedCommandSynchronicitySynchronous;
     }
 
@@ -1480,7 +1480,7 @@
     std::string m_class_name;
     std::string m_funct_name;
     std::string m_short_help;
-    bool m_overwrite = false;
+    LazyBool m_overwrite_lazy;
     ScriptedCommandSynchronicity m_synchronicity =
         eScriptedCommandSynchronicitySynchronous;
   };
@@ -1499,7 +1499,6 @@
 
     ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
     if (interpreter) {
-
       StringList lines;
       lines.SplitIntoLines(data);
       if (lines.GetSize() > 0) {
@@ -1562,8 +1561,19 @@
       result.AppendError("'command script add' requires at least one argument");
       return false;
     }
-    // Store the options in case we get multi-line input
-    m_overwrite = m_options.m_overwrite;
+    // Store the options in case we get multi-line input, also figure out the
+    // default if not user supplied:
+    switch (m_options.m_overwrite_lazy) {
+      case eLazyBoolCalculate:
+        m_overwrite = !GetDebugger().GetCommandInterpreter().GetRequireCommandOverwrite();
+        break;
+      case eLazyBoolYes:
+        m_overwrite = true;
+        break;
+      case eLazyBoolNo:
+        m_overwrite = false;
+    }
+    
     Status path_error;
     m_container = GetCommandInterpreter().VerifyUserMultiwordCmdPath(
         command, true, path_error);
@@ -1637,7 +1647,7 @@
   std::string m_cmd_name;
   CommandObjectMultiword *m_container = nullptr;
   std::string m_short_help;
-  bool m_overwrite = false;
+  bool m_overwrite = eLazyBoolCalculate;
   ScriptedCommandSynchronicity m_synchronicity =
       eScriptedCommandSynchronicitySynchronous;
 };
Index: lldb/include/lldb/Interpreter/CommandInterpreter.h
===================================================================
--- lldb/include/lldb/Interpreter/CommandInterpreter.h
+++ lldb/include/lldb/Interpreter/CommandInterpreter.h
@@ -549,6 +549,8 @@
   void SetEchoCommentCommands(bool enable);
 
   bool GetRepeatPreviousCommand() const;
+  
+  bool GetRequireCommandOverwrite() const;
 
   const CommandObject::CommandMap &GetUserCommands() const {
     return m_user_dict;
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to