6.1 Writing a test: how dg- comments drive DejaGnu

Source references in this chapter are to GCC 15.2.0 (releases/gcc-15.2.0).

You have a test that checks the assembly. It is a dg-do compile test with a scan-assembler line, and it passes. You want it to check the result of the code too, so you change dg-do compile to dg-do run. The run passes. The scan line now reports UNRESOLVED, and the log says that the output file does not exist. The C code is valid, and so is every directive.

A GCC test declares its whole behaviour in comments in the test file. Each dg- comment is a Tcl command, and the harness procedures that run it agree on file names only by convention. dg-do run writes an executable. scan-assembler reads a .s file that nothing wrote.

This chapter opens Part VI. The earlier parts were for someone who uses or builds a toolchain. This part is for someone who is about to change GCC and send the change upstream. If you do not plan to do that, you can stop before this part.

This chapter assumes Chapter 3.7, which runs the whole testsuite against a built toolchain and explains target boards and .sum files. It also uses the stop options -S and -c from the table in Chapter 1.1. Chapter 3.7 tests a toolchain. This chapter writes the test that goes with a change, and runs only that test.

A test is a C file with Tcl in its comments: pr63637-1.c

Here is a complete test from gcc/testsuite/gcc.dg:

/* PR rtl-optimization/63637 */
/* { dg-do compile } */
/* { dg-options "-O2" } */

int
foo (void)
{
  int a, b, c;
  asm ("# Magic instruction" : "=r" (a));
  asm ("# Magic instruction" : "=r" (b));
  asm ("# Magic instruction" : "=r" (c));
  return a + b + c;
}

/* { dg-final { scan-assembler-times "# Magic instruction" 1 } } */

Listing 6.1-1: a whole test (gcc/testsuite/gcc.dg/pr63637-1.c:1-15).

Three directives say everything the harness needs to know:

  • dg-do compile compiles with -S and keeps the assembly file. compile is also the default when a test has no dg-do (sourcebuild.texi:1044-1063).
  • dg-options "-O2" replaces the default options of the directory with -O2.
  • dg-final { scan-assembler-times "# Magic instruction" 1 } runs after the compile. It counts the matches of the regular expression in the .s file, and passes only if the count is exactly 1.

The three asm statements are identical. The test expects one copy of their text in the output. The first line names the bug report that the test came from.

The internals manual documents every directive and its arguments, in Directives and Final Actions. It also says where the directives come from. "Some of these are defined within DejaGnu and others are local to the GCC testsuite" (sourcebuild.texi:1029-1031). The manual calls dg-options, dg-error and dg-final DejaGnu directives. The commands inside dg-final, such as scan-assembler, are GCC's (sourcebuild.texi:3546-3547), and so is every dg-require-*.

Each directive is a Tcl procedure called with its line number

DejaGnu reads the test file, finds each { dg-... } group in it, and calls it as a Tcl command. It puts the line number of the comment in front of the arguments. gcc-dg.exp loads DejaGnu's dg.exp first (gcc-dg.exp:17), and then defines its own dg-final, which takes the place of DejaGnu's. It shows the line number:

proc dg-final { args } {
    upvar dg-final-code final-code

    if { [llength $args] > 2 } {
	error "[lindex $args 0]: too many arguments"
    }
    set line [lindex $args 0]
    set code [lindex $args 1]
    set directive [lindex $code 0]
    switch $directive {
	gdb-test {
	    set code [linsert $code 1 $line]
	}
    }
    append final-code "$code\n"
}

Listing 6.1-2: dg-final (gcc/testsuite/lib/gcc-dg.exp:111-126).

dg-final runs nothing when DejaGnu reads it. It appends its Tcl code to dg-final-code, and upvar makes that name refer to a local variable of the procedure that called it. That procedure is DejaGnu's dg-test, which runs the test. After the compile, and after the run if there is one, dg-test evaluates the collected code in the order of the file (sourcebuild.texi:1358-1363).

