https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/246765
>From bb05b56d122312d9389e01c8c4e72bcf385989bb Mon Sep 17 00:00:00 2001 From: Fangrui Song <[email protected]> Date: Fri, 27 Nov 2026 13:37:18 -0800 Subject: [PATCH 1/2] [c-index-test] Switch to OptTable Parse the `c-index-test core` 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. `c-index-test core -help` now also lists the options that cl::HideUnrelatedOptions hid. LLM-aided --- clang/tools/c-index-test/CMakeLists.txt | 8 ++ clang/tools/c-index-test/Opts.td | 16 +++ clang/tools/c-index-test/core_main.cpp | 98 +++++++++---------- .../clang/tools/c-index-test/BUILD.gn | 9 ++ .../llvm-project-overlay/clang/BUILD.bazel | 11 +++ 5 files changed, 89 insertions(+), 53 deletions(-) create mode 100644 clang/tools/c-index-test/Opts.td diff --git a/clang/tools/c-index-test/CMakeLists.txt b/clang/tools/c-index-test/CMakeLists.txt index 08cbe77bf6e21f..d716cf906a89a7 100644 --- a/clang/tools/c-index-test/CMakeLists.txt +++ b/clang/tools/c-index-test/CMakeLists.txt @@ -1,10 +1,18 @@ set(LLVM_LINK_COMPONENTS + Option support ) +set(LLVM_TARGET_DEFINITIONS Opts.td) +llvm_tablegen(Opts.inc -gen-opt-parser-defs) +add_public_tablegen_target(CIndexTestOptsTableGen) + add_clang_executable(c-index-test c-index-test.c core_main.cpp + + DEPENDS + CIndexTestOptsTableGen ) if(NOT MSVC) diff --git a/clang/tools/c-index-test/Opts.td b/clang/tools/c-index-test/Opts.td new file mode 100644 index 00000000000000..9d583c9536d41d --- /dev/null +++ b/clang/tools/c-index-test/Opts.td @@ -0,0 +1,16 @@ +include "llvm/Option/OptParser.td" + +class F<string name, string help> : Flag<["-", "--"], name>, HelpText<help>; + +multiclass Eq<string name, string help> { + def NAME : Separate<["-", "--"], name>, HelpText<help>; + def : Joined<["-", "--"], name #"=">, Alias<!cast<Separate>(NAME)>; +} + +def dump_imported_module_files : F<"dump-imported-module-files", "Print symbols and input files from imported modules">; +defm fmodule_format : Eq<"fmodule-format", "Container format for clang modules and PCH, 'raw' or 'obj'">, MetaVarName<"<format>">; +def help : F<"help", "Display available options">; +def ignore_macros : F<"ignore-macros", "Skip indexing macros">; +def include_locals : F<"include-locals", "Print local symbols">; +defm module_file : Eq<"module-file", "Path to module file to print symbols from">, MetaVarName<"<file>">; +def print_source_symbols : F<"print-source-symbols", "Print symbols from source">; diff --git a/clang/tools/c-index-test/core_main.cpp b/clang/tools/c-index-test/core_main.cpp index 28a41ba7491f6b..84f058f1bb07fe 100644 --- a/clang/tools/c-index-test/core_main.cpp +++ b/clang/tools/c-index-test/core_main.cpp @@ -20,6 +20,8 @@ #include "clang/Serialization/ASTReader.h" #include "clang/Serialization/ObjectFilePCHContainerReader.h" #include "clang/UnifiedSymbolResolution/USRGeneration.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/OptTable.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/PrettyStackTrace.h" @@ -38,45 +40,21 @@ extern "C" int indextest_perform_shell_execution(const char *command_line); namespace { -enum class ActionType { - None, - PrintSourceSymbols, +using namespace llvm::opt; +enum ID { + OPT_INVALID = 0, +#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__), +#include "Opts.inc" +#undef OPTION }; -namespace options { +#define OPTTABLE_CODE +#include "Opts.inc" -static cl::OptionCategory IndexTestCoreCategory("index-test-core options"); - -static cl::opt<ActionType> -Action(cl::desc("Action:"), cl::init(ActionType::None), - cl::values( - clEnumValN(ActionType::PrintSourceSymbols, - "print-source-symbols", "Print symbols from source")), - cl::cat(IndexTestCoreCategory)); - -static cl::extrahelp MoreHelp( - "\nAdd \"-- <compiler arguments>\" at the end to setup the compiler " - "invocation\n" -); - -static cl::opt<bool> -DumpModuleImports("dump-imported-module-files", - cl::desc("Print symbols and input files from imported modules")); - -static cl::opt<bool> -IncludeLocals("include-locals", cl::desc("Print local symbols")); - -static cl::opt<bool> IgnoreMacros("ignore-macros", - cl::desc("Skip indexing macros")); - -static cl::opt<std::string> -ModuleFilePath("module-file", - cl::desc("Path to module file to print symbols from")); -static cl::opt<std::string> - ModuleFormat("fmodule-format", cl::init("raw"), - cl::desc("Container format for clang modules and PCH, 'raw' or 'obj'")); - -} +class IndexTestCoreOptTable : public OptTable { +public: + IndexTestCoreOptTable() : OptTable(optionTables()) {} +}; } // anonymous namespace static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS); @@ -362,29 +340,43 @@ int indextest_core_main(int argc, const char **argv) { argc = DoubleDash - argv; } - cl::HideUnrelatedOptions(options::IndexTestCoreCategory); - cl::ParseCommandLineOptions(argc, argv, "index-test-core"); + BumpPtrAllocator Alloc; + StringSaver Saver(Alloc); + IndexTestCoreOptTable Tbl; + opt::InputArgList Args = Tbl.parseArgs(argc, const_cast<char **>(argv), + OPT_UNKNOWN, Saver, [](StringRef Msg) { + errs() << "error: " << Msg << '\n'; + exit(1); + }); + if (Args.hasArg(OPT_help)) { + Tbl.printHelp(outs(), "c-index-test core [options]", "index-test-core"); + outs() << "\nAdd \"-- <compiler arguments>\" at the end to setup the " + "compiler invocation\n"; + return 0; + } + if (const opt::Arg *A = Args.getLastArg(OPT_INPUT)) { + errs() << "error: unexpected positional argument '" << A->getAsString(Args) + << "'\n"; + return 1; + } - if (options::Action == ActionType::None) { + if (!Args.hasArg(OPT_print_source_symbols)) { errs() << "error: action required; pass '-help' for options\n"; return 1; } - if (options::Action == ActionType::PrintSourceSymbols) { - if (!options::ModuleFilePath.empty()) { - return printSourceSymbolsFromModule(options::ModuleFilePath, - options::ModuleFormat); - } - if (CompArgs.empty()) { - errs() << "error: missing compiler args; pass '-- <compiler arguments>'\n"; - return 1; - } - return printSourceSymbols(Executable.c_str(), CompArgs, - options::DumpModuleImports, - options::IncludeLocals, options::IgnoreMacros); + std::string ModuleFilePath = Args.getLastArgValue(OPT_module_file).str(); + if (!ModuleFilePath.empty()) { + return printSourceSymbolsFromModule( + ModuleFilePath, Args.getLastArgValue(OPT_fmodule_format, "raw")); } - - return 0; + if (CompArgs.empty()) { + errs() << "error: missing compiler args; pass '-- <compiler arguments>'\n"; + return 1; + } + return printSourceSymbols( + Executable.c_str(), CompArgs, Args.hasArg(OPT_dump_imported_module_files), + Args.hasArg(OPT_include_locals), Args.hasArg(OPT_ignore_macros)); } //===----------------------------------------------------------------------===// diff --git a/llvm/utils/gn/secondary/clang/tools/c-index-test/BUILD.gn b/llvm/utils/gn/secondary/clang/tools/c-index-test/BUILD.gn index 0ee16a99993780..b9a96fd5ad21a0 100644 --- a/llvm/utils/gn/secondary/clang/tools/c-index-test/BUILD.gn +++ b/llvm/utils/gn/secondary/clang/tools/c-index-test/BUILD.gn @@ -1,6 +1,14 @@ +import("//llvm/utils/TableGen/tablegen.gni") + +tablegen("Opts") { + visibility = [ ":c-index-test" ] + args = [ "-gen-opt-parser-defs" ] +} + executable("c-index-test") { configs += [ "//llvm/utils/gn/build:clang_code" ] deps = [ + ":Opts", "//clang/include/clang/Config", "//clang/lib/AST", "//clang/lib/Basic", @@ -11,6 +19,7 @@ executable("c-index-test") { "//clang/lib/Serialization", "//clang/lib/UnifiedSymbolResolution", "//clang/tools/libclang", + "//llvm/lib/Option", "//llvm/lib/Support", "//llvm/utils/gn/build/libs/xml", ] diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel index cb87c57044231e..b3f19f4e577d2a 100644 --- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel @@ -2544,6 +2544,15 @@ filegroup( ], ) +gentbl_cc_library( + name = "CIndexTestOptsTableGen", + strip_include_prefix = "tools/c-index-test", + tbl_outs = {"tools/c-index-test/Opts.inc": ["-gen-opt-parser-defs"]}, + tblgen = "//llvm:llvm-tblgen", + td_file = "tools/c-index-test/Opts.td", + deps = ["//llvm:OptParserTdFiles"], +) + cc_binary( name = "c-index-test", testonly = True, @@ -2560,6 +2569,7 @@ cc_binary( }), stamp = 0, deps = [ + ":CIndexTestOptsTableGen", ":ast", ":basic", ":codegen", @@ -2573,6 +2583,7 @@ cc_binary( ":unified_symbol_resolution", "//llvm:Core", "//llvm:MC", + "//llvm:Option", "//llvm:Support", "//third-party:libxml2", ], >From ba8336b070d625d90d32a747ce82f9e425afc692 Mon Sep 17 00:00:00 2001 From: Fangrui Song <[email protected]> Date: Sun, 29 Nov 2026 10:33:16 -0800 Subject: [PATCH 2/2] Rebase; parse into an options struct (#246881) --- clang/tools/c-index-test/Opts.td | 23 ++++------ clang/tools/c-index-test/core_main.cpp | 60 +++++++++----------------- 2 files changed, 29 insertions(+), 54 deletions(-) diff --git a/clang/tools/c-index-test/Opts.td b/clang/tools/c-index-test/Opts.td index 9d583c9536d41d..047ae3a983a12e 100644 --- a/clang/tools/c-index-test/Opts.td +++ b/clang/tools/c-index-test/Opts.td @@ -1,16 +1,11 @@ -include "llvm/Option/OptParser.td" +include "llvm/Option/LibraryOptions.td" -class F<string name, string help> : Flag<["-", "--"], name>, HelpText<help>; +def IndexTestCoreOptions : OptionsStruct<"">; -multiclass Eq<string name, string help> { - def NAME : Separate<["-", "--"], name>, HelpText<help>; - def : Joined<["-", "--"], name #"=">, Alias<!cast<Separate>(NAME)>; -} - -def dump_imported_module_files : F<"dump-imported-module-files", "Print symbols and input files from imported modules">; -defm fmodule_format : Eq<"fmodule-format", "Container format for clang modules and PCH, 'raw' or 'obj'">, MetaVarName<"<format>">; -def help : F<"help", "Display available options">; -def ignore_macros : F<"ignore-macros", "Skip indexing macros">; -def include_locals : F<"include-locals", "Print local symbols">; -defm module_file : Eq<"module-file", "Path to module file to print symbols from">, MetaVarName<"<file>">; -def print_source_symbols : F<"print-source-symbols", "Print symbols from source">; +defm : BoolField<"dump-imported-module-files", "false", "Print symbols and input files from imported modules">; +defm : ValueField<"fmodule-format", "StringRef", "\"raw\"", "Container format for clang modules and PCH, 'raw' or 'obj'">, MetaVarName<"<format>">; +defm : BoolField<"help", "false", "Display available options">; +defm : BoolField<"ignore-macros", "false", "Skip indexing macros">; +defm : BoolField<"include-locals", "false", "Print local symbols">; +defm : ValueField<"module-file", "StringRef", "", "Path to module file to print symbols from">, MetaVarName<"<file>">; +defm : BoolField<"print-source-symbols", "false", "Print symbols from source">; diff --git a/clang/tools/c-index-test/core_main.cpp b/clang/tools/c-index-test/core_main.cpp index 84f058f1bb07fe..1c71b7db8b33ff 100644 --- a/clang/tools/c-index-test/core_main.cpp +++ b/clang/tools/c-index-test/core_main.cpp @@ -20,8 +20,7 @@ #include "clang/Serialization/ASTReader.h" #include "clang/Serialization/ObjectFilePCHContainerReader.h" #include "clang/UnifiedSymbolResolution/USRGeneration.h" -#include "llvm/Option/ArgList.h" -#include "llvm/Option/OptTable.h" +#include "llvm/Option/OptionsStruct.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/PrettyStackTrace.h" @@ -38,25 +37,10 @@ using namespace llvm; extern "C" int indextest_core_main(int argc, const char **argv); extern "C" int indextest_perform_shell_execution(const char *command_line); -namespace { - -using namespace llvm::opt; -enum ID { - OPT_INVALID = 0, -#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__), -#include "Opts.inc" -#undef OPTION -}; - -#define OPTTABLE_CODE +#define OPTIONS_STRUCT_DECL +#define OPTIONS_STRUCT_DEFS #include "Opts.inc" -class IndexTestCoreOptTable : public OptTable { -public: - IndexTestCoreOptTable() : OptTable(optionTables()) {} -}; -} // anonymous namespace - static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS); static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx, raw_ostream &OS); @@ -341,42 +325,38 @@ int indextest_core_main(int argc, const char **argv) { } BumpPtrAllocator Alloc; - StringSaver Saver(Alloc); - IndexTestCoreOptTable Tbl; - opt::InputArgList Args = Tbl.parseArgs(argc, const_cast<char **>(argv), - OPT_UNKNOWN, Saver, [](StringRef Msg) { - errs() << "error: " << Msg << '\n'; - exit(1); - }); - if (Args.hasArg(OPT_help)) { - Tbl.printHelp(outs(), "c-index-test core [options]", "index-test-core"); + IndexTestCoreOptions Opts; + SmallVector<StringRef, 0> Inputs; + if (Error E = opt::parseToolArgs(Opts, argc, argv, Alloc, Inputs)) { + errs() << "error: " << toString(std::move(E)) << '\n'; + return 1; + } + if (Opts.help) { + IndexTestCoreOptions::optTable().printHelp( + outs(), "c-index-test core [options]", "index-test-core"); outs() << "\nAdd \"-- <compiler arguments>\" at the end to setup the " "compiler invocation\n"; return 0; } - if (const opt::Arg *A = Args.getLastArg(OPT_INPUT)) { - errs() << "error: unexpected positional argument '" << A->getAsString(Args) - << "'\n"; + if (!Inputs.empty()) { + errs() << "error: unexpected positional argument '" << Inputs[0] << "'\n"; return 1; } - if (!Args.hasArg(OPT_print_source_symbols)) { + if (!Opts.print_source_symbols) { errs() << "error: action required; pass '-help' for options\n"; return 1; } - std::string ModuleFilePath = Args.getLastArgValue(OPT_module_file).str(); - if (!ModuleFilePath.empty()) { - return printSourceSymbolsFromModule( - ModuleFilePath, Args.getLastArgValue(OPT_fmodule_format, "raw")); - } + if (!Opts.module_file.empty()) + return printSourceSymbolsFromModule(Opts.module_file, Opts.fmodule_format); if (CompArgs.empty()) { errs() << "error: missing compiler args; pass '-- <compiler arguments>'\n"; return 1; } - return printSourceSymbols( - Executable.c_str(), CompArgs, Args.hasArg(OPT_dump_imported_module_files), - Args.hasArg(OPT_include_locals), Args.hasArg(OPT_ignore_macros)); + return printSourceSymbols(Executable.c_str(), CompArgs, + Opts.dump_imported_module_files, + Opts.include_locals, Opts.ignore_macros); } //===----------------------------------------------------------------------===// _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
