brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.4 KiB · 022a27d Raw
154 lines · cpp
1//===-- OptionValueString.cpp ---------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Interpreter/OptionValueString.h"10 11#include "lldb/Host/OptionParser.h"12#include "lldb/Interpreter/OptionValue.h"13#include "lldb/Utility/Args.h"14#include "lldb/Utility/Stream.h"15 16using namespace lldb;17using namespace lldb_private;18 19static void DumpString(Stream &strm, const std::string &str, bool escape,20                       bool raw) {21  if (escape) {22    std::string escaped_str;23    Args::ExpandEscapedCharacters(str.c_str(), escaped_str);24    DumpString(strm, escaped_str, false, raw);25    return;26  }27 28  if (raw)29    strm.PutCString(str);30  else31    strm.QuotedCString(str.c_str());32}33 34void OptionValueString::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,35                                  uint32_t dump_mask) {36  if (dump_mask & eDumpOptionType)37    strm.Printf("(%s)", GetTypeAsCString());38  if (dump_mask & eDumpOptionValue) {39    if (dump_mask & eDumpOptionType)40      strm.PutCString(" = ");41    const bool escape = m_options.Test(eOptionEncodeCharacterEscapeSequences);42    const bool raw = dump_mask & eDumpOptionRaw;43    if (!m_current_value.empty() || m_value_was_set)44      DumpString(strm, m_current_value, escape, raw);45 46    if (dump_mask & eDumpOptionDefaultValue &&47        m_current_value != m_default_value && !m_default_value.empty()) {48      DefaultValueFormat label(strm);49      DumpString(strm, m_default_value, escape, raw);50    }51  }52}53 54Status OptionValueString::SetValueFromString(llvm::StringRef value,55                                             VarSetOperationType op) {56  Status error;57 58  std::string value_str = value.str();59  value = value.trim();60  if (value.size() > 0) {61    switch (value.front()) {62    case '"':63    case '\'': {64      if (value.size() <= 1 || value.back() != value.front()) {65        error = Status::FromErrorString("mismatched quotes");66        return error;67      }68      value = value.drop_front().drop_back();69    } break;70    }71    value_str = value.str();72  }73 74  switch (op) {75  case eVarSetOperationInvalid:76  case eVarSetOperationInsertBefore:77  case eVarSetOperationInsertAfter:78  case eVarSetOperationRemove:79    if (m_validator) {80      error = m_validator(value_str.c_str(), m_validator_baton);81      if (error.Fail())82        return error;83    }84    error = OptionValue::SetValueFromString(value, op);85    break;86 87  case eVarSetOperationAppend: {88    std::string new_value(m_current_value);89    if (value.size() > 0) {90      if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {91        std::string str;92        Args::EncodeEscapeSequences(value_str.c_str(), str);93        new_value.append(str);94      } else95        new_value.append(std::string(value));96    }97    if (m_validator) {98      error = m_validator(new_value.c_str(), m_validator_baton);99      if (error.Fail())100        return error;101    }102    m_current_value.assign(new_value);103    NotifyValueChanged();104  } break;105 106  case eVarSetOperationClear:107    Clear();108    NotifyValueChanged();109    break;110 111  case eVarSetOperationReplace:112  case eVarSetOperationAssign:113    if (m_validator) {114      error = m_validator(value_str.c_str(), m_validator_baton);115      if (error.Fail())116        return error;117    }118    m_value_was_set = true;119    if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) {120      Args::EncodeEscapeSequences(value_str.c_str(), m_current_value);121    } else {122      SetCurrentValue(value_str);123    }124    NotifyValueChanged();125    break;126  }127  return error;128}129 130Status OptionValueString::SetCurrentValue(llvm::StringRef value) {131  if (m_validator) {132    Status error(m_validator(value.str().c_str(), m_validator_baton));133    if (error.Fail())134      return error;135  }136  m_current_value.assign(std::string(value));137  return Status();138}139 140Status OptionValueString::AppendToCurrentValue(const char *value) {141  if (value && value[0]) {142    if (m_validator) {143      std::string new_value(m_current_value);144      new_value.append(value);145      Status error(m_validator(value, m_validator_baton));146      if (error.Fail())147        return error;148      m_current_value.assign(new_value);149    } else150      m_current_value.append(value);151  }152  return Status();153}154