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]
 
