[llvm] [CommandLine] Set the 'ValueStr' with the selected argument (PR #90816)

Bill Wendling via llvm-commits llvm-commits at lists.llvm.org
Thu May 2 15:08:15 PDT 2024


https://github.com/bwendling updated https://github.com/llvm/llvm-project/pull/90816

>From 53791cd4be3e8e4b305a70e1a7b0760d912007b1 Mon Sep 17 00:00:00 2001
From: Bill Wendling <morbo at google.com>
Date: Wed, 1 May 2024 19:04:06 -0700
Subject: [PATCH 1/2] [CommandLine] Set the 'ValueStr' with the selected
 argument

Setting ValueStr allows us to query which option was selected.
---
 llvm/include/llvm/Support/CommandLine.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h
index b035209406b680..8acdd5b12efaec 100644
--- a/llvm/include/llvm/Support/CommandLine.h
+++ b/llvm/include/llvm/Support/CommandLine.h
@@ -865,6 +865,7 @@ template <class DataType> class parser : public generic_parser_base {
 
     for (size_t i = 0, e = Values.size(); i != e; ++i)
       if (Values[i].Name == ArgVal) {
+        O.setValueStr(Values[i].Name);
         V = Values[i].V.getValue();
         return false;
       }

>From 2404c1c71324da6ddebef52b048a415fd8f696fb Mon Sep 17 00:00:00 2001
From: Bill Wendling <morbo at google.com>
Date: Thu, 2 May 2024 15:07:32 -0700
Subject: [PATCH 2/2] Add unittest

---
 llvm/unittests/Support/CommandLineTest.cpp | 29 ++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index 23f6081cd32a45..2f8095efbdb4d5 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -2329,4 +2329,33 @@ TEST(CommandLineTest, HelpWithEmptyCategory) {
   cl::ResetCommandLineParser();
 }
 
+class ValueStrTestParser : public cl::parser<const char *> {
+public:
+  ValueStrTestParser(cl::Option &O) : cl::parser<const char *>(O) {}
+
+  void initialize() {
+    cl::parser<const char *>::initialize();
+
+    this->addLiteralOption("default", "", "");
+    this->addLiteralOption("first_option", "1", "");
+    this->addLiteralOption("other_option", "2", "");
+    this->addLiteralOption("expected_option", "3", "");
+    this->addLiteralOption("another_option", "4", "");
+  }
+};
+
+TEST(CommandLineTest, ValueStr) {
+  cl::ResetCommandLineParser();
+
+  cl::opt<const char *, false, ValueStrTestParser> Option("option",
+                                                          cl::init("default"));
+  const char *args[] = {"prog", "-option=expected_option"};
+
+  EXPECT_TRUE(cl::ParseCommandLineOptions(std::size(args), args, StringRef(),
+                                          &llvm::nulls()));
+
+  EXPECT_EQ(Option.ArgStr, "option");
+  EXPECT_EQ(Option.ValueStr, "expected_option");
+}
+
 } // anonymous namespace



More information about the llvm-commits mailing list