The other GCC directives work the same way. dg-additional-options appends to dg-extra-tool-flags (gcc-defs.exp:284-285). dg-require-effective-target drops the line number and edits dg-do-what (target-supports-dg.exp:241-264). A helper in the same file states the rule in its comment. It looks for "the 'dg-do-what' variable living inside DejaGnu's 'dg-test' procedure, as a local variable" (target-supports-dg.exp:61-62). So a GCC directive is a procedure that edits the state of dg-test while dg-test reads the file. That is all the Tcl a test writer needs. A test writer does not need to know expect.

gcc.dg/dg.exp: which driver runs your file

The make rule that runs the C testsuite ends in one command, $(RUNTEST) --tool $(check_p_tool) $(RUNTESTFLAGS), where the tool is gcc (gcc/Makefile.in:4709). The testsuite's README says what --tool is for. It "is mainly used to find the testsuite directories for a particular tool when several testsuites are in the same directory" (gcc/testsuite/README:53-56). For the tool gcc, these are the gcc.* directories: gcc.dg, gcc.c-torture, gcc.target and others. DejaGnu runs each .exp file in them, and each .exp file chooses its own tests. Here is the driver for the top of gcc.dg:

# Load support procs.
load_lib gcc-dg.exp

# If a testcase doesn't have special options, use these.
global DEFAULT_CFLAGS
if ![info exists DEFAULT_CFLAGS] then {
    set DEFAULT_CFLAGS " -ansi -pedantic-errors"
}

# Initialize `dg'.
dg-init

