Skip to content

[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

Merged
merged 2 commits into from
Aug 13, 2024

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Aug 11, 2024

  • Create a helper template function to parse bool, to eliminate code duplication.

@jurahul jurahul force-pushed the cl_dedup_bool_parse branch from bd967d2 to f2de3bb Compare August 11, 2024 15:07
Copy link

github-actions bot commented Aug 11, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@jurahul jurahul force-pushed the cl_dedup_bool_parse branch 2 times, most recently from d8e7b5a to 8be13c8 Compare August 11, 2024 15:15
@jurahul jurahul requested review from MaskRay and joker-eph August 11, 2024 15:17
@jurahul jurahul marked this pull request as ready for review August 11, 2024 15:18
@llvmbot
Copy link
Member

llvmbot commented Aug 11, 2024

@llvm/pr-subscribers-llvm-support

Author: Rahul Joshi (jurahul)

Changes
  • Create a helper template function to parse bool, to eliminate code duplication.

Full diff: https://github.com/llvm/llvm-project/pull/102818.diff

1 Files Affected:

  • (modified) llvm/lib/Support/CommandLine.cpp (+18-24)
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

@jurahul
Copy link
Contributor Author

jurahul commented Aug 11, 2024

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.
@jurahul jurahul force-pushed the cl_dedup_bool_parse branch from 8be13c8 to 624f5a4 Compare August 11, 2024 21:47
@jurahul
Copy link
Contributor Author

jurahul commented Aug 11, 2024

@MaskRay, can you please merge this PR?

@jurahul
Copy link
Contributor Author

jurahul commented Aug 13, 2024

@MaskRay or @joker-eph can one of you please merge the PR?

@MaskRay MaskRay merged commit 136e5f4 into llvm:main Aug 13, 2024
8 checks passed
@jurahul
Copy link
Contributor Author

jurahul commented Aug 13, 2024

Thanks!

@jurahul jurahul deleted the cl_dedup_bool_parse branch August 13, 2024 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants