I needed an option to ignore changes in comments, so I made a patch
and it seems to work. If it's ok, please apply it to the main source.
The patch was made against diff 3.0.9-53de.

Piotr Stefaniak
diff --git a/src/diff.c b/src/diff.c
index 807d38f..341f57f 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -124,6 +124,7 @@ enum
   NORMAL_OPTION,
   SDIFF_MERGE_ASSIST_OPTION,
   STRIP_TRAILING_CR_OPTION,
+  STRIP_ANSIC_COMMENTS_OPTION,
   SUPPRESS_BLANK_EMPTY_OPTION,
   SUPPRESS_COMMON_LINES_OPTION,
   TABSIZE_OPTION,
@@ -202,6 +203,7 @@ static struct option const longopts[] =
   {"speed-large-files", 0, 0, 'H'},
   {"starting-file", 1, 0, 'S'},
   {"strip-trailing-cr", 0, 0, STRIP_TRAILING_CR_OPTION},
+  {"strip-ansic-comments", 0, 0, STRIP_ANSIC_COMMENTS_OPTION},
   {"suppress-blank-empty", 0, 0, SUPPRESS_BLANK_EMPTY_OPTION},
   {"suppress-common-lines", 0, 0, SUPPRESS_COMMON_LINES_OPTION},
   {"tabsize", 1, 0, TABSIZE_OPTION},
@@ -575,6 +577,10 @@ main (int argc, char **argv)
 	  strip_trailing_cr = true;
 	  break;
 
+	case STRIP_ANSIC_COMMENTS_OPTION:
+	  strip_ansic_comments = true;
+	  break;
+
 	case SUPPRESS_BLANK_EMPTY_OPTION:
 	  suppress_blank_empty = true;
 	  break;
@@ -853,6 +859,7 @@ static char const * const option_help_msgid[] = {
   N_("-B  --ignore-blank-lines  Ignore changes whose lines are all blank."),
   N_("-I RE  --ignore-matching-lines=RE  Ignore changes whose lines all match RE."),
   N_("--strip-trailing-cr  Strip trailing carriage return on input."),
+  N_("--strip-ansic-comments  Strip ANSI C comments on input."),
 #if O_BINARY
   N_("--binary  Read and write data in binary mode."),
 #endif
diff --git a/src/diff.h b/src/diff.h
index 97f8d96..0e1769b 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -156,6 +156,9 @@ XTERN bool suppress_blank_empty;
 /* Remove trailing carriage returns from input.  */
 XTERN bool strip_trailing_cr;
 
+/* Remove ANSI C comments from input.  */
+XTERN bool strip_ansic_comments;
+
 /* In directory comparison, specify file to start with (-S).
    This is used for resuming an aborted comparison.
    All file names less than this name are ignored.  */
diff --git a/src/diff3.c b/src/diff3.c
index 0f11fdc..a445dd5 100644
--- a/src/diff3.c
+++ b/src/diff3.c
@@ -139,6 +139,9 @@ static bool text;
 /* Remove trailing carriage returns from input.  */
 static bool strip_trailing_cr;
 
+/* Remove ANSI C comments from input.  */
+static bool strip_ansic_comments;
+
 /* If nonzero, write out an ed script instead of the standard diff3 format.  */
 static bool edscript;
 
diff --git a/src/io.c b/src/io.c
index 031be3d..63bbb67 100644
--- a/src/io.c
+++ b/src/io.c
@@ -523,6 +523,46 @@ prepare_text (struct file_data *current)
   /* Don't use uninitialized storage when planting or using sentinels.  */
   memset (p + buffered, 0, sizeof (word));
 
+  if (strip_ansic_comments && (dst = memchr (p, '/', buffered)))
+    {
+      char const *src = dst;
+      char const *srclim = p + buffered;
+      int in = 0;
+
+      do
+	{
+	  if (!in)
+	    {
+	      if (*src == '/')
+		{
+		  if (++src >= srclim)
+		    {
+		      *dst++ = *src;
+		      break;
+		    }
+
+		  if (*src == '*')
+		    {
+		      src++;
+		      in = 1;
+		      continue;
+		    }
+		  else
+		    *dst++ = '/';
+		}
+	      *dst++ = *src++;
+	    }
+	  else if (*src++ == '*' && *src == '/')
+	    {
+	      src++;
+	      in = 0;
+	    }
+	}
+      while (src < srclim);
+
+      buffered -= src - dst;
+    }
+
   if (strip_trailing_cr && (dst = memchr (p, '\r', buffered)))
     {
       char const *src = dst;

Reply via email to