[PATCH] D69501: [CommandLine] Add inline ArgName printing

Daan Sprenkels via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 28 09:28:25 PDT 2019


dsprenkels updated this revision to Diff 226685.
dsprenkels added a comment.

Added OptionErrorMessage test case in CommandLineTest for PR42943.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69501/new/

https://reviews.llvm.org/D69501

Files:
  llvm/lib/Support/CommandLine.cpp
  llvm/unittests/Support/CommandLineTest.cpp


Index: llvm/unittests/Support/CommandLineTest.cpp
===================================================================
--- llvm/unittests/Support/CommandLineTest.cpp
+++ llvm/unittests/Support/CommandLineTest.cpp
@@ -1653,4 +1653,32 @@
   EXPECT_TRUE(Errs.empty()); Errs.clear();
   cl::ResetAllOptionOccurrences();
 }
+
+TEST(CommandLineTest, OptionErrorMessage) {
+  // Test the fix for PR42943.
+  //
+  // When there is an error, we expect some error message like:
+  //   prog: for the -a option: [...]
+  //
+  // Test whether the "for the -a option"-part is correctly formatted.
+  cl::ResetCommandLineParser();
+
+  StackOption<bool> OptA("a", cl::desc("Some option"));
+  StackOption<bool> OptLong("long", cl::desc("Some long option"));
+
+  std::string Errs;
+  raw_string_ostream OS(Errs);
+
+  OptA.error("custom error", OS);
+  OS.flush();
+  EXPECT_FALSE(Errs.find("for the -a option:") == std::string::npos);
+  Errs.clear();
+
+  OptLong.error("custom error", OS);
+  OS.flush();
+  EXPECT_FALSE(Errs.find("for the --long option:") == std::string::npos);
+  Errs.clear();
+
+  cl::ResetAllOptionOccurrences();
+}
 }  // anonymous namespace
Index: llvm/lib/Support/CommandLine.cpp
===================================================================
--- llvm/lib/Support/CommandLine.cpp
+++ llvm/lib/Support/CommandLine.cpp
@@ -129,6 +129,19 @@
   return OS;
 }
 
+// Use for inline printing (omit leading spaces)
+class PrintArgInline {
+  StringRef ArgName;
+public:
+  PrintArgInline(StringRef ArgName) : ArgName(ArgName) {}
+  friend raw_ostream &operator<<(raw_ostream &OS, const PrintArgInline &);
+};
+
+raw_ostream &operator<<(raw_ostream &OS, const PrintArgInline &Arg) {
+  OS << argPrefix(Arg.ArgName).ltrim() << Arg.ArgName;
+  return OS;
+}
+
 class CommandLineParser {
 public:
   // Globals for name and overview of program.  Program name is not a string to
@@ -1447,7 +1460,7 @@
         if (NearestHandler) {
           // If we know a near match, report it as well.
           *Errs << ProgramName << ": Did you mean '"
-                << PrintArg(NearestHandlerString) << "'?\n";
+                << PrintArgInline(NearestHandlerString) << "'?\n";
         }
 
         ErrorParsing = true;
@@ -1601,7 +1614,7 @@
   if (ArgName.empty())
     Errs << HelpStr; // Be nice for positional arguments
   else
-    Errs << GlobalParser->ProgramName << ": for the " << PrintArg(ArgName);
+    Errs << GlobalParser->ProgramName << ": for the " << PrintArgInline(ArgName);
 
   Errs << " option: " << Message << "\n";
   return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69501.226685.patch
Type: text/x-patch
Size: 2557 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191028/9101c5cf/attachment.bin>


More information about the llvm-commits mailing list