Author: Fangrui Song Date: 2026-11-28T00:59:29-08:00 New Revision: a6321f2d019cc9563b2f552e486b7550d3ec51ce
URL: https://github.com/llvm/llvm-project/commit/a6321f2d019cc9563b2f552e486b7550d3ec51ce DIFF: https://github.com/llvm/llvm-project/commit/a6321f2d019cc9563b2f552e486b7550d3ec51ce.diff LOG: [clang-diff] Switch to OptTable (#246766) 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 Added: clang/tools/clang-diff/Opts.td Modified: clang/tools/clang-diff/CMakeLists.txt clang/tools/clang-diff/ClangDiff.cpp llvm/utils/gn/secondary/clang/tools/clang-diff/BUILD.gn utils/bazel/llvm-project-overlay/clang/BUILD.bazel Removed: ################################################################################ diff --git a/clang/tools/clang- diff /CMakeLists.txt b/clang/tools/clang- diff /CMakeLists.txt index 41f61fe968f3f6..d366462fd37df7 100644 --- a/clang/tools/clang- diff /CMakeLists.txt +++ b/clang/tools/clang- diff /CMakeLists.txt @@ -1,9 +1,17 @@ set(LLVM_LINK_COMPONENTS + Option Support ) +set(LLVM_TARGET_DEFINITIONS Opts.td) +llvm_tablegen(Opts.inc -gen-opt-parser-defs) +add_public_tablegen_target(ClangDiffOptsTableGen) + add_clang_executable(clang- diff ClangDiff.cpp + + DEPENDS + ClangDiffOptsTableGen ) clang_target_link_libraries(clang- diff diff --git a/clang/tools/clang- diff /ClangDiff.cpp b/clang/tools/clang- diff /ClangDiff.cpp index 1e1e8fdbb05031..1cee8f62cd9f5d 100644 --- a/clang/tools/clang- diff /ClangDiff.cpp +++ b/clang/tools/clang- diff /ClangDiff.cpp @@ -14,60 +14,34 @@ #include "clang/Tooling/ASTDiff/ASTDiff.h" #include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Tooling.h" -#include "llvm/Support/CommandLine.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/OptTable.h" using namespace llvm; using namespace clang; using namespace clang::tooling; -static cl::OptionCategory ClangDiffCategory("clang- diff options"); +namespace { +using namespace llvm::opt; +enum ID { + OPT_INVALID = 0, +#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__), +#include "Opts.inc" +#undef OPTION +}; -static cl::opt<bool> - ASTDump("ast-dump", - cl::desc("Print the internal representation of the AST."), - cl::init(false), cl::cat(ClangDiffCategory)); +#define OPTTABLE_CODE +#include "Opts.inc" -static cl::opt<bool> ASTDumpJson( - "ast-dump-json", - cl::desc("Print the internal representation of the AST as JSON."), - cl::init(false), cl::cat(ClangDiffCategory)); +class ClangDiffOptTable : public OptTable { +public: + ClangDiffOptTable() : OptTable(optionTables()) {} +}; +} // namespace -static cl::opt<bool> PrintMatches("dump-matches", - cl::desc("Print the matched nodes."), - cl::init(false), cl::cat(ClangDiffCategory)); - -static cl::opt<bool> HtmlDiff("html", - cl::desc("Output a side-by-side diff in HTML."), - cl::init(false), cl::cat(ClangDiffCategory)); - -static cl::opt<std::string> SourcePath(cl::Positional, cl::desc("<source>"), - cl::Required, - cl::cat(ClangDiffCategory)); - -static cl::opt<std::string> DestinationPath(cl::Positional, - cl::desc("<destination>"), - cl::Optional, - cl::cat(ClangDiffCategory)); - -static cl::opt<std::string> StopAfter("stop- diff -after", - cl::desc("<topdown|bottomup>"), - cl::init(""), cl::cat(ClangDiffCategory)); - -static cl::opt<int> MaxSize("s", cl::desc("<maxsize>"), cl::init(-1), - cl::cat(ClangDiffCategory)); - -static cl::opt<std::string> BuildPath("p", cl::desc("Build path"), cl::init(""), - cl::cat(ClangDiffCategory)); - -static cl::list<std::string> ArgsAfter( - "extra-arg", - cl::desc("Additional argument to append to the compiler command line"), - cl::cat(ClangDiffCategory)); - -static cl::list<std::string> ArgsBefore( - "extra-arg-before", - cl::desc("Additional argument to prepend to the compiler command line"), - cl::cat(ClangDiffCategory)); +static std::string BuildPath; +static std::vector<std::string> ArgsAfter; +static std::vector<std::string> ArgsBefore; static void addExtraArgs(std::unique_ptr<CompilationDatabase> &Compilations) { if (!Compilations) @@ -445,11 +419,42 @@ int main(int argc, const char **argv) { FixedCompilationDatabase::loadFromCommandLine(argc, argv, ErrorMessage); if (!CommonCompilations && !ErrorMessage.empty()) llvm::errs() << ErrorMessage; - cl::HideUnrelatedOptions(ClangDiffCategory); - if (!cl::ParseCommandLineOptions(argc, argv)) { - cl::PrintOptionValues(); + BumpPtrAllocator Alloc; + StringSaver Saver(Alloc); + ClangDiffOptTable Tbl; + opt::InputArgList Args = Tbl.parseArgs( + argc, const_cast<char **>(argv), OPT_UNKNOWN, Saver, [](StringRef Msg) { + llvm::errs() << "Error: " << Msg << "\n"; + exit(1); + }); + if (Args.hasArg(OPT_help)) { + Tbl.printHelp(llvm::outs(), "clang- diff [options] <source> [<destination>]", + "Compare source files by AST nodes"); + return 0; + } + std::vector<std::string> Inputs = Args.getAllArgValues(OPT_INPUT); + if (Inputs.empty() || Inputs.size() > 2) { + llvm::errs() << "Error: Expected one or two paths.\n"; return 1; } + std::string SourcePath = Inputs[0]; + std::string DestinationPath = Inputs.size() > 1 ? Inputs[1] : ""; + bool ASTDump = Args.hasArg(OPT_ast_dump); + bool ASTDumpJson = Args.hasArg(OPT_ast_dump_json); + bool PrintMatches = Args.hasArg(OPT_dump_matches); + bool HtmlDiff = Args.hasArg(OPT_html); + std::string StopAfter = Args.getLastArgValue(OPT_stop_ diff _after).str(); + int MaxSize = -1; + if (const opt::Arg *A = Args.getLastArg(OPT_s)) { + if (StringRef(A->getValue()).getAsInteger(0, MaxSize)) { + llvm::errs() << "Error: Invalid argument '" << A->getValue() + << "' for -s\n"; + return 1; + } + } + BuildPath = Args.getLastArgValue(OPT_p).str(); + ArgsAfter = Args.getAllArgValues(OPT_extra_arg); + ArgsBefore = Args.getAllArgValues(OPT_extra_arg_before); addExtraArgs(CommonCompilations); diff --git a/clang/tools/clang- diff /Opts.td b/clang/tools/clang- diff /Opts.td new file mode 100644 index 00000000000000..c0a7d108889c49 --- /dev/null +++ b/clang/tools/clang- diff /Opts.td @@ -0,0 +1,19 @@ +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 ast_dump : F<"ast-dump", "Print the internal representation of the AST.">; +def ast_dump_json : F<"ast-dump-json", "Print the internal representation of the AST as JSON.">; +def dump_matches : F<"dump-matches", "Print the matched nodes.">; +defm extra_arg : Eq<"extra-arg", "Additional argument to append to the compiler command line">, MetaVarName<"<arg>">; +defm extra_arg_before : Eq<"extra-arg-before", "Additional argument to prepend to the compiler command line">, MetaVarName<"<arg>">; +def help : F<"help", "Display available options">; +def html : F<"html", "Output a side-by-side diff in HTML.">; +defm p : Eq<"p", "Build path">, MetaVarName<"<build-path>">; +defm s : Eq<"s", "Maximum subtree size for the optimal bottom-up mapping">, MetaVarName<"<maxsize>">; +defm stop_ diff _after : Eq<"stop- diff -after", "Stop after the given matching phase">, MetaVarName<"<topdown|bottomup>">; diff --git a/llvm/utils/gn/secondary/clang/tools/clang- diff /BUILD.gn b/llvm/utils/gn/secondary/clang/tools/clang- diff /BUILD.gn index cbf40be649f3db..64d5954f646451 100644 --- a/llvm/utils/gn/secondary/clang/tools/clang- diff /BUILD.gn +++ b/llvm/utils/gn/secondary/clang/tools/clang- diff /BUILD.gn @@ -1,10 +1,19 @@ +import("//llvm/utils/TableGen/tablegen.gni") + +tablegen("Opts") { + visibility = [ ":clang- diff " ] + args = [ "-gen-opt-parser-defs" ] +} + executable("clang- diff ") { configs += [ "//llvm/utils/gn/build:clang_code" ] deps = [ + ":Opts", "//clang/lib/Basic", "//clang/lib/Frontend", "//clang/lib/Tooling", "//clang/lib/Tooling/ASTDiff", + "//llvm/lib/Option", "//llvm/lib/Support", ] sources = [ "ClangDiff.cpp" ] diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel index 805a66bac7ad6d..2c5d0fec0828d1 100644 --- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel @@ -2768,13 +2768,24 @@ cc_binary( ], ) +gentbl_cc_library( + name = "ClangDiffOptsTableGen", + strip_include_prefix = "tools/clang- diff ", + tbl_outs = {"tools/clang- diff /Opts.inc": ["-gen-opt-parser-defs"]}, + tblgen = "//llvm:llvm-tblgen", + td_file = "tools/clang- diff /Opts.td", + deps = ["//llvm:OptParserTdFiles"], +) + cc_binary( name = "clang- diff ", srcs = glob(["tools/clang- diff /*.cpp"]), stamp = 0, deps = [ + ":ClangDiffOptsTableGen", ":ast- diff ", ":tooling", + "//llvm:Option", "//llvm:Support", ], ) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
