[llvm] [Support] Accept -no-<name> to negate cl::opt<bool> (PR #224978)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 20:49:46 PDT 2026


================
@@ -221,13 +221,15 @@ literally into the content of the string variable... we obviously cannot do that
 in the boolean case, however, so we must use a smarter parser.  In the case of
 the boolean parser, it allows no options (in which case it assigns the value of
 true to the variable), or it allows the values "`true`" or "`false`" to be
-specified, allowing any of the following inputs:
+specified; a boolean option can also be negated with a `no-` prefix, allowing
+any of the following inputs:
----------------
MaskRay wrote:

Will do as part of the follow-up that adds this conflict detection:
```patch
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -1827,6 +1827,19 @@ bool CommandLineParser::ParseCommandLineOptions(
     default:
       break;
     }
+    // -no-<name> negates a boolean <name>, so no-<name> and <name> cannot both
+    // be registered when either is boolean.
+    StringRef Base = Opt.first;
+    if (Base.consume_front("no-")) {
+      auto It = OptionsMap.find(Base);
+      if (It != OptionsMap.end() &&
+          ((Opt.second->getMiscFlags() | It->second->getMiscFlags()) &
+           Negatable)) {
+        *Errs << ProgramName << ": CommandLine Error: Option '" << Opt.first
+              << "' conflicts with option '" << Base << "'!\n";
+        ErrorParsing = true;
+      }
+    }
```

https://github.com/llvm/llvm-project/pull/224978


More information about the llvm-commits mailing list