[llvm] [NFC][Support] Create helper function to parse bool. (PR #102818)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 11 08:07:01 PDT 2024
https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/102818
- Create a helper template function to parse bool, to eliminate code duplication.
>From bd967d2a5a86fbbcb92de3df19cb47bbfd359326 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Sun, 11 Aug 2024 08:04:39 -0700
Subject: [PATCH] [NFC][Support] Create helper function to parse bool.
- Create a helper template function to parse bool, to eliminate code
duplication.
---
llvm/lib/Support/CommandLine.cpp | 42 ++++++++++++++------------------
1 file changed, 18 insertions(+), 24 deletions(-)
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index ecc487a17ccca..a5f1c069d08f7 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
More information about the llvm-commits
mailing list