This is an archive of the discontinued LLVM Phabricator instance.

[llvm-ar] Support an options string that start with a dash
ClosedPublic

Authored by mstorsjo on Nov 2 2017, 2:20 AM.

Details

Summary

Some projects call $AR like "$AR -crs output input1 input2".

This implementation isn't very pretty - any suggestions on a better way of handling it?

Diff Detail

Repository
rL LLVM

Event Timeline

mstorsjo created this revision.Nov 2 2017, 2:20 AM
mstorsjo added a reviewer: smeenai.
smeenai edited edge metadata.Nov 2 2017, 8:59 AM

Not sure of a better implementation strategy, but I am a fan of adding support for the dashed variants, since I've hit the issue as well in the past.

rnk accepted this revision.Nov 2 2017, 9:30 AM

lgtm

tools/llvm-ar/llvm-ar.cpp
894 ↗(On Diff #121256)

Let's break if argv[i] is "--". That way, users have a way of escaping positional arguments that happen to match this pattern.

This revision is now accepted and ready to land.Nov 2 2017, 9:30 AM
ruiu edited edge metadata.Nov 2 2017, 10:39 AM

Why don't you just strip the leading dash? I think the first argument is always flags (e.g. ar somesome.a doesn't make sense), so we could always handle ar -something as if ar something.

In D39538#914200, @ruiu wrote:

Why don't you just strip the leading dash? I think the first argument is always flags (e.g. ar somesome.a doesn't make sense), so we could always handle ar -something as if ar something.

The big issue is that llvm-ar has got options of its own that are dash/doubledash-prefixed, so you can have llvm-ar -format gnu [-]crs foo.a obj.o. We don't know what of these are options and what are "all the other args" until we run the command line parser.

ruiu added a comment.Nov 2 2017, 11:49 AM

Ah, makes sense. Thank you for explaining.

mstorsjo added inline comments.Nov 2 2017, 1:33 PM
tools/llvm-ar/llvm-ar.cpp
894 ↗(On Diff #121256)

Ok, will do. I'll also break once we strip a dash from some argument, since only one argument should have such a dash.

mstorsjo updated this revision to Diff 121361.Nov 2 2017, 1:35 PM

Break out of the loop on '--' and once an argument with a stripped dash is found.

ruiu added inline comments.Nov 3 2017, 12:41 PM
tools/llvm-ar/llvm-ar.cpp
892–893 ↗(On Diff #121361)

If you are going to use StringRef, then something like this is probably better than using stdlib's string functions.

StringRef S = argv[i];
if (S.startswith("-") && S.find_first_of("dmpqrtxabiosSTucv") == StringRef::npos)
mstorsjo added inline comments.Nov 3 2017, 12:47 PM
tools/llvm-ar/llvm-ar.cpp
892–893 ↗(On Diff #121361)

Ok, I can do something like that. Although I prefer to keep the definition of the chars string where I have it right now (close to the docs listing the option chars).

smeenai added inline comments.Nov 3 2017, 12:50 PM
tools/llvm-ar/llvm-ar.cpp
892–893 ↗(On Diff #121361)

Did you mean find_first_not_of?

ruiu added inline comments.Nov 3 2017, 12:51 PM
tools/llvm-ar/llvm-ar.cpp
892–893 ↗(On Diff #121361)

Ah, yes. Thank you for the correction.

This revision was automatically updated to reflect the committed changes.