Skip to content

[upstreaming] Split out Args.[h/cpp] modifications #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions lldb/include/lldb/Utility/Args.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,6 @@ class Args {
/// If the argument was originally quoted, put in the quote char here.
void Unshift(llvm::StringRef arg_str, char quote_char = '\0');

bool GetOptionValueAsString(const char *option, std::string &value);

int GetOptionValuesAsStrings(const char *option,
std::vector<std::string> &values);

// Clear the arguments.
//
// For re-setting or blanking out the list of arguments.
Expand Down
102 changes: 102 additions & 0 deletions lldb/include/lldb/Utility/OptionParsing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//===-- OptionParsing.h -----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_UTILITY_OPTION_PARSING_H
#define LLDB_UTILITY_OPTION_PARSING_H

#include "lldb/Utility/Args.h"

namespace lldb_private {
namespace OptionParsing {
inline bool GetOptionValueAsString(Args &args, const char *option,
std::string &value) {
for (size_t ai = 0, ae = args.GetArgumentCount(); ai != ae; ++ai) {
const char *arg = args.GetArgumentAtIndex(ai);
const char *option_loc = strstr(arg, option);

const bool is_long_option = (option[0] == '-' && option[1] == '-');

if (option_loc == arg) {
const char *after_option = option_loc + strlen(option);

switch (*after_option) {
default:
if (is_long_option) {
continue;
} else {
value = after_option;
return true;
}
break;
case '=':
value = after_option + 1;
return true;
case '\0': {
const char *next_value = args.GetArgumentAtIndex(ai + 1);
if (next_value) {
value = next_value;
return true;
} else {
return false;
}
}
}
}
}

return false;
}

inline int GetOptionValuesAsStrings(Args &args, const char *option,
std::vector<std::string> &values) {
int ret = 0;

for (size_t ai = 0, ae = args.GetArgumentCount(); ai != ae; ++ai) {
const char *arg = args.GetArgumentAtIndex(ai);
const char *option_loc = strstr(arg, option);

const bool is_long_option = (option[0] == '-' && option[1] == '-');

if (option_loc == arg) {
const char *after_option = option_loc + strlen(option);

switch (*after_option) {
default:
if (is_long_option) {
continue;
} else {
values.push_back(after_option);
++ret;
}
break;
case '=':
values.push_back(after_option + 1);
++ret;
break;
case '\0': {
const char *next_value = args.GetArgumentAtIndex(ai + 1);
if (next_value) {
values.push_back(next_value);
++ret;
++ai;
break;
} else {
return ret;
}
}
}
}
}

return ret;
}

} // namespace OptionParsing
} // namespace lldb_private

#endif // LLDB_UTILITY_OPTION_PARSING_H
13 changes: 9 additions & 4 deletions lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/Value.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/OptionParsing.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Scalar.h"
#include "lldb/Utility/StreamString.h"
Expand Down Expand Up @@ -3000,7 +3001,8 @@ bool SymbolFileDWARF::GetCompileOption(const char *option, std::string &value,
if (strstr(flags, option)) {
Args compiler_args(flags);

return compiler_args.GetOptionValueAsString(option, value);
return OptionParsing::GetOptionValueAsString(compiler_args,
option, value);
}
}
}
Expand All @@ -3019,7 +3021,8 @@ bool SymbolFileDWARF::GetCompileOption(const char *option, std::string &value,
if (strstr(flags, option)) {
Args compiler_args(flags);

return compiler_args.GetOptionValueAsString(option, value);
return OptionParsing::GetOptionValueAsString(compiler_args,
option, value);
}
}
}
Expand Down Expand Up @@ -3050,7 +3053,8 @@ int SymbolFileDWARF::GetCompileOptions(const char *option,
if (strstr(flags, option)) {
Args compiler_args(flags);

return compiler_args.GetOptionValuesAsStrings(option, values);
return OptionParsing::GetOptionValuesAsStrings(compiler_args,
option, values);
}
}
}
Expand All @@ -3071,7 +3075,8 @@ int SymbolFileDWARF::GetCompileOptions(const char *option,
if (strstr(flags, option)) {
Args compiler_args(flags);

return compiler_args.GetOptionValuesAsStrings(option, values);
return OptionParsing::GetOptionValuesAsStrings(compiler_args,
option, values);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion lldb/source/Target/SwiftLanguageRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#include "lldb/Target/ThreadPlanRunToAddress.h"
#include "lldb/Target/ThreadPlanStepInRange.h"
#include "lldb/Target/ThreadPlanStepOverRange.h"
#include "lldb/Utility/OptionParsing.h"
#include "lldb/Utility/Status.h"

#include "llvm/ADT/ScopeExit.h"
Expand Down Expand Up @@ -3406,7 +3407,8 @@ Status SwiftLanguageRuntime::SwiftExceptionPrecondition::ConfigurePrecondition(
Args &args) {
Status error;
std::vector<std::string> object_typenames;
args.GetOptionValuesAsStrings("exception-typename", object_typenames);
OptionParsing::GetOptionValuesAsStrings(args, "exception-typename",
object_typenames);
for (auto type_name : object_typenames)
AddTypeName(type_name.c_str());
return error;
Expand Down
82 changes: 0 additions & 82 deletions lldb/source/Utility/Args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,88 +443,6 @@ uint32_t Args::StringToGenericRegister(llvm::StringRef s) {
return result;
}

bool Args::GetOptionValueAsString(const char *option, std::string &value) {
for (size_t ai = 0, ae = GetArgumentCount(); ai != ae; ++ai) {
const char *arg = GetArgumentAtIndex(ai);
const char *option_loc = strstr(arg, option);

const bool is_long_option = (option[0] == '-' && option[1] == '-');

if (option_loc == arg) {
const char *after_option = option_loc + strlen(option);

switch (*after_option) {
default:
if (is_long_option) {
continue;
} else {
value = after_option;
return true;
}
break;
case '=':
value = after_option + 1;
return true;
case '\0': {
const char *next_value = GetArgumentAtIndex(ai + 1);
if (next_value) {
value = next_value;
return true;
} else {
return false;
}
}
}
}
}

return false;
}

int Args::GetOptionValuesAsStrings(const char *option,
std::vector<std::string> &value) {
int ret = 0;

for (size_t ai = 0, ae = GetArgumentCount(); ai != ae; ++ai) {
const char *arg = GetArgumentAtIndex(ai);
const char *option_loc = strstr(arg, option);

const bool is_long_option = (option[0] == '-' && option[1] == '-');

if (option_loc == arg) {
const char *after_option = option_loc + strlen(option);

switch (*after_option) {
default:
if (is_long_option) {
continue;
} else {
value.push_back(after_option);
++ret;
}
break;
case '=':
value.push_back(after_option + 1);
++ret;
break;
case '\0': {
const char *next_value = GetArgumentAtIndex(ai + 1);
if (next_value) {
value.push_back(next_value);
++ret;
++ai;
break;
} else {
return ret;
}
}
}
}
}

return ret;
}

void Args::EncodeEscapeSequences(const char *src, std::string &dst) {
dst.clear();
if (src) {
Expand Down