[PATCH] D59746: [LibTooling] Fix '-h' option

Don Hinton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 24 01:10:27 PDT 2019


hintonda created this revision.
hintonda added a reviewer: alexfh.
Herald added a project: clang.

LibTooling's '-h' option was intended to be as an alias for
'-help', but since the '-help' option is declared as a function scoped
static, it isn't visible, leaving it to individual clients to handle
'-h' -- which none seem to do.

This fix scans argv and expands '-h' to '-help' so that llvm's
CommandLineParser can handle it appropriately.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D59746

Files:
  clang/lib/Tooling/CommonOptionsParser.cpp


Index: clang/lib/Tooling/CommonOptionsParser.cpp
===================================================================
--- clang/lib/Tooling/CommonOptionsParser.cpp
+++ clang/lib/Tooling/CommonOptionsParser.cpp
@@ -114,8 +114,24 @@
   if (!ErrorMessage.empty())
     ErrorMessage.append("\n");
   llvm::raw_string_ostream OS(ErrorMessage);
+
+  // Expand "-h" to "-help" so it's static handler can be used.
+  SmallVector<const char *, 20> NewArgv;
+  // Append options from command line.
+  bool SawDoubleDash = false;
+  for (int I = 0; I < argc; ++I) {
+    StringRef Arg(argv[I]);
+    if (Arg == "--")
+      SawDoubleDash = true;
+    if(!SawDoubleDash && Arg == "-h")
+      NewArgv.push_back("-help");
+    else
+      NewArgv.push_back(argv[I]);
+  }
+  int NewArgc = static_cast<int>(NewArgv.size());
+
   // Stop initializing if command-line option parsing failed.
-  if (!cl::ParseCommandLineOptions(argc, argv, Overview, &OS)) {
+  if (!cl::ParseCommandLineOptions(NewArgc, &NewArgv[0], Overview, &OS)) {
     OS.flush();
     return llvm::make_error<llvm::StringError>("[CommonOptionsParser]: " +
                                                    ErrorMessage,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59746.192025.patch
Type: text/x-patch
Size: 1175 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190324/94533d67/attachment.bin>


More information about the cfe-commits mailing list