[clang] [llvm] [apinotes-test] Switch to OptTable (PR #246760)
Fangrui Song via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 27 13:54:25 PST 2026
https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/246760
Parse the command line with OptTable instead of cl::opt, as part of
removing cl::opt from clang/. Help output uses the OptTable format, and
LLVM's global options (e.g. -debug-only, --version) are no longer
accepted.
LLM-aided
>From efebc8e21af2ca650069fe21fe8c7cf61f337d79 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Fri, 27 Nov 2026 13:22:18 -0800
Subject: [PATCH] [apinotes-test] Switch to OptTable
Parse the command line with OptTable instead of cl::opt, as part of
removing cl::opt from clang/. Help output uses the OptTable format, and
LLVM's global options (e.g. -debug-only, --version) are no longer
accepted.
LLM-aided
---
clang/tools/apinotes-test/APINotesTest.cpp | 49 ++++++++++++++++---
clang/tools/apinotes-test/CMakeLists.txt | 9 ++++
clang/tools/apinotes-test/Opts.td | 6 +++
.../clang/tools/apinotes-test/BUILD.gn | 9 ++++
.../llvm-project-overlay/clang/BUILD.bazel | 11 +++++
5 files changed, 76 insertions(+), 8 deletions(-)
create mode 100644 clang/tools/apinotes-test/Opts.td
diff --git a/clang/tools/apinotes-test/APINotesTest.cpp b/clang/tools/apinotes-test/APINotesTest.cpp
index feafdde78a2fda..76e23971d0ed5c 100644
--- a/clang/tools/apinotes-test/APINotesTest.cpp
+++ b/clang/tools/apinotes-test/APINotesTest.cpp
@@ -7,30 +7,63 @@
//===----------------------------------------------------------------------===//
#include "clang/APINotes/APINotesYAMLCompiler.h"
-#include "llvm/Support/CommandLine.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/OptTable.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/WithColor.h"
-static llvm::cl::list<std::string> APINotes(llvm::cl::Positional,
- llvm::cl::desc("[<apinotes> ...]"),
- llvm::cl::Required);
+namespace {
+using namespace llvm::opt;
+enum ID {
+ OPT_INVALID = 0,
+#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
+#include "Opts.inc"
+#undef OPTION
+};
-static llvm::cl::opt<std::string>
- OutputFileName("o", llvm::cl::desc("output filename"),
- llvm::cl::value_desc("filename"), llvm::cl::init("-"));
+#define OPTTABLE_CODE
+#include "Opts.inc"
+
+class APINotesTestOptTable : public OptTable {
+public:
+ APINotesTestOptTable() : OptTable(optionTables()) {
+ setDashDashParsing(true);
+ }
+};
+} // namespace
int main(int argc, const char **argv) {
const bool DisableCrashReporting = true;
llvm::sys::PrintStackTraceOnErrorSignal(argv[0], DisableCrashReporting);
- llvm::cl::ParseCommandLineOptions(argc, argv);
auto Error = [](const llvm::Twine &Msg) {
llvm::WithColor::error(llvm::errs(), "apinotes-test") << Msg << '\n';
};
+ llvm::BumpPtrAllocator A;
+ llvm::StringSaver Saver(A);
+ APINotesTestOptTable Tbl;
+ llvm::opt::InputArgList Args =
+ Tbl.parseArgs(argc, const_cast<char **>(argv), OPT_UNKNOWN, Saver,
+ [&](llvm::StringRef Msg) {
+ Error(Msg);
+ exit(EXIT_FAILURE);
+ });
+ if (Args.hasArg(OPT_help)) {
+ Tbl.printHelp(llvm::outs(), "apinotes-test [options] <apinotes> ...",
+ "API notes testing tool");
+ return EXIT_SUCCESS;
+ }
+ std::vector<std::string> APINotes = Args.getAllArgValues(OPT_INPUT);
+ if (APINotes.empty()) {
+ Error("no input files");
+ return EXIT_FAILURE;
+ }
+ std::string OutputFileName = Args.getLastArgValue(OPT_o, "-").str();
+
std::error_code EC;
auto Out = std::make_unique<llvm::ToolOutputFile>(OutputFileName, EC,
llvm::sys::fs::OF_None);
diff --git a/clang/tools/apinotes-test/CMakeLists.txt b/clang/tools/apinotes-test/CMakeLists.txt
index 82c3b7bcb6483f..7260d2c43a3429 100644
--- a/clang/tools/apinotes-test/CMakeLists.txt
+++ b/clang/tools/apinotes-test/CMakeLists.txt
@@ -1,7 +1,16 @@
set(LLVM_LINK_COMPONENTS
+ Option
Support)
+
+set(LLVM_TARGET_DEFINITIONS Opts.td)
+llvm_tablegen(Opts.inc -gen-opt-parser-defs)
+add_public_tablegen_target(APINotesTestOptsTableGen)
+
add_clang_executable(apinotes-test
APINotesTest.cpp
+
+ DEPENDS
+ APINotesTestOptsTableGen
)
clang_target_link_libraries(apinotes-test PRIVATE
clangAPINotes)
diff --git a/clang/tools/apinotes-test/Opts.td b/clang/tools/apinotes-test/Opts.td
new file mode 100644
index 00000000000000..552e87b5462baa
--- /dev/null
+++ b/clang/tools/apinotes-test/Opts.td
@@ -0,0 +1,6 @@
+include "llvm/Option/OptParser.td"
+
+def help : Flag<["-", "--"], "help">, HelpText<"Display available options">;
+def o : Separate<["-", "--"], "o">, MetaVarName<"<filename>">,
+ HelpText<"Output filename">;
+def : Joined<["-", "--"], "o=">, Alias<o>;
diff --git a/llvm/utils/gn/secondary/clang/tools/apinotes-test/BUILD.gn b/llvm/utils/gn/secondary/clang/tools/apinotes-test/BUILD.gn
index 1b8dc5232566f2..e4b6692b22cc30 100644
--- a/llvm/utils/gn/secondary/clang/tools/apinotes-test/BUILD.gn
+++ b/llvm/utils/gn/secondary/clang/tools/apinotes-test/BUILD.gn
@@ -1,7 +1,16 @@
+import("//llvm/utils/TableGen/tablegen.gni")
+
+tablegen("Opts") {
+ visibility = [ ":apinotes-test" ]
+ args = [ "-gen-opt-parser-defs" ]
+}
+
executable("apinotes-test") {
configs += [ "//llvm/utils/gn/build:clang_code" ]
deps = [
+ ":Opts",
"//clang/lib/APINotes",
+ "//llvm/lib/Option",
"//llvm/lib/Support",
]
sources = [ "APINotesTest.cpp" ]
diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
index cb87c57044231e..805a66bac7ad6d 100644
--- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -2594,13 +2594,24 @@ cc_library(
],
)
+gentbl_cc_library(
+ name = "APINotesTestOptsTableGen",
+ strip_include_prefix = "tools/apinotes-test",
+ tbl_outs = {"tools/apinotes-test/Opts.inc": ["-gen-opt-parser-defs"]},
+ tblgen = "//llvm:llvm-tblgen",
+ td_file = "tools/apinotes-test/Opts.td",
+ deps = ["//llvm:OptParserTdFiles"],
+)
+
cc_binary(
name = "apinotes-test",
testonly = True,
srcs = ["tools/apinotes-test/APINotesTest.cpp"],
stamp = 0,
deps = [
+ ":APINotesTestOptsTableGen",
":apinotes",
+ "//llvm:Option",
"//llvm:Support",
],
)
More information about the cfe-commits
mailing list