Path: blob/main/contrib/llvm-project/lldb/source/Interpreter/OptionGroupPythonClassWithDict.cpp
39587 views
//===-- OptionGroupPythonClassWithDict.cpp --------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Interpreter/OptionGroupPythonClassWithDict.h"910#include "lldb/Host/OptionParser.h"1112using namespace lldb;13using namespace lldb_private;1415OptionGroupPythonClassWithDict::OptionGroupPythonClassWithDict(16const char *class_use, bool is_class, int class_option, int key_option,17int value_option, uint16_t required_options)18: m_is_class(is_class), m_required_options(required_options) {19m_key_usage_text.assign("The key for a key/value pair passed to the "20"implementation of a ");21m_key_usage_text.append(class_use);22m_key_usage_text.append(". Pairs can be specified more than once.");2324m_value_usage_text.assign("The value for the previous key in the pair passed "25"to the implementation of a ");26m_value_usage_text.append(class_use);27m_value_usage_text.append(". Pairs can be specified more than once.");2829m_class_usage_text.assign("The name of the ");30m_class_usage_text.append(m_is_class ? "class" : "function");31m_class_usage_text.append(" that will manage a ");32m_class_usage_text.append(class_use);33m_class_usage_text.append(".");3435m_option_definition[0].usage_mask = LLDB_OPT_SET_1;36m_option_definition[0].required = m_required_options.Test(eScriptClass);37m_option_definition[0].long_option = "script-class";38m_option_definition[0].short_option = class_option;39m_option_definition[0].validator = nullptr;40m_option_definition[0].option_has_arg = OptionParser::eRequiredArgument;41m_option_definition[0].enum_values = {};42m_option_definition[0].completion_type = 0;43m_option_definition[0].argument_type = eArgTypePythonClass;44m_option_definition[0].usage_text = m_class_usage_text.data();4546m_option_definition[1].usage_mask = LLDB_OPT_SET_2;47m_option_definition[1].required = m_required_options.Test(eDictKey);48m_option_definition[1].long_option = "structured-data-key";49m_option_definition[1].short_option = key_option;50m_option_definition[1].validator = nullptr;51m_option_definition[1].option_has_arg = OptionParser::eRequiredArgument;52m_option_definition[1].enum_values = {};53m_option_definition[1].completion_type = 0;54m_option_definition[1].argument_type = eArgTypeNone;55m_option_definition[1].usage_text = m_key_usage_text.data();5657m_option_definition[2].usage_mask = LLDB_OPT_SET_2;58m_option_definition[2].required = m_required_options.Test(eDictValue);59m_option_definition[2].long_option = "structured-data-value";60m_option_definition[2].short_option = value_option;61m_option_definition[2].validator = nullptr;62m_option_definition[2].option_has_arg = OptionParser::eRequiredArgument;63m_option_definition[2].enum_values = {};64m_option_definition[2].completion_type = 0;65m_option_definition[2].argument_type = eArgTypeNone;66m_option_definition[2].usage_text = m_value_usage_text.data();6768m_option_definition[3].usage_mask = LLDB_OPT_SET_3;69m_option_definition[3].required = m_required_options.Test(ePythonFunction);70m_option_definition[3].long_option = "python-function";71m_option_definition[3].short_option = class_option;72m_option_definition[3].validator = nullptr;73m_option_definition[3].option_has_arg = OptionParser::eRequiredArgument;74m_option_definition[3].enum_values = {};75m_option_definition[3].completion_type = 0;76m_option_definition[3].argument_type = eArgTypePythonFunction;77m_option_definition[3].usage_text = m_class_usage_text.data();78}7980Status OptionGroupPythonClassWithDict::SetOptionValue(81uint32_t option_idx,82llvm::StringRef option_arg,83ExecutionContext *execution_context) {84Status error;85switch (option_idx) {86case 0:87case 3: {88m_name.assign(std::string(option_arg));89} break;90case 1: {91if (!m_dict_sp)92m_dict_sp = std::make_shared<StructuredData::Dictionary>();93if (m_current_key.empty())94m_current_key.assign(std::string(option_arg));95else96error.SetErrorStringWithFormat("Key: \"%s\" missing value.",97m_current_key.c_str());9899} break;100case 2: {101if (!m_dict_sp)102m_dict_sp = std::make_shared<StructuredData::Dictionary>();103if (!m_current_key.empty()) {104if (!option_arg.empty()) {105double d = 0;106std::string opt = option_arg.lower();107108if (llvm::to_integer(option_arg, d)) {109if (opt[0] == '-')110m_dict_sp->AddIntegerItem(m_current_key, static_cast<int64_t>(d));111else112m_dict_sp->AddIntegerItem(m_current_key,113static_cast<uint64_t>(d));114} else if (llvm::to_float(option_arg, d)) {115m_dict_sp->AddFloatItem(m_current_key, d);116} else if (opt == "true" || opt == "false") {117m_dict_sp->AddBooleanItem(m_current_key, opt == "true");118} else {119m_dict_sp->AddStringItem(m_current_key, option_arg);120}121}122123m_current_key.clear();124}125else126error.SetErrorStringWithFormat("Value: \"%s\" missing matching key.",127option_arg.str().c_str());128} break;129default:130llvm_unreachable("Unimplemented option");131}132return error;133}134135void OptionGroupPythonClassWithDict::OptionParsingStarting(136ExecutionContext *execution_context) {137m_current_key.erase();138// Leave the dictionary shared pointer unset. That way you can tell that139// the user didn't pass any -k -v pairs. We want to be able to warn if these140// were passed when the function they passed won't use them.141m_dict_sp.reset();142m_name.clear();143}144145Status OptionGroupPythonClassWithDict::OptionParsingFinished(146ExecutionContext *execution_context) {147Status error;148// If we get here and there's contents in the m_current_key, somebody must149// have provided a key but no value.150if (!m_current_key.empty())151error.SetErrorStringWithFormat("Key: \"%s\" missing value.",152m_current_key.c_str());153return error;154}155156157158