Skip to content

Commit 0e0587c

Browse files
authored
Merge pull request #246 from Teemperor/RemoveOptionParsingFromArg
[upstreaming] Split out Args.[h/cpp] modifications
2 parents d9b6a86 + f06b031 commit 0e0587c

File tree

5 files changed

+114
-92
lines changed

5 files changed

+114
-92
lines changed

lldb/include/lldb/Utility/Args.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,6 @@ class Args {
247247
/// If the argument was originally quoted, put in the quote char here.
248248
void Unshift(llvm::StringRef arg_str, char quote_char = '\0');
249249

250-
bool GetOptionValueAsString(const char *option, std::string &value);
251-
252-
int GetOptionValuesAsStrings(const char *option,
253-
std::vector<std::string> &values);
254-
255250
// Clear the arguments.
256251
//
257252
// For re-setting or blanking out the list of arguments.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//===-- OptionParsing.h -----------------------------------------*- C++ -*-===//
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-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLDB_UTILITY_OPTION_PARSING_H
10+
#define LLDB_UTILITY_OPTION_PARSING_H
11+
12+
#include "lldb/Utility/Args.h"
13+
14+
namespace lldb_private {
15+
namespace OptionParsing {
16+
inline bool GetOptionValueAsString(Args &args, const char *option,
17+
std::string &value) {
18+
for (size_t ai = 0, ae = args.GetArgumentCount(); ai != ae; ++ai) {
19+
const char *arg = args.GetArgumentAtIndex(ai);
20+
const char *option_loc = strstr(arg, option);
21+
22+
const bool is_long_option = (option[0] == '-' && option[1] == '-');
23+
24+
if (option_loc == arg) {
25+
const char *after_option = option_loc + strlen(option);
26+
27+
switch (*after_option) {
28+
default:
29+
if (is_long_option) {
30+
continue;
31+
} else {
32+
value = after_option;
33+
return true;
34+
}
35+
break;
36+
case '=':
37+
value = after_option + 1;
38+
return true;
39+
case '\0': {
40+
const char *next_value = args.GetArgumentAtIndex(ai + 1);
41+
if (next_value) {
42+
value = next_value;
43+
return true;
44+
} else {
45+
return false;
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
return false;
53+
}
54+
55+
inline int GetOptionValuesAsStrings(Args &args, const char *option,
56+
std::vector<std::string> &values) {
57+
int ret = 0;
58+
59+
for (size_t ai = 0, ae = args.GetArgumentCount(); ai != ae; ++ai) {
60+
const char *arg = args.GetArgumentAtIndex(ai);
61+
const char *option_loc = strstr(arg, option);
62+
63+
const bool is_long_option = (option[0] == '-' && option[1] == '-');
64+
65+
if (option_loc == arg) {
66+
const char *after_option = option_loc + strlen(option);
67+
68+
switch (*after_option) {
69+
default:
70+
if (is_long_option) {
71+
continue;
72+
} else {
73+
values.push_back(after_option);
74+
++ret;
75+
}
76+
break;
77+
case '=':
78+
values.push_back(after_option + 1);
79+
++ret;
80+
break;
81+
case '\0': {
82+
const char *next_value = args.GetArgumentAtIndex(ai + 1);
83+
if (next_value) {
84+
values.push_back(next_value);
85+
++ret;
86+
++ai;
87+
break;
88+
} else {
89+
return ret;
90+
}
91+
}
92+
}
93+
}
94+
}
95+
96+
return ret;
97+
}
98+
99+
} // namespace OptionParsing
100+
} // namespace lldb_private
101+
102+
#endif // LLDB_UTILITY_OPTION_PARSING_H

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "lldb/Core/StreamFile.h"
2121
#include "lldb/Core/Value.h"
2222
#include "lldb/Utility/ArchSpec.h"
23+
#include "lldb/Utility/OptionParsing.h"
2324
#include "lldb/Utility/RegularExpression.h"
2425
#include "lldb/Utility/Scalar.h"
2526
#include "lldb/Utility/StreamString.h"
@@ -3000,7 +3001,8 @@ bool SymbolFileDWARF::GetCompileOption(const char *option, std::string &value,
30003001
if (strstr(flags, option)) {
30013002
Args compiler_args(flags);
30023003

3003-
return compiler_args.GetOptionValueAsString(option, value);
3004+
return OptionParsing::GetOptionValueAsString(compiler_args,
3005+
option, value);
30043006
}
30053007
}
30063008
}
@@ -3019,7 +3021,8 @@ bool SymbolFileDWARF::GetCompileOption(const char *option, std::string &value,
30193021
if (strstr(flags, option)) {
30203022
Args compiler_args(flags);
30213023

3022-
return compiler_args.GetOptionValueAsString(option, value);
3024+
return OptionParsing::GetOptionValueAsString(compiler_args,
3025+
option, value);
30233026
}
30243027
}
30253028
}
@@ -3050,7 +3053,8 @@ int SymbolFileDWARF::GetCompileOptions(const char *option,
30503053
if (strstr(flags, option)) {
30513054
Args compiler_args(flags);
30523055

3053-
return compiler_args.GetOptionValuesAsStrings(option, values);
3056+
return OptionParsing::GetOptionValuesAsStrings(compiler_args,
3057+
option, values);
30543058
}
30553059
}
30563060
}
@@ -3071,7 +3075,8 @@ int SymbolFileDWARF::GetCompileOptions(const char *option,
30713075
if (strstr(flags, option)) {
30723076
Args compiler_args(flags);
30733077

3074-
return compiler_args.GetOptionValuesAsStrings(option, values);
3078+
return OptionParsing::GetOptionValuesAsStrings(compiler_args,
3079+
option, values);
30753080
}
30763081
}
30773082
}

lldb/source/Target/SwiftLanguageRuntime.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "lldb/Target/ThreadPlanRunToAddress.h"
7373
#include "lldb/Target/ThreadPlanStepInRange.h"
7474
#include "lldb/Target/ThreadPlanStepOverRange.h"
75+
#include "lldb/Utility/OptionParsing.h"
7576
#include "lldb/Utility/Status.h"
7677

7778
#include "llvm/ADT/ScopeExit.h"
@@ -3406,7 +3407,8 @@ Status SwiftLanguageRuntime::SwiftExceptionPrecondition::ConfigurePrecondition(
34063407
Args &args) {
34073408
Status error;
34083409
std::vector<std::string> object_typenames;
3409-
args.GetOptionValuesAsStrings("exception-typename", object_typenames);
3410+
OptionParsing::GetOptionValuesAsStrings(args, "exception-typename",
3411+
object_typenames);
34103412
for (auto type_name : object_typenames)
34113413
AddTypeName(type_name.c_str());
34123414
return error;

lldb/source/Utility/Args.cpp

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -443,88 +443,6 @@ uint32_t Args::StringToGenericRegister(llvm::StringRef s) {
443443
return result;
444444
}
445445

446-
bool Args::GetOptionValueAsString(const char *option, std::string &value) {
447-
for (size_t ai = 0, ae = GetArgumentCount(); ai != ae; ++ai) {
448-
const char *arg = GetArgumentAtIndex(ai);
449-
const char *option_loc = strstr(arg, option);
450-
451-
const bool is_long_option = (option[0] == '-' && option[1] == '-');
452-
453-
if (option_loc == arg) {
454-
const char *after_option = option_loc + strlen(option);
455-
456-
switch (*after_option) {
457-
default:
458-
if (is_long_option) {
459-
continue;
460-
} else {
461-
value = after_option;
462-
return true;
463-
}
464-
break;
465-
case '=':
466-
value = after_option + 1;
467-
return true;
468-
case '\0': {
469-
const char *next_value = GetArgumentAtIndex(ai + 1);
470-
if (next_value) {
471-
value = next_value;
472-
return true;
473-
} else {
474-
return false;
475-
}
476-
}
477-
}
478-
}
479-
}
480-
481-
return false;
482-
}
483-
484-
int Args::GetOptionValuesAsStrings(const char *option,
485-
std::vector<std::string> &value) {
486-
int ret = 0;
487-
488-
for (size_t ai = 0, ae = GetArgumentCount(); ai != ae; ++ai) {
489-
const char *arg = GetArgumentAtIndex(ai);
490-
const char *option_loc = strstr(arg, option);
491-
492-
const bool is_long_option = (option[0] == '-' && option[1] == '-');
493-
494-
if (option_loc == arg) {
495-
const char *after_option = option_loc + strlen(option);
496-
497-
switch (*after_option) {
498-
default:
499-
if (is_long_option) {
500-
continue;
501-
} else {
502-
value.push_back(after_option);
503-
++ret;
504-
}
505-
break;
506-
case '=':
507-
value.push_back(after_option + 1);
508-
++ret;
509-
break;
510-
case '\0': {
511-
const char *next_value = GetArgumentAtIndex(ai + 1);
512-
if (next_value) {
513-
value.push_back(next_value);
514-
++ret;
515-
++ai;
516-
break;
517-
} else {
518-
return ret;
519-
}
520-
}
521-
}
522-
}
523-
}
524-
525-
return ret;
526-
}
527-
528446
void Args::EncodeEscapeSequences(const char *src, std::string &dst) {
529447
dst.clear();
530448
if (src) {

0 commit comments

Comments
 (0)