This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix -H missing multiple guards reporting


Hi!

The new gcc.dg/pch/cpp-3.c testcase fails on some hosts and doesn't on
others.  Turns out the problem is that for the
Multiple include guards may be useful for:
reporting the printing was done from a htab_traverse callback, but returned
0 (which means stop traversing).  This means that at most one header has
been reported, sometimes none (if the first entry in the hash table didn't
need a multiple include guard).
The following patch fixes it and additionally qsorts the headers to avoid
outputting them in hash table order, as that's quite unstable.

So far tested with
make -j4 check RUNTESTFLAGS=pch.exp
make -j4 check RUNTESTFLAGS=cpp.exp
full bootstrap/regression testing starting.  Ok for trunk/4.3 if it
succeeds?

2008-07-31  Jakub Jelinek  <jakub@redhat.com>

	PR preprocessor/36649
	* files.c (struct report_missing_guard_data): New type.
	(report_missing_guard): Put paths into an array instead of printing
	them right away.  Return 1 rather than 0.
	(report_missing_guard_cmp): New function.
	(_cpp_report_missing_guards): Sort and print paths gathered by
	report_missing_guard callback.

	* gcc.dg/pch/cpp-3.hs: Add include guards.
	* gcc.dg/pch/cpp-3a.h: Likewise.
	* gcc.dg/pch/cpp-3b.h: Likewise.
	* gcc.dg/cpp/mi8.c: New test.
	* gcc.dg/cpp/mi8a.h: New file.
	* gcc.dg/cpp/mi8b.h: New file.
	* gcc.dg/cpp/mi8c.h: New file.
	* gcc.dg/cpp/mi8d.h: New file.

--- libcpp/files.c.jj	2008-04-21 22:59:49.000000000 +0200
+++ libcpp/files.c	2008-07-31 17:38:08.000000000 +0200
@@ -1221,12 +1221,19 @@ cpp_change_file (cpp_reader *pfile, enum
   _cpp_do_file_change (pfile, reason, new_name, 1, 0);
 }
 
+struct report_missing_guard_data
+{
+  const char **paths;
+  size_t count;
+};
+
 /* Callback function for htab_traverse.  */
 static int
-report_missing_guard (void **slot, void *b)
+report_missing_guard (void **slot, void *d)
 {
   struct file_hash_entry *entry = (struct file_hash_entry *) *slot;
-  int *bannerp = (int *) b;
+  struct report_missing_guard_data *data
+    = (struct report_missing_guard_data *) d;
 
   /* Skip directories.  */
   if (entry->start_dir != NULL)
@@ -1236,19 +1243,25 @@ report_missing_guard (void **slot, void 
       /* We don't want MI guard advice for the main file.  */
       if (file->cmacro == NULL && file->stack_count == 1 && !file->main_file)
 	{
-	  if (*bannerp == 0)
+	  if (data->paths == NULL)
 	    {
-	      fputs (_("Multiple include guards may be useful for:\n"),
-		     stderr);
-	      *bannerp = 1;
+	      data->paths = XCNEWVEC (const char *, data->count);
+	      data->count = 0;
 	    }
 
-	  fputs (entry->u.file->path, stderr);
-	  putc ('\n', stderr);
+	  data->paths[data->count++] = file->path;
 	}
     }
 
-  return 0;
+  /* Keep traversing the hash table.  */
+  return 1;
+}
+
+/* Comparison function for qsort.  */
+static int
+report_missing_guard_cmp (const void *p1, const void *p2)
+{
+  return strcmp (*(const char *const *) p1, *(const char *const *) p2);
 }
 
 /* Report on all files that might benefit from a multiple include guard.
@@ -1256,9 +1269,29 @@ report_missing_guard (void **slot, void 
 void
 _cpp_report_missing_guards (cpp_reader *pfile)
 {
-  int banner = 0;
+  struct report_missing_guard_data data;
+
+  data.paths = NULL;
+  data.count = htab_elements (pfile->file_hash);
+  htab_traverse (pfile->file_hash, report_missing_guard, &data);
+
+  if (data.paths != NULL)
+    {
+      size_t i;
 
-  htab_traverse (pfile->file_hash, report_missing_guard, &banner);
+      /* Sort the paths to avoid outputting them in hash table
+	 order.  */
+      qsort (data.paths, data.count, sizeof (const char *),
+	     report_missing_guard_cmp);
+      fputs (_("Multiple include guards may be useful for:\n"),
+	     stderr);
+      for (i = 0; i < data.count; i++)
+	{
+	  fputs (data.paths[i], stderr);
+	  putc ('\n', stderr);
+	}
+      free (data.paths);
+    }
 }
 
 /* Locate HEADER, and determine whether it is newer than the current
--- gcc/testsuite/gcc.dg/pch/cpp-3.hs.jj	2008-07-31 09:57:56.000000000 +0200
+++ gcc/testsuite/gcc.dg/pch/cpp-3.hs	2008-07-31 17:44:13.000000000 +0200
@@ -1 +1,4 @@
+#ifndef CPP_3_H
+#define CPP_3_H
 /* empty */
