From 86922fe34567374fa31c343b4fd6b072716958ce Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Wed, 8 Jan 2020 16:13:05 -0500
Subject: [PATCH 001/113] {committed} testsuite: add dg-enable-nn-line-numbers

Jeff approved this here:
  https://gcc.gnu.org/ml/gcc-patches/2020-01/msg00619.html

Changed in v6:
- moved to multiline.exp to avoid having to add a new .exp file (which
  would have to be loaded manually via load_gcc_lib in various places
  before prune.exp due to lack of lib path support in tcl; one of these
  places is libgo.exp which we only mirror)
- clarify a comment to indicate that it doesn't have to be at the start
  of a line

Changed in v5:
- added, replacing the "-fdiagnostics-nn-line-numbers" option

This patch adds support for obscuring the line numbers printed in the
left-hand margin when printing the source code, converting them to "NN",
e.g from:

  7111 |   if (!(flags & 0x0001)) {
       |      ^
       |      |
       |      (1) following 'true' branch...
  7112 |

to:

   NN  |   if (!(flags & 0x0001)) {
       |      ^
       |      |
       |      (1) following 'true' branch...
   NN  |

This is useful in followup patches e.g. when testing how interprocedural
paths are printed using multiline.exp, to avoid depending on precise line
numbers.

This replaces the "-fdiagnostics-nn-line-numbers" option in earlier
versions of the analyzer patch kit, by doing it at the DejaGnu level
instead.

gcc/testsuite/ChangeLog:
	* lib/gcc-dg.exp (cleanup-after-saved-dg-test): Reset global
	nn_line_numbers_enabled.
	* lib/multiline.exp (nn_line_numbers_enabled): New global.
	(dg-enable-nn-line-numbers): New proc.
	(maybe-handle-nn-line-numbers): New proc.
	* lib/prune.exp (prune_gcc_output): Call maybe-handle-nn-line-numbers.
---
 gcc/testsuite/lib/gcc-dg.exp    |  2 +
 gcc/testsuite/lib/multiline.exp | 88 +++++++++++++++++++++++++++++++++
 gcc/testsuite/lib/prune.exp     |  4 ++
 3 files changed, 94 insertions(+)

diff --git a/gcc/testsuite/lib/gcc-dg.exp b/gcc/testsuite/lib/gcc-dg.exp
index e6875de2383..cccd3ce4742 100644
--- a/gcc/testsuite/lib/gcc-dg.exp
+++ b/gcc/testsuite/lib/gcc-dg.exp
@@ -940,6 +940,7 @@ if { [info procs saved-dg-test] == [list] } {
 	global set_compiler_env_var
 	global saved_compiler_env_var
 	global keep_saved_temps_suffixes
+	global nn_line_numbers_enabled
 	global multiline_expected_outputs
 	global freeform_regexps
 	global save_linenr_varnames
@@ -967,6 +968,7 @@ if { [info procs saved-dg-test] == [list] } {
 	if [info exists testname_with_flags] {
 	    unset testname_with_flags
 	}
+	set nn_line_numbers_enabled 0
 	set multiline_expected_outputs []
 	set freeform_regexps []
 
diff --git a/gcc/testsuite/lib/multiline.exp b/gcc/testsuite/lib/multiline.exp
index e965d8946f7..627ad68817f 100644
--- a/gcc/testsuite/lib/multiline.exp
+++ b/gcc/testsuite/lib/multiline.exp
@@ -60,6 +60,9 @@ set _multiline_last_beginning_line -1
 # This is cleared at the end of each test by gcc-dg.exp's wrapper for dg-test.
 set multiline_expected_outputs []
 
+# Was dg-enable-nn-line-numbers called?
+set nn_line_numbers_enabled 0
+
 ############################################################################
 # Exported functions.
 ############################################################################
@@ -177,6 +180,91 @@ proc handle-multiline-outputs { text } {
     return $text
 }
 
+# DejaGnu directive to enable post-processing the line numbers printed in
+# the left-hand margin when printing the source code, converting them to
+# "NN", e.g from:
+#
+#    100 |   if (flag)
+#        |      ^
+#        |      |
+#        |      (1) following 'true' branch...
+#    101 |     {
+#    102 |       foo ();
+#        |       ^
+#        |       |
+#        |       (2) ...to here
+#
+# to:
+#
+#     NN |   if (flag)
+#        |      ^
+#        |      |
+#        |      (1) following 'true' branch...
+#     NN |     {
+#     NN |       foo ();
+#        |       ^
+#        |       |
+#        |       (2) ...to here
+#
+# This is useful e.g. when testing how interprocedural paths are printed
+# via dg-begin/end-multiline-output, to avoid depending on precise line
+# numbers.
+
+proc dg-enable-nn-line-numbers { args } {
+    verbose "dg-nn-line-numbers: args: $args" 2
+    global nn_line_numbers_enabled
+    set nn_line_numbers_enabled 1
+}
+
+# Hook to be called by prune.exp's prune_gcc_output to convert such line
+# numbers to "NN" form.
+#
+# Match substrings of the form:
+#  "   25 |"
+# and convert them to:
+#  "   NN |"
+#
+# It returns a copy of its input, with the above changes.
+
+proc maybe-handle-nn-line-numbers { text } {
+    global testname_with_flags
+
+    verbose "maybe-handle-nn-line-numbers" 3
+
+    global nn_line_numbers_enabled
+    if { [expr {!$nn_line_numbers_enabled}] } {
+	verbose "nn_line_numbers_enabled false; bailing out" 3
+	return $text
+    }
+    
+    verbose "maybe-handle-nn-line-numbers: text before: ${text}" 4
+
+    # dg.exp's dg-test trims leading whitespace from the output
+    # in this line:
+    #   set comp_output [string trimleft $comp_output]
+    # so we can't rely on the exact leading whitespace for the
+    # first line in the output.
+    # Match initial input lines that start like:
+    #  "25 |"
+    # and convert them to:
+    #  "   NN |"
+    set rexp2 {(^[0-9]+ \|)}
+    set count_a [regsub -all $rexp2 $text "   NN |" text]
+    verbose "maybe-handle-nn-line-numbers: count_a: $count_a" 4
+    
+    # Match lines that start like:
+    #  "   25 |"
+    # and convert them to:
+    #  "   NN |"
+    set rexp {([ ]+[0-9]+ \|)}
+    set count_b [regsub -all $rexp $text "   NN |" text]
+    verbose "maybe-handle-nn-line-numbers: count_b: $count_b" 4
+
+    verbose "maybe-handle-nn-line-numbers: text after: ${text}" 4
+
+    return $text
+}
+
 ############################################################################
 # Internal functions
 ############################################################################
diff --git a/gcc/testsuite/lib/prune.exp b/gcc/testsuite/lib/prune.exp
index 6e2e84ba171..60402208c37 100644
--- a/gcc/testsuite/lib/prune.exp
+++ b/gcc/testsuite/lib/prune.exp
@@ -74,6 +74,10 @@ proc prune_gcc_output { text } {
     # Ignore harmless warnings from Xcode 4.0.
     regsub -all "(^|\n)\[^\n\]*ld: warning: could not create compact unwind for\[^\n\]*" $text "" text
 
+    # If dg-enable-nn-line-numbers was provided, then obscure source-margin
+    # line numbers by converting them to "NN" form.
+    set text [maybe-handle-nn-line-numbers $text]
+    
     # Call into multiline.exp to handle any multiline output directives.
     set text [handle-multiline-outputs $text]
 
-- 
2.21.0

