Fix problem that alias can be defined or not depending on the order.
authorPhilippe Waroquiers <philippe.waroquiers@skynet.be>
Tue, 5 May 2020 19:38:38 +0000 (21:38 +0200)
committerPhilippe Waroquiers <philippe.waroquiers@skynet.be>
Fri, 15 May 2020 20:17:45 +0000 (22:17 +0200)
When an alias name starts with the name of another alias,
GDB was accepting to define the aliases in one order (short first, long after),
but refused it the other way around.

So, fix the logic to recognise an already existing alias by using
lookup_cmd_composition.

Also, this revealed a bug in lookup_cmd_composition:
when the searched command is a prefix command, lookup_cmd_composition
was not returning the fact that a command was found even if the
TEXT to parse was fully consumed.

gdb/ChangeLog
YYYY-MM-DD  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

* cli/cli-cmds.c (alias_command): Check for an existing alias
using lookup_cmd_composition, as valid_command_p is too strict
and forbids aliases that are the prefix of an existing alias
or command.
* cli/cli-decode.c (lookup_cmd_composition): Ensure a prefix
command is properly recognised as a valid command.

gdb/testsuite/ChangeLog
2020-05-15  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

* gdb.base/alias.exp: Test aliases starting with a prefix of
another alias.

gdb/ChangeLog
gdb/cli/cli-cmds.c
gdb/cli/cli-decode.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/alias.exp

index 30500f4a7427796b60f39f1e20a9fbc308c34607..e5708da7d4b8b583f78e148cfb155127e4da7c6f 100644 (file)
@@ -1,3 +1,12 @@
+2020-05-15  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
+
+       * cli/cli-cmds.c (alias_command): Check for an existing alias
+       using lookup_cmd_composition, as valid_command_p is too strict
+       and forbids aliases that are the prefix of an existing alias
+       or command.
+       * cli/cli-decode.c (lookup_cmd_composition): Ensure a prefix
+       command is properly recognised as a valid command.
+
 2020-05-15  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
 
        * unittests/help-doc-selftests.c: Rename to
index c17521b1f69fd0ec065a478c9fd24b2864ee9a70..8538fadd9c3306778c5b4c2af8b3c543c12276da 100644 (file)
@@ -1694,8 +1694,29 @@ alias_command (const char *args, int from_tty)
   /* ALIAS must not exist.  */
   std::string alias_string (argv_to_string (alias_argv, alias_argc));
   alias = alias_string.c_str ();
-  if (valid_command_p (alias))
-    error (_("Alias already exists: %s"), alias);
+  {
+    cmd_list_element *alias_cmd, *prefix_cmd, *cmd;
+
+    if (lookup_cmd_composition (alias, &alias_cmd, &prefix_cmd, &cmd))
+      {
+       const char *alias_name = alias_argv[alias_argc-1];
+
+       /* If we found an existing ALIAS_CMD, check that the prefix differ or
+          the name differ.  */
+
+       if (alias_cmd != nullptr
+           && alias_cmd->prefix == prefix_cmd
+           && strcmp (alias_name, alias_cmd->name) == 0)
+         error (_("Alias already exists: %s"), alias);
+
+       /* Check ALIAS differs from the found CMD.  */
+
+       if (cmd->prefix == prefix_cmd
+           && strcmp (alias_name, cmd->name) == 0)
+         error (_("Alias %s is the name of an existing command"), alias);
+      }
+  }
+
 
   /* If ALIAS is one word, it is an alias for the entire COMMAND.
      Example: alias spe = set print elements
index d951ead1c9578e5b691ab44f7da2328b06ac8b5c..78b8901084620c7cf4d0c4e320714b7ef44cc400 100644 (file)
@@ -1843,6 +1843,8 @@ lookup_cmd_composition (const char *text,
 
   cur_list = cmdlist;
 
+  text = skip_spaces (text);
+
   while (1)
     {
       /* Go through as many command lists as we need to,
@@ -1850,9 +1852,6 @@ lookup_cmd_composition (const char *text,
 
       prev_cmd = *cmd;
 
-      while (*text == ' ' || *text == '\t')
-       (text)++;
-
       /* Identify the name of the command.  */
       len = find_command_name_length (text);
 
@@ -1861,7 +1860,7 @@ lookup_cmd_composition (const char *text,
        return 0;
 
       /* TEXT is the start of the first command word to lookup (and
-        it's length is len).  We copy this into a local temporary.  */
+        it's length is LEN).  We copy this into a local temporary.  */
 
       command = (char *) alloca (len + 1);
       memcpy (command, text, len);
@@ -1890,12 +1889,14 @@ lookup_cmd_composition (const char *text,
            }
          *prefix_cmd = prev_cmd;
        }
-      if ((*cmd)->prefixlist)
+
+      text += len;
+      text = skip_spaces (text);
+
+      if ((*cmd)->prefixlist && *text != '\0')
        cur_list = *(*cmd)->prefixlist;
       else
        return 1;
-
-      text += len;
     }
 }
 
index dda5b68473223c1d5082eb5ea4afb315347ad5a0..820af44416cc9eb704763778415c1c91799aa4ea 100644 (file)
@@ -1,3 +1,8 @@
+2020-05-15  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
+
+       * gdb.base/alias.exp: Test aliases starting with a prefix of
+       another alias.
+
 2020-05-15  Gary Benson <gbenson@redhat.com>
 
        * gdb.base/info-os.c (main): Add return statement.
index be78d9e936de3549c8b5779a2b1d9e0730877224..9a9557668e57282db4e4f452846f9581e8244004 100644 (file)
@@ -116,3 +116,9 @@ gdb_test "show print elements" "Limit .* is 56\[.\]" "verify 56"
 
 gdb_test_no_output "set print max-elements 57"
 gdb_test "show print elements" "Limit .* is 57\[.\]" "verify 57"
+
+# Test aliases having a common prefix.
+gdb_test_no_output "alias abcd  = backtrace"
+gdb_test_no_output "alias abcde = backtrace"
+gdb_test_no_output "alias fghij = backtrace"
+gdb_test_no_output "alias fghi  = backtrace"
This page took 0.048256 seconds and 4 git commands to generate.