+#endif
--- gcc/testsuite/gcc.dg/pch/cpp-3a.h.jj	2008-07-31 09:57:56.000000000 +0200
+++ gcc/testsuite/gcc.dg/pch/cpp-3a.h	2008-07-31 17:44:33.000000000 +0200
@@ -1 +1,4 @@
+#ifndef CPP_3A_H
+#define CPP_3A_H
 #include "cpp-3b.h"
+#endif
--- gcc/testsuite/gcc.dg/pch/cpp-3b.h.jj	2008-07-31 09:57:56.000000000 +0200
+++ gcc/testsuite/gcc.dg/pch/cpp-3b.h	2008-07-31 17:44:50.000000000 +0200
@@ -1 +1,4 @@
+#ifndef CPP_3B_H
+#define CPP_3B_H
 /* empty */
+#endif
--- gcc/testsuite/gcc.dg/cpp/mi8.c.jj	2008-07-31 17:54:00.000000000 +0200
+++ gcc/testsuite/gcc.dg/cpp/mi8.c	2008-07-31 18:04:32.000000000 +0200
@@ -0,0 +1,8 @@
+/* Test multiple include guards suggestions.  */
+
+/* { dg-do preprocess }
+   { dg-options "-H" }
+   { dg-message "mi8a\.h\n\[^\n\]*mi8c\.h\n\[^\n\]*mi8b\.h\n\[^\n\]*mi8d\.h\nMultiple include guards may be useful for:\n\[^\n\]*mi8a\.h\n\[^\n\]*mi8d\.h\n" "" { target *-*-* } 0 } */
+
+#include "mi8a.h"
+#include "mi8b.h"
--- gcc/testsuite/gcc.dg/cpp/mi8a.h.jj	2008-07-31 17:54:03.000000000 +0200
+++ gcc/testsuite/gcc.dg/cpp/mi8a.h	2008-07-31 18:01:36.000000000 +0200
@@ -0,0 +1 @@
+#include "mi8c.h"
--- gcc/testsuite/gcc.dg/cpp/mi8b.h.jj	2008-07-31 17:54:06.000000000 +0200
+++ gcc/testsuite/gcc.dg/cpp/mi8b.h	2008-07-31 18:01:29.000000000 +0200
@@ -0,0 +1,4 @@
+#ifndef GUARDB
+#define GUARDB
+#include "mi8d.h"
+#endif
--- gcc/testsuite/gcc.dg/cpp/mi8c.h.jj	2008-07-31 17:54:06.000000000 +0200
+++ gcc/testsuite/gcc.dg/cpp/mi8c.h	2008-07-31 18:01:56.000000000 +0200
@@ -0,0 +1,4 @@
+#ifndef GUARDC
+#define GUARDC
+/* Empty */
+#endif
--- gcc/testsuite/gcc.dg/cpp/mi8d.h.jj	2008-07-31 17:54:03.000000000 +0200
+++ gcc/testsuite/gcc.dg/cpp/mi8d.h	2008-07-31 18:02:10.000000000 +0200
@@ -0,0 +1 @@
+/* Empty */

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]