# Main loop.
dg-runtest [lsort [glob -nocomplain $srcdir/$subdir/*.\[cSi\]]] \
	"" $DEFAULT_CFLAGS

# C/C++ common tests.
dg-runtest [lsort [glob -nocomplain $srcdir/c-c++-common/*.\[cSi\]]] \
	" -Wc++-compat " ""

# All done.
dg-finish

Listing 6.1-3: the gcc.dg driver (gcc/testsuite/gcc.dg/dg.exp:19-40).

Four facts follow from these lines, and each one decides where your test goes.

The glob does not go down into subdirectories. dg.exp runs the files at the top of gcc.dg and nothing below it. A file in gcc.dg/torture belongs to dg-torture.exp, which is seven lines long and calls gcc-dg-runtest instead (gcc.dg/torture/dg-torture.exp:1-7). gcc-dg-runtest compiles each test once for each set of options in its list (gcc-dg.exp:742-748). The default list goes from -O0 to -Os (gcc-dg.exp:93-99). Put a test there when the bug appears only at some optimization levels.

A test with no dg-options gets -ansi -pedantic-errors. That is the third argument of dg-runtest, the default options of the directory. A dg-options line replaces them (sourcebuild.texi:1080-1083). An empty one, dg-options "", replaces them with nothing, as in vla-init-1.c (gcc.dg/vla-init-1.c:5).

c-c++-common runs twice. gcc.dg/dg.exp runs it as C, with -Wc++-compat. g++.dg/dg.exp runs the same files as C++ (g++.dg/dg.exp:49). A test that must hold for both languages goes there.

A target directory can opt out completely. gcc.target/i386/i386.exp returns before it runs a single test if the target is not x86 (gcc.target/i386/i386.exp:19-22). On an AArch64 build, nothing in that directory runs, and nothing reports it as skipped.

The internals manual lists the C directories and what each one is for, under C Tests. Its Test Idioms node gives the naming rule: a test for bug report N is prN-1.c (sourcebuild.texi:937-946).

dg-do names the output file, and scan-assembler assumes the name

The dg-do keyword reaches GCC as the do_what argument of gcc-dg-test-1. That procedure chooses the kind of output and its name:

	"compile" {
	    set compile_type "assembly"
	    set output_file "[file rootname [file tail $prog]].s"
	}

Listing 6.1-4: the output of dg-do compile (gcc/testsuite/lib/gcc-dg.exp:244-247).

pr63637-1.c compiles to pr63637-1.s, in the directory where runtest runs. The scan procedure never receives that name. It rebuilds it:

    # This must match the rule in gcc-dg.exp.
    set output_file "[file rootname [file tail $filename]].s"

    set files [glob -nocomplain $output_file]
    if { $files == "" } {
	verbose -log "$testcase: output file does not exist"
	unresolved "$testcase scan-assembler-times $pp_pattern $times"
	return
    }

Listing 6.1-5: scan-assembler-times finds its file by name (gcc/testsuite/lib/scanasm.exp:493-501).

The comment is the contract between the two files, and nothing checks it. scan-assembler and scan-assembler-not build the same name (scanasm.exp:104-113). Now look at dg-do run. Its output is ./pr63637-1.exe, and the compile writes no .s file (gcc-dg.exp:266-271). The scan finds no file and reports UNRESOLVED. That is the failure from the start of this chapter. A test that scans the assembly stays a dg-do compile test. Check the run in a second test.

The dump scans work the same way. scan-dump, which scan-tree-dump and its relatives use, builds a glob pattern from the test's file name and the dump suffix. If no file matches, it reports UNRESOLVED (scandump.exp:103-110). Chapter 5.2 explains how GCC names dump files.

A scan directive adds options to the compile: -fno-ident

Before it compiles, gcc-dg-test-1 reads the collected dg-final code. For each directive that has a procedure named <directive>_required_options, it adds the options that procedure returns (gcc-dg.exp:286-299). Every scan of the assembly gets such a procedure:

proc set_required_options_for { test } {
    if { [info proc $test] == "" } {
	perror "$test does not exist"
	exit 1
    }
    proc ${test}_required_options {} {
	global gcc_set_required_options
	upvar 1 extra_tool_flags extra_tool_flags
	if {[regexp -- "^scan-assembler" [info level 0]]
	    && ![string match "*-fident*" $extra_tool_flags]} {
	    # Do not let .ident confuse assembler scan tests
	    return [list $gcc_set_required_options "-fno-ident"]
	}
	return $gcc_set_required_options
    }
}

Listing 6.1-6: options that a scan forces (gcc/testsuite/lib/target-supports.exp:12704-12719).

So pr63637-1.c is not compiled with -O2 alone. The compile also gets -fno-ident. When the target supports LTO, gcc_set_required_options holds -ffat-lto-objects (gcc-dg.exp:101-103). The comment above the variable gives the reason: some torture options "cause intermediate code output, unusable for testing using e.g. scan-assembler" (gcc-dg.exp:77-79). The GCC manual documents -fno-ident under Code Gen Options with one sentence: "Ignore the #ident directive". The option also removes a line that GCC writes itself. At the end of every assembly file, the compiler emits an .ident directive. Its text is GCC: , then the package string and the version (gcc/toplev.cc:563-576). -fno-ident stops it. Without the option, a pattern that looks for a digit or for GCC would match that line.

dg-error and dg-warning: one message, one line, one column

A test for a diagnostic puts the directive on the line that must get the message:

/* { dg-do compile } */

void
foo (int n)
{
  switch (n)
    case 0: case 3: case 0.2: case 5:; /* { dg-error "21:case label does not reduce to an integer constant" } */
}

Listing 6.1-7: a dg-error test with a column (gcc/testsuite/gcc.dg/case-bogus-1.c:1-8).

This test has no dg-options, so it compiles with -ansi -pedantic-errors. The directive is on line 7, so the error must be for line 7. The 21: at the start of the expression is a column. Column 21 is where the third case starts.

DejaGnu's dg-error knows nothing about columns. GCC renames it to saved-dg-error and puts a wrapper in its place that calls process-message (gcc-dg.exp:1123-1133). The wrapper rewrites the expression before DejaGnu stores it:

    if [regexp {^-:} $expmsg] {
	# The expected column is -, so shouldn't appear.
	set expmsg [string range $expmsg 2 end]
    } elseif [regexp {^[0-9]+:} $expmsg column] {
	# The expression in the directive included a column number.
	# Remove it from the original expression and move it
	# to the proper place in the search expression.
	set expmsg [string range $expmsg [string length $column] end]
	set column "$column "
    } elseif [string match "" [lindex $newentry 0]] {
	# The specified line number is 0; don't expect a column number.
    } else {
	# There is no column number in the search expression, but we
	# should expect one in the message itself.
	set column {[0-9]+: }
    }
    set expmsg "$column$msgprefix\[^\n\]*$expmsg"

Listing 6.1-8: how GCC rewrites a message expression (gcc/testsuite/lib/gcc-dg.exp:1262-1278).

For dg-error the prefix is (fatal )?error:, and for dg-warning it is warning: (gcc.exp:113-114). So the directive in Listing 6.1-7 looks for 21: (fatal )?error:, then any text, then the message. Without a column, it accepts any column. With -:, it expects none. dg-message uses the same code with no prefix (gcc-dg.exp:1287-1290). It is the directive for a note: that comes with an error, as in decl-4.c (gcc.dg/decl-4.c:4-5).

DejaGnu passes the compiler output through GCC's gcc-dg-prune, which calls prune_gcc_output (gcc-dg.exp:396). That procedure removes the lines that GCC does not want tests to see, such as "In function" headers and include stacks (prune.exp:43-58). Compiler output that no message directive handles is an excess error, and the compile fails (sourcebuild.texi:1300-1304). So a test fails if the compiler emits a diagnostic that no directive expects.

The output must have the same form on every host. gcc-dg.exp sets LC_ALL and LANG to C "so that we get the same error messages as expected" (gcc-dg.exp:39-41). prune.exp adds -fdiagnostics-plain-output to TEST_ALWAYS_FLAGS (prune.exp:21-30), and gcc_target_compile puts those flags first on every compile (gcc.exp:137-142). The GCC manual documents the option under Diagnostic Message Formatting Options. It lists the options that it implies, among them -fno-diagnostics-show-caret, and says that it "may be useful when running dejagnu" (invoke.texi:5334-5337). The manual does not say that the testsuite adds it to every compile.

dg-require-effective-target: a probe compile in target-supports.exp

A test that needs a property of the target says so, and the harness checks the property by compiling a small program. The checks are procedures named check_effective_target_<keyword>, and is-effective-target calls the one that matches the keyword (target-supports.exp:10301-10310). Here is lp64:

proc check_effective_target_lp64 { } {
    return [check_no_compiler_messages lp64 object {
	int dummy[sizeof (int) == 4
		  && sizeof (void *) == 8
		  && sizeof (long) == 8 ? 1 : -1];
    }]
}

Listing 6.1-9: the lp64 keyword (gcc/testsuite/lib/target-supports.exp:3885-3891).

On a target that is not LP64, the array has size -1 and the compiler reports an error. check_no_compiler_messages returns true only if the compile printed nothing (target-supports.exp:187-202). It writes the program to a file and compiles it with the same ${tool}_target_compile that compiles the tests (target-supports.exp:46-125). It keeps the answer in et_cache, with one entry for each keyword and target name (target-supports.exp:140-159). So the probe sees the multilib options of the board, as the test does. It does not see the dg-options of the test, except for keywords whose names end in _nocache (sourcebuild.texi:1496-1500).

When the answer is false, dg-require-effective-target changes the second element of dg-do-what to N (target-supports-dg.exp:262-264). The test is then "skipped and reported as unsupported" (sourcebuild.texi:1383-1384). The internals manual documents the keywords under Effective-Target Keywords, and says that the directive "must appear after any dg-do directive" (sourcebuild.texi:1189-1197).

target-supports.exp has 14,395 lines at the pin, and 771 of them start with proc check_effective_target_. It is the file you will read most often when you write tests. Before you write a probe of your own, search it for the property that you need.

RUNTESTFLAGS="dg.exp=…": running only your test

Running the testsuite needs a built GCC tree. Nobody ran the commands in this section at the pin for this book. The install manual documents the method under Testing: run make check-gcc in the gcc directory of the build tree, and select tests with RUNTESTFLAGS. Its example is RUNTESTFLAGS="old-deja.exp=9805*". The text after name.exp= is a list of glob patterns, separated by white space (install.texi:3448-3488). For the test in Listing 6.1-1:

$ cd objdir/gcc
$ make check-gcc RUNTESTFLAGS="dg.exp=pr63637-1.c"

The part before = must name the driver of the directory where the test is. dg.exp= finds a file at the top of gcc.dg or in c-c++-common. A file in gcc.dg/torture needs dg-torture.exp=. Name the wrong driver and no test runs. Nothing fails either, so check that the summary counts at least one test.

The rest of the run is in gcc/Makefile.in. check-gcc is in lang_checks_parallelized, and check-c is an alias for it (gcc/c/Make-lang.in:174-180). So it goes through the parallelized rule (gcc/Makefile.in:4657-4686), and that rule starts one or more check-parallel-gcc jobs (gcc/Makefile.in:4688-4713). Each job changes to its own directory, testsuite/gcc for the first one, and runs runtest --tool gcc with your RUNTESTFLAGS. A driver runs a file only if DejaGnu's runtest_file_p says that the file is in your list. The loop in GCC's gcc-dg-runtest shows the check (gcc-dg.exp:726-730). With make -j, several runtest processes go through the same drivers. A marker file decides which process runs each batch of ten tests (gcc-defs.exp:180-197), and contrib/dg-extract-results.sh merges their .sum and .log files (gcc/Makefile.in:4677-4680). The results go to testsuite/gcc/gcc.sum and gcc.log.

check-gcc at the top level is not check-gcc in gcc/

The name means two different things. In the top-level build directory, check-gcc changes to gcc and runs make check there (Makefile.in:12847-12853). In gcc/, check depends on one check-<language> target for each language that you configured (gcc/Makefile.in:4581-4583, gcc/configure.ac:7385-7389). So the top-level check-gcc runs the testsuite of every language that you configured, each with your RUNTESTFLAGS. The top-level target for C alone is check-gcc-c (Makefile.in:64243-64248). The top-level Makefile passes RUNTESTFLAGS down to gcc/ (Makefile.in:869).

Read the result in gcc.sum. A check reports its own line. For scan-assembler-times, the line holds the test name, the directive, the pattern and the count (scanasm.exp:508-514). Run the test once without your fix and once with it. A test that passes on the old compiler does not test your change. Chapter 3.7 explains the status codes in .sum files, and how to compare two runs.

Reproducing one test's compile by hand

You can read a test, and repeat its compile, with no build tree. This is also how you debug a test that fails: take one compile out of the harness and run it yourself. The install manual says that the .log files "contain a detailed log of the compiler invocations" (install.texi:3563-3565). After a run, copy the command from gcc.log. The compiler in that command is GCC_UNDER_TEST, which the harness gets from DejaGnu's find_gcc (gcc.exp:101-106). In a build tree it gets a -B option, so that it does not use "GCC files from the install tree" (gcc/Makefile.in:4523-4526).

Without a build, rebuild the command from the directives. For Listing 6.1-1, the pieces are the plain-output flag from TEST_ALWAYS_FLAGS, the dg-options, the -fno-ident from the scan, and -S from dg-do compile:

$ curl -sO https://raw.githubusercontent.com/gcc-mirror/gcc/releases/gcc-15.2.0/gcc/testsuite/gcc.dg/pr63637-1.c
$ LC_ALL=C gcc -fdiagnostics-plain-output -O2 -fno-ident -S pr63637-1.c -o pr63637-1.s
$ grep -o '# Magic instruction' pr63637-1.s | wc -l

The directive passes when the count is 1. grep -o counts each match, as Tcl's regexp -all does in scan-assembler-times (scanasm.exp:508). Nobody ran these commands at the pin for this book, so this chapter shows no output. An installed compiler of another version can give another count. The test is a statement about the compiler in the tree under test.

For Listing 6.1-7, the default options of the driver take the place of dg-options:

$ LC_ALL=C gcc -fdiagnostics-plain-output -ansi -pedantic-errors -S case-bogus-1.c

The test passes if the output has a line for line 7, column 21, with error: and the expected text, and no other diagnostic. With the command in a shell, you can add the dump options of Chapter 5.2 and read the dumps. The test file does not change.

Three things to carry away

A GCC test is a source file whose dg- comments are Tcl commands. DejaGnu calls each one with its line number, and GCC's directives edit the local variables of DejaGnu's dg-test.

dg-do and the scan directives agree on file names only by convention. scan-assembler reads the .s file that dg-do compile writes, and it adds -fno-ident to the compile.

make check-gcc RUNTESTFLAGS="dg.exp=name.c" in gcc/ runs one test. The .exp name must be the driver of the test's directory. At the top level, use check-gcc-c.

Things that surprise people

dg-do run with scan-assembler gives UNRESOLVED. No .s file exists for the scan to read.

A directory of target tests can vanish without a trace. gcc.target/i386/i386.exp returns at once on a target that is not x86, and the .sum file does not mention its tests.

The top-level make check-gcc tests every configured language. It is make check in gcc/. check-gcc-c is the C testsuite alone.

A wrong .exp= name runs nothing, and reports nothing. Check that the summary counts at least one test.

Documentation coverage

The install manual documents make check, the check-<language> targets, RUNTESTFLAGS, the name.exp=pattern syntax, and the .sum and .log files under Testing (gcc/doc/install.texi:3393, selected tests at :3448). It requires DejaGnu 1.5.3 or later (:574). The internals manual documents the testsuites in its Testsuites chapter (gcc/doc/sourcebuild.texi:912). Every directive is in Directives (:1026), the scans in Final Actions (:3543), the keywords in Effective-Target Keywords (:1484) and the C directories in C Tests (:3909). The GCC manual documents -fdiagnostics-plain-output (gcc/doc/invoke.texi:5334) and -fno-ident (:19858).

What is not documented:

  • That a directive is a Tcl procedure, called with its line number, which edits the local variables of DejaGnu's dg-test.
  • That gcc.dg/dg.exp runs only the top of gcc.dg, plus c-c++-common, and that the .exp= name must match the driver of the test's directory.
  • That scan-assembler finds its file by name, so it gives UNRESOLVED in a dg-do run test.
  • That an assembly scan adds -fno-ident to the compile, and -ffat-lto-objects when the target supports LTO. The manual entry for -fno-ident mentions only #ident, not the .ident line that GCC writes itself.
  • That every compile in the testsuite gets -fdiagnostics-plain-output and runs with LC_ALL=C.
  • That a dg-error expression with no column still requires a column in the message, and that a leading N: requires exactly column N.
  • That the top-level check-gcc runs the testsuite of every configured language.
  • The C Tests node says that gcc.dg tests run with -ansi -pedantic (sourcebuild.texi:3924). dg.exp uses -ansi -pedantic-errors.

Next: the .texi files in gcc/doc that this chapter cites by line, how they become the manuals, and why a patch never edits tm.texi.


All source references in this chapter are to GCC 15.2.0 (releases/gcc-15.2.0). Line numbers in other releases will differ; the surrounding code rarely does. Where behaviour itself changed across a major version, it is flagged inline.