libiberty: allow comments in option file

Message ID 20210911064712.179872-1-hujialun@comp.nus.edu.sg
State New
Headers show
Series
  • libiberty: allow comments in option file
Related show

Commit Message

Hu Jialun Sept. 11, 2021, 6:47 a.m.
Enables putting line comments into option files included using '@'.

Comments begin with a ';' followed by whitespace, and terminates at the
end of the line.  Backward compability should be satisfactory since ';'
is a character disallowed in DOS filenames and rarely used in filenames
or options anywhere else as well.

Related discussion: https://gcc.gnu.org/legacy-ml/gcc/2011-02/msg00422.html
---
 libiberty/argv.c                      |  5 +++++
 libiberty/testsuite/test-expandargv.c | 12 ++++++++++++
 2 files changed, 17 insertions(+)

-- 
2.33.0

Patch

diff --git a/libiberty/argv.c b/libiberty/argv.c
index 48dcd102461..2bc7569b718 100644
--- a/libiberty/argv.c
+++ b/libiberty/argv.c
@@ -194,6 +194,11 @@  char **buildargv (const char *input)
 	{
 	  /* Pick off argv[argc] */
 	  consume_whitespace (&input);
+	  if (*input == ';')
+	    {
+	      for (; *input != '\n' && *input != EOS; ++input);
+	      continue;
+	    }
 
 	  if ((maxargc == 0) || (argc >= (maxargc - 1)))
 	    {
diff --git a/libiberty/testsuite/test-expandargv.c b/libiberty/testsuite/test-expandargv.c
index 56c170f9ec6..5640b2b41cf 100644
--- a/libiberty/testsuite/test-expandargv.c
+++ b/libiberty/testsuite/test-expandargv.c
@@ -142,6 +142,18 @@  const char *test_data[] = {
   "b",
   0,
 
+  /* Test 7 - Check for comments in option file.  */
+  "abc\n;def\nxy \\;z \\ ;gh",    /* Test 7 data */
+  ARGV0,
+  "@test-expandargv-7.lst",
+  0,
+  ARGV0,
+  "abc",
+  "xy",
+  ";z",
+  " ;gh",
+  0,
+
   0 /* Test done marker, don't remove. */
 };