From 9049a546f88523aba9ddcfb9df329dc04063993d Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 18 Sep 2015 13:25:48 -0400
Subject: [PATCH 31/34] FIXME: semi-working attempt to suppress range-printing
 for macro expansion

This filters out "awkward" ranges in diagnostic_show_locus; doing
so involved reworking it only work on ranges within m_per_range_vec.

It does reject printing the caret though, since it rejects range 0.

TODO: combine this with the delayed expansion work in the levenshtein wc
---
 gcc/diagnostic-show-locus.c                        | 29 +++++++++++
 .../gcc.dg/plugin/diagnostic-test-expressions-1.c  | 59 ++++++++++++++++++++++
 2 files changed, 88 insertions(+)

diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c
index f188aca..58f7749 100644
--- a/gcc/diagnostic-show-locus.c
+++ b/gcc/diagnostic-show-locus.c
@@ -713,6 +713,7 @@ layout::layout (diagnostic_context * context,
   m_x_offset (0)
 {
   source_location primary_loc = richloc->get_range (0)->m_loc;
+  const line_map *loc_linemap = linemap_lookup (line_table, primary_loc);
 
   for (unsigned int idx = 0; idx < richloc->get_num_locations (); idx++)
     {
@@ -777,6 +778,34 @@ layout::layout (diagnostic_context * context,
 	    continue;
 	}
 
+#if 0
+      /* If the primary location is within a macro expansion, reject all ranges
+	 for now.
+	 Doing so fixes some crazy output, but drops the caret from the output
+	 if the range is range 0.  */
+      if (linemap_location_from_macro_expansion_p (line_table,
+						   m_richloc->get_loc ()))
+	continue;
+
+      /* Ignore ranges that start/finish within a macro expansion.  */
+      if (linemap_location_from_macro_expansion_p (line_table,
+						   loc_range->m_src_range.m_start))
+	continue;
+      if (linemap_location_from_macro_expansion_p (line_table,
+						   loc_range->m_src_range.m_finish))
+	continue;
+#endif
+
+#if 0
+      /* If they're not all in the same line-map, ignore the range.  */
+      if (linemap_lookup (line_table, loc_range->m_src_range.m_start)
+	  != loc_linemap)
+	continue;
+      if (linemap_lookup (line_table, loc_range->m_src_range.m_finish)
+	  != loc_linemap)
+	continue;
+#endif
+
       /* Passed all the tests; add the range to m_layout_ranges so that
 	 it will be printed.  */
       m_layout_ranges.safe_push (ri);
diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-1.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-1.c
index 9372936..8ff751e 100644
--- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-1.c
+++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-expressions-1.c
@@ -654,6 +654,65 @@ void test_macro (long double xl)
    { dg-end-multiline-output "" } */
 }
 
+/* Examples of macros.  *******************************************/
+
+#if 1
+#define EXPECT(EXPR, RES) __builtin_expect ((expr), res)
+
+void test_expect (int expr, int res)
+{
+  __emit_expression_range (0, EXPECT (expr, res)); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+ #define EXPECT(EXPR, RES) __builtin_expect ((expr), res)
+                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+   { dg-end-multiline-output "" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, EXPECT (expr, res));
+                               ^
+   { dg-end-multiline-output "" } */
+
+}
+#endif
+
+
+#if  __GNUC__ >= 3
+# define _IO_BE(expr, res) __builtin_expect ((expr), res)
+#else
+# define _IO_BE(expr, res) (expr)
+#endif
+
+typedef struct _FILE
+{
+  unsigned char *_IO_write_ptr;
+  unsigned char *_IO_write_end;
+} FILE;
+
+extern unsigned char __overflow (FILE *__fp, unsigned char ch);
+
+#define _IO_putc_unlocked(_ch, _fp) \
+  ( _IO_BE ((_fp)->_IO_write_ptr >= (_fp)->_IO_write_end, 0) \
+    ? __overflow (_fp, (unsigned char) (_ch)) \
+    : (unsigned char) (*(_fp)->_IO_write_ptr++ = (_ch)))
+
+void
+fputc_unlocked (int __c, FILE *__stream)
+{
+  __emit_expression_range (0, _IO_putc_unlocked (__c, __stream)); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   { dg-end-multiline-output "" } */
+
+  __emit_expression_range (0, (_IO_putc_unlocked (__c, __stream) + 1)); /* { dg-warning "range" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, _IO_putc_unlocked (__c, __stream));
+                               ^
+   { dg-end-multiline-output "" } */
+/* { dg-begin-multiline-output "" }
+   __emit_expression_range (0, (_IO_putc_unlocked (__c, __stream) + 1));
+                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
+   { dg-end-multiline-output "" } */
+}
+
 /* Verify that we can underline expressions that span multiple
    ordinary maps.  */
 
-- 
1.8.5.3

