Hi!

This is something I've been unhappy for a long time with, and finally got to
write something for it.
When some test expects more than one error or warning or message on the same
source line, people have to use absolute line number on the dg-* directives
that is not on the right line, as DejaGNU handles just . (current line, the
default if the directive doesn't have all the arguments), 0 (no expected
line) and <number> for absolute line number.

This patch extends it, so one can write relative line numbers, . +/- <number>
as a single argument, say .-1 for the previous line number, .+1 for the next 
line
number, etc.  While one still has to supply the comment and target/xfail
arguments, the advantage of doing this is that if you adjust the testcase,
say add a line somewhere early, etc., you don't have to renumber all the
line numbers, and from what I saw in some clang testcases, it can be also
useful if multiple errors are issued for consecutive lines with different
wordings depending on some target supports etc. macros, one can e.g. use
  stmt1;        /* { dg-error "..." "" { target c } } */
  stmt2;        /* { dg-error "..." "" { target c } } */
  stmt3;        /* { dg-error "..." "" { target c } } */
                /* { dg-error "..." "" { target c++ } .-3 } */
                /* { dg-error "..." "" { target c++ } .-3 } */
                /* { dg-error "..." "" { target c++ } .-3 } */
which would be more readable than intermix the two sets of diagnostics.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2016-09-22  Jakub Jelinek  <ja...@redhat.com>

        * lib/gcc-dg.exp (process-message): Support relative line number
        notation - .+4 or .-1 etc.
        * gcc.dg/dg-test-1.c: New test.

--- gcc/testsuite/lib/gcc-dg.exp.jj     2016-06-24 12:59:19.000000000 +0200
+++ gcc/testsuite/lib/gcc-dg.exp        2016-09-22 17:29:21.912995332 +0200
@@ -986,6 +986,13 @@ if { [info procs saved-dg-error] == [lis
 proc process-message { msgproc msgprefix dgargs } {
     upvar dg-messages dg-messages
 
+    # Handle relative line specification, .+1 or .-1 etc.
+    if { [llength $dgargs] == 5
+        && [regsub "^\.\[+-\](\[0-9\])$" [lindex $dgargs 4] "\\1" num] } {
+       set num [expr [lindex $dgargs 0] [string index [lindex $dgargs 4] 1] 
$num]
+       set dgargs [lreplace $dgargs 4 4 $num]
+    }
+
     # Process the dg- directive, including adding the regular expression
     # to the new message entry in dg-messages.
     set msgcnt [llength ${dg-messages}]
--- gcc/testsuite/gcc.dg/dg-test-1.c.jj 2016-09-22 17:19:39.984407351 +0200
+++ gcc/testsuite/gcc.dg/dg-test-1.c    2016-09-22 17:22:14.000000000 +0200
@@ -0,0 +1,18 @@
+/* Test relative line number specification extensions over what DejaGNU 
supports.  */
+/* { dg-do compile } */
+/* { dg-options "-Wunused-parameter" } */
+
+void
+foo (void)
+{                      /* { dg-error "'a' undeclared" "err1" { target *-*-* } 
.+1 } */
+  int z = a + b + c + d;/* { dg-error "'b' undeclared" "err2" { target *-*-* } 
. } */
+}                      /* { dg-error "'c' undeclared" "err3" { target *-*-* } 
.-1 } */
+
+
+/* { dg-error "'d' undeclared" "err4" { target *-*-* } .-4 } */
+/* { dg-warning "unused parameter 'e'" "warn1" { target *-*-* } .+3 } */
+
+void                           /* { dg-warning "unused parameter 'f'" "warn2" 
{ target *-*-* } .+1 } */
+bar (int e, int f, int g, int h)/* { dg-warning "unused parameter 'g'" "warn3" 
{ target *-*-* } . } */
+{                              /* { dg-warning "unused parameter 'h'" "warn4" 
{ target *-*-* } .-1 } */
+}

        Jakub

Reply via email to