1 /* Self tests for GDB command definitions for GDB, the GNU debugger.
3 Copyright (C) 2019-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "cli/cli-cmds.h"
22 #include "cli/cli-decode.h"
23 #include "gdbsupport/selftest.h"
29 /* Verify some invariants of GDB commands documentation. */
31 namespace help_doc_tests
{
33 static unsigned int nr_failed_invariants
;
35 /* Report a broken invariant and increments nr_failed_invariants. */
38 broken_doc_invariant (const char *prefix
, const char *name
, const char *msg
)
40 gdb_printf ("help doc broken invariant: command '%s%s' help doc %s\n",
42 nr_failed_invariants
++;
45 /* Recursively walk the commandlist structures, and check doc invariants:
46 - The first line of the doc must end with a '.'.
47 - the doc must not end with a new line.
48 If an invariant is not respected, produce a message and increment
50 Note that we do not call SELF_CHECK in this function, as we want
51 all commands to be checked before making the test fail. */
54 check_doc (struct cmd_list_element
*commandlist
, const char *prefix
)
56 struct cmd_list_element
*c
;
58 /* Walk through the commands. */
59 for (c
= commandlist
; c
; c
= c
->next
)
61 /* Checks the doc has a first line terminated with a '.'. */
62 const char *p
= c
->doc
;
64 /* Position p on the first LF, or on terminating null byte. */
65 while (*p
&& *p
!= '\n')
70 "is missing the first line terminated with a '.' character");
71 else if (*(p
-1) != '.')
74 "first line is not terminated with a '.' character");
76 /* Checks the doc is not terminated with a new line. */
77 if (c
->doc
[strlen (c
->doc
) - 1] == '\n')
80 "has a superfluous trailing end of line");
82 /* Check if this command has subcommands and is not an
83 abbreviation. We skip checking subcommands of abbreviations
84 in order to avoid duplicates in the output. */
85 if (c
->is_prefix () && !c
->abbrev_flag
)
87 /* Recursively call ourselves on the subcommand list,
88 passing the right prefix in. */
89 check_doc (*c
->subcommands
, c
->prefixname ().c_str ());
95 help_doc_invariants_tests ()
97 nr_failed_invariants
= 0;
98 check_doc (cmdlist
, "");
99 SELF_CHECK (nr_failed_invariants
== 0);
102 } /* namespace help_doc_tests */
104 /* Verify some invariants of GDB command structure. */
106 namespace command_structure_tests
{
108 /* Nr of commands in which a duplicated list is found. */
109 static unsigned int nr_duplicates
= 0;
110 /* Nr of commands in a list having no valid prefix cmd. */
111 static unsigned int nr_invalid_prefixcmd
= 0;
113 /* A map associating a list with the prefix leading to it. */
115 static std::map
<cmd_list_element
**, const char *> lists
;
117 /* Store each command list in lists, associated with the prefix to reach it. A
118 list must only be found once.
120 Verifies that all elements of the list have the same non-null prefix
124 traverse_command_structure (struct cmd_list_element
**list
,
127 struct cmd_list_element
*c
, *prefixcmd
;
129 auto dupl
= lists
.find (list
);
130 if (dupl
!= lists
.end ())
132 gdb_printf ("list %p duplicated,"
133 " reachable via prefix '%s' and '%s'."
134 " Duplicated list first command is '%s'\n",
136 prefix
, dupl
->second
,
142 lists
.insert ({list
, prefix
});
144 /* All commands of *list must have a prefix command equal to PREFIXCMD,
145 the prefix command of the first command. */
146 if (*list
== nullptr)
147 prefixcmd
= nullptr; /* A prefix command with an empty subcommand list. */
149 prefixcmd
= (*list
)->prefix
;
151 /* Walk through the commands. */
152 for (c
= *list
; c
; c
= c
->next
)
154 /* If this command has subcommands and is not an alias,
155 traverse the subcommands. */
156 if (c
->is_prefix () && !c
->is_alias ())
158 /* Recursively call ourselves on the subcommand list,
159 passing the right prefix in. */
160 traverse_command_structure (c
->subcommands
, c
->prefixname ().c_str ());
162 if (prefixcmd
!= c
->prefix
163 || (prefixcmd
== nullptr && *list
!= cmdlist
))
165 if (c
->prefix
== nullptr)
166 gdb_printf ("list %p reachable via prefix '%s'."
167 " command '%s' has null prefixcmd\n",
171 gdb_printf ("list %p reachable via prefix '%s'."
172 " command '%s' has a different prefixcmd\n",
175 nr_invalid_prefixcmd
++;
180 /* Verify that a list of commands is present in the tree only once. */
183 command_structure_invariants_tests ()
186 nr_invalid_prefixcmd
= 0;
188 traverse_command_structure (&cmdlist
, "");
190 /* Release memory, be ready to be re-run. */
193 SELF_CHECK (nr_duplicates
== 0);
194 SELF_CHECK (nr_invalid_prefixcmd
== 0);
199 } /* namespace selftests */
201 void _initialize_command_def_selftests ();
203 _initialize_command_def_selftests ()
205 selftests::register_test
206 ("help_doc_invariants",
207 selftests::help_doc_tests::help_doc_invariants_tests
);
209 selftests::register_test
210 ("command_structure_invariants",
211 selftests::command_structure_tests::command_structure_invariants_tests
);