1 /* Self tests for GDB command definitions for GDB, the GNU debugger.
3 Copyright (C) 2019-2021 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 fprintf_filtered (gdb_stdout
,
41 "help doc broken invariant: command '%s%s' help doc %s\n",
43 nr_failed_invariants
++;
46 /* Recursively walk the commandlist structures, and check doc invariants:
47 - The first line of the doc must end with a '.'.
48 - the doc must not end with a new line.
49 If an invariant is not respected, produce a message and increment
51 Note that we do not call SELF_CHECK in this function, as we want
52 all commands to be checked before making the test fail. */
55 check_doc (struct cmd_list_element
*commandlist
, const char *prefix
)
57 struct cmd_list_element
*c
;
59 /* Walk through the commands. */
60 for (c
= commandlist
; c
; c
= c
->next
)
62 /* Checks the doc has a first line terminated with a '.'. */
63 const char *p
= c
->doc
;
65 /* Position p on the first LF, or on terminating null byte. */
66 while (*p
&& *p
!= '\n')
71 "is missing the first line terminated with a '.' character");
72 else if (*(p
-1) != '.')
75 "first line is not terminated with a '.' character");
77 /* Checks the doc is not terminated with a new line. */
78 if (c
->doc
[strlen (c
->doc
) - 1] == '\n')
81 "has a superfluous trailing end of line");
83 /* Check if this command has subcommands and is not an
84 abbreviation. We skip checking subcommands of abbreviations
85 in order to avoid duplicates in the output. */
86 if (c
->prefixlist
!= NULL
&& !c
->abbrev_flag
)
88 /* Recursively call ourselves on the subcommand list,
89 passing the right prefix in. */
90 check_doc (*c
->prefixlist
, c
->prefixname
);
96 help_doc_invariants_tests ()
98 nr_failed_invariants
= 0;
99 check_doc (cmdlist
, "");
100 SELF_CHECK (nr_failed_invariants
== 0);
103 } /* namespace help_doc_tests */
105 /* Verify some invariants of GDB command structure. */
107 namespace command_structure_tests
{
109 /* Nr of commands in which a duplicated list is found. */
110 unsigned int nr_duplicates
= 0;
111 /* Nr of commands in a list having no valid prefix cmd. */
112 unsigned int nr_invalid_prefixcmd
= 0;
114 /* A map associating a list with the prefix leading to it. */
116 std::map
<cmd_list_element
**, const char *> lists
;
118 /* Store each command list in lists, associated with the prefix to reach it. A
119 list must only be found once.
121 Verifies that all elements of the list have the same non-null prefix
125 traverse_command_structure (struct cmd_list_element
**list
,
128 struct cmd_list_element
*c
, *prefixcmd
;
130 auto dupl
= lists
.find (list
);
131 if (dupl
!= lists
.end ())
133 fprintf_filtered (gdb_stdout
,
134 "list %p duplicated,"
135 " reachable via prefix '%s' and '%s'."
136 " Duplicated list first command is '%s'\n",
138 prefix
, dupl
->second
,
144 lists
.insert ({list
, prefix
});
146 /* All commands of *list must have a prefix command equal to PREFIXCMD,
147 the prefix command of the first command. */
148 if (*list
== nullptr)
149 prefixcmd
= nullptr; /* A prefix command with an empty subcommand list. */
151 prefixcmd
= (*list
)->prefix
;
153 /* Walk through the commands. */
154 for (c
= *list
; c
; c
= c
->next
)
156 /* If this command has subcommands and is not an alias,
157 traverse the subcommands. */
158 if (c
->prefixlist
!= NULL
&& c
->cmd_pointer
== nullptr)
160 /* Recursively call ourselves on the subcommand list,
161 passing the right prefix in. */
162 traverse_command_structure (c
->prefixlist
, c
->prefixname
);
164 if (prefixcmd
!= c
->prefix
165 || (prefixcmd
== nullptr && *list
!= cmdlist
))
167 if (c
->prefix
== nullptr)
168 fprintf_filtered (gdb_stdout
,
169 "list %p reachable via prefix '%s'."
170 " command '%s' has null prefixcmd\n",
174 fprintf_filtered (gdb_stdout
,
175 "list %p reachable via prefix '%s'."
176 " command '%s' has a different prefixcmd\n",
179 nr_invalid_prefixcmd
++;
184 /* Verify that a list of commands is present in the tree only once. */
187 command_structure_invariants_tests ()
190 nr_invalid_prefixcmd
= 0;
192 traverse_command_structure (&cmdlist
, "");
194 /* Release memory, be ready to be re-run. */
197 SELF_CHECK (nr_duplicates
== 0);
198 SELF_CHECK (nr_invalid_prefixcmd
== 0);
203 } /* namespace selftests */
205 void _initialize_command_def_selftests ();
207 _initialize_command_def_selftests ()
209 selftests::register_test
210 ("help_doc_invariants",
211 selftests::help_doc_tests::help_doc_invariants_tests
);
213 selftests::register_test
214 ("command_structure_invariants",
215 selftests::command_structure_tests::command_structure_invariants_tests
);