From df90a15b1ac938559a8c3af12126559c1e1e9558 Mon Sep 17 00:00:00 2001 From: Tatyana Krasnukha Date: Wed, 11 Mar 2020 19:51:40 +0300 Subject: [PATCH] [lldb] Clear all settings during a test's setUp Global properties are shared between debugger instances and if a test doesn't clear changes in settings it made, this leads to side effects in other tests. Differential Revision: https://reviews.llvm.org/D75537 --- lldb/packages/Python/lldbsuite/test/lldbtest.py | 3 ++ lldb/source/Commands/CommandObjectSettings.cpp | 50 ++++++++++++++++++++++++- lldb/source/Commands/Options.td | 5 +++ lldb/test/API/commands/settings/TestSettings.py | 26 +++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/lldb/packages/Python/lldbsuite/test/lldbtest.py b/lldb/packages/Python/lldbsuite/test/lldbtest.py index a9d6e50..cd48747 100644 --- a/lldb/packages/Python/lldbsuite/test/lldbtest.py +++ b/lldb/packages/Python/lldbsuite/test/lldbtest.py @@ -687,6 +687,9 @@ class Base(unittest2.TestCase): @classmethod def setUpCommands(cls): commands = [ + # First of all, clear all settings to have clean state of global properties. + "settings clear -all", + # Disable Spotlight lookup. The testsuite creates # different binaries with the same UUID, because they only # differ in the debug info, which is not being hashed. diff --git a/lldb/source/Commands/CommandObjectSettings.cpp b/lldb/source/Commands/CommandObjectSettings.cpp index 4d64ae4..cc1080c 100644 --- a/lldb/source/Commands/CommandObjectSettings.cpp +++ b/lldb/source/Commands/CommandObjectSettings.cpp @@ -1043,13 +1043,16 @@ protected: }; // CommandObjectSettingsClear +#define LLDB_OPTIONS_settings_clear +#include "CommandOptions.inc" class CommandObjectSettingsClear : public CommandObjectParsed { public: CommandObjectSettingsClear(CommandInterpreter &interpreter) : CommandObjectParsed( interpreter, "settings clear", - "Clear a debugger setting array, dictionary, or string.", nullptr) { + "Clear a debugger setting array, dictionary, or string. " + "If '-a' option is specified, it clears all settings.", nullptr) { CommandArgumentEntry arg; CommandArgumentData var_name_arg; @@ -1077,11 +1080,53 @@ public: request, nullptr); } + Options *GetOptions() override { return &m_options; } + + class CommandOptions : public Options { + public: + CommandOptions() = default; + + ~CommandOptions() override = default; + + Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, + ExecutionContext *execution_context) override { + const int short_option = m_getopt_table[option_idx].val; + switch (short_option) { + case 'a': + m_clear_all = true; + break; + default: + llvm_unreachable("Unimplemented option"); + } + return Status(); + } + + void OptionParsingStarting(ExecutionContext *execution_context) override { + m_clear_all = false; + } + + llvm::ArrayRef GetDefinitions() override { + return llvm::makeArrayRef(g_settings_clear_options); + } + + bool m_clear_all = false; + }; + protected: bool DoExecute(Args &command, CommandReturnObject &result) override { result.SetStatus(eReturnStatusSuccessFinishNoResult); const size_t argc = command.GetArgumentCount(); + if (m_options.m_clear_all) { + if (argc != 0) { + result.AppendError("'settings clear --all' doesn't take any arguments"); + result.SetStatus(eReturnStatusFailed); + return false; + } + GetDebugger().GetValueProperties()->Clear(); + return result.Succeeded(); + } + if (argc != 1) { result.AppendError("'settings clear' takes exactly one argument"); result.SetStatus(eReturnStatusFailed); @@ -1106,6 +1151,9 @@ protected: return result.Succeeded(); } + + private: + CommandOptions m_options; }; // CommandObjectMultiwordSettings diff --git a/lldb/source/Commands/Options.td b/lldb/source/Commands/Options.td index 1456630..fa8f522 100644 --- a/lldb/source/Commands/Options.td +++ b/lldb/source/Commands/Options.td @@ -39,6 +39,11 @@ let Command = "settings read" in { Desc<"The file from which to read the settings.">; } +let Command = "settings clear" in { + def setclear_all : Option<"all", "a">, + Desc<"Clear all settings.">; +} + let Command = "breakpoint list" in { // FIXME: We need to add an "internal" command, and then add this sort of // thing to it. But I need to see it for now, and don't want to wait. diff --git a/lldb/test/API/commands/settings/TestSettings.py b/lldb/test/API/commands/settings/TestSettings.py index 571ada82..ffb194f 100644 --- a/lldb/test/API/commands/settings/TestSettings.py +++ b/lldb/test/API/commands/settings/TestSettings.py @@ -520,6 +520,32 @@ class SettingsCommandTestCase(TestBase): self.expect("settings remove ''", error=True, substrs=["'settings remove' command requires a valid variable name"]) + def test_settings_clear_all(self): + # Change a dictionary. + self.runCmd("settings set target.env-vars a=1 b=2 c=3") + # Change an array. + self.runCmd("settings set target.run-args a1 b2 c3") + # Change a single boolean value. + self.runCmd("settings set auto-confirm true") + # Change a single integer value. + self.runCmd("settings set tab-size 2") + + # Clear everything. + self.runCmd("settings clear --all") + + # Check that settings have their default values after clearing. + self.expect("settings show target.env-vars", patterns=['^target.env-vars \(dictionary of strings\) =\s*$']) + self.expect("settings show target.run-args", patterns=['^target.run-args \(arguments\) =\s*$']) + self.expect("settings show auto-confirm", substrs=["false"]) + self.expect("settings show tab-size", substrs=["4"]) + + # Check that the command fails if we combine '--all' option with any arguments. + self.expect( + "settings clear --all auto-confirm", + COMMAND_FAILED_AS_EXPECTED, + error=True, + substrs=["'settings clear --all' doesn't take any arguments"]) + def test_all_settings_exist(self): self.expect("settings show", substrs=["auto-confirm", -- 2.7.4