[PATCH] D39538: [llvm-ar] Support an options string that start with a dash

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 2 09:38:15 PDT 2017


LGTM, we can start with that and try to improve it afterwards.

This is pr21712.

Cheers,
Rafael

Martin Storsjö via Phabricator <reviews at reviews.llvm.org> writes:

> mstorsjo created this revision.
>
> 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?
>
>
> https://reviews.llvm.org/D39538
>
> Files:
>   test/tools/llvm-ar/default-add.test
>   tools/llvm-ar/llvm-ar.cpp
>
>
> Index: tools/llvm-ar/llvm-ar.cpp
> ===================================================================
> --- tools/llvm-ar/llvm-ar.cpp
> +++ tools/llvm-ar/llvm-ar.cpp
> @@ -127,6 +127,8 @@
>    "  [v] - be verbose about actions taken\n"
>  );
>  
> +static const char OptionChars[] = "dmpqrtxabiosSTucv";
> +
>  // This enumeration delineates the kinds of operations on an archive
>  // that are permitted.
>  enum ArchiveOperation {
> @@ -879,6 +881,19 @@
>        Stem.find("lib") != StringRef::npos)
>      return libDriverMain(makeArrayRef(argv, argc));
>  
> +  for (int i = 1; i < argc; i++) {
> +    // If an argument starts with a dash and only contains chars
> +    // that belong to the options chars set, remove the dash.
> +    // We can't handle it after the command line options parsing
> +    // is done, since it will error out on an unrecognized string
> +    // starting with a dash.
> +    // Make sure this doesn't match the actual llvm-ar specific options
> +    // that start with a dash.
> +    if (argv[i][0] == '-' &&
> +        strspn(&argv[i][1], OptionChars) + 1 == strlen(argv[i]))
> +      argv[i]++;
> +  }
> +
>    // Have the command line options parsed and handle things
>    // like --help and --version.
>    cl::ParseCommandLineOptions(argc, argv,
> Index: test/tools/llvm-ar/default-add.test
> ===================================================================
> --- test/tools/llvm-ar/default-add.test
> +++ test/tools/llvm-ar/default-add.test
> @@ -4,7 +4,8 @@
>  RUN: rm -f %t.ar
>  RUN: llvm-ar crs %t.ar %t-macho.o
>  RUN: grep -q __.SYMDEF %t.ar
> -RUN: llvm-ar crs %t.ar %t-coff.o
> +Test that an option string prefixed by a dash works.
> +RUN: llvm-ar -crs %t.ar %t-coff.o
>  RUN: grep -q __.SYMDEF %t.ar
>  
>  RUN: rm -f %t.ar
>
>
> Index: tools/llvm-ar/llvm-ar.cpp
> ===================================================================
> --- tools/llvm-ar/llvm-ar.cpp
> +++ tools/llvm-ar/llvm-ar.cpp
> @@ -127,6 +127,8 @@
>    "  [v] - be verbose about actions taken\n"
>  );
>  
> +static const char OptionChars[] = "dmpqrtxabiosSTucv";
> +
>  // This enumeration delineates the kinds of operations on an archive
>  // that are permitted.
>  enum ArchiveOperation {
> @@ -879,6 +881,19 @@
>        Stem.find("lib") != StringRef::npos)
>      return libDriverMain(makeArrayRef(argv, argc));
>  
> +  for (int i = 1; i < argc; i++) {
> +    // If an argument starts with a dash and only contains chars
> +    // that belong to the options chars set, remove the dash.
> +    // We can't handle it after the command line options parsing
> +    // is done, since it will error out on an unrecognized string
> +    // starting with a dash.
> +    // Make sure this doesn't match the actual llvm-ar specific options
> +    // that start with a dash.
> +    if (argv[i][0] == '-' &&
> +        strspn(&argv[i][1], OptionChars) + 1 == strlen(argv[i]))
> +      argv[i]++;
> +  }
> +
>    // Have the command line options parsed and handle things
>    // like --help and --version.
>    cl::ParseCommandLineOptions(argc, argv,
> Index: test/tools/llvm-ar/default-add.test
> ===================================================================
> --- test/tools/llvm-ar/default-add.test
> +++ test/tools/llvm-ar/default-add.test
> @@ -4,7 +4,8 @@
>  RUN: rm -f %t.ar
>  RUN: llvm-ar crs %t.ar %t-macho.o
>  RUN: grep -q __.SYMDEF %t.ar
> -RUN: llvm-ar crs %t.ar %t-coff.o
> +Test that an option string prefixed by a dash works.
> +RUN: llvm-ar -crs %t.ar %t-coff.o
>  RUN: grep -q __.SYMDEF %t.ar
>  
>  RUN: rm -f %t.ar


More information about the llvm-commits mailing list