-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[NFC][Support] Create helper function to parse bool. #102818
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
Conversation
jurahul
commented
Aug 11, 2024
- Create a helper template function to parse bool, to eliminate code duplication.
bd967d2
to
f2de3bb
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
d8e7b5a
to
8be13c8
Compare
@llvm/pr-subscribers-llvm-support Author: Rahul Joshi (jurahul) Changes
Full diff: https://github.com/llvm/llvm-project/pull/102818.diff 1 Files Affected:
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index ecc487a17ccca..2ef2e58b657dd 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -400,6 +400,22 @@ class CommandLineParser {
SubCommand *LookupSubCommand(StringRef Name, std::string &NearestString);
};
+template <typename T, T TrueVal, T FalseVal>
+bool parseBool(Option &O, StringRef ArgName, StringRef Arg, T &Value) {
+ if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
+ Arg == "1") {
+ Value = TrueVal;
+ return false;
+ }
+
+ if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
+ Value = FalseVal;
+ return false;
+ }
+ return O.error("'" + Arg +
+ "' is invalid value for boolean argument! Try 0 or 1");
+}
+
} // namespace
static ManagedStatic<CommandLineParser> GlobalParser;
@@ -1954,36 +1970,14 @@ void basic_parser_impl::printOptionName(const Option &O,
//
bool parser<bool>::parse(Option &O, StringRef ArgName, StringRef Arg,
bool &Value) {
- if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
- Arg == "1") {
- Value = true;
- return false;
- }
-
- if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
- Value = false;
- return false;
- }
- return O.error("'" + Arg +
- "' is invalid value for boolean argument! Try 0 or 1");
+ return parseBool<bool, true, false>(O, ArgName, Arg, Value);
}
// parser<boolOrDefault> implementation
//
bool parser<boolOrDefault>::parse(Option &O, StringRef ArgName, StringRef Arg,
boolOrDefault &Value) {
- if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
- Arg == "1") {
- Value = BOU_TRUE;
- return false;
- }
- if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
- Value = BOU_FALSE;
- return false;
- }
-
- return O.error("'" + Arg +
- "' is invalid value for boolean argument! Try 0 or 1");
+ return parseBool<boolOrDefault, BOU_TRUE, BOU_FALSE>(O, ArgName, Arg, Value);
}
// parser<int> implementation
|
Note, linux build failues look like infra issues: | fatal: not a git repository (or any parent up to mount point /var/lib) I don't have permission to rekick. Hoping it gets kicked when I update the branch again (and the infa issue resolve by that time) |
- Create a helper template function to parse bool, to eliminate code duplication.
8be13c8
to
624f5a4
Compare
@MaskRay, can you please merge this PR? |
@MaskRay or @joker-eph can one of you please merge the PR? |
Thanks! |