2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
8 Syntax and Descriptions of test directives
9 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11 Test directives appear within comments in a test source file and begin
12 with ``dg-``. Some of these are defined within DejaGnu and others
13 are local to the GCC testsuite.
15 The order in which test directives appear in a test can be important:
16 directives local to GCC sometimes override information used by the
17 DejaGnu directives, which know nothing about the GCC directives, so the
18 DejaGnu directives must precede GCC directives.
20 Several test directives include selectors (see :ref:`selectors`)
21 which are usually preceded by the keyword ``target`` or ``xfail``.
23 Specify how to build the test
24 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 :samp:`{ dg-do {do-what-keyword} [{ target/xfail {selector} }] }`
27 :samp:`{do-what-keyword}` specifies how the test is compiled and whether
28 it is executed. It is one of:
31 Compile with :option:`-E` to run only the preprocessor.
34 Compile with :option:`-S` to produce an assembly code file.
37 Compile with :option:`-c` to produce a relocatable object file.
40 Compile, assemble, and link to produce an executable file.
43 Produce and run an executable file, which is expected to return
46 The default is ``compile``. That can be overridden for a set of
47 tests by redefining ``dg-do-what-default`` within the ``.exp``
50 If the directive includes the optional :samp:`{ target {selector} }`
51 then the test is skipped unless the target system matches the
54 If :samp:`{do-what-keyword}` is ``run`` and the directive includes
55 the optional :samp:`{ xfail {selector} }` and the selector is met
56 then the test is expected to fail. The ``xfail`` clause is ignored
57 for other values of :samp:`{do-what-keyword}` ; those tests can use
58 directive ``dg-xfail-if``.
60 Specify additional compiler options
61 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63 :samp:`{ dg-options {options} [{ target {selector} }] }`
64 This DejaGnu directive provides a list of compiler options, to be used
65 if the target system matches :samp:`{selector}`, that replace the default
66 options used for this set of tests.
68 :samp:`{ dg-add-options {feature} ... }`
69 Add any compiler options that are needed to access certain features.
70 This directive does nothing on targets that enable the features by
71 default, or that don't provide them at all. It must come after
72 all ``dg-options`` directives.
73 For supported values of :samp:`{feature}` see :ref:`add-options`.
75 :samp:`{ dg-additional-options {options} [{ target {selector} }] }`
76 This directive provides a list of compiler options, to be used
77 if the target system matches :samp:`{selector}`, that are added to the default
78 options used for this set of tests.
80 Modify the test timeout value
81 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83 The normal timeout limit, in seconds, is found by searching the
86 * the value defined by an earlier ``dg-timeout`` directive in
89 * variable :samp:`{tool_timeout}` defined by the set of tests
91 * :samp:`{gcc}`, :samp:`{timeout}` set in the target board
95 :samp:`{ dg-timeout {n} [{target {selector} }] }`
96 Set the time limit for the compilation and for the execution of the test
97 to the specified number of seconds.
99 :samp:`{ dg-timeout-factor {x} [{ target {selector} }] }`
100 Multiply the normal time limit for compilation and execution of the test
101 by the specified floating-point factor.
103 Skip a test for some targets
104 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106 :samp:`{ dg-skip-if {comment} { {selector} } [{ {include-opts} } [{ {exclude-opts} }]] }`
107 Arguments :samp:`{include-opts}` and :samp:`{exclude-opts}` are lists in which
108 each element is a string of zero or more GCC options.
109 Skip the test if all of the following conditions are met:
111 * the test system is included in :samp:`{selector}`
113 * for at least one of the option strings in :samp:`{include-opts}`,
114 every option from that string is in the set of options with which
115 the test would be compiled; use :samp:`"*"` for an :samp:`{include-opts}` list
116 that matches any options; that is the default if :samp:`{include-opts}` is
119 * for each of the option strings in :samp:`{exclude-opts}`, at least one
120 option from that string is not in the set of options with which the test
121 would be compiled; use :samp:`""` for an empty :samp:`{exclude-opts}` list;
122 that is the default if :samp:`{exclude-opts}` is not specified
124 For example, to skip a test if option ``-Os`` is present:
128 /* { dg-skip-if "" { *-*-* } { "-Os" } { "" } } */
130 To skip a test if both options ``-O2`` and ``-g`` are present:
134 /* { dg-skip-if "" { *-*-* } { "-O2 -g" } { "" } } */
136 To skip a test if either ``-O2`` or ``-O3`` is present:
140 /* { dg-skip-if "" { *-*-* } { "-O2" "-O3" } { "" } } */
142 To skip a test unless option ``-Os`` is present:
146 /* { dg-skip-if "" { *-*-* } { "*" } { "-Os" } } */
148 To skip a test if either ``-O2`` or ``-O3`` is used with ``-g``
149 but not if ``-fpic`` is also present:
153 /* { dg-skip-if "" { *-*-* } { "-O2 -g" "-O3 -g" } { "-fpic" } } */
155 :samp:`{ dg-require-effective-target {keyword} [{ target {selector} }] }`
156 Skip the test if the test target, including current multilib flags,
157 is not covered by the effective-target keyword.
158 If the directive includes the optional :samp:`{ {selector} }`
159 then the effective-target test is only performed if the target system
160 matches the :samp:`{selector}`.
161 This directive must appear after any ``dg-do`` directive in the test
162 and before any ``dg-additional-sources`` directive.
163 See :ref:`effective-target-keywords`.
165 :samp:`{ dg-require-{support} args }`
166 Skip the test if the target does not provide the required support.
167 These directives must appear after any ``dg-do`` directive in the test
168 and before any ``dg-additional-sources`` directive.
169 They require at least one argument, which can be an empty string if the
170 specific procedure does not examine the argument.
171 See :ref:`require-support`, for a complete list of these directives.
173 Expect a test to fail for some targets
174 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
176 :samp:`{ dg-xfail-if {comment} { {selector} } [{ {include-opts} } [{ {exclude-opts} }]] }`
177 Expect the test to fail if the conditions (which are the same as for
178 ``dg-skip-if``) are met. This does not affect the execute step.
180 :samp:`{ dg-xfail-run-if {comment} { {selector} } [{ {include-opts} } [{ {exclude-opts} }]] }`
181 Expect the execute step of a test to fail if the conditions (which are
182 the same as for ``dg-skip-if``) are met.
184 Expect the compiler to crash
185 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187 :samp:`{ dg-ice {comment} [{ {selector} } [{ {include-opts} } [{ {exclude-opts} }]]] }`
188 Expect the compiler to crash with an internal compiler error and return
189 a nonzero exit status if the conditions (which are the same as for
190 ``dg-skip-if``) are met. Used for tests that test bugs that have not been
193 Expect the test executable to fail
194 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
196 :samp:`{ dg-shouldfail {comment} [{ {selector} } [{ {include-opts} } [{ {exclude-opts} }]]] }`
197 Expect the test executable to return a nonzero exit status if the
198 conditions (which are the same as for ``dg-skip-if``) are met.
200 Verify compiler messages
201 ~~~~~~~~~~~~~~~~~~~~~~~~
203 Where :samp:`{line}` is an accepted argument for these commands, a value of :samp:`0`
204 can be used if there is no line associated with the message.
206 :samp:`{ dg-error {regexp} [{comment} [{ target/xfail {selector} } [{line}] ]] }`
207 This DejaGnu directive appears on a source line that is expected to get
208 an error message, or else specifies the source line associated with the
209 message. If there is no message for that line or if the text of that
210 message is not matched by :samp:`{regexp}` then the check fails and
211 :samp:`{comment}` is included in the ``FAIL`` message. The check does
212 not look for the string :samp:`error` unless it is part of :samp:`{regexp}`.
214 :samp:`{ dg-warning {regexp} [{comment} [{ target/xfail {selector} } [{line}] ]] }`
215 This DejaGnu directive appears on a source line that is expected to get
216 a warning message, or else specifies the source line associated with the
217 message. If there is no message for that line or if the text of that
218 message is not matched by :samp:`{regexp}` then the check fails and
219 :samp:`{comment}` is included in the ``FAIL`` message. The check does
220 not look for the string :samp:`warning` unless it is part of :samp:`{regexp}`.
222 :samp:`{ dg-message {regexp} [{comment} [{ target/xfail {selector} } [{line}] ]] }`
223 The line is expected to get a message other than an error or warning.
224 If there is no message for that line or if the text of that message is
225 not matched by :samp:`{regexp}` then the check fails and :samp:`{comment}` is
226 included in the ``FAIL`` message.
228 :samp:`{ dg-note {regexp} [{comment} [{ target/xfail {selector} } [{line}] ]] }`
229 The line is expected to get a :samp:`note` message.
230 If there is no message for that line or if the text of that message is
231 not matched by :samp:`{regexp}` then the check fails and :samp:`{comment}` is
232 included in the ``FAIL`` message.
234 By default, any *excess* :samp:`note` messages are pruned, meaning
235 their appearance doesn't trigger *excess errors*.
236 However, if :samp:`dg-note` is used at least once in a testcase,
237 they're not pruned and instead must *all* be handled explicitly.
238 Thus, if looking for just single instances of messages with
239 :samp:`note:` prefixes without caring for all of them, use
240 :samp:`dg-message "note: [...]"` instead of :samp:`dg-note`, or use
241 :samp:`dg-note` together with :samp:`dg-prune-output "note: "`.
243 :samp:`{ dg-bogus {regexp} [{comment} [{ target/xfail {selector} } [{line}] ]] }`
244 This DejaGnu directive appears on a source line that should not get a
245 message matching :samp:`{regexp}`, or else specifies the source line
246 associated with the bogus message. It is usually used with :samp:`xfail`
247 to indicate that the message is a known problem for a particular set of
250 :samp:`{ dg-line {linenumvar} }`
251 This DejaGnu directive sets the variable :samp:`{linenumvar}` to the line number of
252 the source line. The variable :samp:`{linenumvar}` can then be used in subsequent
253 ``dg-error``, ``dg-warning``, ``dg-message``, ``dg-note``
255 directives. For example:
259 int a; /* { dg-line first_def_a } */
260 float a; /* { dg-error "conflicting types of" } */
261 /* { dg-message "previous declaration of" "" { target *-*-* } first_def_a } */
263 :samp:`{ dg-excess-errors {comment} [{ target/xfail {selector} }] }`
264 This DejaGnu directive indicates that the test is expected to fail due
265 to compiler messages that are not handled by :samp:`dg-error`,
266 :samp:`dg-warning`, ``dg-message``, :samp:`dg-note` or
268 For this directive :samp:`xfail`
269 has the same effect as :samp:`target`.
271 :samp:`{ dg-prune-output {regexp} }`
272 Prune messages matching :samp:`{regexp}` from the test output.
274 Verify output of the test executable
275 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
277 :samp:`{ dg-output {regexp} [{ target/xfail {selector} }] }`
278 This DejaGnu directive compares :samp:`{regexp}` to the combined output
279 that the test executable writes to :samp:`stdout` and :samp:`stderr`.
281 Specify environment variables for a test
282 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
284 :samp:`{ dg-set-compiler-env-var {var_name} "{var_value}" }`
285 Specify that the environment variable :samp:`{var_name}` needs to be set
286 to :samp:`{var_value}` before invoking the compiler on the test file.
288 :samp:`{ dg-set-target-env-var {var_name} "{var_value}" }`
289 Specify that the environment variable :samp:`{var_name}` needs to be set
290 to :samp:`{var_value}` before execution of the program created by the test.
292 Specify additional files for a test
293 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
295 :samp:`{ dg-additional-files "{filelist}" }`
296 Specify additional files, other than source files, that must be copied
297 to the system where the compiler runs.
299 :samp:`{ dg-additional-sources "{filelist}" }`
300 Specify additional source files to appear in the compile line
301 following the main test file.
303 Add checks at the end of a test
304 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
306 :samp:`{ dg-final { {local-directive} } }`
307 This DejaGnu directive is placed within a comment anywhere in the
308 source file and is processed after the test has been compiled and run.
309 Multiple :samp:`dg-final` commands are processed in the order in which
310 they appear in the source file. See :ref:`final-actions`, for a list
311 of directives that can be used within ``dg-final``.