[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 14:47:55 PDT 2024
https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/102818
>From 624f5a4510144d6ef328afa382ee200b62061e1a 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 ecc487a17cccae..e34a770b1b53e5 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -404,6 +404,22 @@ class CommandLineParser {
static ManagedStatic<CommandLineParser> GlobalParser;
+template <typename T, T TrueVal, T FalseVal>
+static 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");
+}
+
void cl::AddLiteralOption(Option &O, StringRef Name) {
GlobalParser->addLiteralOption(O, Name);
}
@@ -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