[llvm] 42b886e - [Support] Accept only true, false, 1 and 0 for cl::opt<bool> (#224975)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 21 00:31:14 PDT 2026
Author: Fangrui Song
Date: 2026-09-21T00:31:07-07:00
New Revision: 42b886e3f1b8c0f90f69ec16d2945dde8bfeae06
URL: https://github.com/llvm/llvm-project/commit/42b886e3f1b8c0f90f69ec16d2945dde8bfeae06
DIFF: https://github.com/llvm/llvm-project/commit/42b886e3f1b8c0f90f69ec16d2945dde8bfeae06.diff
LOG: [Support] Accept only true, false, 1 and 0 for cl::opt<bool> (#224975)
For these internal developer options, accepting `-flag=` (empty
value)/`-flag=True`/`-flag=FALSE` is a weird quirk. To the best of my
knowledge, they are nearly never used. Drop the spellings. In the
future, we will migrate to `-flag`/`-no-flag` (or their double-dash
form).
LLM-aided
Added:
Modified:
llvm/docs/CommandLine.md
llvm/lib/Support/CommandLine.cpp
llvm/unittests/Support/CommandLineTest.cpp
mlir/test/Dialect/SparseTensor/external_direct.mlir
Removed:
################################################################################
diff --git a/llvm/docs/CommandLine.md b/llvm/docs/CommandLine.md
index 409f7a003ee93..e31be88a46bad 100644
--- a/llvm/docs/CommandLine.md
+++ b/llvm/docs/CommandLine.md
@@ -226,8 +226,8 @@ specified, allowing any of the following inputs:
```
compiler -f # No value, 'Force' == true
compiler -f=true # Value specified, 'Force' == true
-compiler -f=TRUE # Value specified, 'Force' == true
-compiler -f=FALSE # Value specified, 'Force' == false
+compiler -f=1 # Value specified, 'Force' == true
+compiler -f=false # Value specified, 'Force' == false
```
... you get the idea. The {ref}`bool parser <bool parser>` just turns the string values into
@@ -1497,8 +1497,8 @@ work with new data types and new ways of interpreting the same data. See the
(bool parser)=
* The **parser<bool> specialization** is used to convert boolean strings to a
- boolean value. Currently accepted strings are "`true`", "`TRUE`",
- "`True`", "`1`", "`false`", "`FALSE`", "`False`", and "`0`".
+ boolean value. Currently accepted strings are "`true`", "`1`",
+ "`false`", and "`0`".
* The **parser<boolOrDefault> specialization** is used for cases where the value
is boolean, but we also need to know whether the option was specified at all.
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 4503049cc4d80..b7ca1e6c19143 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -429,13 +429,14 @@ static 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") {
+ // ProvideOption passes a null Arg for a bare -flag (treated as true) and an
+ // empty one for -flag= (treated as invalid).
+ if (!Arg.data() || Arg == "true" || Arg == "1") {
Value = TrueVal;
return false;
}
- if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
+ if (Arg == "false" || Arg == "0") {
Value = FalseVal;
return false;
}
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index 8fa69ff9c2987..f810778d175bd 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -1666,6 +1666,35 @@ TEST_F(GetOptionWidthTest,
ExpectedStrSize);
}
+TEST(CommandLineTest, BoolValues) {
+ cl::ResetCommandLineParser();
+
+ StackOption<bool> OptF("f", cl::init(true));
+ StackOption<bool> OptFlag("flag");
+
+ const char *args1[] = {"prog", "-flag", "--f=false"};
+ EXPECT_TRUE(
+ cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
+ EXPECT_TRUE(OptFlag);
+ EXPECT_FALSE(OptF);
+ cl::ResetAllOptionOccurrences();
+
+ // An empty value is not the same as no value.
+ const char *args2[] = {"prog", "-flag="};
+ EXPECT_FALSE(
+ cl::ParseCommandLineOptions(2, args2, StringRef(), &llvm::nulls()));
+ cl::ResetAllOptionOccurrences();
+
+ const char *args3[] = {"prog", "-flag=yes"};
+ EXPECT_FALSE(
+ cl::ParseCommandLineOptions(2, args3, StringRef(), &llvm::nulls()));
+ cl::ResetAllOptionOccurrences();
+
+ const char *args4[] = {"prog", "-flag=True"};
+ EXPECT_FALSE(
+ cl::ParseCommandLineOptions(2, args4, StringRef(), &llvm::nulls()));
+}
+
TEST(CommandLineTest, PrefixOptions) {
cl::ResetCommandLineParser();
diff --git a/mlir/test/Dialect/SparseTensor/external_direct.mlir b/mlir/test/Dialect/SparseTensor/external_direct.mlir
index 78c4a295686b3..0980f8bf9e992 100644
--- a/mlir/test/Dialect/SparseTensor/external_direct.mlir
+++ b/mlir/test/Dialect/SparseTensor/external_direct.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s --sparse-assembler="direct-out=True" -split-input-file | FileCheck %s
+// RUN: mlir-opt %s --sparse-assembler="direct-out=true" -split-input-file | FileCheck %s
// -----
More information about the llvm-commits
mailing list