This is an archive of the discontinued LLVM Phabricator instance.

[Option] Support special argument "--"
ClosedPublic

Authored by MaskRay on Jun 6 2023, 9:43 AM.

Details

Summary

Many command line option implementations, including getopt_long and our
Support/CommandLine.cpp, support -- as an end-of-option indicator. All
the subsequent arguments are then treated as positional arguments.

D1387 added KIND_REMAINING_ARGS and 76ff1d915c9c42823a3f2b08ff936cf7a48933c5 dropped special handling of --.
Users need to add def DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>; and
append OPT_DASH_DASH to the OPT_INPUT list., which is not ergonomic.

Restore this feature under an option and modify llvm-strings to utilize the
feature as an example. In the future, we probably should enable this feature by
default and exclude some tools that handle DASH_DASH differently (clang,
clang-scan-deps, etc. I suspect that many are workarounds for LLVMOption not
supporting -- as a built-in feature).

Diff Detail

Event Timeline

MaskRay created this revision.Jun 6 2023, 9:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2023, 9:43 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
MaskRay requested review of this revision.Jun 6 2023, 9:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2023, 9:43 AM
MaskRay updated this revision to Diff 528916.Jun 6 2023, 10:01 AM

remove untested DashDash from Opts.td. It would make -- without DashDashParsing mode an option.

MaskRay added a subscriber: hans.Jun 6 2023, 10:05 AM

(CC @hans Just noticed that you have D1387 which allows us to remove the hard-coded handling of "--".
I don't find DASH_DASH in modern lld, but Clang does have a special case.)

MaskRay edited the summary of this revision. (Show Details)Jun 6 2023, 10:11 AM
MaskRay added a reviewer: hans.

Big fan of this change. I added a few potential minor suggestions.
We may want to also modify the generated help to suggest the special meaning of --?

llvm/lib/Option/OptTable.cpp
475

Probably pedantic, but the other part of the code use `std::make_unique<Arg>(...) instead which at least convey the ownership very clearly.

llvm/unittests/Option/OptionParsingTest.cpp
418

Would be great to have a test without any trailing argument after the -- too.

This revision is now accepted and ready to land.Jun 6 2023, 11:47 AM
MaskRay updated this revision to Diff 528997.Jun 6 2023, 12:50 PM
MaskRay marked an inline comment as done.

improve test

MaskRay marked an inline comment as done.Jun 6 2023, 12:57 PM
MaskRay added inline comments.
llvm/lib/Option/OptTable.cpp
475

The primary code path has Args.append(A.release());. I think it's better to use new than make_unique + release.

MaskRay edited the summary of this revision. (Show Details)Jun 6 2023, 1:00 PM
llvm/lib/Option/OptTable.cpp
475

Missed that. Thanks o/

hans added a comment.Jun 7 2023, 1:45 AM

(CC @hans Just noticed that you have D1387 which allows us to remove the hard-coded handling of "--".
I don't find DASH_DASH in modern lld, but Clang does have a special case.)

Thanks! The motivation for introducing KIND_REMAINING_ARGS was to support the /link flag. I think at the time I figured it was more elegant that -- could now be implemented with the regular option parsing machinery rather than hard-coding, but as you say since most option parsing libraries support --, maybe it makes sense to put the hard-coded behavior back.

Couple of nits from me. I've got no objections to this change, but am a bit rusty on the code, so won't approve myself.

llvm/include/llvm/Option/OptTable.h
144
llvm/lib/Option/OptTable.cpp
471–472

Either "marks all trailing..." or "arguments positional" (but not both).

MaskRay updated this revision to Diff 529302.Jun 7 2023, 7:52 AM
MaskRay marked 3 inline comments as done.

improve comments

MaskRay edited the summary of this revision. (Show Details)Jun 7 2023, 7:52 AM
This revision was landed with ongoing or failed builds.Jun 7 2023, 8:06 AM
This revision was automatically updated to reflect the committed changes.