[llvm] [readtapi] Use OptParser.td for options (PR #72183)

Cyndy Ishida via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 14 14:57:18 PST 2023


================
@@ -11,80 +11,132 @@
 //===----------------------------------------------------------------------===//
 #include "DiffEngine.h"
 #include "llvm/Object/TapiUniversal.h"
+#include "llvm/Option/Arg.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/Option.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/TextAPI/TextAPIError.h"
 #include <cstdlib>
 
 using namespace llvm;
 using namespace MachO;
 using namespace object;
 
 namespace {
-cl::OptionCategory TapiCat("llvm-readtapi options");
-cl::OptionCategory CompareCat("llvm-readtapi --compare options");
-
-cl::opt<std::string> InputFileName(cl::Positional, cl::desc("<tapi file>"),
-                                   cl::Required, cl::cat(TapiCat));
-cl::opt<std::string> CompareInputFileName(cl::Positional,
-                                          cl::desc("<comparison file>"),
-                                          cl::Required, cl::cat(CompareCat));
-enum OutputKind {
-  Compare,
+using namespace llvm::opt;
+enum ID {
+  OPT_INVALID = 0, // This is not an option ID.
+#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
+#include "Opts.inc"
+#undef OPTION
 };
 
-cl::opt<OutputKind>
-    Output(cl::desc("choose command action:"),
-           cl::values(clEnumValN(Compare, "compare",
-                                 "compare tapi file for library differences")),
-           cl::init(OutputKind::Compare), cl::cat(TapiCat));
-} // anonymous namespace
+#define PREFIX(NAME, VALUE)                                                    \
+  static constexpr StringLiteral NAME##_init[] = VALUE;                        \
+  static constexpr ArrayRef<StringLiteral> NAME(NAME##_init,                   \
+                                                std::size(NAME##_init) - 1);
+#include "Opts.inc"
+#undef PREFIX
+
+static constexpr opt::OptTable::Info InfoTable[] = {
+#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
+#include "Opts.inc"
+#undef OPTION
+};
+
+class TAPIOptTable : public opt::GenericOptTable {
+public:
+  TAPIOptTable() : opt::GenericOptTable(InfoTable) {
+    setGroupedShortOptions(true);
+  }
+};
+
+struct Context {
+  std::vector<std::string> Inputs;
+  std::unique_ptr<llvm::raw_fd_stream> OutStream;
+};
 
-Expected<std::unique_ptr<Binary>> convertFileToBinary(std::string &Filename) {
+Expected<std::unique_ptr<Binary>>
+convertFileToBinary(const StringRef Filename) {
   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
       MemoryBuffer::getFileOrSTDIN(Filename);
   if (BufferOrErr.getError())
     return errorCodeToError(BufferOrErr.getError());
   return createBinary(BufferOrErr.get()->getMemBufferRef());
 }
 
-int main(int Argc, char **Argv) {
-  InitLLVM X(Argc, Argv);
-  cl::HideUnrelatedOptions(TapiCat);
-  cl::ParseCommandLineOptions(Argc, Argv,
-                              "TAPI File Reader and Manipulator Tool");
-
-  if (Output == OutputKind::Compare) {
-    if (InputFileName.empty() || CompareInputFileName.empty()) {
-      cl::PrintHelpMessage();
-      return EXIT_FAILURE;
-    }
+const int NON_TAPI_EXIT_CODE = 2;
----------------
cyndyishida wrote:

`readtapi --compare` behaves like `diff` where exit codes differentiate between the same/different/other kind of error (e.g. missing file) 
I can add a comment to make that clearer.  

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


More information about the llvm-commits mailing list